37 : m_localEndpoint(localEndpoint)
38 , m_pingInterval(10_s)
44 m_server.clear_access_channels(websocketpp::log::alevel::all);
45 m_server.clear_error_channels(websocketpp::log::elevel::all);
49 m_server.set_tcp_pre_bind_handler([isV6 = m_localEndpoint.address().is_v6()] (
const auto& acceptor) {
51 acceptor->set_option(boost::asio::ip::v6_only(
true));
53 return websocketpp::lib::error_code{};
55 m_server.set_open_handler(bind(&WebSocketChannel::handleOpen,
this, _1));
56 m_server.set_close_handler(bind(&WebSocketChannel::handleClose,
this, _1));
57 m_server.set_message_handler(bind(&WebSocketChannel::handleMessage,
this, _1, _2));
60 m_server.set_pong_handler(bind(&WebSocketChannel::handlePong,
this, _1));
61 m_server.set_pong_timeout_handler(bind(&WebSocketChannel::handlePongTimeout,
this, _1));
64 m_server.set_reuse_addr(
true);
68 WebSocketChannel::setPingInterval(time::milliseconds interval)
70 BOOST_ASSERT(!m_server.is_listening());
72 m_pingInterval = interval;
76 WebSocketChannel::setPongTimeout(time::milliseconds timeout)
78 BOOST_ASSERT(!m_server.is_listening());
80 m_server.set_pong_timeout(static_cast<long>(timeout.count()));
84 WebSocketChannel::handlePongTimeout(websocketpp::connection_hdl hdl)
86 auto it = m_channelFaces.find(hdl);
87 if (it != m_channelFaces.end()) {
88 static_cast<WebSocketTransport*>(it->second->getTransport())->handlePongTimeout();
96 WebSocketChannel::handlePong(websocketpp::connection_hdl hdl)
98 auto it = m_channelFaces.find(hdl);
99 if (it != m_channelFaces.end()) {
100 static_cast<WebSocketTransport*>(it->second->getTransport())->handlePong();
108 WebSocketChannel::handleMessage(websocketpp::connection_hdl hdl,
109 websocket::Server::message_ptr msg)
111 auto it = m_channelFaces.find(hdl);
112 if (it != m_channelFaces.end()) {
113 static_cast<WebSocketTransport*>(it->second->getTransport())->receiveMessage(msg->get_payload());
121 WebSocketChannel::handleOpen(websocketpp::connection_hdl hdl)
123 NFD_LOG_CHAN_TRACE(
"Incoming connection from " << m_server.get_con_from_hdl(hdl)->get_remote_endpoint());
125 auto linkService = make_unique<GenericLinkService>();
126 auto transport = make_unique<WebSocketTransport>(hdl, std::ref(m_server), m_pingInterval);
127 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
129 BOOST_ASSERT(m_channelFaces.count(hdl) == 0);
130 m_channelFaces[hdl] = face;
133 m_onFaceCreatedCallback(face);
137 WebSocketChannel::handleClose(websocketpp::connection_hdl hdl)
139 auto it = m_channelFaces.find(hdl);
140 if (it != m_channelFaces.end()) {
156 m_onFaceCreatedCallback = onFaceCreated;
158 m_server.listen(m_localEndpoint);
159 m_server.start_accept();
void listen(const FaceCreatedCallback &onFaceCreated)
Enable listening on the local endpoint, accept connections, and create faces when remote host makes a...
void setUri(const FaceUri &uri)
Class implementing WebSocket-based channel to create faces.
detail::SimulatorIo & getGlobalIoService()
std::function< void(const shared_ptr< Face > &face)> FaceCreatedCallback
Prototype for the callback that is invoked when a face is created (in response to an incoming connect...
void connectFaceClosedSignal(Face &face, const std::function< void()> &f)
invokes a callback when the face is closed
#define NFD_LOG_CHAN_DEBUG(msg)
Log a message at DEBUG level.
WebSocketChannel(const websocket::Endpoint &localEndpoint)
Create WebSocket channel for the local endpoint.
Copyright (c) 2011-2015 Regents of the University of California.
#define NFD_LOG_CHAN_INFO(msg)
Log a message at INFO level.
#define NFD_LOG_CHAN_TRACE(msg)
Log a message at TRACE level.
#define NFD_LOG_CHAN_WARN(msg)
Log a message at WARN level.
represents the underlying protocol and address used by a Face
bool isListening() const override
Returns whether the channel is listening.
boost::asio::ip::tcp::endpoint Endpoint
#define NFD_LOG_INIT(name)