diff --git a/daemon/face/websocket-channel.cpp b/daemon/face/websocket-channel.cpp index 855dd999..7997b1e7 100644 --- a/daemon/face/websocket-channel.cpp +++ b/daemon/face/websocket-channel.cpp @@ -102,26 +102,30 @@ WebSocketChannel::handleMessage(websocketpp::connection_hdl hdl, void WebSocketChannel::handleOpen(websocketpp::connection_hdl hdl) { - std::string remote; try { - remote = "wsclient://" + m_server.get_con_from_hdl(hdl)->get_remote_endpoint(); + std::string remote = "wsclient://" + m_server.get_con_from_hdl(hdl)->get_remote_endpoint(); + auto face = make_shared(FaceUri(remote), this->getUri(), + hdl, ref(m_server)); + m_onFaceCreatedCallback(face); + m_channelFaces[hdl] = face; + // Schedule ping message + scheduler::EventId pingEvent = scheduler::schedule(m_pingInterval, + bind(&WebSocketChannel::sendPing, + this, hdl)); + face->setPingEventId(pingEvent); } - catch (const websocketpp::lib::error_code& e) { - NFD_LOG_WARN("Cannot get remote URI"); - websocketpp::lib::error_code error; - m_server.close(hdl, websocketpp::close::status::normal, "closed by channel", error); - return; + catch (const FaceUri::Error& e) { + NFD_LOG_WARN(e.what()); + websocketpp::lib::error_code ec; + m_server.close(hdl, websocketpp::close::status::normal, "closed by channel", ec); + // ignore error on close + } + catch (const websocketpp::exception& e) { + NFD_LOG_WARN("Cannot get remote connection: " << e.what()); + websocketpp::lib::error_code ec; + m_server.close(hdl, websocketpp::close::status::normal, "closed by channel", ec); + // ignore error on close } - - auto face = make_shared(FaceUri(remote), this->getUri(), - hdl, ref(m_server)); - m_onFaceCreatedCallback(face); - m_channelFaces[hdl] = face; - - // Schedule ping message - scheduler::EventId pingEvent = scheduler::schedule(m_pingInterval, - bind(&WebSocketChannel::sendPing, this, hdl)); - face->setPingEventId(pingEvent); } void @@ -131,19 +135,20 @@ WebSocketChannel::sendPing(websocketpp::connection_hdl hdl) if (it != m_channelFaces.end()) { NFD_LOG_TRACE("Sending ping to " << it->second->getRemoteUri()); - try { - m_server.ping(hdl, "NFD-WebSocket"); - } - catch (const websocketpp::lib::error_code& e) { - NFD_LOG_WARN("Failed to ping " << it->second->getRemoteUri()); - it->second->close(); - m_channelFaces.erase(it); - return; - } + websocketpp::lib::error_code ec; + m_server.ping(hdl, "NFD-WebSocket", ec); + if (ec) + { + NFD_LOG_WARN("Failed to ping " << it->second->getRemoteUri() << ": " << ec.message()); + it->second->close(); + m_channelFaces.erase(it); + return; + } // Schedule next ping message scheduler::EventId pingEvent = scheduler::schedule(m_pingInterval, - bind(&WebSocketChannel::sendPing, this, hdl)); + bind(&WebSocketChannel::sendPing, + this, hdl)); it->second->setPingEventId(pingEvent); } } diff --git a/daemon/face/websocket-face.cpp b/daemon/face/websocket-face.cpp index 007e800c..4c781c33 100644 --- a/daemon/face/websocket-face.cpp +++ b/daemon/face/websocket-face.cpp @@ -54,13 +54,11 @@ WebSocketFace::sendInterest(const Interest& interest) const Block& payload = interest.wireEncode(); this->getMutableCounters().getNOutBytes() += payload.size(); - try { - m_server.send(m_handle, payload.wire(), payload.size(), - websocketpp::frame::opcode::binary); - } - catch (const websocketpp::lib::error_code& e) { - NFD_LOG_FACE_WARN("Failed to send Interest: " << e << " (" << e.message() << ")"); - } + websocketpp::lib::error_code ec; + m_server.send(m_handle, payload.wire(), payload.size(), + websocketpp::frame::opcode::binary, ec); + if (ec) + NFD_LOG_FACE_WARN("Failed to send Interest: " << ec.message()); } void @@ -76,13 +74,11 @@ WebSocketFace::sendData(const Data& data) const Block& payload = data.wireEncode(); this->getMutableCounters().getNOutBytes() += payload.size(); - try { - m_server.send(m_handle, payload.wire(), payload.size(), - websocketpp::frame::opcode::binary); - } - catch (const websocketpp::lib::error_code& e) { - NFD_LOG_FACE_WARN("Failed to send Data: " << e << " (" << e.message() << ")"); - } + websocketpp::lib::error_code ec; + m_server.send(m_handle, payload.wire(), payload.size(), + websocketpp::frame::opcode::binary, ec); + if (ec) + NFD_LOG_FACE_WARN("Failed to send Data: " << ec.message()); } void @@ -95,9 +91,9 @@ WebSocketFace::close() m_closed = true; scheduler::cancel(m_pingEventId); - websocketpp::lib::error_code ecode; - m_server.close(m_handle, websocketpp::close::status::normal, "closed by nfd", ecode); - + websocketpp::lib::error_code ec; + m_server.close(m_handle, websocketpp::close::status::normal, "closed by nfd", ec); + // ignore error on close fail("Face closed"); }