From 4d0ed822e39b4a190cac779f2a39df01b557741a Mon Sep 17 00:00:00 2001 From: Gabriel Braga Date: Tue, 9 Apr 2024 17:31:36 +0200 Subject: [PATCH] Change monotonic_clock to get time in milliseconds for the epoll API Previously the clock would return in seconds, which is not the epoll's standards. This fixes it. --- src/command_client.cpp | 8 ++++---- src/monotonic_clock.cpp | 27 +++++++-------------------- src/monotonic_clock.hxx | 3 +-- src/server.cpp | 4 ++-- 4 files changed, 14 insertions(+), 28 deletions(-) diff --git a/src/command_client.cpp b/src/command_client.cpp index 0780f73..ec4280f 100644 --- a/src/command_client.cpp +++ b/src/command_client.cpp @@ -128,9 +128,9 @@ std::string command_client::read_packet(const int &millisec_timeout) while(!(got_packet=c->get_packet(resultpacket)) && my_timeout > 0 && !c->is_closed()) { - timeout_checkpoint=monotonic_clock_gettime_sec(); + timeout_checkpoint=monotonic_clock_gettime_msec(); c->fill_buffer(millisec_timeout); - my_timeout -= abs(monotonic_clock_gettime_sec() - timeout_checkpoint); + my_timeout -= abs(monotonic_clock_gettime_msec() - timeout_checkpoint); } @@ -151,9 +151,9 @@ void command_client::read_hello() int my_timeout=hello_timeout_millisec, timeout_checkpoint; while(!(got_packet=c->get_packet(resultpacket)) && my_timeout > 0 && !c->is_closed()) { - timeout_checkpoint=monotonic_clock_gettime_sec(); + timeout_checkpoint=monotonic_clock_gettime_msec(); c->fill_buffer(my_timeout); - my_timeout -= abs(monotonic_clock_gettime_sec() - timeout_checkpoint); + my_timeout -= abs(monotonic_clock_gettime_msec() - timeout_checkpoint); c->peek_packet(resultpacket); check_hello(resultpacket); // will throw before timeout if wrong data received diff --git a/src/monotonic_clock.cpp b/src/monotonic_clock.cpp index 3b4e720..7445f14 100644 --- a/src/monotonic_clock.cpp +++ b/src/monotonic_clock.cpp @@ -26,31 +26,18 @@ on this file might be covered by the GNU General Public License. /** * @brief fetches the value from the monotonic clock source. - * @param[out] seconds the seconds. - * @param[out] nano_seconds the nano seconds. - * @return @a true if the clock was successfully read. + * @return the time since system start in milliseconds, -1 if read was unsuccessful */ -bool monotonic_clock_gettime(long int& seconds, long int& nano_seconds) +int monotonic_clock_gettime_msec() { struct timespec tp[1]; + int millisec, nano_seconds; int res= clock_gettime (CLOCK_MONOTONIC, tp); if (0 == res) { - seconds= tp->tv_sec; nano_seconds= tp->tv_nsec; + millisec = nano_seconds/1000000; + return millisec; } - return (res==0); -} - -/** - * @brief fetches the value from the monotonic clock source. - * @return the time since system start in seconds, 0 if read was unsuccessful - */ -int monotonic_clock_gettime_sec() -{ - long int seconds=0; - long int nano_seconds; - - monotonic_clock_gettime(seconds,nano_seconds); - return seconds; -} + return -1; +} \ No newline at end of file diff --git a/src/monotonic_clock.hxx b/src/monotonic_clock.hxx index 98a9583..f5c4830 100644 --- a/src/monotonic_clock.hxx +++ b/src/monotonic_clock.hxx @@ -22,7 +22,6 @@ on this file might be covered by the GNU General Public License. #ifndef __LIBT2N_MONOTONIC_CLOCK #define __LIBT2N_MONOTONIC_CLOCK -bool monotonic_clock_gettime(long int& seconds, long int& nano_seconds); -int monotonic_clock_gettime_sec(); +int monotonic_clock_gettime_msec(); #endif \ No newline at end of file diff --git a/src/server.cpp b/src/server.cpp index c61dbf4..45f8672 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -66,7 +66,7 @@ std::ostream* server_connection::get_logstream(log_level_values level) /// check if timeout is expired, close connection if so void server_connection::check_timeout() { - if (timeout != -1 && last_action_time+timeout < monotonic_clock_gettime_sec()) + if (timeout != -1 && last_action_time+timeout < monotonic_clock_gettime_msec()) { LOGSTREAM(debug,"timeout on connection " << connection_id << ", closing"); this->close(); @@ -76,7 +76,7 @@ void server_connection::check_timeout() /// reset the timeout, e.g. if something is received void server_connection::reset_timeout() { - last_action_time=monotonic_clock_gettime_sec(); + last_action_time=monotonic_clock_gettime_msec(); } /** @brief add a callback to one connection -- 1.7.1