X-Git-Url: http://p2p-next.cs.pub.ro/gitweb/?a=blobdiff_plain;f=src%2Flibswift_udp%2Fcompat.cpp;fp=src%2Flibswift_udp%2Fcompat.cpp;h=04e3f6f765021e3c6d8eccfb42246872dfbdc5a6;hb=46f3ca346fe499ba3e8f595fc5c2aa2b3e3c4d5f;hp=0000000000000000000000000000000000000000;hpb=d8da6fc1273a2acee935a15761b77a73424faef5;p=swifty.git diff --git a/src/libswift_udp/compat.cpp b/src/libswift_udp/compat.cpp new file mode 100644 index 0000000..04e3f6f --- /dev/null +++ b/src/libswift_udp/compat.cpp @@ -0,0 +1,233 @@ +/* + * compat.cpp + * swift + * + * Created by Arno Bakker, Victor Grishchenko + * Copyright 2009-2012 TECHNISCHE UNIVERSITEIT DELFT. All rights reserved. + * + */ + +#include "compat.h" +#include +#include +#include +#ifdef _WIN32 +#include +#include +#include +#include +#include +#else +#include +#include +#endif + +namespace swift { + +#ifdef _WIN32 +static HANDLE map_handles[1024]; +#endif + +int64_t file_size (int fd) { + +#ifdef WIN32 + struct _stat32i64 st; + _fstat32i64(fd, &st); +#else + struct stat st; + fstat(fd, &st); +#endif + return st.st_size; +} + +int file_seek (int fd, int64_t offset) { +#ifndef _WIN32 + return lseek(fd,offset,SEEK_SET); +#else + return _lseeki64(fd,offset,SEEK_SET); +#endif +} + +int file_resize (int fd, int64_t new_size) { +#ifndef _WIN32 + return ftruncate(fd, new_size); +#else + // Arno, 2011-10-27: Use 64-bit version + if (_chsize_s(fd,new_size) != 0) + return -1; + else + return 0; +#endif +} + +void print_error(const char* msg) { + perror(msg); +#ifdef _WIN32 + int e = WSAGetLastError(); + if (e) + fprintf(stderr,"windows error #%u\n",e); +#endif +} + +void* memory_map (int fd, size_t size) { + if (!size) + size = file_size(fd); + void *mapping; +#ifndef _WIN32 + mapping = mmap (NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); + if (mapping==MAP_FAILED) + return NULL; + return mapping; +#else + HANDLE fhandle = (HANDLE)_get_osfhandle(fd); + assert(fd<1024); + HANDLE maphandle = CreateFileMapping( fhandle, + NULL, + PAGE_READWRITE, + 0, + 0, + NULL ); + if (maphandle == NULL) + return NULL; + map_handles[fd] = maphandle; + + mapping = MapViewOfFile ( maphandle, + FILE_MAP_WRITE, + 0, + 0, + 0 ); + + return mapping; +#endif +} + +void memory_unmap (int fd, void* mapping, size_t size) { +#ifndef _WIN32 + munmap(mapping,size); + close(fd); +#else + UnmapViewOfFile(mapping); + CloseHandle(map_handles[fd]); +#endif +} + +#ifdef _WIN32 + +size_t pread(int fildes, void *buf, size_t nbyte, __int64 offset) +{ + _lseeki64(fildes,offset,SEEK_SET); + return read(fildes,buf,nbyte); +} + +size_t pwrite(int fildes, const void *buf, size_t nbyte, __int64 offset) +{ + _lseeki64(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 + +#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) +{ + static LARGE_INTEGER last_time; + LARGE_INTEGER cur_time; + QueryPerformanceCounter(&cur_time); + if (cur_time.QuadPart tempPath(result + 1); + result = ::GetTempPath(static_cast(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(result)); +#else + return std::string("/tmp/"); +#endif +} + +bool make_socket_nonblocking(evutil_socket_t fd) { +#ifdef _WIN32 + u_long enable = 1; + return 0==ioctlsocket(fd, FIONBIO, &enable); +#else + return 0==fcntl(fd, F_SETFL, O_NONBLOCK); +#endif +} + +bool close_socket (evutil_socket_t sock) { +#ifdef _WIN32 + return 0==closesocket(sock); +#else + return 0==::close(sock); +#endif +} + + +// Arno: not thread safe! +struct timeval* tint2tv (tint t) { + static struct timeval tv; + tv.tv_usec = t%TINT_SEC; + tv.tv_sec = t/TINT_SEC; + return &tv; +} + + +}