-
Notifications
You must be signed in to change notification settings - Fork 34
/
znet.hpp
340 lines (276 loc) · 8.81 KB
/
znet.hpp
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#ifndef ZNET_HPP_INCLUDED
#define ZNET_HPP_INCLUDED
#ifdef ZNPP_STATIC_API
# define ZN_IMPLEMENTATION
# define ZN_API static inline
#endif
#include "znet.h"
#define ZNPP_NS_BEGIN namespace zsummer { namespace network {
#define ZNPP_NS_END } }
#include <functional>
#include <memory>
#include <string>
#include <unordered_map>
ZNPP_NS_BEGIN
/* declarations */
struct EventLoop;
struct TcpAccept;
struct TcpSocket;
struct UdpSocket;
enum NetErrorCode
{
NEC_SUCCESS = ZN_OK,
NEC_ERROR = ZN_ERROR,
NEC_REMOTE_CLOSED = ZN_ECLOSED,
NEC_REMOTE_HANGUP = ZN_EHANGUP,
};
/* pointers */
using EventLoopPtr = std::shared_ptr<EventLoop>;
using TcpAcceptPtr = std::shared_ptr<TcpAccept>;
using TcpSocketPtr = std::shared_ptr<TcpSocket>;
using UdpSocketPtr = std::shared_ptr<UdpSocket>;
using TimerID = zn_Timer*;
/* callbacks */
#ifdef ZN_USE_NEW_TIMERHANDLER
using OnTimerHandler = std::function<zn_Time()>;
#else
using OnTimerHandler = std::function<void()>;
#endif
using OnPostHandler = std::function<void()>;
using OnAcceptHandler = std::function<void(NetErrorCode, TcpSocketPtr)>;
using OnConnectHandler = std::function<void(NetErrorCode)>;
using OnSendHandler = std::function<void(NetErrorCode, unsigned)>;
using OnRecvHandler = std::function<void(NetErrorCode, unsigned)>;
using OnRecvFromHandler = std::function<void(NetErrorCode, char const*, unsigned short, unsigned)>;
const TimerID InvalidTimerID = nullptr;
/* interfaces */
struct EventLoop : public std::enable_shared_from_this<EventLoop>
{
EventLoop(zn_State *S = nullptr)
: S(S) { }
~EventLoop() { if (S) zn_close(S); }
zn_State *S = nullptr;
std::unordered_map<TimerID, OnTimerHandler> timers;
bool initialize();
void runOnce(bool isImeediately = false);
void post(OnPostHandler&& h);
TimerID createTimer(zn_Time delayms, OnTimerHandler&& h);
bool cancelTimer(TimerID timerID);
};
struct TcpAccept : public std::enable_shared_from_this<TcpAccept>
{
TcpAccept(zn_Accept *accept = nullptr)
: accept(accept) { }
~TcpAccept() { if (accept) zn_delaccept(accept); }
zn_Accept *accept;
TcpSocketPtr client;
OnAcceptHandler acceptHandler;
bool initialize(EventLoopPtr const& summer);
bool openAccept(std::string const& ip, unsigned short port);
bool doAccept(TcpSocketPtr const& s, OnAcceptHandler&& h);
};
struct TcpSocket : public std::enable_shared_from_this<TcpSocket>
{
TcpSocket(zn_Tcp *tcp = nullptr)
: tcp(tcp) { }
~TcpSocket() { if (tcp) zn_deltcp(tcp); }
zn_Tcp *tcp = nullptr;
OnConnectHandler connectHandler;
OnSendHandler sendHandler;
OnRecvHandler recvHandler;
bool initialize(EventLoopPtr const& summer);
bool getPeerInfo(std::string& remoteIP, unsigned short& remotePort) const;
bool doConnect(std::string const& remoteIP, unsigned short remotePort, OnConnectHandler&& h);
bool doSend(char const* buf, unsigned len, OnSendHandler&& h);
bool doRecv(char* buf, unsigned len, OnRecvHandler&& h);
bool doClose();
};
struct UdpSocket : public std::enable_shared_from_this<UdpSocket>
{
UdpSocket(zn_Udp *udp = nullptr)
: udp(udp) { }
~UdpSocket() { if (udp) zn_deludp(udp); }
zn_Udp *udp = nullptr;
OnRecvFromHandler recvFromHandler;
bool initialize(EventLoopPtr const& summer, std::string const& ip, unsigned short port);
bool doSendTo(char const* buf, unsigned len, std::string const& remoteIP, unsigned short remotePort);;
bool doRecvFrom(char* buf, unsigned len, OnRecvFromHandler&& h);
};
/* implements */
inline void EventLoop::runOnce(bool isImeediately/* = false*/)
{ zn_run(S, isImeediately ? ZN_RUN_CHECK : ZN_RUN_ONCE); }
inline bool EventLoop::initialize()
{
if (S == nullptr)
return (S = zn_newstate()) != nullptr;
return true;
}
inline void EventLoop::post(OnPostHandler&& h)
{
zn_post(S, [](void *ud, zn_State *) {
std::unique_ptr<OnPostHandler> h
(reinterpret_cast<OnPostHandler*>(ud));
(*h)();
}, new OnPostHandler(std::move(h)));
}
static inline zn_Time timer_cb(void *ud, zn_Timer *timer, unsigned elapsed)
{
auto loop = static_cast<EventLoop*>(ud);
auto it = loop->timers.find(timer);
(void)elapsed;
if (it == loop->timers.end()) return 0;
#ifdef ZN_USE_NEW_TIMERHANDLER
auto ret = it->second();
if (ret == 0) loop->timers.erase(it);
return ret;
#else
zn_Time ret = 0;
auto h = std::move(it->second);
h();
loop->timers.erase(it);
#endif
return ret;
}
inline TimerID EventLoop::createTimer(zn_Time delayms, OnTimerHandler&& h)
{
auto t = zn_newtimer(S, timer_cb, this);
timers[t] = std::move(h);
zn_starttimer(t, delayms);
return t;
}
inline bool EventLoop::cancelTimer(TimerID timerID)
{
auto it = timers.find(timerID);
if (it == timers.end()) return false;
zn_canceltimer(timerID);
timers.erase(it);
return true;
}
inline bool TcpAccept::openAccept(std::string const& ip, unsigned short port)
{ return zn_listen(accept, ip.c_str(), port) == ZN_OK; }
inline bool TcpAccept::initialize(EventLoopPtr const& summer)
{
if (accept == nullptr)
return (accept = zn_newaccept(summer->S, 0)) != nullptr;
return true;
}
static inline void accept_cb(void *ud, zn_Accept *accept, unsigned err, zn_Tcp *tcp)
{
auto ta = static_cast<TcpAccept*>(ud);
auto h(std::move(ta->acceptHandler));
(void)accept;
ta->client->tcp = tcp;
h(static_cast<NetErrorCode>(err), std::move(ta->client));
}
inline bool TcpAccept::doAccept(TcpSocketPtr const& s, OnAcceptHandler&& h)
{
if (zn_accept(accept, accept_cb, this) == ZN_OK)
{
client = s;
acceptHandler = std::move(h);
return true;
}
return false;
}
inline bool TcpSocket::doClose()
{ return zn_closetcp(tcp) == ZN_OK; }
inline bool TcpSocket::initialize(EventLoopPtr const& summer)
{
if (tcp == nullptr)
return (tcp = zn_newtcp(summer->S)) != nullptr;
return true;
}
inline bool TcpSocket::getPeerInfo(std::string& remoteIP, unsigned short& remotePort) const
{
zn_PeerInfo info;
zn_getpeerinfo(tcp, &info);
remoteIP = info.addr;
remotePort = info.port;
return true;
}
static inline void connect_cb(void *ud, zn_Tcp *tcp, unsigned err)
{
auto ts = static_cast<TcpSocket*>(ud);
auto h(std::move(ts->connectHandler));
(void)tcp;
h(static_cast<NetErrorCode>(err));
}
inline bool TcpSocket::doConnect(std::string const& remoteIP, unsigned short remotePort, OnConnectHandler&& h)
{
if (zn_connect(tcp, remoteIP.c_str(), remotePort, 0, connect_cb, this) == ZN_OK)
{
connectHandler = std::move(h);
return true;
}
return false;
}
static inline void send_cb(void *ud, zn_Tcp *tcp, unsigned err, unsigned count)
{
auto ts = static_cast<TcpSocket*>(ud);
auto h(std::move(ts->sendHandler));
(void)tcp;
h(static_cast<NetErrorCode>(err), count);
}
inline bool TcpSocket::doSend(char const* buf, unsigned len, OnSendHandler&& h)
{
if (zn_send(tcp, buf, len, send_cb, this) == ZN_OK)
{
sendHandler = std::move(h);
return true;
}
return false;
}
static inline void recv_cb(void *ud, zn_Tcp *tcp, unsigned err, unsigned count)
{
auto ts = static_cast<TcpSocket*>(ud);
auto h(std::move(ts->recvHandler));
(void)tcp;
h(static_cast<NetErrorCode>(err), count);
}
inline bool TcpSocket::doRecv(char* buf, unsigned len, OnRecvHandler&& h)
{
if (zn_recv(tcp, buf, len, recv_cb, this) == ZN_OK)
{
recvHandler = std::move(h);
return true;
}
return false;
}
inline bool UdpSocket::doSendTo(char const* buf, unsigned len, std::string const& remoteIP, unsigned short remotePort)
{ return zn_sendto(udp, buf, len, remoteIP.c_str(), remotePort) == ZN_OK; }
inline bool UdpSocket::initialize(EventLoopPtr const& summer, std::string const& ip, unsigned short port)
{
if (udp == nullptr)
return (udp = zn_newudp(summer->S, ip.c_str(), port)) != nullptr;
return true;
}
static inline void recvfrom_cb(void *ud, zn_Udp *udp, unsigned err, unsigned count, const char *ip, unsigned port)
{
auto us = static_cast<UdpSocket*>(ud);
auto h(std::move(us->recvFromHandler));
(void)udp;
h(static_cast<NetErrorCode>(err), ip, port, count);
}
inline bool UdpSocket::doRecvFrom(char* buf, unsigned len, OnRecvFromHandler&& h)
{
if (zn_recvfrom(udp, buf, len, recvfrom_cb, this) == ZN_OK)
{
recvFromHandler = std::move(h);
return true;
}
return false;
}
#ifndef ZNPP_NO_ENV
class ZSummerEnvironment {
public:
ZSummerEnvironment() { zn_initialize(); }
~ZSummerEnvironment() { zn_deinitialize(); }
};
extern ZSummerEnvironment g_appEnvironment;
# ifdef ZNPP_DEFINE_ENV
ZSummerEnvironment g_appEnvironment;
# endif
#endif
ZNPP_NS_END
#endif /* ZNET_HPP_INCLUDED */
/* cc: flags+='-std=c++11' */