-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket-factory.cpp
58 lines (46 loc) · 1.48 KB
/
socket-factory.cpp
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
#include "socket-factory.h"
#include "udp-socket.h"
#ifndef NO_CAN_SOCKETS
#include "can-socket.h"
#endif
#include <system_error>
socket_factory_t::socket_factory_t()
{
register_socket(AF_INET, []{ return new udp_socket_t(); });
#ifndef NO_CAN_SOCKETS
register_socket(AF_CAN, []{ return new can_socket_t(); });
#endif
}
basic_socket_t* socket_factory_t::create_broadcast(const std::string &url,
broadcast_mode mode) const
{
std::unique_ptr<endpoint_t> endpoint(endpoint_t::create(url));
return create_broadcast(*endpoint, mode);
}
basic_socket_t* socket_factory_t::create_broadcast(const endpoint_t &endpoint,
broadcast_mode mode) const
{
const auto found = creators_.find(endpoint.family());
if (found == creators_.end()) {
throw std::system_error(EAFNOSUPPORT, std::generic_category());
}
std::unique_ptr<basic_socket_t> socket(found->second());
switch (mode) {
case broadcast_mode::client:
socket->set_option(reuse_address_option_t());
socket->bind(endpoint);
break;
case broadcast_mode::server:
socket->set_option(broadcast_option_t());
break;
}
return socket.release();
}
void socket_factory_t::register_socket(int family, const socket_factory_t::creator_t &creator)
{
creators_[family] = creator;
}
void socket_factory_t::unregister_all()
{
creators_.clear();
}