-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket-option.h
59 lines (49 loc) · 1.22 KB
/
socket-option.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
#ifndef SOCKETOPTION_H
#define SOCKETOPTION_H
#include "endpoint.h"
#include <vector>
#include <cstring>
#include <type_traits>
class socket_option_t
{
public:
socket_option_t(int level, int name, const std::vector<char> &value)
: level_(level)
, name_(name)
, value_(value)
{}
int level() const { return level_; }
int name() const { return name_; }
const std::vector<char>& value() const { return value_; }
private:
int level_;
int name_;
std::vector<char> value_;
};
template <class T>
class typed_socket_option_t : public socket_option_t
{
public:
typed_socket_option_t(int level, int name, const T &value)
: socket_option_t(level, name, make_binary(value))
{}
private:
static std::vector<char> make_binary(const T &value)
{
static_assert(std::is_pod<T>::value, "T must be POD");
std::vector<char> buffer(sizeof(T));
std::memcpy(buffer.data(), &value, sizeof(T));
return buffer;
}
};
class broadcast_option_t : public typed_socket_option_t<int>
{
public:
broadcast_option_t();
};
class reuse_address_option_t : public typed_socket_option_t<int>
{
public:
reuse_address_option_t();
};
#endif // SOCKETOPTION_H