From: unknown Date: Wed, 24 Feb 2010 20:12:59 +0000 (+0100) Subject: Windows: compiles with MinGW and runs. X-Git-Url: http://p2p-next.cs.pub.ro/gitweb/?a=commitdiff_plain;h=16c67c8c51ce815882fa3e9ed436ad08e18ea199;p=swift-upb.git Windows: compiles with MinGW and runs. --- diff --git a/bin64.h b/bin64.h index 3550f22..6ddce3f 100644 --- a/bin64.h +++ b/bin64.h @@ -9,11 +9,7 @@ #ifndef BIN64_H #define BIN64_H #include -#ifdef _MSC_VER - #include "compat/stdint.h" -#else - #include -#endif +#include "compat.h" /** Numbering for (aligned) logarithmical bins. diff --git a/compat.cpp b/compat.cpp index c81bdbf..30b0607 100644 --- a/compat.cpp +++ b/compat.cpp @@ -15,7 +15,6 @@ #include #include #include -#include "compat/hirestimeofday.h" #else #include #include @@ -121,10 +120,28 @@ int inet_aton(const char *cp, struct in_addr *inp) 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 #endif @@ -27,9 +34,17 @@ #ifdef _WIN32 #define open(a,b,c) _open(a,b,c) +#endif +#ifndef S_IRUSR #define S_IRUSR _S_IREAD -#define S_IWUSR _S_IWRITE +#endif +#ifndef S_IWUSR +#define S_IWUSR _S_IWRITE +#endif +#ifndef S_IRGRP #define S_IRGRP _S_IREAD +#endif +#ifndef S_IROTH #define S_IROTH _S_IREAD #endif diff --git a/compat/hirestimeofday.cpp b/compat/hirestimeofday.cpp deleted file mode 100644 index 8aa7b1e..0000000 --- a/compat/hirestimeofday.cpp +++ /dev/null @@ -1,157 +0,0 @@ -/* - * Inspired by - * - http://msdn.microsoft.com/en-us/library/ms644904%28VS.85%29.aspx - * - Python-2.6.3/Modules/timemodule.c - */ - -#include -#include "hirestimeofday.h" - -#ifndef _WIN32 -#include -#endif - -namespace swift { - -HiResTimeOfDay* HiResTimeOfDay::_instance = 0; - -HiResTimeOfDay* HiResTimeOfDay::Instance() -{ - if (_instance == 0) - _instance = new HiResTimeOfDay(); - return _instance; -} - - -#ifdef _WIN32 -#include -#include - - -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 swift.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 - -using namespace swift; - -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 - diff --git a/compat/hirestimeofday.h b/compat/hirestimeofday.h deleted file mode 100644 index bb88e9c..0000000 --- a/compat/hirestimeofday.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * 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 -#endif -#ifdef _WIN32 -#include -#endif - -namespace swift { - -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 diff --git a/compat/stdint.h b/compat/stdint.h deleted file mode 100644 index 6cb10e6..0000000 --- a/compat/stdint.h +++ /dev/null @@ -1,18 +0,0 @@ -/* - * 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_ */ diff --git a/compat/unixio.cpp b/compat/unixio.cpp deleted file mode 100644 index 614a31b..0000000 --- a/compat/unixio.cpp +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Written by Arno Bakker - * see LICENSE.txt for license information - */ -#ifdef _WIN32 - -#include "unixio.h" -#include -#include -#include - -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 diff --git a/compat/unixio.h b/compat/unixio.h deleted file mode 100644 index f19757c..0000000 --- a/compat/unixio.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * 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 diff --git a/compat/util.cpp b/compat/util.cpp deleted file mode 100644 index 7469c4a..0000000 --- a/compat/util.cpp +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Written by Arno Bakker - * see LICENSE.txt for license information - */ - -#include "util.h" -#include - -#ifdef _WIN32 -#include -#include -#endif - -namespace swift -{ - -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 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 -} - - - - -}; // namespace - diff --git a/compat/util.h b/compat/util.h deleted file mode 100644 index 42f7de4..0000000 --- a/compat/util.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * util.h - * - * Created on: 20-Oct-2009 - * Author: arno - */ - -#ifndef UTIL_H_ -#define UTIL_H_ - -#include - -namespace swift -{ - /** - * 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_ */ diff --git a/datagram.h b/datagram.h index a7a162e..a8e1be4 100644 --- a/datagram.h +++ b/datagram.h @@ -9,11 +9,12 @@ #ifndef DATAGRAM_H #define DATAGRAM_H -#ifdef _MSC_VER - #include "compat/stdint.h" -#else - #include -#endif +#include +#include +#include +#include +#include +#include #ifdef _WIN32 #include #include "compat.h" @@ -26,14 +27,7 @@ #include #include #endif -#include -#include -#include -#include -#include -#include #include "hashtree.h" -#include "compat/util.h" #include "compat.h" diff --git a/sendrecv.cpp b/sendrecv.cpp index 2317e61..d432e6c 100644 --- a/sendrecv.cpp +++ b/sendrecv.cpp @@ -7,8 +7,6 @@ * */ #include "swift.h" -#include "compat/util.h" - using namespace swift; using namespace std; diff --git a/swift.cpp b/swift.cpp index f916e29..883fd99 100644 --- a/swift.cpp +++ b/swift.cpp @@ -95,17 +95,14 @@ int main (int argc, char** argv) { LibraryInit(); - if (root_hash==Sha1Hash() && bindaddr==Address()) - exit(0); - if (bindaddr!=Address()) { // seeding if (Listen(bindaddr)<=0) quit("cant listen to %s\n",bindaddr.str()) if (wait_time==0) wait_time=TINT_NEVER; - } else { + } else if (tracker!=Address()) { // leeching int base = rand()%10000, i; - for (i=0; i<100 && Listen(Address(INADDR_ANY,i*7+base))<=0; i++); + for (i=0; i<100 && Listen(Address((uint32_t)INADDR_ANY,i*7+base))<=0; i++); if (i==100) quit("cant listen to a port\n"); } @@ -117,6 +114,9 @@ int main (int argc, char** argv) { // FIXME open err printf("Root hash: %s\n", RootMerkleHash(file).hex().c_str()); + if (root_hash==Sha1Hash() && bindaddr==Address() && tracker==Address()) + exit(0); + tint start_time = NOW; tint end_time = TINT_NEVER; diff --git a/swift.h b/swift.h index d833c3d..5b61f46 100644 --- a/swift.h +++ b/swift.h @@ -49,11 +49,6 @@ Messages #ifndef SWIFT_H #define SWIFT_H -#ifdef _MSC_VER -#include "compat/stdint.h" -#else -#include -#endif #include #include #include @@ -311,7 +306,9 @@ namespace swift { HashTree& file () { return transfer_->file(); } const Address& peer() const { return peer_; } tint ack_timeout () { - return std::min(30*TINT_SEC,rtt_avg_ + std::max(dev_avg_,MIN_DEV)*4); + tint dev = dev_avg_ < MIN_DEV ? MIN_DEV : dev_avg_; + tint tmo = rtt_avg_ + dev * 4; + return tmo < 30*TINT_SEC ? tmo : 30*TINT_SEC; } uint32_t id () const { return id_; } diff --git a/transfer.cpp b/transfer.cpp index e769bec..c8953bb 100644 --- a/transfer.cpp +++ b/transfer.cpp @@ -6,16 +6,10 @@ * Copyright 2009 Delft University of Technology. All rights reserved. * */ -#ifdef _WIN32 -#include "compat.h" -#else -#include -#endif #include #include #include #include "swift.h" -#include "compat/util.h" #include "ext/seq_picker.cpp" // FIXME FIXME FIXME FIXME