[ntcore] Improve disconnect error reporting (#5085)

Also fix memory leak in WebSocketConnection destructor.
This commit is contained in:
Peter Johnson
2023-02-11 22:56:29 -08:00
committed by GitHub
parent 01f0394419
commit e57ded8c39
5 changed files with 21 additions and 5 deletions

View File

@@ -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);

View File

@@ -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) {

View File

@@ -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<const ClientMessage> msgs) {

View File

@@ -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);
}

View File

@@ -5,6 +5,8 @@
#pragma once
#include <memory>
#include <string>
#include <string_view>
#include <vector>
#include <wpi/SmallVector.h>
@@ -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