blob: 3c6b493a1c06603735275f88099f32dcf78bcf08 [file] [log] [blame]
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#ifndef NFD_FACE_UNIX_STREAM_CHANNEL_HPP
8#define NFD_FACE_UNIX_STREAM_CHANNEL_HPP
9
Junxiao Shi61e3cc52014-03-03 20:40:28 -070010#include "channel.hpp"
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010011#include "unix-stream-face.hpp"
12
13namespace nfd {
14
15namespace unix_stream {
Junxiao Shi61e3cc52014-03-03 20:40:28 -070016typedef boost::asio::local::stream_protocol::endpoint Endpoint;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010017} // namespace unix_stream
18
19/**
20 * \brief Class implementing a local channel to create faces
21 *
22 * Channel can create faces as a response to incoming IPC connections
23 * (UnixStreamChannel::listen needs to be called for that to work).
24 */
Junxiao Shi61e3cc52014-03-03 20:40:28 -070025class UnixStreamChannel : public Channel
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010026{
27public:
28 /**
Davide Pesavento9cec8ee2014-02-19 11:21:59 +010029 * \brief UnixStreamChannel-related error
30 */
31 struct Error : public std::runtime_error
32 {
33 Error(const std::string& what) : std::runtime_error(what) {}
34 };
35
36 /**
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010037 * \brief Create UnixStream channel for the specified endpoint
38 *
39 * To enable creation of faces upon incoming connections, one
40 * needs to explicitly call UnixStreamChannel::listen method.
41 */
Junxiao Shi61e3cc52014-03-03 20:40:28 -070042 explicit
43 UnixStreamChannel(const unix_stream::Endpoint& endpoint);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010044
Junxiao Shi61e3cc52014-03-03 20:40:28 -070045 virtual
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010046 ~UnixStreamChannel();
47
48 /**
49 * \brief Enable listening on the local endpoint, accept connections,
50 * and create faces when a connection is made
51 * \param onFaceCreated Callback to notify successful creation of the face
52 * \param onAcceptFailed Callback to notify when channel fails (accept call
53 * returns an error)
54 * \param backlog The maximum length of the queue of pending incoming
55 * connections
56 */
57 void
58 listen(const FaceCreatedCallback& onFaceCreated,
59 const ConnectFailedCallback& onAcceptFailed,
60 int backlog = boost::asio::local::stream_protocol::acceptor::max_connections);
61
62private:
63 void
64 createFace(const shared_ptr<boost::asio::local::stream_protocol::socket>& socket,
65 const FaceCreatedCallback& onFaceCreated);
66
67 void
68 handleSuccessfulAccept(const boost::system::error_code& error,
69 const shared_ptr<boost::asio::local::stream_protocol::socket>& socket,
70 const FaceCreatedCallback& onFaceCreated,
71 const ConnectFailedCallback& onConnectFailed);
72
73private:
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010074 unix_stream::Endpoint m_endpoint;
75 shared_ptr<boost::asio::local::stream_protocol::acceptor> m_acceptor;
76};
77
78} // namespace nfd
79
80#endif // NFD_FACE_UNIX_STREAM_CHANNEL_HPP