blob: 1bca300db5ecf03b9f42c324023692818fbabe32 (
plain)
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
|
#include "common.hh"
#include <fcntl.h>
#include <sys/time.h>
#include <unistd.h>
#include "sockutils.hh"
namespace stuff {
bool make_nonblocking(int sock) {
int flags = fcntl(sock, F_GETFL, 0);
if (flags < 0) {
return false;
}
if (!(flags & O_NONBLOCK)) {
flags |= O_NONBLOCK;
if (fcntl(sock, F_SETFL, flags) < 0) {
return false;
}
}
return true;
}
bool calc_timeout(const struct timeval* target, struct timeval* timeout) {
gettimeofday(timeout, nullptr);
if (target->tv_sec == timeout->tv_sec) {
timeout->tv_sec = 0;
if (target->tv_usec >= timeout->tv_usec) {
timeout->tv_usec = target->tv_usec - timeout->tv_usec;
} else {
return false;
}
} else if (target->tv_sec > timeout->tv_sec) {
timeout->tv_sec = target->tv_sec - timeout->tv_sec;
if (target->tv_usec >= timeout->tv_usec) {
timeout->tv_usec = target->tv_usec - timeout->tv_usec;
} else {
timeout->tv_sec--;
timeout->tv_usec = 1000000l + target->tv_usec - timeout->tv_usec;
}
} else {
return false;
}
return true;
}
// static
void sockguard::close(int sock) {
::close(sock);
}
} // namespace stuff
namespace std {
void swap(stuff::sockguard& s1, stuff::sockguard& s2) noexcept {
s1.swap(s2);
}
} // namespace std
|