-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsocket_async.cpp
166 lines (124 loc) · 3.69 KB
/
socket_async.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
#include "sockpuppet/socket_async.h"
#include "driver_impl.h" // for DriverImpl
#include "socket_async_impl.h" // for SocketAsyncImpl
#include "todo_impl.h" // for ToDoImpl
#include <stdexcept> // for std::logic_error
namespace sockpuppet {
namespace {
template<typename Fn>
std::function<Fn> &checked(std::function<Fn> &handler)
{
if(!handler) {
throw std::logic_error("invalid handler");
}
return handler;
}
} // unnamed namespace
Driver::Driver()
: impl(std::make_shared<DriverImpl>())
{
}
void Driver::Step(Duration timeout)
{
impl->Step(timeout);
}
void Driver::Run()
{
impl->Run();
}
void Driver::Stop()
{
impl->Stop();
}
Driver::Driver(Driver &&other) noexcept = default;
Driver::~Driver() = default;
Driver &Driver::operator=(Driver &&other) noexcept = default;
ToDo::ToDo(Driver &driver, std::function<void()> task)
: impl(std::make_shared<ToDoImpl>(driver.impl, std::move(task)))
{
}
ToDo::ToDo(Driver &driver, std::function<void()> task, TimePoint when)
: impl(std::make_shared<ToDoImpl>(driver.impl, std::move(task), when))
{
driver.impl->ToDoInsert(impl);
}
ToDo::ToDo(Driver &driver, std::function<void()> task, Duration delay)
: impl(std::make_shared<ToDoImpl>(driver.impl, std::move(task), Clock::now() + delay))
{
driver.impl->ToDoInsert(impl);
}
void ToDo::Cancel()
{
impl->Cancel();
}
void ToDo::Shift(TimePoint when)
{
impl->Shift(when);
}
void ToDo::Shift(Duration delay)
{
impl->Shift(Clock::now() + delay);
}
ToDo::ToDo(ToDo &&other) noexcept = default;
ToDo::~ToDo() = default;
ToDo &ToDo::operator=(ToDo &&other) noexcept = default;
SocketUdpAsync::SocketUdpAsync(SocketUdpBuffered &&buff,
Driver &driver, ReceiveFromHandler handleReceiveFrom)
: impl(std::make_unique<SocketAsyncImpl>(
std::move(buff.impl),
driver.impl,
std::move(checked(handleReceiveFrom))))
{
}
std::future<void> SocketUdpAsync::SendTo(BufferPtr &&buffer,
Address const &dstAddress)
{
return impl->SendTo(std::move(buffer), dstAddress.impl);
}
Address SocketUdpAsync::LocalAddress() const
{
return Address(impl->buff->sock->GetSockName());
}
SocketUdpAsync::SocketUdpAsync(SocketUdpAsync &&other) noexcept = default;
SocketUdpAsync::~SocketUdpAsync() = default;
SocketUdpAsync &SocketUdpAsync::operator=(SocketUdpAsync &&other) noexcept = default;
SocketTcpAsync::SocketTcpAsync(SocketTcpBuffered &&buff, Driver &driver,
ReceiveHandler handleReceive, DisconnectHandler handleDisconnect)
: impl(std::make_unique<SocketAsyncImpl>(
std::move(buff.impl),
driver.impl,
std::move(checked(handleReceive)),
std::move(checked(handleDisconnect))))
{
}
std::future<void> SocketTcpAsync::Send(BufferPtr &&buffer)
{
return impl->Send(std::move(buffer));
}
Address SocketTcpAsync::LocalAddress() const
{
return Address(impl->buff->sock->GetSockName());
}
Address SocketTcpAsync::PeerAddress() const
{
return Address(impl->buff->sock->GetPeerName());
}
SocketTcpAsync::SocketTcpAsync(SocketTcpAsync &&other) noexcept = default;
SocketTcpAsync::~SocketTcpAsync() = default;
SocketTcpAsync &SocketTcpAsync::operator=(SocketTcpAsync &&other) noexcept = default;
AcceptorAsync::AcceptorAsync(Acceptor &&sock, Driver &driver, ConnectHandler handleConnect)
: impl(std::make_unique<SocketAsyncImpl>(
std::move(sock.impl),
driver.impl,
std::move(checked(handleConnect))))
{
impl->buff->sock->Listen();
}
Address AcceptorAsync::LocalAddress() const
{
return Address(impl->buff->sock->GetSockName());
}
AcceptorAsync::AcceptorAsync(AcceptorAsync &&other) noexcept = default;
AcceptorAsync::~AcceptorAsync() = default;
AcceptorAsync &AcceptorAsync::operator=(AcceptorAsync &&other) noexcept = default;
} // namespace sockpuppet