diff options
author | Pierre Ossman <ossman@cendio.se> | 2022-11-18 09:29:08 +0100 |
---|---|---|
committer | Pierre Ossman <ossman@cendio.se> | 2025-02-25 16:58:30 +0100 |
commit | d1e1e122ca5ab3e7f0b75f70421105f3046a4a0e (patch) | |
tree | e3c0e2a37710992edfe320296f7a1cd2941e2754 | |
parent | 0fcb68f8a4e86a3090f9cc3542be9c1a851c8a1d (diff) | |
download | tigervnc-d1e1e122ca5ab3e7f0b75f70421105f3046a4a0e.tar.gz tigervnc-d1e1e122ca5ab3e7f0b75f70421105f3046a4a0e.zip |
Move time utilities to separate file
Let's clear things up by categorizing our utility functions.
-rw-r--r-- | common/core/CMakeLists.txt | 1 | ||||
-rw-r--r-- | common/core/Timer.cxx | 1 | ||||
-rw-r--r-- | common/core/time.cxx | 65 | ||||
-rw-r--r-- | common/core/time.h | 51 | ||||
-rw-r--r-- | common/core/util.cxx | 35 | ||||
-rw-r--r-- | common/core/util.h | 23 | ||||
-rw-r--r-- | common/rdr/FdOutStream.cxx | 2 | ||||
-rw-r--r-- | common/rfb/Congestion.cxx | 1 | ||||
-rw-r--r-- | common/rfb/VNCSConnectionST.cxx | 2 | ||||
-rw-r--r-- | common/rfb/VNCServerST.cxx | 2 | ||||
-rw-r--r-- | tests/perf/fbperf.cxx | 1 | ||||
-rw-r--r-- | vncviewer/BaseTouchHandler.cxx | 2 | ||||
-rw-r--r-- | vncviewer/DesktopWindow.cxx | 1 | ||||
-rw-r--r-- | vncviewer/GestureHandler.cxx | 2 | ||||
-rw-r--r-- | win/rfb_win32/SocketManager.cxx | 2 |
15 files changed, 129 insertions, 62 deletions
diff --git a/common/core/CMakeLists.txt b/common/core/CMakeLists.txt index 7f3e9983..d92d82a4 100644 --- a/common/core/CMakeLists.txt +++ b/common/core/CMakeLists.txt @@ -7,6 +7,7 @@ add_library(core STATIC LogWriter.cxx Region.cxx Timer.cxx + time.cxx util.cxx) target_link_libraries(core os) diff --git a/common/core/Timer.cxx b/common/core/Timer.cxx index 5c0fcc29..ee9e0e08 100644 --- a/common/core/Timer.cxx +++ b/common/core/Timer.cxx @@ -30,6 +30,7 @@ #include <core/LogWriter.h> #include <core/Timer.h> +#include <core/time.h> #include <core/util.h> using namespace core; diff --git a/common/core/time.cxx b/common/core/time.cxx new file mode 100644 index 00000000..dbd42371 --- /dev/null +++ b/common/core/time.cxx @@ -0,0 +1,65 @@ +/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. + * Copyright 2011-2019 Pierre Ossman for Cendio AB + * + * This is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + * USA. + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <stddef.h> +#include <sys/time.h> + +#include <core/time.h> + +namespace core { + + unsigned msBetween(const struct timeval *first, + const struct timeval *second) + { + unsigned diff; + + diff = (second->tv_sec - first->tv_sec) * 1000; + + diff += second->tv_usec / 1000; + diff -= first->tv_usec / 1000; + + return diff; + } + + unsigned msSince(const struct timeval *then) + { + struct timeval now; + + gettimeofday(&now, nullptr); + + return msBetween(then, &now); + } + + bool isBefore(const struct timeval *first, + const struct timeval *second) + { + if (first->tv_sec < second->tv_sec) + return true; + if (first->tv_sec > second->tv_sec) + return false; + if (first->tv_usec < second->tv_usec) + return true; + return false; + } + +} diff --git a/common/core/time.h b/common/core/time.h new file mode 100644 index 00000000..05707ff2 --- /dev/null +++ b/common/core/time.h @@ -0,0 +1,51 @@ +/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. + * Copyright 2011-2019 Pierre Ossman for Cendio AB + * + * This is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + * USA. + */ + +// +// time.h - time helper functions +// + +#ifndef __CORE_TIME_H__ +#define __CORE_TIME_H__ + +#include <limits.h> + +struct timeval; + +namespace core { + + // secsToMillis() turns seconds into milliseconds, capping the value so it + // can't wrap round and become -ve + inline int secsToMillis(int secs) { + return (secs < 0 || secs > (INT_MAX/1000) ? INT_MAX : secs * 1000); + } + + // Returns time elapsed between two moments in milliseconds. + unsigned msBetween(const struct timeval *first, + const struct timeval *second); + + // Returns time elapsed since given moment in milliseconds. + unsigned msSince(const struct timeval *then); + + // Returns true if first happened before seconds + bool isBefore(const struct timeval *first, + const struct timeval *second); +} + +#endif diff --git a/common/core/util.cxx b/common/core/util.cxx index 1daff5ae..d6199ccd 100644 --- a/common/core/util.cxx +++ b/common/core/util.cxx @@ -26,7 +26,6 @@ #include <stdarg.h> #include <stdio.h> #include <string.h> -#include <sys/time.h> #include <core/util.h> @@ -604,40 +603,6 @@ namespace core { return true; } - unsigned msBetween(const struct timeval *first, - const struct timeval *second) - { - unsigned diff; - - diff = (second->tv_sec - first->tv_sec) * 1000; - - diff += second->tv_usec / 1000; - diff -= first->tv_usec / 1000; - - return diff; - } - - unsigned msSince(const struct timeval *then) - { - struct timeval now; - - gettimeofday(&now, nullptr); - - return msBetween(then, &now); - } - - bool isBefore(const struct timeval *first, - const struct timeval *second) - { - if (first->tv_sec < second->tv_sec) - return true; - if (first->tv_sec > second->tv_sec) - return false; - if (first->tv_usec < second->tv_usec) - return true; - return false; - } - static std::string doPrefix(long long value, const char *unit, unsigned divisor, const char **prefixes, size_t prefixCount, int precision) { diff --git a/common/core/util.h b/common/core/util.h index e5927773..9666a93a 100644 --- a/common/core/util.h +++ b/common/core/util.h @@ -24,14 +24,11 @@ #ifndef __CORE_UTIL_H__ #define __CORE_UTIL_H__ -#include <limits.h> #include <stdint.h> #include <string> #include <vector> -struct timeval; - namespace core { // Formats according to printf(), with a dynamic allocation @@ -71,24 +68,8 @@ namespace core { bool isValidUTF8(const char* str, size_t bytes = (size_t)-1); bool isValidUTF16(const wchar_t* wstr, size_t units = (size_t)-1); - // HELPER functions for timeout handling - - // secsToMillis() turns seconds into milliseconds, capping the value so it - // can't wrap round and become -ve - inline int secsToMillis(int secs) { - return (secs < 0 || secs > (INT_MAX/1000) ? INT_MAX : secs * 1000); - } - - // Returns time elapsed between two moments in milliseconds. - unsigned msBetween(const struct timeval *first, - const struct timeval *second); - - // Returns time elapsed since given moment in milliseconds. - unsigned msSince(const struct timeval *then); - - // Returns true if first happened before seconds - bool isBefore(const struct timeval *first, - const struct timeval *second); + // Convert a value to a string using the correct prefix to reduce + // the length of the string std::string siPrefix(long long value, const char *unit, int precision=6); diff --git a/common/rdr/FdOutStream.cxx b/common/rdr/FdOutStream.cxx index 705f410f..d72a34a6 100644 --- a/common/rdr/FdOutStream.cxx +++ b/common/rdr/FdOutStream.cxx @@ -45,7 +45,7 @@ #endif #include <core/Exception.h> -#include <core/util.h> +#include <core/time.h> #include <rdr/FdOutStream.h> diff --git a/common/rfb/Congestion.cxx b/common/rfb/Congestion.cxx index afe66399..7008bc33 100644 --- a/common/rfb/Congestion.cxx +++ b/common/rfb/Congestion.cxx @@ -51,6 +51,7 @@ #include <core/LogWriter.h> #include <core/util.h> +#include <core/time.h> #include <rfb/Congestion.h> diff --git a/common/rfb/VNCSConnectionST.cxx b/common/rfb/VNCSConnectionST.cxx index 20439d18..caecc2a1 100644 --- a/common/rfb/VNCSConnectionST.cxx +++ b/common/rfb/VNCSConnectionST.cxx @@ -23,7 +23,7 @@ #endif #include <core/LogWriter.h> -#include <core/util.h> +#include <core/time.h> #include <rdr/FdInStream.h> #include <rdr/FdOutStream.h> diff --git a/common/rfb/VNCServerST.cxx b/common/rfb/VNCServerST.cxx index b45d926d..51bc5103 100644 --- a/common/rfb/VNCServerST.cxx +++ b/common/rfb/VNCServerST.cxx @@ -56,7 +56,7 @@ #include <stdlib.h> #include <core/LogWriter.h> -#include <core/util.h> +#include <core/time.h> #include <rdr/FdOutStream.h> diff --git a/tests/perf/fbperf.cxx b/tests/perf/fbperf.cxx index ab7f4ba7..0f87dcf5 100644 --- a/tests/perf/fbperf.cxx +++ b/tests/perf/fbperf.cxx @@ -28,6 +28,7 @@ #include <FL/fl_draw.H> #include <FL/x.H> +#include <core/time.h> #include <core/util.h> #include "../vncviewer/PlatformPixelBuffer.h" diff --git a/vncviewer/BaseTouchHandler.cxx b/vncviewer/BaseTouchHandler.cxx index 02cd30c7..3100f86b 100644 --- a/vncviewer/BaseTouchHandler.cxx +++ b/vncviewer/BaseTouchHandler.cxx @@ -24,7 +24,7 @@ #include <stdlib.h> #include <math.h> -#include <core/util.h> +#include <core/time.h> #define XK_MISCELLANY #include <rfb/keysymdef.h> diff --git a/vncviewer/DesktopWindow.cxx b/vncviewer/DesktopWindow.cxx index 5e468e1d..c2f0dcbc 100644 --- a/vncviewer/DesktopWindow.cxx +++ b/vncviewer/DesktopWindow.cxx @@ -29,6 +29,7 @@ #include <sys/time.h> #include <core/LogWriter.h> +#include <core/time.h> #include <core/util.h> #include <rfb/CMsgWriter.h> diff --git a/vncviewer/GestureHandler.cxx b/vncviewer/GestureHandler.cxx index f39ec8f2..2ede991b 100644 --- a/vncviewer/GestureHandler.cxx +++ b/vncviewer/GestureHandler.cxx @@ -25,7 +25,7 @@ #include <math.h> #include <core/LogWriter.h> -#include <core/util.h> +#include <core/time.h> #include "GestureHandler.h" diff --git a/win/rfb_win32/SocketManager.cxx b/win/rfb_win32/SocketManager.cxx index 2f80e28b..9a22ee14 100644 --- a/win/rfb_win32/SocketManager.cxx +++ b/win/rfb_win32/SocketManager.cxx @@ -28,7 +28,7 @@ #include <core/Exception.h> #include <core/LogWriter.h> #include <core/Timer.h> -#include <core/util.h> +#include <core/time.h> #include <rdr/FdOutStream.h> |