13#define VRPN_PI (3.14159265358979323846)
14#define VRPN_INCHES_TO_METERS (2.54/100.0)
15#define VRPN_DEGREES_TO_RADIANS (VRPN_PI/180.0)
23#if defined(__ANDROID__)
39#undef VRPN_USE_WINSOCK_SOCKETS
44#if defined(_WIN32) && \
45 (!defined(__CYGWIN__) || defined(VRPN_CYGWIN_USES_WINSOCK_SOCKETS))
46#define VRPN_USE_WINSOCK_SOCKETS
49#ifndef VRPN_USE_WINSOCK_SOCKETS
51#define INVALID_SOCKET -1
55#if !(defined(_WIN32) && defined(VRPN_USE_WINSOCK_SOCKETS))
56#include <sys/select.h>
57#include <netinet/in.h>
61#define perror(x) fprintf(stderr, "%s\n", x);
72#define VRPN_USE_WINDOWS_GETHOSTBYNAME_HACK
93#if (!defined(VRPN_USE_WINSOCK_SOCKETS))
98#ifndef VRPN_USE_STD_CHRONO
99 #define vrpn_gettimeofday gettimeofday
116#ifndef WIN32_LEAN_AND_MEAN
117#define WIN32_LEAN_AND_MEAN
121#include <sys/timeb.h>
123#ifdef VRPN_USE_WINSOCK2
140#if defined(VRPN_EXPORT_GETTIMEOFDAY)
143#define gettimeofday vrpn_gettimeofday
164 const struct timeval &tv2);
166 const struct timeval &tv2);
172 struct timeval startT);
177 struct timeval startT);
180 const struct timeval &tv2);
182 const struct timeval &tv2);
205static const int vrpn_int_data_for_endian_test = 1;
206static const char *vrpn_char_data_for_endian_test =
207 static_cast<const char*
>(
static_cast<const void *
>((&vrpn_int_data_for_endian_test)));
208static const bool vrpn_big_endian = (vrpn_char_data_for_endian_test[0] != 1);
212 const char *
string, vrpn_int32 length);
270 inline vrpn_uint8
hton(vrpn_uint8 hostval) {
return hostval; }
273 inline vrpn_uint8
ntoh(vrpn_uint8 netval) {
return netval; }
276 inline vrpn_uint16
hton(vrpn_uint16 hostval) {
return htons(hostval); }
279 inline vrpn_uint16
ntoh(vrpn_uint16 netval) {
return ntohs(netval); }
282 inline vrpn_uint32
hton(vrpn_uint32 hostval) {
return htonl(hostval); }
285 inline vrpn_uint32
ntoh(vrpn_uint32 netval) {
return ntohl(netval); }
295 template <
typename T>
inline T
hton(T input)
301 inVal.asInput = input;
302 outVal.asInt =
hton(inVal.asInt);
303 return outVal.asInput;
308 template <
typename T>
inline T
ntoh(T input)
314 inVal.asInput = input;
315 outVal.asInt =
ntoh(inVal.asInt);
316 return outVal.asInput;
334 enum { SIZE_OF_BUFFER_ITEM_IS_NOT_ONE_BYTE };
338#ifdef VRPN_USE_STATIC_ASSERTIONS
341#if defined(__GXX_EXPERIMENTAL_CXX0X__) || \
342 (defined(_MSC_VER) && (_MSC_VER >= 1600))
343#define VRPN_STATIC_ASSERT(CONDITION, MESSAGE) \
344 static_assert(CONDITION, #MESSAGE)
346#define VRPN_STATIC_ASSERT(CONDITION, MESSAGE) \
347 (void)(::vrpn_detail::vrpn_static_assert<CONDITION>::MESSAGE)
352#define VRPN_STATIC_ASSERT(CONDITION, MESSAGE) assert((CONDITION) && #MESSAGE)
359template <
typename T,
typename ByteT>
360static inline T vrpn_unbuffer_from_little_endian(ByteT *&input)
368 typename ::vrpn_detail::remove_const<ByteT>::type bytes[
sizeof(T)];
373 for (
unsigned int i = 0, j =
sizeof(T) - 1; i <
sizeof(T); ++i, --j) {
374 value.bytes[i] = input[j];
381 return ntoh(value.typed);
396 typename ::vrpn_detail::remove_const<ByteT>::type bytes[
sizeof(T)];
401 memcpy(value.bytes, input,
sizeof(T));
407 return ntoh(value.typed);
414template <
typename T,
typename ByteT>
421 if ((insertPt == NULL) || (buflen == NULL)) {
422 fprintf(stderr,
"vrpn_buffer: NULL pointer\n");
426 if (
sizeof(T) >
static_cast<size_t>(*buflen)) {
427 fprintf(stderr,
"vrpn_buffer: buffer not large enough\n");
433 typename ::vrpn_detail::remove_const<ByteT>::type bytes[
sizeof(T)];
438 value.typed = hton(inVal);
441 for (
unsigned int i = 0, j =
sizeof(T) - 1; i <
sizeof(T); ++i, --j) {
442 (*insertPt)[i] = value.bytes[j];
446 *insertPt +=
sizeof(T);
448 *buflen -=
sizeof(T);
457template <
typename T,
typename ByteT>
458inline int vrpn_buffer(ByteT **insertPt, vrpn_int32 *buflen,
const T inVal)
464 if ((insertPt == NULL) || (buflen == NULL)) {
465 fprintf(stderr,
"vrpn_buffer: NULL pointer\n");
469 if (
sizeof(T) >
static_cast<size_t>(*buflen)) {
470 fprintf(stderr,
"vrpn_buffer: buffer not large enough\n");
476 typename ::vrpn_detail::remove_const<ByteT>::type bytes[
sizeof(T)];
481 value.typed = hton(inVal);
484 memcpy(*insertPt, value.bytes,
sizeof(T));
487 *insertPt +=
sizeof(T);
489 *buflen -=
sizeof(T);
494template <
typename T,
typename ByteT>
497 *lvalue = ::vrpn_unbuffer<T, ByteT>(*input);
510template <
size_t charCount>
void vrpn_strcpy(
char (&to)[charCount],
const char* pSrc)
514 strncpy_s(to, pSrc, charCount);
516 strncpy(to, pSrc, charCount - 1);
519 to[charCount - 1] = 0;
Internal header providing unbuffering facilities for a number of types.
vrpn_uint8 hton(vrpn_uint8 hostval)
host to network byte order for 8-bit uints is a no-op
vrpn_uint8 ntoh(vrpn_uint8 netval)
network to host byte order for 8-bit uints is a no-op
Traits class to get the uint type of a given size.
vrpn_float64 vrpn_ntohd(vrpn_float64 d)
vrpn_float64 vrpn_htond(vrpn_float64 d)
VRPN_API struct timeval vrpn_TimevalNormalize(const struct timeval &tv)
VRPN_API int vrpn_buffer(char **insertPt, vrpn_int32 *buflen, const char *string, vrpn_int32 length)
Utility routine for placing a character string of given length into a buffer that is to be sent as a ...
void vrpn_strcpy(char(&to)[charCount], const char *pSrc)
Null-terminated-string copy function that both guarantees not to overrun the buffer and guarantees th...
VRPN_API unsigned long vrpn_TimevalDuration(struct timeval endT, struct timeval startT)
Return number of microseconds between startT and endT.
VRPN_API vrpn_float64 vrpn_htond(vrpn_float64 d)
bool vrpn_test_pack_unpack(void)
VRPN_API struct timeval vrpn_MsecsTimeval(const double dMsecs)
#define VRPN_STATIC_ASSERT(CONDITION, MESSAGE)
Fall back to normal asserts.
VRPN_API vrpn_float64 vrpn_ntohd(vrpn_float64 d)
VRPN_API void vrpn_SleepMsecs(double dMilliSecs)
VRPN_API struct timeval vrpn_TimevalScale(const struct timeval &tv, double scale)
VRPN_API int vrpn_unbuffer(const char **buffer, char *string, vrpn_int32 length)
Utility routine for taking a string of specified length from a buffer that was sent as a message.
VRPN_API double vrpn_TimevalMsecs(const struct timeval &tv1)
#define vrpn_gettimeofday
int vrpn_buffer_to_little_endian(ByteT **insertPt, vrpn_int32 *buflen, const T inVal)
Function template to buffer values to a buffer stored in little- endian order. Specify the type to bu...
VRPN_API struct timeval vrpn_TimevalSum(const struct timeval &tv1, const struct timeval &tv2)
VRPN_API double vrpn_TimevalDurationSeconds(struct timeval endT, struct timeval startT)
Return the number of seconds between startT and endT as a floating-point value.
VRPN_API bool vrpn_TimevalEqual(const struct timeval &tv1, const struct timeval &tv2)
VRPN_API struct timeval vrpn_TimevalDiff(const struct timeval &tv1, const struct timeval &tv2)
VRPN_API bool vrpn_TimevalGreater(const struct timeval &tv1, const struct timeval &tv2)
Header containing vrpn_Thread, vrpn_Semaphore (formerly in vrpn_Shared.h), as well as a lock-guard cl...