[ntcore] Use last received time instead of last ping response

This relaxes the timeout constraint for long message transmissions.
This commit is contained in:
Peter Johnson
2024-01-19 21:26:00 -08:00
parent 9a5366bb83
commit 789af2ad26
8 changed files with 14 additions and 28 deletions

View File

@@ -403,7 +403,6 @@ void NetworkClient::WsConnected(wpi::WebSocket& ws, uv::Tcp& tcp,
m_wire =
std::make_shared<net::WebSocketConnection>(ws, connInfo.protocol_version);
m_wire->Start();
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

@@ -281,7 +281,6 @@ void NetworkServer::ServerConnection4::ProcessWsUpgrade() {
INFO("CONNECTED NT4 client '{}' (from {})", dedupName, m_connInfo);
m_info.remote_id = dedupName;
m_server.AddConnection(this, m_info);
m_wire->Start();
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,

View File

@@ -12,14 +12,15 @@ bool NetworkPing::Send(uint64_t curTimeMs) {
if (curTimeMs < m_nextPingTimeMs) {
return true;
}
// if we didn't receive a timely response to our last ping, disconnect
uint64_t lastPing = m_wire.GetLastPingResponse();
// if we haven't received data in a while, disconnect
// (we should at least be getting PONG responses)
uint64_t lastData = m_wire.GetLastReceivedTime();
// DEBUG4("WS ping: lastPing={} curTime={} pongTimeMs={}\n", lastPing,
// curTimeMs, m_pongTimeMs);
if (lastPing == 0) {
lastPing = m_pongTimeMs;
if (lastData == 0) {
lastData = m_pongTimeMs;
}
if (m_pongTimeMs != 0 && curTimeMs > (lastPing + kPingTimeoutMs)) {
if (m_pongTimeMs != 0 && curTimeMs > (lastData + kPingTimeoutMs)) {
m_wire.Disconnect("connection timed out");
return false;
}

View File

@@ -116,18 +116,6 @@ WebSocketConnection::~WebSocketConnection() {
}
}
void WebSocketConnection::Start() {
m_ws.pong.connect([selfweak = weak_from_this()](auto data) {
if (data.size() != 8) {
return;
}
if (auto self = selfweak.lock()) {
self->m_lastPingResponse =
wpi::support::endian::read64<wpi::support::native>(data.data());
}
});
}
void WebSocketConnection::SendPing(uint64_t time) {
auto buf = AllocBuf();
buf.len = 8;

View File

@@ -26,8 +26,6 @@ class WebSocketConnection final
WebSocketConnection(const WebSocketConnection&) = delete;
WebSocketConnection& operator=(const WebSocketConnection&) = delete;
void Start();
unsigned int GetVersion() const final { return m_version; }
void SendPing(uint64_t time) final;
@@ -51,7 +49,9 @@ class WebSocketConnection final
uint64_t GetLastFlushTime() const final { return m_lastFlushTime; }
uint64_t GetLastPingResponse() const final { return m_lastPingResponse; }
uint64_t GetLastReceivedTime() const final {
return m_ws.GetLastReceivedTime();
}
void Disconnect(std::string_view reason) final;
@@ -92,7 +92,6 @@ class WebSocketConnection final
State m_state = kEmpty;
std::string m_reason;
uint64_t m_lastFlushTime = 0;
uint64_t m_lastPingResponse = 0;
unsigned int m_version;
};

View File

@@ -51,8 +51,8 @@ class WireConnection {
virtual uint64_t GetLastFlushTime() const = 0; // in microseconds
// Gets the timestamp of the last ping we got a reply to
virtual uint64_t GetLastPingResponse() const = 0; // in microseconds
// Gets the timestamp of the last incoming data
virtual uint64_t GetLastReceivedTime() const = 0; // in microseconds
virtual void Disconnect(std::string_view reason) = 0;
};