-/*\r
- * Inspired by\r
- * - http://msdn.microsoft.com/en-us/library/ms644904%28VS.85%29.aspx\r
- * - Python-2.6.3/Modules/timemodule.c\r
- */\r
-\r
-#include <iostream>\r
-#include "hirestimeofday.h"\r
-\r
-#ifndef _WIN32\r
-#include <sys/time.h>\r
-#endif\r
-\r
-namespace p2tp {\r
-\r
-HiResTimeOfDay* HiResTimeOfDay::_instance = 0;\r
-\r
-HiResTimeOfDay* HiResTimeOfDay::Instance()\r
-{\r
- if (_instance == 0)\r
- _instance = new HiResTimeOfDay();\r
- return _instance;\r
-}\r
-\r
-\r
-#ifdef _WIN32\r
-#include <windows.h>\r
-#include <sys/timeb.h>\r
-\r
-\r
-HiResTimeOfDay::HiResTimeOfDay(void)\r
-{\r
- frequency = getFrequency();\r
- epochstart = getFTime();\r
- epochcounter = getCounter();\r
-}\r
-\r
-\r
-tint HiResTimeOfDay::getTimeUSec(void)\r
-{\r
- LARGE_INTEGER currentcounter;\r
- tint currentstart;\r
-\r
- currentstart = getFTime();\r
- currentcounter = getCounter();\r
-\r
- if (currentcounter.QuadPart < epochcounter.QuadPart)\r
- {\r
-\r
- // Wrap around detected, reestablish baseline\r
- epochstart = currentstart;\r
- epochcounter = currentcounter;\r
- }\r
- return epochstart + (1000000*(currentcounter.QuadPart-epochcounter.QuadPart))/frequency.QuadPart;\r
-}\r
-\r
-\r
-// Private\r
-tint HiResTimeOfDay::getFTime()\r
-{\r
- struct timeb t;\r
- ftime(&t);\r
- tint usec;\r
- usec = t.time * 1000000;\r
- usec += t.millitm * 1000;\r
- return usec;\r
-}\r
-\r
-\r
-\r
-LARGE_INTEGER HiResTimeOfDay::getFrequency(void)\r
-{\r
- LARGE_INTEGER proc_freq;\r
-\r
- if (!::QueryPerformanceFrequency(&proc_freq))\r
- std::cerr << "HiResTimeOfDay: QueryPerformanceFrequency() failed";\r
-\r
- return proc_freq;\r
-}\r
-\r
-LARGE_INTEGER HiResTimeOfDay::getCounter()\r
-{\r
- LARGE_INTEGER counter;\r
-\r
- DWORD_PTR oldmask = ::SetThreadAffinityMask(::GetCurrentThread(), 0);\r
- if (!::QueryPerformanceCounter(&counter))\r
- std::cerr << "HiResTimeOfDay: QueryPerformanceCounter() failed";\r
- ::SetThreadAffinityMask(::GetCurrentThread(), oldmask);\r
-\r
- return counter;\r
-}\r
-\r
-#else\r
-\r
-HiResTimeOfDay::HiResTimeOfDay(void)\r
-{\r
-}\r
-\r
-\r
-tint HiResTimeOfDay::getTimeUSec(void)\r
-{\r
- struct timeval t;\r
- gettimeofday(&t,NULL);\r
- tint ret;\r
- ret = t.tv_sec;\r
- ret *= 1000000;\r
- ret += t.tv_usec;\r
- return ret;\r
-}\r
-#endif\r
-\r
- \r
- \r
- \r
-// ARNOTODO: move to p2tp.cpp\r
-\r
-#ifdef _WIN32\r
-static WSADATA _WSAData;\r
-#endif\r
- \r
-void LibraryInit(void)\r
-{\r
-#ifdef _WIN32\r
- // win32 requires you to initialize the Winsock DLL with the desired\r
- // specification version\r
- WORD wVersionRequested;\r
- wVersionRequested = MAKEWORD(2, 2);\r
- WSAStartup(wVersionRequested, &_WSAData);\r
-#endif\r
-}\r
- \r
- \r
-} // end of namespace\r
-\r
-\r
-\r
-\r
-#ifdef TEST\r
-#include <iostream>\r
-\r
-using namespace p2tp;\r
-\r
-int main()\r
-{\r
- HiResTimeOfDay *t = HiResTimeOfDay::Instance();\r
- for (int i=0; i<100; i++)\r
- {\r
- tint st = t->getTimeUSec();\r
- Sleep(1000);\r
- tint et = t->getTimeUSec();\r
- tint diff = et - st;\r
- std::cout << "diffxTime is " << diff << "\n";\r
- }\r
- return 0;\r
-}\r
-#endif\r
-\r
+/*
+ * Inspired by
+ * - http://msdn.microsoft.com/en-us/library/ms644904%28VS.85%29.aspx
+ * - Python-2.6.3/Modules/timemodule.c
+ */
+
+#include <iostream>
+#include "hirestimeofday.h"
+
+#ifndef _WIN32
+#include <sys/time.h>
+#endif
+
+namespace p2tp {
+
+HiResTimeOfDay* HiResTimeOfDay::_instance = 0;
+
+HiResTimeOfDay* HiResTimeOfDay::Instance()
+{
+ if (_instance == 0)
+ _instance = new HiResTimeOfDay();
+ return _instance;
+}
+
+
+#ifdef _WIN32
+#include <windows.h>
+#include <sys/timeb.h>
+
+
+HiResTimeOfDay::HiResTimeOfDay(void)
+{
+ frequency = getFrequency();
+ epochstart = getFTime();
+ epochcounter = getCounter();
+}
+
+
+tint HiResTimeOfDay::getTimeUSec(void)
+{
+ LARGE_INTEGER currentcounter;
+ tint currentstart;
+
+ currentstart = getFTime();
+ currentcounter = getCounter();
+
+ if (currentcounter.QuadPart < epochcounter.QuadPart)
+ {
+
+ // Wrap around detected, reestablish baseline
+ epochstart = currentstart;
+ epochcounter = currentcounter;
+ }
+ return epochstart + (1000000*(currentcounter.QuadPart-epochcounter.QuadPart))/frequency.QuadPart;
+}
+
+
+// Private
+tint HiResTimeOfDay::getFTime()
+{
+ struct timeb t;
+ ftime(&t);
+ tint usec;
+ usec = t.time * 1000000;
+ usec += t.millitm * 1000;
+ return usec;
+}
+
+
+
+LARGE_INTEGER HiResTimeOfDay::getFrequency(void)
+{
+ LARGE_INTEGER proc_freq;
+
+ if (!::QueryPerformanceFrequency(&proc_freq))
+ std::cerr << "HiResTimeOfDay: QueryPerformanceFrequency() failed";
+
+ return proc_freq;
+}
+
+LARGE_INTEGER HiResTimeOfDay::getCounter()
+{
+ LARGE_INTEGER counter;
+
+ DWORD_PTR oldmask = ::SetThreadAffinityMask(::GetCurrentThread(), 0);
+ if (!::QueryPerformanceCounter(&counter))
+ std::cerr << "HiResTimeOfDay: QueryPerformanceCounter() failed";
+ ::SetThreadAffinityMask(::GetCurrentThread(), oldmask);
+
+ return counter;
+}
+
+#else
+
+HiResTimeOfDay::HiResTimeOfDay(void)
+{
+}
+
+
+tint HiResTimeOfDay::getTimeUSec(void)
+{
+ struct timeval t;
+ gettimeofday(&t,NULL);
+ tint ret;
+ ret = t.tv_sec;
+ ret *= 1000000;
+ ret += t.tv_usec;
+ return ret;
+}
+#endif
+
+
+
+
+// ARNOTODO: move to p2tp.cpp
+
+#ifdef _WIN32
+static WSADATA _WSAData;
+#endif
+
+void LibraryInit(void)
+{
+#ifdef _WIN32
+ // win32 requires you to initialize the Winsock DLL with the desired
+ // specification version
+ WORD wVersionRequested;
+ wVersionRequested = MAKEWORD(2, 2);
+ WSAStartup(wVersionRequested, &_WSAData);
+#endif
+}
+
+
+} // end of namespace
+
+
+
+
+#ifdef TEST
+#include <iostream>
+
+using namespace p2tp;
+
+int main()
+{
+ HiResTimeOfDay *t = HiResTimeOfDay::Instance();
+ for (int i=0; i<100; i++)
+ {
+ tint st = t->getTimeUSec();
+ Sleep(1000);
+ tint et = t->getTimeUSec();
+ tint diff = et - st;
+ std::cout << "diffxTime is " << diff << "\n";
+ }
+ return 0;
+}
+#endif
+
-/*\r
- * Written by Arno Bakker\r
- * see LICENSE.txt for license information\r
- *\r
- * Singleton class to retrieve a time-of-day in UTC in usec in a platform-\r
- * independent manner.\r
- */\r
-#ifndef HIRESTIMEOFDAY_H\r
-#define HIRESTIMEOFDAY_H\r
-\r
-#ifdef _MSC_VER\r
-#include "compat/stdint.h"\r
-#else\r
-#include <stdint.h>\r
-#endif\r
-#ifdef _WIN32\r
-#include <windows.h>\r
-#endif\r
-\r
-namespace p2tp {\r
-\r
-typedef int64_t tint;\r
-#define TINT_SEC ((tint)1000000)\r
-#define TINT_MSEC ((tint)1000)\r
-#define TINT_uSEC ((tint)1)\r
-#define TINT_NEVER ((tint)0x7fffffffffffffffLL)\r
-\r
-\r
-class HiResTimeOfDay\r
-{\r
-public:\r
- HiResTimeOfDay(void);\r
- tint getTimeUSec(void);\r
- static HiResTimeOfDay* Instance();\r
-\r
-private:\r
-#ifdef _WIN32\r
- tint epochstart; // in usec\r
- LARGE_INTEGER epochcounter;\r
- LARGE_INTEGER last;\r
- LARGE_INTEGER frequency;\r
-\r
- tint HiResTimeOfDay::getFTime();\r
- LARGE_INTEGER getFrequency(void);\r
- LARGE_INTEGER getCounter(void);\r
-#endif\r
-\r
- static HiResTimeOfDay* _instance;\r
-};\r
-\r
-};\r
-#endif\r
+/*
+ * Written by Arno Bakker
+ * see LICENSE.txt for license information
+ *
+ * Singleton class to retrieve a time-of-day in UTC in usec in a platform-
+ * independent manner.
+ */
+#ifndef HIRESTIMEOFDAY_H
+#define HIRESTIMEOFDAY_H
+
+#ifdef _MSC_VER
+#include "compat/stdint.h"
+#else
+#include <stdint.h>
+#endif
+#ifdef _WIN32
+#include <windows.h>
+#endif
+
+namespace p2tp {
+
+typedef int64_t tint;
+#define TINT_SEC ((tint)1000000)
+#define TINT_MSEC ((tint)1000)
+#define TINT_uSEC ((tint)1)
+#define TINT_NEVER ((tint)0x7fffffffffffffffLL)
+
+
+class HiResTimeOfDay
+{
+public:
+ HiResTimeOfDay(void);
+ tint getTimeUSec(void);
+ static HiResTimeOfDay* Instance();
+
+private:
+#ifdef _WIN32
+ tint epochstart; // in usec
+ LARGE_INTEGER epochcounter;
+ LARGE_INTEGER last;
+ LARGE_INTEGER frequency;
+
+ tint HiResTimeOfDay::getFTime();
+ LARGE_INTEGER getFrequency(void);
+ LARGE_INTEGER getCounter(void);
+#endif
+
+ static HiResTimeOfDay* _instance;
+};
+
+};
+#endif
-/*\r
- * Written by Arno Bakker\r
- * see LICENSE.txt for license information\r
- */\r
-\r
-#ifndef STDINT_H_\r
-#define STDINT_H_\r
-\r
-typedef unsigned char uint8_t;\r
-typedef signed char int8_t;\r
-typedef unsigned short uint16_t;\r
-typedef short int16_t;\r
-typedef unsigned int uint32_t;\r
-typedef int int32_t;\r
-typedef __int64 int64_t;\r
-typedef unsigned __int64 uint64_t;\r
-\r
-#endif /* STDINT_H_ */\r
+/*
+ * Written by Arno Bakker
+ * see LICENSE.txt for license information
+ */
+
+#ifndef STDINT_H_
+#define STDINT_H_
+
+typedef unsigned char uint8_t;
+typedef signed char int8_t;
+typedef unsigned short uint16_t;
+typedef short int16_t;
+typedef unsigned int uint32_t;
+typedef int int32_t;
+typedef __int64 int64_t;
+typedef unsigned __int64 uint64_t;
+
+#endif /* STDINT_H_ */
-/*\r
- * Written by Arno Bakker\r
- * see LICENSE.txt for license information\r
- */\r
-#ifdef _WIN32\r
-\r
-#include "unixio.h"\r
-#include <stdio.h>\r
-#include <io.h>\r
-#include <winsock2.h>\r
-\r
-size_t pread(int fildes, void *buf, size_t nbyte, long offset)\r
-{\r
- _lseek(fildes,offset,SEEK_SET);\r
- return read(fildes,buf,nbyte);\r
-}\r
-\r
-size_t pwrite(int fildes, const void *buf, size_t nbyte, long offset)\r
-{\r
- _lseek(fildes,offset,SEEK_SET);\r
- return write(fildes,buf,nbyte);\r
-}\r
-\r
-\r
-int inet_aton(const char *cp, struct in_addr *inp)\r
-{\r
- inp->S_un.S_addr = inet_addr(cp);\r
- return 1;\r
-}\r
-\r
-\r
-\r
-#endif\r
+/*
+ * Written by Arno Bakker
+ * see LICENSE.txt for license information
+ */
+#ifdef _WIN32
+
+#include "unixio.h"
+#include <stdio.h>
+#include <io.h>
+#include <winsock2.h>
+
+size_t pread(int fildes, void *buf, size_t nbyte, long offset)
+{
+ _lseek(fildes,offset,SEEK_SET);
+ return read(fildes,buf,nbyte);
+}
+
+size_t pwrite(int fildes, const void *buf, size_t nbyte, long offset)
+{
+ _lseek(fildes,offset,SEEK_SET);
+ return write(fildes,buf,nbyte);
+}
+
+
+int inet_aton(const char *cp, struct in_addr *inp)
+{
+ inp->S_un.S_addr = inet_addr(cp);
+ return 1;
+}
+
+
+
+#endif
-/*\r
- * Written by Arno Bakker\r
- * see LICENSE.txt for license information\r
- *\r
- * Defines UNIX like I/O calls and parameters for Win32\r
- */\r
-#ifdef _WIN32\r
-\r
-#ifndef UNIXIO_H_\r
-#define UNIXIO_H_\r
-\r
-#define open(a,b,c) _open(a,b,c)\r
-#define S_IRUSR _S_IREAD\r
-#define S_IWUSR _S_IWRITE\r
-#define S_IRGRP _S_IREAD\r
-#define S_IROTH _S_IREAD\r
-#define ftruncate(a, b) _chsize(a,b)\r
-\r
-size_t pread(int fildes, void *buf, size_t nbyte, long offset);\r
-/** UNIX pread approximation. Does change file pointer. Is not thread-safe */\r
-size_t pwrite(int fildes, const void *buf, size_t nbyte, long offset);\r
-/** UNIX pwrite approximation. Does change file pointer. Is not thread-safe */\r
-\r
-int inet_aton(const char *cp, struct in_addr *inp);\r
-\r
-\r
-#endif /* UNIXIO_H_ */\r
-\r
+/*
+ * Written by Arno Bakker
+ * see LICENSE.txt for license information
+ *
+ * Defines UNIX like I/O calls and parameters for Win32
+ */
+#ifdef _WIN32
+
+#ifndef UNIXIO_H_
+#define UNIXIO_H_
+
+#define open(a,b,c) _open(a,b,c)
+#define S_IRUSR _S_IREAD
+#define S_IWUSR _S_IWRITE
+#define S_IRGRP _S_IREAD
+#define S_IROTH _S_IREAD
+#define ftruncate(a, b) _chsize(a,b)
+
+size_t pread(int fildes, void *buf, size_t nbyte, long offset);
+/** UNIX pread approximation. Does change file pointer. Is not thread-safe */
+size_t pwrite(int fildes, const void *buf, size_t nbyte, long offset);
+/** UNIX pwrite approximation. Does change file pointer. Is not thread-safe */
+
+int inet_aton(const char *cp, struct in_addr *inp);
+
+
+#endif /* UNIXIO_H_ */
+
#endif // WIN32
\ No newline at end of file
-/*\r
- * Written by Arno Bakker\r
- * see LICENSE.txt for license information\r
- */\r
-\r
-#include "util.h"\r
-#include <vector>\r
-\r
-#ifdef _WIN32\r
-#include <windows.h>\r
-#include <Tchar.h>\r
-#endif\r
-\r
-namespace p2tp\r
-{\r
-\r
-std::string gettmpdir(void)\r
-{\r
-#ifdef _WIN32\r
- DWORD result = ::GetTempPath(0, _T(""));\r
- if (result == 0)\r
- throw std::runtime_error("Could not get system temp path");\r
-\r
- std::vector<TCHAR> tempPath(result + 1);\r
- result = ::GetTempPath(static_cast<DWORD>(tempPath.size()), &tempPath[0]);\r
- if((result == 0) || (result >= tempPath.size()))\r
- throw std::runtime_error("Could not get system temp path");\r
-\r
- return std::string(tempPath.begin(), tempPath.begin() + static_cast<std::size_t>(result));\r
-#else\r
- return std::string("/tmp/");\r
-#endif\r
-}\r
-\r
-\r
- \r
- \r
-}; // namespace\r
-\r
+/*
+ * Written by Arno Bakker
+ * see LICENSE.txt for license information
+ */
+
+#include "util.h"
+#include <vector>
+
+#ifdef _WIN32
+#include <windows.h>
+#include <Tchar.h>
+#endif
+
+namespace p2tp
+{
+
+std::string gettmpdir(void)
+{
+#ifdef _WIN32
+ DWORD result = ::GetTempPath(0, _T(""));
+ if (result == 0)
+ throw std::runtime_error("Could not get system temp path");
+
+ std::vector<TCHAR> tempPath(result + 1);
+ result = ::GetTempPath(static_cast<DWORD>(tempPath.size()), &tempPath[0]);
+ if((result == 0) || (result >= tempPath.size()))
+ throw std::runtime_error("Could not get system temp path");
+
+ return std::string(tempPath.begin(), tempPath.begin() + static_cast<std::size_t>(result));
+#else
+ return std::string("/tmp/");
+#endif
+}
+
+
+
+
+}; // namespace
+
-/*\r
- * util.h\r
- *\r
- * Created on: 20-Oct-2009\r
- * Author: arno\r
- */\r
-\r
-#ifndef UTIL_H_\r
-#define UTIL_H_\r
-\r
-#include <string>\r
-\r
-namespace p2tp\r
-{\r
- /**\r
- * Return path of temporary directory.\r
- *\r
- * From http://msdn.microsoft.com/en-us/library/aa364992%28VS.85%29.aspx\r
- *\r
- * TODO: Unicode... (gets hairy with open() call on Linux. Win32 has _wopen)\r
- */\r
- std::string gettmpdir(void);\r
-\r
- void print_error(const char* msg);\r
- \r
-};\r
-\r
-#endif /* UTIL_H_ */\r
+/*
+ * util.h
+ *
+ * Created on: 20-Oct-2009
+ * Author: arno
+ */
+
+#ifndef UTIL_H_
+#define UTIL_H_
+
+#include <string>
+
+namespace p2tp
+{
+ /**
+ * Return path of temporary directory.
+ *
+ * From http://msdn.microsoft.com/en-us/library/aa364992%28VS.85%29.aspx
+ *
+ * TODO: Unicode... (gets hairy with open() call on Linux. Win32 has _wopen)
+ */
+ std::string gettmpdir(void);
+
+ void print_error(const char* msg);
+
+};
+
+#endif /* UTIL_H_ */