forked from RipcordSoftware/libhttpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket_timeval_option.h
More file actions
77 lines (56 loc) · 1.76 KB
/
Copy pathsocket_timeval_option.h
File metadata and controls
77 lines (56 loc) · 1.76 KB
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 RS_LIBHTTPSERVER_SOCKET_TIMEVAL_OPTION_H
#define RS_LIBHTTPSERVER_SOCKET_TIMEVAL_OPTION_H
#include <sys/time.h>
namespace rs {
namespace httpserver {
// based on: http://stackoverflow.com/questions/292997/can-you-set-so-rcvtimeo-and-so-sndtimeo-socket-options-in-boost-asio
template <int Level, int Name>
class SocketTimevalOption final {
public:
SocketTimevalOption() : value_(zero_timeval()) {}
explicit SocketTimevalOption(int seconds, int microseconds = 0) : value_({.tv_sec=seconds, .tv_usec=microseconds}) {}
explicit SocketTimevalOption(const ::timeval& v) : value_(v) {}
::timeval value() const {
return value_;
}
time_t seconds() const {
return value_.tv_sec;
}
suseconds_t microseconds() const {
return value_.tv_usec;
}
template <typename Protocol>
int level(const Protocol&) const {
return Level;
}
template <typename Protocol>
int name(const Protocol&) const {
return Name;
}
template <typename Protocol>
::timeval* data(const Protocol&) {
return &value_;
}
template <typename Protocol>
const ::timeval* data(const Protocol&) const {
return &value_;
}
template <typename Protocol>
std::size_t size(const Protocol&) const {
return sizeof(value_);
}
template <typename Protocol>
void resize(const Protocol&, std::size_t s) {
if (s != sizeof(value_)) {
throw std::length_error("timeval socket option resize");
}
}
private:
static ::timeval zero_timeval() {
::timeval result = {};
return result;
}
::timeval value_;
};
}}
#endif /* RS_LIBHTTPSERVER_SOCKET_TIMEVAL_OPTION_H */