#ifndef BIN64_H
#define BIN64_H
#include <assert.h>
-#ifdef _MSC_VER
- #include "compat/stdint.h"
-#else
- #include <stdint.h>
-#endif
+#include "compat.h"
/** Numbering for (aligned) logarithmical bins.
#include <Tchar.h>
#include <io.h>
#include <sys/timeb.h>
-#include "compat/hirestimeofday.h"
#else
#include <unistd.h>
#include <sys/time.h>
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;
}
#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
+}
+
}
#define SWIFT_COMPAT_H
#ifdef _MSC_VER
-#include "compat/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;
#else
#include <stdint.h>
#endif
#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
+++ /dev/null
-/*
- * 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 swift {
-
-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 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 <iostream>
-
-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
-
+++ /dev/null
-/*
- * 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 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
+++ /dev/null
-/*
- * 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_ */
+++ /dev/null
-/*
- * 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
+++ /dev/null
-/*
- * 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
+++ /dev/null
-/*
- * 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 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<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
-
+++ /dev/null
-/*
- * util.h
- *
- * Created on: 20-Oct-2009
- * Author: arno
- */
-
-#ifndef UTIL_H_
-#define UTIL_H_
-
-#include <string>
-
-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_ */
#ifndef DATAGRAM_H
#define DATAGRAM_H
-#ifdef _MSC_VER
- #include "compat/stdint.h"
-#else
- #include <stdint.h>
-#endif
+#include <stdlib.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <string.h>
+#include <stdio.h>
+#include <string>
#ifdef _WIN32
#include <winsock2.h>
#include "compat.h"
#include <netinet/in.h>
#include <unistd.h>
#endif
-#include <stdlib.h>
-#include <fcntl.h>
-#include <sys/stat.h>
-#include <string.h>
-#include <stdio.h>
-#include <string>
#include "hashtree.h"
-#include "compat/util.h"
#include "compat.h"
*
*/
#include "swift.h"
-#include "compat/util.h"
-
using namespace swift;
using namespace std;
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");
}
// 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;
#ifndef SWIFT_H
#define SWIFT_H
-#ifdef _MSC_VER
-#include "compat/stdint.h"
-#else
-#include <stdint.h>
-#endif
#include <deque>
#include <vector>
#include <algorithm>
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_; }
* Copyright 2009 Delft University of Technology. All rights reserved.
*
*/
-#ifdef _WIN32
-#include "compat.h"
-#else
-#include <sys/mman.h>
-#endif
#include <errno.h>
#include <string>
#include <sstream>
#include "swift.h"
-#include "compat/util.h"
#include "ext/seq_picker.cpp" // FIXME FIXME FIXME FIXME