blob: 3bbc8a314d5faf4fad06c9e4d78689bd5b6db425 [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
10#include "common.hpp"
11#include "unix-stream-face.hpp"
12
13namespace nfd {
14
15namespace unix_stream {
16 typedef boost::asio::local::stream_protocol::endpoint Endpoint;
17} // 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 */
25class UnixStreamChannel // : protected SessionBasedChannel
26{
27public:
28 /**
29 * \brief Prototype for the callback called when a face is created
30 * (as a response to an incoming connection)
31 */
32 typedef function<void(const shared_ptr<UnixStreamFace>& newFace)> FaceCreatedCallback;
33
34 /**
35 * \brief Prototype for the callback that is called when a face
36 * fails to be created
37 */
38 typedef function<void(const std::string& reason)> ConnectFailedCallback;
39
40 /**
Davide Pesavento9cec8ee2014-02-19 11:21:59 +010041 * \brief UnixStreamChannel-related error
42 */
43 struct Error : public std::runtime_error
44 {
45 Error(const std::string& what) : std::runtime_error(what) {}
46 };
47
48 /**
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010049 * \brief Create UnixStream channel for the specified endpoint
50 *
51 * To enable creation of faces upon incoming connections, one
52 * needs to explicitly call UnixStreamChannel::listen method.
53 */
54 UnixStreamChannel(boost::asio::io_service& ioService,
55 const unix_stream::Endpoint& endpoint);
56
57 ~UnixStreamChannel();
58
59 /**
60 * \brief Enable listening on the local endpoint, accept connections,
61 * and create faces when a connection is made
62 * \param onFaceCreated Callback to notify successful creation of the face
63 * \param onAcceptFailed Callback to notify when channel fails (accept call
64 * returns an error)
65 * \param backlog The maximum length of the queue of pending incoming
66 * connections
67 */
68 void
69 listen(const FaceCreatedCallback& onFaceCreated,
70 const ConnectFailedCallback& onAcceptFailed,
71 int backlog = boost::asio::local::stream_protocol::acceptor::max_connections);
72
73private:
74 void
75 createFace(const shared_ptr<boost::asio::local::stream_protocol::socket>& socket,
76 const FaceCreatedCallback& onFaceCreated);
77
78 void
79 handleSuccessfulAccept(const boost::system::error_code& error,
80 const shared_ptr<boost::asio::local::stream_protocol::socket>& socket,
81 const FaceCreatedCallback& onFaceCreated,
82 const ConnectFailedCallback& onConnectFailed);
83
84private:
85 boost::asio::io_service& m_ioService;
86 unix_stream::Endpoint m_endpoint;
87 shared_ptr<boost::asio::local::stream_protocol::acceptor> m_acceptor;
88};
89
90} // namespace nfd
91
92#endif // NFD_FACE_UNIX_STREAM_CHANNEL_HPP