Add alternate timestamp implementations
Add mach_absolute_time and gettimeofday timestamp implementations, and fix clock_gettime check so that it can actually succeed
This commit is contained in:
committed by
Keith Winstein
parent
26e9e91adb
commit
582bf347c9
+8
-1
@@ -70,7 +70,14 @@ AC_FUNC_FORK
|
||||
AC_FUNC_MALLOC
|
||||
AC_FUNC_MBRTOWC
|
||||
AC_FUNC_REALLOC
|
||||
AC_CHECK_FUNCS([clock_gettime gettimeofday inet_ntoa iswprint memchr memset nl_langinfo setenv setlocale socket strchr strdup strerror strtol wcwidth])
|
||||
AC_CHECK_FUNCS([gettimeofday inet_ntoa iswprint memchr memset nl_langinfo setenv setlocale socket strchr strdup strerror strtol wcwidth])
|
||||
|
||||
AC_SEARCH_LIBS([clock_gettime], [rt], [AC_DEFINE([HAVE_CLOCK_GETTIME], [1], [Define if clock_gettime is available.])])
|
||||
|
||||
AC_CHECK_DECL([mach_absolute_time],
|
||||
[AC_DEFINE([HAVE_MACH_ABSOLUTE_TIME], [1],
|
||||
[Define if mach_absolute_time is available.])],
|
||||
, [[#include <mach/mach_time.h>]])
|
||||
|
||||
AC_CHECK_DECL([htobe64],
|
||||
[AC_DEFINE([HAVE_HTOBE64], [1],
|
||||
|
||||
@@ -20,4 +20,4 @@ termemu_LDADD = ../terminal/libmoshterminal.a ../util/libmoshutil.a ../statesync
|
||||
|
||||
ntester_SOURCES = ntester.cc
|
||||
ntester_CPPFLAGS = -I$(srcdir)/../statesync -I$(srcdir)/../terminal -I$(srcdir)/../network -I$(srcdir)/../crypto -I$(builddir)/../protobufs
|
||||
ntester_LDADD = ../statesync/libmoshstatesync.a ../terminal/libmoshterminal.a ../network/libmoshnetwork.a ../crypto/libmoshcrypto.a ../protobufs/libmoshprotos.a -lutil -lrt -lm $(protobuf_LIBS)
|
||||
ntester_LDADD = ../statesync/libmoshstatesync.a ../terminal/libmoshterminal.a ../network/libmoshnetwork.a ../crypto/libmoshcrypto.a ../protobufs/libmoshprotos.a -lutil -lm $(protobuf_LIBS)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
AM_CPPFLAGS = -I$(srcdir)/../statesync -I$(srcdir)/../terminal -I$(srcdir)/../network -I$(srcdir)/../crypto -I$(builddir)/../protobufs -I$(srcdir)/../util
|
||||
AM_CXXFLAGS = $(WARNING_CXXFLAGS) $(PICKY_CXXFLAGS) -fno-default-inline -pipe
|
||||
LDADD = ../crypto/libmoshcrypto.a ../network/libmoshnetwork.a ../statesync/libmoshstatesync.a ../terminal/libmoshterminal.a ../util/libmoshutil.a ../protobufs/libmoshprotos.a -lutil -lrt -lm $(protobuf_LIBS)
|
||||
LDADD = ../crypto/libmoshcrypto.a ../network/libmoshnetwork.a ../statesync/libmoshstatesync.a ../terminal/libmoshterminal.a ../util/libmoshutil.a ../protobufs/libmoshprotos.a -lutil -lm $(protobuf_LIBS)
|
||||
|
||||
bin_PROGRAMS = mosh-client mosh-server
|
||||
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
@@ -23,6 +25,14 @@
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
|
||||
#if HAVE_CLOCK_GETTIME
|
||||
#include <time.h>
|
||||
#elif HAVE_MACH_ABSOLUTE_TIME
|
||||
#include <mach/mach_time.h>
|
||||
#elif HAVE_GETTIMEOFDAY
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
#include "dos_assert.h"
|
||||
#include "byteorder.h"
|
||||
#include "network.h"
|
||||
@@ -282,6 +292,7 @@ int Connection::port( void ) const
|
||||
|
||||
uint64_t Network::timestamp( void )
|
||||
{
|
||||
#if HAVE_CLOCK_GETTIME
|
||||
struct timespec tp;
|
||||
|
||||
if ( clock_gettime( CLOCK_MONOTONIC, &tp ) < 0 ) {
|
||||
@@ -292,6 +303,30 @@ uint64_t Network::timestamp( void )
|
||||
millis += uint64_t( tp.tv_sec ) * 1000;
|
||||
|
||||
return millis;
|
||||
#elif HAVE_MACH_ABSOLUTE_TIME
|
||||
static mach_timebase_info_data_t s_timebase_info;
|
||||
|
||||
if (s_timebase_info.denom == 0) {
|
||||
mach_timebase_info(&s_timebase_info);
|
||||
}
|
||||
|
||||
// NB: mach_absolute_time() returns "absolute time units"
|
||||
// We need to apply a conversion to get milliseconds.
|
||||
return ((mach_absolute_time() * s_timebase_info.numer) / (1000000 * s_timebase_info.denom));
|
||||
#elif HAVE_GETTIMEOFDAY
|
||||
// NOTE: If time steps backwards, timeouts may be confused.
|
||||
struct timeval tv;
|
||||
if ( gettimeofday(&tv, NULL) ) {
|
||||
throw NetworkException( "gettimeofday", errno );
|
||||
}
|
||||
|
||||
uint64_t millis = tv.tv_usec / 1000;
|
||||
millis += uint64_t( tv.tv_sec ) * 1000;
|
||||
|
||||
return millis;
|
||||
#else
|
||||
# error "Don't know how to get a timestamp on this platform"
|
||||
#endif
|
||||
}
|
||||
|
||||
uint16_t Network::timestamp16( void )
|
||||
|
||||
Reference in New Issue
Block a user