48b4c725
Peter M. Groen
Setting up Socket-pp
|
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
|
acceptor acceptor::create(int domain)
{
acceptor acc(create_handle(domain));
if (!acc)
acc.clear(get_last_error());
return acc;
}
// --------------------------------------------------------------------------
// This attempts to open the acceptor, bind to the requested address, and
// start listening. On any error it will be sure to leave the underlying
// socket in an unopened/invalid state.
// If the acceptor appears to already be opened, this will quietly succeed
// without doing anything.
bool acceptor::open(const sock_address& addr,
int queSize /*=DFLT_QUE_SIZE*/,
bool reuseSock /*=true*/)
{
// TODO: What to do if we are open but bound to a different address?
if (is_open())
return true;
sa_family_t domain = addr.family();
socket_t h = create_handle(domain);
if (!check_socket_bool(h))
return false;
reset(h);
#if defined(_WIN32)
const int REUSE = SO_REUSEADDR;
#else
const int REUSE = SO_REUSEPORT;
#endif
if (reuseSock && (domain == AF_INET || domain == AF_INET6)) {
int reuse = 1;
if (!set_option(SOL_SOCKET, REUSE, reuse))
return close_on_err();
}
if (!bind(addr) || !listen(queSize))
return close_on_err();
return true;
}
// --------------------------------------------------------------------------
stream_socket acceptor::accept(sock_address* clientAddr /*=nullptr*/)
{
sockaddr* p = clientAddr ? clientAddr->sockaddr_ptr() : nullptr;
socklen_t len = clientAddr ? clientAddr->size() : 0;
socket_t s = check_socket(::accept(handle(), p, clientAddr ? &len : nullptr));
return stream_socket(s);
}
|