Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ntcore] NT4 client: close timed-out connections #5175

Merged
merged 1 commit into from
Mar 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ntcore/src/main/native/cpp/NetworkClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ void NCImpl4::WsConnected(wpi::WebSocket& ws, uv::Tcp& tcp) {
});
ws.binary.connect([this](std::span<const uint8_t> data, bool) {
if (m_clientImpl) {
m_clientImpl->ProcessIncomingBinary(data);
m_clientImpl->ProcessIncomingBinary(m_loop.Now().count(), data);
}
});
}
Expand Down
19 changes: 15 additions & 4 deletions ntcore/src/main/native/cpp/net/ClientImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class CImpl : public ServerMessageHandler {
timeSyncUpdated,
std::function<void(uint32_t repeatMs)> setPeriodic);

void ProcessIncomingBinary(std::span<const uint8_t> data);
void ProcessIncomingBinary(uint64_t curTimeMs, std::span<const uint8_t> data);
void HandleLocal(std::vector<ClientMessage>&& msgs);
bool SendControl(uint64_t curTimeMs);
void SendValues(uint64_t curTimeMs, bool flush);
Expand Down Expand Up @@ -91,6 +91,7 @@ class CImpl : public ServerMessageHandler {
// timestamp handling
static constexpr uint32_t kPingIntervalMs = 3000;
uint64_t m_nextPingTimeMs{0};
uint64_t m_pongTimeMs{0};
uint32_t m_rtt2Us{UINT32_MAX};
bool m_haveTimeOffset{false};
int64_t m_serverTimeOffsetUs{0};
Expand Down Expand Up @@ -125,7 +126,8 @@ CImpl::CImpl(
m_setPeriodic(m_periodMs);
}

void CImpl::ProcessIncomingBinary(std::span<const uint8_t> data) {
void CImpl::ProcessIncomingBinary(uint64_t curTimeMs,
std::span<const uint8_t> data) {
for (;;) {
if (data.empty()) {
break;
Expand All @@ -150,6 +152,7 @@ void CImpl::ProcessIncomingBinary(std::span<const uint8_t> data) {
}
DEBUG4("RTT ping response time {} value {}", value.time(),
value.GetInteger());
m_pongTimeMs = curTimeMs;
int64_t now = wpi::Now();
int64_t rtt2 = (now - value.GetInteger()) / 2;
if (rtt2 < m_rtt2Us) {
Expand Down Expand Up @@ -207,6 +210,12 @@ bool CImpl::SendControl(uint64_t curTimeMs) {

// start a timestamp RTT ping if it's time to do one
if (curTimeMs >= m_nextPingTimeMs) {
// if we didn't receive a response to our last ping, disconnect
if (m_nextPingTimeMs != 0 && m_pongTimeMs == 0) {
m_wire.Disconnect("timed out");
return false;
}

if (!CheckNetworkReady(curTimeMs)) {
return false;
}
Expand All @@ -215,6 +224,7 @@ bool CImpl::SendControl(uint64_t curTimeMs) {
WireEncodeBinary(m_wire.SendBinary().Add(), -1, 0, Value::MakeInteger(now));
// drift isn't critical here, so just go from current time
m_nextPingTimeMs = curTimeMs + kPingIntervalMs;
m_pongTimeMs = 0;
}

if (!m_outgoing.empty()) {
Expand Down Expand Up @@ -465,8 +475,9 @@ void ClientImpl::ProcessIncomingText(std::string_view data) {
WireDecodeText(data, *m_impl, m_impl->m_logger);
}

void ClientImpl::ProcessIncomingBinary(std::span<const uint8_t> data) {
m_impl->ProcessIncomingBinary(data);
void ClientImpl::ProcessIncomingBinary(uint64_t curTimeMs,
std::span<const uint8_t> data) {
m_impl->ProcessIncomingBinary(curTimeMs, data);
}

void ClientImpl::HandleLocal(std::vector<ClientMessage>&& msgs) {
Expand Down
2 changes: 1 addition & 1 deletion ntcore/src/main/native/cpp/net/ClientImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ClientImpl {
~ClientImpl();

void ProcessIncomingText(std::string_view data);
void ProcessIncomingBinary(std::span<const uint8_t> data);
void ProcessIncomingBinary(uint64_t curTimeMs, std::span<const uint8_t> data);
void HandleLocal(std::vector<ClientMessage>&& msgs);

void SendControl(uint64_t curTimeMs);
Expand Down