source: network-game/common/Common.cpp@ 3ef8cf4

Last change on this file since 3ef8cf4 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
Line 
1#include "Common.h"
2
3#include <sstream>
4#include <cmath>
5
6#if defined WINDOWS
7 #include <Windows.h>
8#endif
9
10#include <ctime>
11
12using namespace std;
13
14void set_nonblock(int sock)
15{
16 #if defined WINDOWS
17 unsigned long mode = 1;
18 ioctlsocket(sock, FIONBIO, &mode);
19 #elif defined LINUX
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}
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}
42
43string getCurrentDateTimeString() {
44 ostringstream timeString;
45
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
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.