forked from sorokin/echo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket_apple.cpp
252 lines (216 loc) · 7.57 KB
/
socket_apple.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include "socket_apple.h"
#include <sys/socket.h>
#include <errno.h>
#include <netinet/ip.h>
#include <fcntl.h>
#include "throw_error.h"
namespace
{
int get_fd_flags(int fd);
void set_fd_flags(int fd, int flags);
file_descriptor make_socket(int domain, int type)
{
int fd = ::socket(domain, type, 0);
if (fd == -1)
throw_error(errno, "socket()");
return file_descriptor{fd};
}
void start_listen(int fd)
{
int res = ::listen(fd, SOMAXCONN);
if (res == -1)
throw_error(errno, "listen()");
}
void bind_socket(int fd, uint16_t port_net, uint32_t addr_net)
{
sockaddr_in saddr{};
saddr.sin_family = AF_INET;
saddr.sin_port = port_net;
saddr.sin_addr.s_addr = addr_net;
int res = ::bind(fd, reinterpret_cast<sockaddr const*>(&saddr), sizeof saddr);
if (res == -1)
throw_error(errno, "bind()");
}
void connect_socket(int fd, uint16_t port_net, uint32_t addr_net)
{
sockaddr_in saddr{};
saddr.sin_family = AF_INET;
saddr.sin_port = port_net;
saddr.sin_addr.s_addr = addr_net;
int res = ::connect(fd, reinterpret_cast<sockaddr const*>(&saddr), sizeof saddr);
if (res == -1)
throw_error(errno, "connect()");
}
int get_fd_flags(int fd)
{
int res = fcntl(fd, F_GETFL, 0);
if (res == -1)
throw_error(errno, "fcntl(F_GETFL)");
return res;
}
void set_fd_flags(int fd, int flags)
{
int res = fcntl(fd, F_SETFL, flags);
if (res == -1)
throw_error(errno, "fcntl(F_SETFL)");
}
}
client_socket::client_socket(sysapi::epoll &ep, file_descriptor fd, on_ready_t on_disconnect)
: client_socket(ep, std::move(fd), std::move(on_disconnect), on_ready_t{}, on_ready_t{})
{}
client_socket::client_socket(epoll& ep,
file_descriptor fd,
on_ready_t on_disconnect,
on_ready_t on_read_ready,
on_ready_t on_write_ready)
: pimpl(new impl(ep, std::move(fd), std::move(on_disconnect), std::move(on_read_ready), std::move(on_write_ready)))
{}
client_socket::impl::impl(sysapi::epoll &ep, file_descriptor fd, on_ready_t on_disconnect, on_ready_t on_read_ready, on_ready_t on_write_ready)
: ep(ep)
, fd(std::move(fd))
, on_disconnect(std::move(on_disconnect))
, on_read_ready(std::move(on_read_ready))
, on_write_ready(std::move(on_write_ready))
, reg(ep, this->fd.getfd(), {EVFILT_READ}, [this](struct kevent event)
{
assert(event.filter == EVFILT_READ || event.filter == EVFILT_WRITE);
bool is_destroyed = false;
assert(destroyed == nullptr);
destroyed = &is_destroyed;
try
{
if ((event.filter == EVFILT_READ && event.flags & EV_EOF) || (event.filter == EVFILT_WRITE && event.flags & EV_EOF))
{
this->on_disconnect();
if (is_destroyed)
return;
}
if (event.filter == EVFILT_READ)
{
this->on_read_ready();
if (is_destroyed)
return;
}
if (event.filter == EVFILT_WRITE)
{
this->on_write_ready();
if (is_destroyed)
return;
}
}
catch (...)
{
destroyed = nullptr;
throw;
}
destroyed = nullptr;
})
, destroyed(nullptr)
{}
client_socket::impl::~impl()
{
if (destroyed)
*destroyed = true;
}
void client_socket::impl::update_registration()
{
reg.modify(calculate_flags());
}
std::list<int16_t> client_socket::impl::calculate_flags() const
{
std::list<int16_t> ev_list;
if (on_read_ready)
ev_list.push_back(EVFILT_READ);
if (on_write_ready)
ev_list.push_back(EVFILT_WRITE);
return ev_list;
}
void client_socket::set_on_read_write(on_ready_t on_read_ready,
on_ready_t on_write_ready)
{
pimpl->on_read_ready = std::move(on_read_ready);
pimpl->on_write_ready = std::move(on_write_ready);
pimpl->update_registration();
}
void client_socket::set_on_read(on_ready_t on_ready)
{
// TODO: not exception safe
pimpl->on_read_ready = on_ready;
pimpl->update_registration();
}
void client_socket::set_on_write(client_socket::on_ready_t on_ready)
{
pimpl->on_write_ready = on_ready;
pimpl->update_registration();
}
size_t client_socket::write_some(const void *data, size_t size)
{
return ::write_some(pimpl->fd, data, size);
}
size_t client_socket::read_some(void* data, size_t size)
{
return ::read_some(pimpl->fd, data, size);
}
client_socket client_socket::connect(sysapi::epoll &ep, const ipv4_endpoint &remote, on_ready_t on_disconnect)
{
file_descriptor fd = make_socket(AF_INET, SOCK_STREAM);
connect_socket(fd.getfd(), remote.port_net, remote.addr_net);
set_fd_flags(fd.getfd(), get_fd_flags(fd.getfd()) | O_NONBLOCK);
client_socket res{ep, std::move(fd), std::move(on_disconnect)};
return res;
}
server_socket::server_socket(epoll& ep, on_connected_t on_connected)
: fd(make_socket(AF_INET, SOCK_STREAM))
, on_connected(on_connected)
, reg(ep, fd.getfd(), {EVFILT_READ}, [this](struct kevent event) {
assert(event.filter == EVFILT_READ);
this->on_connected();
})
{
set_fd_flags(fd.getfd(), get_fd_flags(fd.getfd()) | O_NONBLOCK);
start_listen(fd.getfd());
}
server_socket::server_socket(epoll& ep, ipv4_endpoint local_endpoint, on_connected_t on_connected)
: fd(make_socket(AF_INET, SOCK_STREAM))
, on_connected(on_connected)
, reg(ep, fd.getfd(), {EVFILT_READ}, [this](struct kevent event) {
assert(event.filter & EVFILT_READ);
this->on_connected();
})
{
set_fd_flags(fd.getfd(), get_fd_flags(fd.getfd()) | O_NONBLOCK);
bind_socket(fd.getfd(), local_endpoint.port_net, local_endpoint.addr_net);
start_listen(fd.getfd());
}
ipv4_endpoint server_socket::local_endpoint() const
{
sockaddr_in saddr{};
socklen_t saddr_len = sizeof saddr;
int res = ::getsockname(fd.getfd(), reinterpret_cast<sockaddr*>(&saddr), &saddr_len);
if (res == -1)
throw_error(errno, "getsockname()");
assert(saddr_len == sizeof saddr);
return ipv4_endpoint{saddr.sin_port, saddr.sin_addr.s_addr};
}
client_socket server_socket::accept(client_socket::on_ready_t on_disconnect) const
{
int res = ::accept(fd.getfd(), nullptr, nullptr);
if (res == -1)
throw_error(errno, "accept()");
set_fd_flags(fd.getfd(), get_fd_flags(fd.getfd()) | O_NONBLOCK);
const int set = 1;
::setsockopt(res, SOL_SOCKET, SO_NOSIGPIPE, &set, sizeof(set)); // NOSIGPIPE FOR SEND
return client_socket{reg.get_epoll(), {res}, std::move(on_disconnect)};
}
client_socket server_socket::accept(client_socket::on_ready_t on_disconnect,
client_socket::on_ready_t on_read_ready,
client_socket::on_ready_t on_write_ready) const
{
int res = ::accept(fd.getfd(), nullptr, nullptr);
if (res == -1)
throw_error(errno, "accept4()");
set_fd_flags(fd.getfd(), get_fd_flags(fd.getfd()) | O_NONBLOCK);
const int set = 1;
::setsockopt(res, SOL_SOCKET, SO_NOSIGPIPE, &set, sizeof(set)); // NOSIGPIPE FOR SEND
return client_socket{reg.get_epoll(), {res}, std::move(on_disconnect), std::move(on_read_ready), std::move(on_write_ready)};
}