forked from sorokin/echo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket_apple.h
87 lines (68 loc) · 2.36 KB
/
socket_apple.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
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
#ifndef SOCKET_APPLE_H
#define SOCKET_APPLE_H
#include "file_descriptor.h"
#include "address.h"
#include "kqueue.hpp"
#include <memory>
#include <cstdint>
struct client_socket
{
typedef std::function<void ()> on_ready_t;
client_socket(epoll& ep,
file_descriptor fd,
on_ready_t on_disconnect);
client_socket(epoll& ep,
file_descriptor fd,
on_ready_t on_disconnect,
on_ready_t on_read_ready,
on_ready_t on_write_ready);
void set_on_read_write(on_ready_t on_read_ready, on_ready_t on_write_ready);
void set_on_read(on_ready_t on_ready);
void set_on_write(on_ready_t on_ready);
size_t write_some(void const* data, size_t size);
size_t read_some(void* data, size_t size);
static client_socket connect(epoll& ep, ipv4_endpoint const& remote, on_ready_t on_disconnect);
private:
struct impl
{
impl(epoll& ep, file_descriptor fd, on_ready_t on_disconnect, on_ready_t on_read_ready, on_ready_t on_write_ready);
~impl();
void update_registration();
std::list<int16_t> calculate_flags() const;
epoll& ep;
file_descriptor fd;
on_ready_t on_disconnect;
on_ready_t on_read_ready;
on_ready_t on_write_ready;
epoll_registration reg;
bool* destroyed;
};
std::unique_ptr<impl> pimpl;
};
struct server_socket
{
typedef std::function<void ()> on_connected_t;
server_socket(epoll& ep, on_connected_t on_connected);
server_socket(epoll& ep, ipv4_endpoint local_endpoint, on_connected_t on_connected);
ipv4_endpoint local_endpoint() const;
client_socket accept(client_socket::on_ready_t on_disconnect) const;
client_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;
private:
file_descriptor fd;
on_connected_t on_connected;
epoll_registration reg;
};
struct eventfd
{
typedef std::function<void ()> on_event_t;
eventfd(epoll& ep, bool semaphore, on_event_t on_event);
void notify(uint64_t increment = 1);
void set_on_event(on_event_t on_event);
private:
file_descriptor fd;
on_event_t on_event;
epoll_registration reg;
};
#endif // SOCKET_APPLE_H