diff --git a/ntcore/src/main/native/cpp/NetworkClient.cpp b/ntcore/src/main/native/cpp/NetworkClient.cpp index e3a18e0ef6..a95066db48 100644 --- a/ntcore/src/main/native/cpp/NetworkClient.cpp +++ b/ntcore/src/main/native/cpp/NetworkClient.cpp @@ -457,7 +457,9 @@ void NCImpl4::WsConnected(wpi::WebSocket& ws, uv::Tcp& tcp) { } void NCImpl4::Disconnect(std::string_view reason) { - INFO("DISCONNECTED NT4 connection: {}", reason); + auto realReason = m_wire->GetDisconnectReason(); + INFO("DISCONNECTED NT4 connection: {}", + realReason.empty() ? reason : realReason); m_clientImpl.reset(); m_wire.reset(); NCImpl::Disconnect(reason); diff --git a/ntcore/src/main/native/cpp/NetworkServer.cpp b/ntcore/src/main/native/cpp/NetworkServer.cpp index 4609544453..c60796a7cc 100644 --- a/ntcore/src/main/native/cpp/NetworkServer.cpp +++ b/ntcore/src/main/native/cpp/NetworkServer.cpp @@ -255,8 +255,9 @@ void ServerConnection4::ProcessWsUpgrade() { m_info.remote_id = dedupName; m_server.AddConnection(this, m_info); m_websocket->closed.connect([this](uint16_t, std::string_view reason) { + auto realReason = m_wire->GetDisconnectReason(); INFO("DISCONNECTED NT4 client '{}' (from {}): {}", m_info.remote_id, - m_connInfo, reason); + m_connInfo, realReason.empty() ? reason : realReason); ConnectionClosed(); }); m_websocket->text.connect([this](std::string_view data, bool) { diff --git a/ntcore/src/main/native/cpp/net/ServerImpl.cpp b/ntcore/src/main/native/cpp/net/ServerImpl.cpp index df9f9e9138..2813128249 100644 --- a/ntcore/src/main/native/cpp/net/ServerImpl.cpp +++ b/ntcore/src/main/native/cpp/net/ServerImpl.cpp @@ -2289,9 +2289,10 @@ void ServerImpl::SendControl(uint64_t curTimeMs) { } void ServerImpl::SendValues(int clientId, uint64_t curTimeMs) { - auto client = m_impl->m_clients[clientId].get(); - client->SendOutgoing(curTimeMs); - client->Flush(); + if (auto client = m_impl->m_clients[clientId].get()) { + client->SendOutgoing(curTimeMs); + client->Flush(); + } } void ServerImpl::HandleLocal(std::span msgs) { diff --git a/ntcore/src/main/native/cpp/net/WebSocketConnection.cpp b/ntcore/src/main/native/cpp/net/WebSocketConnection.cpp index 9a7aa0dfe7..682a75e62c 100644 --- a/ntcore/src/main/native/cpp/net/WebSocketConnection.cpp +++ b/ntcore/src/main/native/cpp/net/WebSocketConnection.cpp @@ -25,6 +25,12 @@ WebSocketConnection::~WebSocketConnection() { for (auto&& buf : m_buf_pool) { buf.Deallocate(); } + for (auto&& buf : m_text_buffers) { + buf.Deallocate(); + } + for (auto&& buf : m_binary_buffers) { + buf.Deallocate(); + } } void WebSocketConnection::Flush() { @@ -64,6 +70,7 @@ void WebSocketConnection::Flush() { } void WebSocketConnection::Disconnect(std::string_view reason) { + m_reason = reason; m_ws.Close(1005, reason); } diff --git a/ntcore/src/main/native/cpp/net/WebSocketConnection.h b/ntcore/src/main/native/cpp/net/WebSocketConnection.h index ba15215dc5..40aadb467d 100644 --- a/ntcore/src/main/native/cpp/net/WebSocketConnection.h +++ b/ntcore/src/main/native/cpp/net/WebSocketConnection.h @@ -5,6 +5,8 @@ #pragma once #include +#include +#include #include #include @@ -34,6 +36,8 @@ class WebSocketConnection final void Disconnect(std::string_view reason) final; + std::string_view GetDisconnectReason() const { return m_reason; } + private: void StartSendText() final; void FinishSendText() final; @@ -64,6 +68,7 @@ class WebSocketConnection final size_t m_binary_pos = 0; bool m_in_text = false; int m_sendsActive = 0; + std::string m_reason; }; } // namespace nt::net