36 if (addr.is_loopback()) {
43 auto addr6 = addr.to_v6();
44 if (addr6.is_v4_mapped()) {
45 return boost::asio::ip::make_address_v4(boost::asio::ip::v4_mapped, addr6).is_loopback();
54 time::milliseconds pingInterval)
57 , m_pingInterval(pingInterval)
59 const auto& sock = m_server.get_con_from_hdl(hdl)->get_socket();
60 this->
setLocalUri(FaceUri(sock.local_endpoint(),
"ws"));
61 this->
setRemoteUri(FaceUri(sock.remote_endpoint(),
"wsclient"));
63 if (
isLoopback(sock.local_endpoint().address()) &&
64 isLoopback(sock.remote_endpoint().address())) {
65 this->
setScope(ndn::nfd::FACE_SCOPE_LOCAL);
68 this->
setScope(ndn::nfd::FACE_SCOPE_NON_LOCAL);
72 this->
setLinkType(ndn::nfd::LINK_TYPE_POINT_TO_POINT);
81 WebSocketTransport::doSend(
const Block& packet)
85 websocketpp::lib::error_code error;
86 m_server.send(m_handle, packet.data(), packet.size(),
87 websocketpp::frame::opcode::binary, error);
89 return processErrorCode(error);
101 std::tie(isOk, element) = Block::fromBuffer({
reinterpret_cast<const uint8_t*
>(msg.data()), msg.size()});
111 WebSocketTransport::schedulePing()
113 m_pingEventId =
getScheduler().schedule(m_pingInterval, [
this] { sendPing(); });
117 WebSocketTransport::sendPing()
121 websocketpp::lib::error_code error;
122 m_server.ping(m_handle,
"NFD-WebSocket", error);
124 return processErrorCode(error);
128 this->schedulePing();
148 WebSocketTransport::processErrorCode(
const websocketpp::lib::error_code& error)
168 m_pingEventId.cancel();
171 websocketpp::lib::error_code error;
172 m_server.close(m_handle, websocketpp::close::status::normal,
"closed by NFD", error);
void setScope(ndn::nfd::FaceScope scope) noexcept
void receive(const Block &packet, const EndpointId &endpoint={})
Pass a received link-layer packet to the upper layer for further processing.
void setPersistency(ndn::nfd::FacePersistency newPersistency)
Changes the persistency setting of the transport.
void setMtu(ssize_t mtu) noexcept
TransportState getState() const noexcept
Returns the current transport state.
void setState(TransportState newState)
Set transport state.
void setLocalUri(const FaceUri &uri) noexcept
void setLinkType(ndn::nfd::LinkType linkType) noexcept
void setRemoteUri(const FaceUri &uri) noexcept
PacketCounter nInPongs
Count of incoming pongs.
PacketCounter nOutPings
Count of outgoing pings.
void doClose() final
Performs Transport specific operations to close the transport.
WebSocketTransport(websocketpp::connection_hdl hdl, websocket::Server &server, time::milliseconds pingInterval)
void receiveMessage(const std::string &msg)
Translates a message into a Block and delivers it to the link service.
#define NFD_LOG_FACE_ERROR(msg)
Log a message at ERROR level.
#define NFD_LOG_FACE_DEBUG(msg)
Log a message at DEBUG level.
#define NFD_LOG_FACE_WARN(msg)
Log a message at WARN level.
#define NFD_LOG_FACE_TRACE(msg)
Log a message at TRACE level.
#define NFD_LOG_INIT(name)
static bool isLoopback(const boost::asio::ip::address &addr)
@ CLOSED
the transport is closed, and can be safely deallocated
@ CLOSING
the transport is being closed gracefully, either by the peer or by a call to close()
@ FAILED
the transport is being closed due to a failure
constexpr ssize_t MTU_UNLIMITED
Indicates that the transport has no limit on payload size.
websocketpp::server< websocketpp::config::asio > Server
ndn::Scheduler & getScheduler()
Returns the global Scheduler instance for the calling thread.