Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame^] | 1 | /* -*- 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 | |
| 13 | namespace nfd { |
| 14 | |
| 15 | namespace 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 | */ |
| 25 | class UnixStreamChannel // : protected SessionBasedChannel |
| 26 | { |
| 27 | public: |
| 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 | /** |
| 41 | * \brief Create UnixStream channel for the specified endpoint |
| 42 | * |
| 43 | * To enable creation of faces upon incoming connections, one |
| 44 | * needs to explicitly call UnixStreamChannel::listen method. |
| 45 | */ |
| 46 | UnixStreamChannel(boost::asio::io_service& ioService, |
| 47 | const unix_stream::Endpoint& endpoint); |
| 48 | |
| 49 | ~UnixStreamChannel(); |
| 50 | |
| 51 | /** |
| 52 | * \brief Enable listening on the local endpoint, accept connections, |
| 53 | * and create faces when a connection is made |
| 54 | * \param onFaceCreated Callback to notify successful creation of the face |
| 55 | * \param onAcceptFailed Callback to notify when channel fails (accept call |
| 56 | * returns an error) |
| 57 | * \param backlog The maximum length of the queue of pending incoming |
| 58 | * connections |
| 59 | */ |
| 60 | void |
| 61 | listen(const FaceCreatedCallback& onFaceCreated, |
| 62 | const ConnectFailedCallback& onAcceptFailed, |
| 63 | int backlog = boost::asio::local::stream_protocol::acceptor::max_connections); |
| 64 | |
| 65 | private: |
| 66 | void |
| 67 | createFace(const shared_ptr<boost::asio::local::stream_protocol::socket>& socket, |
| 68 | const FaceCreatedCallback& onFaceCreated); |
| 69 | |
| 70 | void |
| 71 | handleSuccessfulAccept(const boost::system::error_code& error, |
| 72 | const shared_ptr<boost::asio::local::stream_protocol::socket>& socket, |
| 73 | const FaceCreatedCallback& onFaceCreated, |
| 74 | const ConnectFailedCallback& onConnectFailed); |
| 75 | |
| 76 | private: |
| 77 | boost::asio::io_service& m_ioService; |
| 78 | unix_stream::Endpoint m_endpoint; |
| 79 | shared_ptr<boost::asio::local::stream_protocol::acceptor> m_acceptor; |
| 80 | }; |
| 81 | |
| 82 | } // namespace nfd |
| 83 | |
| 84 | #endif // NFD_FACE_UNIX_STREAM_CHANNEL_HPP |