diff --git a/ntcore/src/main/native/cpp/NetworkClient.cpp b/ntcore/src/main/native/cpp/NetworkClient.cpp index 365c73b5c1..3d095df6ae 100644 --- a/ntcore/src/main/native/cpp/NetworkClient.cpp +++ b/ntcore/src/main/native/cpp/NetworkClient.cpp @@ -401,8 +401,8 @@ void NetworkClient::WsConnected(wpi::WebSocket& ws, uv::Tcp& tcp, INFO("CONNECTED NT4 to {} port {}", connInfo.remote_ip, connInfo.remote_port); m_connHandle = m_connList.AddConnection(connInfo); - m_wire = - std::make_shared(ws, connInfo.protocol_version); + m_wire = std::make_shared( + ws, connInfo.protocol_version, m_logger); m_clientImpl = std::make_unique( m_loop.Now().count(), m_inst, *m_wire, m_logger, m_timeSyncUpdated, [this](uint32_t repeatMs) { diff --git a/ntcore/src/main/native/cpp/NetworkServer.cpp b/ntcore/src/main/native/cpp/NetworkServer.cpp index 06f794f975..a77c5c837c 100644 --- a/ntcore/src/main/native/cpp/NetworkServer.cpp +++ b/ntcore/src/main/native/cpp/NetworkServer.cpp @@ -242,7 +242,7 @@ void NetworkServer::ServerConnection4::ProcessWsUpgrade() { m_info.protocol_version = protocol == "v4.1.networktables.first.wpi.edu" ? 0x0401 : 0x0400; m_wire = std::make_shared( - *m_websocket, m_info.protocol_version); + *m_websocket, m_info.protocol_version, m_logger); if (protocol == "rtt.networktables.first.wpi.edu") { INFO("CONNECTED RTT client (from {})", m_connInfo); diff --git a/ntcore/src/main/native/cpp/net/WebSocketConnection.cpp b/ntcore/src/main/native/cpp/net/WebSocketConnection.cpp index eb9a39e0b6..176770db7f 100644 --- a/ntcore/src/main/native/cpp/net/WebSocketConnection.cpp +++ b/ntcore/src/main/native/cpp/net/WebSocketConnection.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -53,6 +54,7 @@ void WebSocketConnection::Stream::write_impl(const char* data, size_t len) { if (data == m_conn.m_bufs.back().base) { // flush_nonempty() case size_t amt = len - m_conn.m_bufs.back().len; + WPI_DEBUG4(m_conn.m_logger, "conn: writing {} bytes (nonempty)", amt); m_conn.m_bufs.back().len = len; m_conn.m_framePos += amt; m_conn.m_written += amt; @@ -77,6 +79,7 @@ void WebSocketConnection::Stream::write_impl(const char* data, size_t len) { size_t amt = (std::min)(static_cast(kAllocSize - buf.len), static_cast(len)); if (amt > 0) { + WPI_DEBUG4(m_conn.m_logger, "conn: writing {} bytes", amt); std::memcpy(buf.base + buf.len, data, amt); buf.len += amt; m_conn.m_framePos += amt; @@ -104,8 +107,9 @@ void WebSocketConnection::Stream::write_impl(const char* data, size_t len) { } WebSocketConnection::WebSocketConnection(wpi::WebSocket& ws, - unsigned int version) - : m_ws{ws}, m_version{version} {} + unsigned int version, + wpi::Logger& logger) + : m_ws{ws}, m_logger{logger}, m_version{version} {} WebSocketConnection::~WebSocketConnection() { for (auto&& buf : m_bufs) { @@ -117,6 +121,7 @@ WebSocketConnection::~WebSocketConnection() { } void WebSocketConnection::SendPing(uint64_t time) { + WPI_DEBUG4(m_logger, "conn: sending ping {}", time); auto buf = AllocBuf(); buf.len = 8; wpi::support::endian::write64(buf.base, time); @@ -133,6 +138,8 @@ void WebSocketConnection::SendPing(uint64_t time) { } void WebSocketConnection::StartFrame(uint8_t opcode) { + WPI_DEBUG4(m_logger, "conn: starting frame {}", + static_cast(opcode)); m_frames.emplace_back(opcode, m_bufs.size(), m_bufs.size() + 1); m_bufs.emplace_back(AllocBuf()); m_bufs.back().len = 0; @@ -168,6 +175,7 @@ int WebSocketConnection::Write( if (kind == kText) { os << (first ? '[' : ','); } + WPI_DEBUG4(m_logger, "writing"); writer(os); } ++m_frames.back().count; @@ -179,6 +187,7 @@ int WebSocketConnection::Write( } int WebSocketConnection::Flush() { + WPI_DEBUG4(m_logger, "conn: flushing"); m_lastFlushTime = wpi::Now(); if (m_state == kEmpty) { return 0; @@ -243,6 +252,7 @@ void WebSocketConnection::Send( os << ']'; } wpi::WebSocket::Frame frame{opcode, os.bufs()}; + WPI_DEBUG4(m_logger, "Send({})", static_cast(opcode)); m_ws.SendFrames({{frame}}, [selfweak = weak_from_this()](auto bufs, auto) { if (auto self = selfweak.lock()) { self->ReleaseBufs(bufs); diff --git a/ntcore/src/main/native/cpp/net/WebSocketConnection.h b/ntcore/src/main/native/cpp/net/WebSocketConnection.h index 60a07f6eef..fc5cfe9833 100644 --- a/ntcore/src/main/native/cpp/net/WebSocketConnection.h +++ b/ntcore/src/main/native/cpp/net/WebSocketConnection.h @@ -15,13 +15,18 @@ #include "WireConnection.h" +namespace wpi { +class Logger; +} // namespace wpi + namespace nt::net { class WebSocketConnection final : public WireConnection, public std::enable_shared_from_this { public: - WebSocketConnection(wpi::WebSocket& ws, unsigned int version); + WebSocketConnection(wpi::WebSocket& ws, unsigned int version, + wpi::Logger& logger); ~WebSocketConnection() override; WebSocketConnection(const WebSocketConnection&) = delete; WebSocketConnection& operator=(const WebSocketConnection&) = delete; @@ -70,6 +75,7 @@ class WebSocketConnection final void ReleaseBufs(std::span bufs); wpi::WebSocket& m_ws; + wpi::Logger& m_logger; class Stream;