face: switch to non-throwing version of websocketpp APIs
Change-Id: I0291dff14373cadeec16d44c73d9db43011fff4b
Refs: #2630
diff --git a/daemon/face/websocket-channel.cpp b/daemon/face/websocket-channel.cpp
index 855dd99..7997b1e 100644
--- a/daemon/face/websocket-channel.cpp
+++ b/daemon/face/websocket-channel.cpp
@@ -102,26 +102,30 @@
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<WebSocketFace>(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
}
-
- auto face = make_shared<WebSocketFace>(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::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
+ }
}
void
@@ -131,19 +135,20 @@
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 007e800..4c781c3 100644
--- a/daemon/face/websocket-face.cpp
+++ b/daemon/face/websocket-face.cpp
@@ -54,13 +54,11 @@
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 @@
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 @@
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");
}