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)
17#include "vrpn_Configure.h"
18#include "vrpn_Types.h"
24#if defined(__ANDROID__)
40#undef VRPN_USE_WINSOCK_SOCKETS
45#if defined(_WIN32) && \
46 (!defined(__CYGWIN__) || defined(VRPN_CYGWIN_USES_WINSOCK_SOCKETS))
47#define VRPN_USE_WINSOCK_SOCKETS
48#define vrpn_SOCKET SOCKET
51#ifndef VRPN_USE_WINSOCK_SOCKETS
53#define INVALID_SOCKET -1
54#define vrpn_SOCKET int
57#if !(defined(_WIN32) && defined(VRPN_USE_WINSOCK_SOCKETS))
58#include <sys/select.h>
59#include <netinet/in.h>
63#define perror(x) fprintf(stderr, "%s\n", x);
74#define VRPN_USE_WINDOWS_GETHOSTBYNAME_HACK
95#if (!defined(VRPN_USE_WINSOCK_SOCKETS))
100#ifndef VRPN_USE_STD_CHRONO
101 #define vrpn_gettimeofday gettimeofday
103 extern "C" VRPN_API
int vrpn_gettimeofday(
struct timeval *tp,
118#ifndef WIN32_LEAN_AND_MEAN
119#define WIN32_LEAN_AND_MEAN
123#include <sys/timeb.h>
125#ifdef VRPN_USE_WINSOCK2
133extern "C" VRPN_API
int vrpn_gettimeofday(
struct timeval *tp,
142#if defined(VRPN_EXPORT_GETTIMEOFDAY)
145#define gettimeofday vrpn_gettimeofday
163extern VRPN_API
struct timeval vrpn_TimevalNormalize(const struct timeval &tv);
165extern VRPN_API
struct timeval vrpn_TimevalSum(const struct timeval &tv1,
166 const struct timeval &tv2);
167extern VRPN_API
struct timeval vrpn_TimevalDiff(const struct timeval &tv1,
168 const struct timeval &tv2);
169extern VRPN_API
struct timeval vrpn_TimevalScale(const struct timeval &tv,
173extern VRPN_API
unsigned long vrpn_TimevalDuration(
struct timeval endT,
174 struct timeval startT);
178extern VRPN_API
double vrpn_TimevalDurationSeconds(
struct timeval endT,
179 struct timeval startT);
181extern VRPN_API
bool vrpn_TimevalGreater(
const struct timeval &tv1,
182 const struct timeval &tv2);
183extern VRPN_API
bool vrpn_TimevalEqual(
const struct timeval &tv1,
184 const struct timeval &tv2);
186extern VRPN_API
double vrpn_TimevalMsecs(
const struct timeval &tv1);
188extern VRPN_API
struct timeval vrpn_MsecsTimeval(const double dMsecs);
189extern VRPN_API
void vrpn_SleepMsecs(
double dMilliSecs);
196extern VRPN_API vrpn_float64 vrpn_htond(vrpn_float64 d);
197extern VRPN_API vrpn_float64 vrpn_ntohd(vrpn_float64 d);
207static const int vrpn_int_data_for_endian_test = 1;
208static const char *vrpn_char_data_for_endian_test =
209 static_cast<const char*
>(
static_cast<const void *
>((&vrpn_int_data_for_endian_test)));
210static const bool vrpn_big_endian = (vrpn_char_data_for_endian_test[0] != 1);
213extern VRPN_API
int vrpn_buffer(
char **insertPt, vrpn_int32 *buflen,
214 const char *
string, vrpn_int32 length);
215extern VRPN_API
int vrpn_unbuffer(
const char **buffer,
char *
string,
219extern VRPN_API
int vrpn_unbuffer(
const char **buffer, timeval *t);
220extern VRPN_API
int vrpn_buffer(
char **insertPt, vrpn_int32 *buflen,
256 namespace vrpn_detail {
261 typedef vrpn_uint8 type;
264 typedef vrpn_uint16 type;
267 typedef vrpn_uint32 type;
272 inline vrpn_uint8
hton(vrpn_uint8 hostval) {
return hostval; }
275 inline vrpn_uint8
ntoh(vrpn_uint8 netval) {
return netval; }
278 inline vrpn_uint16
hton(vrpn_uint16 hostval) {
return htons(hostval); }
281 inline vrpn_uint16
ntoh(vrpn_uint16 netval) {
return ntohs(netval); }
284 inline vrpn_uint32
hton(vrpn_uint32 hostval) {
return htonl(hostval); }
287 inline vrpn_uint32
ntoh(vrpn_uint32 netval) {
return ntohl(netval); }
290 inline vrpn_float64
hton(vrpn_float64 hostval) {
return vrpn_htond(hostval); }
293 inline vrpn_float64
ntoh(vrpn_float64 netval) {
return vrpn_ntohd(netval); }
297 template <
typename T>
inline T
hton(T input)
303 inVal.asInput = input;
304 outVal.asInt =
hton(inVal.asInt);
305 return outVal.asInput;
310 template <
typename T>
inline T
ntoh(T input)
316 inVal.asInput = input;
317 outVal.asInt =
ntoh(inVal.asInt);
318 return outVal.asInput;
322namespace vrpn_detail {
336 enum { SIZE_OF_BUFFER_ITEM_IS_NOT_ONE_BYTE };
340#ifdef VRPN_USE_STATIC_ASSERTIONS
343#if defined(__GXX_EXPERIMENTAL_CXX0X__) || \
344 (defined(_MSC_VER) && (_MSC_VER >= 1600))
345#define VRPN_STATIC_ASSERT(CONDITION, MESSAGE) \
346 static_assert(CONDITION, #MESSAGE)
348#define VRPN_STATIC_ASSERT(CONDITION, MESSAGE) \
349 (void)(::vrpn_detail::vrpn_static_assert<CONDITION>::MESSAGE)
354#define VRPN_STATIC_ASSERT(CONDITION, MESSAGE) assert((CONDITION) && #MESSAGE)
361template <
typename T,
typename ByteT>
362static inline T vrpn_unbuffer_from_little_endian(ByteT *&input)
366 VRPN_STATIC_ASSERT(
sizeof(ByteT) == 1, SIZE_OF_BUFFER_ITEM_IS_NOT_ONE_BYTE);
370 typename ::vrpn_detail::remove_const<ByteT>::type bytes[
sizeof(T)];
375 for (
unsigned int i = 0, j =
sizeof(T) - 1; i <
sizeof(T); ++i, --j) {
376 value.bytes[i] = input[j];
383 return ntoh(value.typed);
390template <
typename T,
typename ByteT>
inline T vrpn_unbuffer(ByteT *&input)
394 VRPN_STATIC_ASSERT(
sizeof(ByteT) == 1, SIZE_OF_BUFFER_ITEM_IS_NOT_ONE_BYTE);
398 typename ::vrpn_detail::remove_const<ByteT>::type bytes[
sizeof(T)];
403 memcpy(value.bytes, input,
sizeof(T));
409 return ntoh(value.typed);
416template <
typename T,
typename ByteT>
417inline int vrpn_buffer_to_little_endian(ByteT **insertPt, vrpn_int32 *buflen,
const T inVal)
421 VRPN_STATIC_ASSERT(
sizeof(ByteT) == 1, SIZE_OF_BUFFER_ITEM_IS_NOT_ONE_BYTE);
423 if ((insertPt == NULL) || (buflen == NULL)) {
424 fprintf(stderr,
"vrpn_buffer: NULL pointer\n");
428 if (
sizeof(T) >
static_cast<size_t>(*buflen)) {
429 fprintf(stderr,
"vrpn_buffer: buffer not large enough\n");
435 typename ::vrpn_detail::remove_const<ByteT>::type bytes[
sizeof(T)];
440 value.typed =
hton(inVal);
443 for (
unsigned int i = 0, j =
sizeof(T) - 1; i <
sizeof(T); ++i, --j) {
444 (*insertPt)[i] = value.bytes[j];
448 *insertPt +=
sizeof(T);
450 *buflen -=
sizeof(T);
459template <
typename T,
typename ByteT>
460inline int vrpn_buffer(ByteT **insertPt, vrpn_int32 *buflen,
const T inVal)
464 VRPN_STATIC_ASSERT(
sizeof(ByteT) == 1, SIZE_OF_BUFFER_ITEM_IS_NOT_ONE_BYTE);
466 if ((insertPt == NULL) || (buflen == NULL)) {
467 fprintf(stderr,
"vrpn_buffer: NULL pointer\n");
471 if (
sizeof(T) >
static_cast<size_t>(*buflen)) {
472 fprintf(stderr,
"vrpn_buffer: buffer not large enough\n");
478 typename ::vrpn_detail::remove_const<ByteT>::type bytes[
sizeof(T)];
483 value.typed =
hton(inVal);
486 memcpy(*insertPt, value.bytes,
sizeof(T));
489 *insertPt +=
sizeof(T);
491 *buflen -=
sizeof(T);
496template <
typename T,
typename ByteT>
497inline int vrpn_unbuffer(ByteT **input, T *lvalue)
499 *lvalue = ::vrpn_unbuffer<T, ByteT>(*input);
504extern bool vrpn_test_pack_unpack(
void);
510inline char *vrpn_strncpynull(
char* dst,
const char* src,
size_t size)
514 for (i = 0; i < size - 1 && src[i]; i++) {
528template <
size_t charCount>
void vrpn_strcpy(
char (&to)[charCount],
const char* pSrc)
532 strncpy_s(to, pSrc, charCount);
534 strncpy(to, pSrc, charCount - 1);
537 to[charCount - 1] = 0;
554 typedef size_t size_type;
556 typedef const T* const_iterator;
557 vrpn_vector() : m_size(0), m_data(0), m_allocated(0) {};
558 vrpn_vector(size_type s) : m_size(0), m_data(0), m_allocated(0) {
560 m_size = m_allocated = s;
563 : m_size(from.size()), m_data(0), m_allocated(from.size()) {
564 m_data =
new T[m_size];
565 for (
size_t i = 0; i < m_size; i++) {
566 m_data[i] = from.m_data[i];
569 template<
class InputIt >
570 vrpn_vector( InputIt first, InputIt last ) : m_size(0), m_data(0), m_allocated(0) {
571 for (InputIt it = first; it != last; ++it) {
577 m_size = m_allocated = 0;
582 m_size = m_allocated = from.size();
583 m_data =
new T[m_size];
584 for (
size_t i = 0; i < m_size; i++) {
585 m_data[i] = from.m_data[i];
589 T* data() {
return m_data; }
590 size_type size()
const {
return m_size; }
591 bool empty()
const {
return m_size == 0; }
592 void resize(size_type s) {
595 if (s <= m_allocated) {
600 T* newData =
new T[s];
601 for (
size_t i = 0; i < m_size; i++) {
602 newData[i] = m_data[i];
606 m_size = m_allocated = s;
609 void push_back (
const T& val) {
611 m_data[m_size-1] = val;
617 return m_data[m_size-1];
619 T& operator[]( size_type pos ) {
622 const T& operator[]( size_type pos )
const {
625 void assign( size_type count,
const T& value ) {
627 for (size_type i = 0; i < size(); i++) {
631 void clear() { m_size = 0; }
632 template<
class InputIt >
633 void assign( InputIt first, InputIt last ) {
635 for (InputIt it = first; it != last; it++) {
639 iterator begin() {
return m_data; }
640 const_iterator begin()
const {
return m_data; }
643 return &m_data[m_size];
648 const_iterator end()
const {
650 return &m_data[m_size];
656 size_type m_size, m_allocated;
661extern bool vrpn_test_vrpn_vector(
void);
Internal header providing unbuffering facilities for a number of types.
定义 vrpn_Shared.h:255
vrpn_uint8 hton(vrpn_uint8 hostval)
host to network byte order for 8-bit uints is a no-op
定义 vrpn_Shared.h:272
vrpn_uint8 ntoh(vrpn_uint8 netval)
network to host byte order for 8-bit uints is a no-op
定义 vrpn_Shared.h:275
Traits class to get the uint type of a given size
定义 vrpn_Shared.h:258
Header containing vrpn_Thread, vrpn_Semaphore (formerly in vrpn_Shared.h), as well as a lock-guard cl...