[ntcore] Use wpi::Now instead of loop time for transmit time

As the send function is called after local processing, there can be a
substantial delay between the loop time and the actual send.
This commit is contained in:
Peter Johnson
2023-02-12 23:21:08 -08:00
parent 226ef35212
commit 843574a810
11 changed files with 34 additions and 7 deletions

View File

@@ -32,7 +32,7 @@ static constexpr uint32_t kMinPeriodMs = 5;
// maximum amount of time the wire can be not ready to send another
// transmission before we close the connection
static constexpr uint32_t kWireMaxNotReadyMs = 1000;
static constexpr uint32_t kWireMaxNotReadyUs = 1000000;
namespace {
@@ -313,7 +313,9 @@ void CImpl::SendInitialValues() {
bool CImpl::CheckNetworkReady(uint64_t curTimeMs) {
if (!m_wire.Ready()) {
if (m_lastSendMs != 0 && curTimeMs > (m_lastSendMs + kWireMaxNotReadyMs)) {
uint64_t lastFlushTime = m_wire.GetLastFlushTime();
uint64_t now = wpi::Now();
if (lastFlushTime != 0 && now > (lastFlushTime + kWireMaxNotReadyUs)) {
m_wire.Disconnect("transmit stalled");
}
return false;

View File

@@ -50,7 +50,7 @@ static constexpr uint32_t kMinPeriodMs = 5;
// maximum amount of time the wire can be not ready to send another
// transmission before we close the connection
static constexpr uint32_t kWireMaxNotReadyMs = 1000;
static constexpr uint32_t kWireMaxNotReadyUs = 1000000;
namespace {
@@ -952,7 +952,9 @@ void ClientData4::SendOutgoing(uint64_t curTimeMs) {
}
if (!m_wire.Ready()) {
if (m_lastSendMs != 0 && curTimeMs > (m_lastSendMs + kWireMaxNotReadyMs)) {
uint64_t lastFlushTime = m_wire.GetLastFlushTime();
uint64_t now = wpi::Now();
if (lastFlushTime != 0 && now > (lastFlushTime + kWireMaxNotReadyUs)) {
m_wire.Disconnect("transmit stalled");
}
return;
@@ -1123,7 +1125,9 @@ void ClientData3::SendOutgoing(uint64_t curTimeMs) {
}
if (!m_wire.Ready()) {
if (m_lastSendMs != 0 && curTimeMs > (m_lastSendMs + kWireMaxNotReadyMs)) {
uint64_t lastFlushTime = m_wire.GetLastFlushTime();
uint64_t now = wpi::Now();
if (lastFlushTime != 0 && now > (lastFlushTime + kWireMaxNotReadyUs)) {
m_wire.Disconnect("transmit stalled");
}
return;

View File

@@ -7,6 +7,7 @@
#include <span>
#include <wpi/SpanExtras.h>
#include <wpi/timestamp.h>
#include <wpinet/WebSocket.h>
using namespace nt;
@@ -67,6 +68,7 @@ void WebSocketConnection::Flush() {
m_binary_buffers.clear();
m_text_pos = 0;
m_binary_pos = 0;
m_lastFlushTime = wpi::Now();
}
void WebSocketConnection::Disconnect(std::string_view reason) {

View File

@@ -34,6 +34,8 @@ class WebSocketConnection final
void Flush() final;
uint64_t GetLastFlushTime() const final { return m_lastFlushTime; }
void Disconnect(std::string_view reason) final;
std::string_view GetDisconnectReason() const { return m_reason; }
@@ -69,6 +71,7 @@ class WebSocketConnection final
bool m_in_text = false;
int m_sendsActive = 0;
std::string m_reason;
uint64_t m_lastFlushTime = 0;
};
} // namespace nt::net

View File

@@ -30,6 +30,8 @@ class WireConnection {
virtual void Flush() = 0;
virtual uint64_t GetLastFlushTime() const = 0; // in microseconds
virtual void Disconnect(std::string_view reason) = 0;
protected: