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:
Quentin Smith
2012-02-26 00:29:29 -05:00
committed by Keith Winstein
parent 26e9e91adb
commit 582bf347c9
4 changed files with 45 additions and 3 deletions
+1 -1
View File
@@ -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 -1
View File
@@ -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
+35
View File
@@ -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 )