/*
* compat.cpp
- * p2tp
+ * swift
*
* Created by Arno Bakker, Victor Grishchenko
* Copyright 2009 Delft University of Technology. All rights reserved.
#include <Tchar.h>
#include <io.h>
#include <sys/timeb.h>
-#include "compat/hirestimeofday.h"
+#include <vector>
+#include <stdexcept>
#else
#include <unistd.h>
#include <sys/time.h>
#endif
-namespace p2tp {
+namespace swift {
#ifdef _WIN32
static HANDLE map_handles[1024];
#ifndef _WIN32
return lseek(fd,offset,SEEK_SET);
#else
- return _lseek(fd,offset,SEEK_SET);
+ return _lseek(fd,offset,SEEK_SET);
#endif
}
#ifdef _WIN32
int e = WSAGetLastError();
if (e)
- fprintf(stderr,"network error #%i\n",e);
+ fprintf(stderr,"network error #%u\n",e);
#endif
}
0,
0,
NULL );
- if (maphandle == NULL)
+ if (maphandle == NULL)
return NULL;
map_handles[fd] = maphandle;
munmap(mapping,size);
close(fd);
#else
- UnmapViewOfFile(mapping);
- CloseHandle(map_handles[fd]);
+ UnmapViewOfFile(mapping);
+ CloseHandle(map_handles[fd]);
#endif
}
return 1;
}
+#endif
+
+#ifdef _WIN32
+
+LARGE_INTEGER get_freq() {
+ LARGE_INTEGER proc_freq;
+ if (!::QueryPerformanceFrequency(&proc_freq))
+ print_error("HiResTimeOfDay: QueryPerformanceFrequency() failed");
+ return proc_freq;
+}
+
tint usec_time(void)
{
- HiResTimeOfDay* tod = HiResTimeOfDay::Instance();
- return tod->getTimeUSec();
+ static LARGE_INTEGER last_time;
+ LARGE_INTEGER cur_time;
+ QueryPerformanceCounter(&cur_time);
+ if (cur_time.QuadPart<last_time.QuadPart)
+ print_error("QueryPerformanceCounter wrapped"); // does this happen?
+ last_time = cur_time;
+ static float freq = 1000000.0/get_freq().QuadPart;
+ tint usec = cur_time.QuadPart * freq;
+ return usec;
}
tint usec_time(void)
{
struct timeval t;
- gettimeofday(&t,NULL);
- tint ret;
- ret = t.tv_sec;
- ret *= 1000000;
- ret += t.tv_usec;
- return ret;
+ gettimeofday(&t,NULL);
+ tint ret;
+ ret = t.tv_sec;
+ ret *= 1000000;
+ ret += t.tv_usec;
+ return ret;
}
#endif
+void LibraryInit(void)
+{
+#ifdef _WIN32
+ static WSADATA _WSAData;
+ // win32 requires you to initialize the Winsock DLL with the desired
+ // specification version
+ WORD wVersionRequested;
+ wVersionRequested = MAKEWORD(2, 2);
+ WSAStartup(wVersionRequested, &_WSAData);
+#endif
+}
+
+
+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
+}
+
+bool make_socket_nonblocking(SOCKET fd) {
+#ifdef _WIN32
+ u_long enable = 1;
+ return 0==ioctlsocket(fd, FIONBIO, &enable);
+#else
+ int enable=1;
+ return 0==fcntl(fd, F_SETFL, O_NONBLOCK);
+#endif
+}
+
+bool close_socket (SOCKET sock) {
+#ifdef _WIN32
+ return 0==closesocket(sock);
+#else
+ return 0==::close(sock);
+#endif
+}
+
+
}