source: network-game/common/Common.cpp@ b48ef09

Last change on this file since b48ef09 was 8271c78, checked in by dportnoy <dmp1488@…>, 11 years ago

The client has basic log files

  • Property mode set to 100644
File size: 1.3 KB
RevLine 
[edfd1d0]1#include "Common.h"
2
[d05086b]3#include <sstream>
[b07eeac]4#include <cmath>
[8f85180]5
6#if defined WINDOWS
7 #include <Windows.h>
8#endif
9
[8271c78]10#include <ctime>
11
[b07eeac]12using namespace std;
13
[edfd1d0]14void set_nonblock(int sock)
15{
[4c202e0]16 #if defined WINDOWS
[edfd1d0]17 unsigned long mode = 1;
18 ioctlsocket(sock, FIONBIO, &mode);
[4c202e0]19 #elif defined LINUX
[edfd1d0]20 int flags;
21 flags = fcntl(sock, F_GETFL,0);
22 assert(flags != -1);
23 fcntl(sock, F_SETFL, flags | O_NONBLOCK);
24 #endif
25}
[8f85180]26
27unsigned long long getCurrentMillis()
28{
29 unsigned long long numMilliseconds;
30
31 #if defined WINDOWS
32 numMilliseconds = GetTickCount();
33 #elif defined LINUX
34 timespec curTime;
35 clock_gettime(CLOCK_REALTIME, &curTime);
36
37 numMilliseconds = curTime.tv_sec*(unsigned long long)1000+curTime.tv_nsec/(unsigned long long)1000000;
38 #endif
39
40 return numMilliseconds;
41}
[b07eeac]42
[d05086b]43string getCurrentDateTimeString() {
[8271c78]44 ostringstream timeString;
45
[d05086b]46 time_t millis = time(NULL);
47 struct tm *time = localtime(&millis);
48
49 timeString << time->tm_hour << ":" << time->tm_min << ":"<< time->tm_sec << " " << (time->tm_mon+1) << "/" << time->tm_mday << "/" << (time->tm_year+1900);
50
51 return timeString.str();
52}
53
[b07eeac]54float posDistance(FLOAT_POSITION pos1, FLOAT_POSITION pos2) {
55 float xDiff = pos2.x - pos1.x;
56 float yDiff = pos2.y - pos1.y;
57
58 return sqrt( pow(xDiff,2) + pow(yDiff,2) );
59}
Note: See TracBrowser for help on using the repository browser.