-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.h
76 lines (60 loc) · 1.66 KB
/
utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#ifndef II_UTILS_H
#define II_UTILS_H
#include <iostream>
#include <string>
#include <sstream>
#include <exception>
#include <algorithm>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
uint32_t const NANOSPERS = 1000000000;
size_t const MAX_FROM_SERVER_DATAGRAM_SIZE = 512;
size_t const MAX_FROM_CLIENT_DATAGRAM_SIZE = 77;
uint32_t const REQUIRED_PLAYERS = 2;
bool operator==(sockaddr_storage const &a1, sockaddr_storage const &a2);
/* Parsing and validation */
class UtilsError : public std::runtime_error {
public:
UtilsError(char const *);
};
// https://stackoverflow.com/questions/809902/64-bit-ntohl-in-c
template <typename T>
constexpr T bswap (T value, char* ptr=0)
{
return
#if __BYTE_ORDER == __LITTLE_ENDIAN
ptr = reinterpret_cast<char*>(&value), std::reverse (ptr, ptr + sizeof(T)),
#endif
value;
}
uint32_t str2uint32_t(std::string);
std::string last_err(std::string);
bool is_valid_port(uint32_t port_number);
/* Connections */
struct AddrInfo {
addrinfo *info;
std::string err;
AddrInfo(char const * node, char const * port, addrinfo const &hints);
~AddrInfo();
};
struct Socket {
int fd;
Socket();
Socket(Socket &&);
Socket& operator=(Socket &&);
~Socket();
};
void create_timer(timer_t &timer, uint64_t nanosecs, void (*handler)(int, siginfo_t *, void *));
bool disarm_timer(timer_t timer, itimerspec &old);
bool resume_timer(timer_t timer, itimerspec &resume);
uint64_t milliseconds_since_epoch();
#endif //II_UTILS_H