[ntcore] Enhance WebSocketConnection debug logging

This commit is contained in:
Peter Johnson
2024-01-16 20:44:10 -08:00
parent 98c0827236
commit 1a7eeb6282
4 changed files with 22 additions and 6 deletions

View File

@@ -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<net::WebSocketConnection>(ws, connInfo.protocol_version);
m_wire = std::make_shared<net::WebSocketConnection>(
ws, connInfo.protocol_version, m_logger);
m_clientImpl = std::make_unique<net::ClientImpl>(
m_loop.Now().count(), m_inst, *m_wire, m_logger, m_timeSyncUpdated,
[this](uint32_t repeatMs) {

View File

@@ -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<net::WebSocketConnection>(
*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);

View File

@@ -8,6 +8,7 @@
#include <span>
#include <wpi/Endian.h>
#include <wpi/Logger.h>
#include <wpi/SpanExtras.h>
#include <wpi/raw_ostream.h>
#include <wpi/timestamp.h>
@@ -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<int>(kAllocSize - buf.len),
static_cast<int>(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<wpi::support::native>(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<unsigned int>(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<uint8_t>(opcode));
m_ws.SendFrames({{frame}}, [selfweak = weak_from_this()](auto bufs, auto) {
if (auto self = selfweak.lock()) {
self->ReleaseBufs(bufs);

View File

@@ -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<WebSocketConnection> {
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<wpi::uv::Buffer> bufs);
wpi::WebSocket& m_ws;
wpi::Logger& m_logger;
class Stream;