From 9a5366bb83d20690340bfcd2eddcebee5c42cc2f Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Fri, 19 Jan 2024 21:25:13 -0800 Subject: [PATCH] [wpiutil] WebSocket: Add GetLastReceivedTime This allows getting the timestamp that any data has been received. --- wpinet/src/main/native/cpp/WebSocket.cpp | 2 ++ wpinet/src/main/native/include/wpinet/WebSocket.h | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/wpinet/src/main/native/cpp/WebSocket.cpp b/wpinet/src/main/native/cpp/WebSocket.cpp index ffe2d2455e..001ba07002 100644 --- a/wpinet/src/main/native/cpp/WebSocket.cpp +++ b/wpinet/src/main/native/cpp/WebSocket.cpp @@ -418,6 +418,8 @@ static inline void Unmask(std::span data, } void WebSocket::HandleIncoming(uv::Buffer& buf, size_t size) { + m_lastReceivedTime = m_stream.GetLoopRef().Now().count(); + // ignore incoming data if we're failed or closed if (m_state == FAILED || m_state == CLOSED) { return; diff --git a/wpinet/src/main/native/include/wpinet/WebSocket.h b/wpinet/src/main/native/include/wpinet/WebSocket.h index 297c1235fd..ef7cbdb2c4 100644 --- a/wpinet/src/main/native/include/wpinet/WebSocket.h +++ b/wpinet/src/main/native/include/wpinet/WebSocket.h @@ -457,6 +457,12 @@ class WebSocket : public std::enable_shared_from_this { */ void Shutdown(); + /** + * Gets the last time data was received on the stream. + * @return Timestamp + */ + uint64_t GetLastReceivedTime() const { return m_lastReceivedTime; } + /** * Open event. Emitted when the connection is open and ready to communicate. * The parameter is the selected subprotocol. @@ -521,6 +527,7 @@ class WebSocket : public std::enable_shared_from_this { State m_state = CONNECTING; // incoming message buffers/state + uint64_t m_lastReceivedTime = 0; SmallVector m_header; size_t m_headerSize = 0; SmallVector m_payload;