forked from starwing/znet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
znet.hpp
303 lines (241 loc) · 8.21 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
#ifndef ZNET_HPP_INCLUDED
#define ZNET_HPP_INCLUDED
#ifndef ZNET_HPP_NO_IMPLEMENTATION
# 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>;
/* callbacks */
using OnPostHandler = std::function<void()>;
using OnTimerHandler = 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)>;
/* interfaces */
struct EventLoop : public std::enable_shared_from_this<EventLoop>
{
EventLoop(zn_State *S = nullptr)
: S(S)
{ }
zn_State *S = nullptr;
std::unordered_map<unsigned long long, OnTimerHandler> timers;
bool initialize();
void runOnce(bool isImeediately = false);
void post(OnPostHandler&& h);
unsigned long long createTimer(unsigned int delayms, OnTimerHandler&& h);
bool cancelTimer(unsigned long long timerID);
};
struct TcpAccept : public std::enable_shared_from_this<TcpAccept>
{
TcpAccept(zn_Accept *accept = nullptr)
: accept(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)
{ }
zn_Tcp *tcp = nullptr;
OnConnectHandler connectHandler;
OnSendHandler sendHandler;
OnRecvHandler recvHandler;
bool initialize(EventLoopPtr const& summer);
bool doClose();
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);
};
struct UdpSocket : public std::enable_shared_from_this<UdpSocket>
{
UdpSocket(zn_Udp *udp = nullptr)
: udp(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 doRecv(char* buf, unsigned len, OnRecvFromHandler&& h);
};
/* implements */
inline bool EventLoop::initialize()
{ return (S = zn_newstate()) != nullptr; }
inline void EventLoop::runOnce(bool isImeediately/* = false*/)
{ zn_run(S, isImeediately ? ZN_RUN_CHECK : ZN_RUN_ONCE); }
inline void post_cb(void *ud, zn_State *)
{
std::unique_ptr<OnPostHandler> h
(reinterpret_cast<OnPostHandler*>(ud));
(*h)();
}
inline void EventLoop::post(OnPostHandler&& h)
{
auto newh = new OnPostHandler(h);
zn_post(S, post_cb, newh);
}
static inline void timer_cb(void *ud, zn_Timer *timer, unsigned elapsed)
{
EventLoop* loop = static_cast<EventLoop*>(ud);
auto it = loop->timers.find(reinterpret_cast<unsigned long long>(timer));
if (it == loop->timers.end()) return;
auto h = std::move(it->second);
loop->timers.erase(it);
h();
}
inline unsigned long long EventLoop::createTimer(unsigned int delayms, OnTimerHandler&& h)
{
zn_Timer *t = zn_newtimer(S, timer_cb, this);
auto timerID = reinterpret_cast<unsigned long long>(t);
timers[timerID] = std::move(h);
zn_starttimer(t, delayms);
return timerID;
}
inline bool EventLoop::cancelTimer(unsigned long long timerID)
{
auto it = timers.find(timerID);
if (it == timers.end()) return false;
zn_canceltimer(reinterpret_cast<zn_Timer*>(timerID));
timers.erase(it);
return true;
}
inline bool TcpAccept::initialize(EventLoopPtr const& summer)
{ return (accept = zn_newaccept(summer->S)) != nullptr; }
inline bool TcpAccept::openAccept(std::string const& ip, unsigned short port)
{ return zn_listen(accept, ip.c_str(), port) == ZN_OK; }
static inline void accept_ud(void *ud, zn_Accept *accept, unsigned err, zn_Tcp *tcp)
{
TcpAccept *ta = static_cast<TcpAccept*>(ud);
OnAcceptHandler h(std::move(ta->acceptHandler));
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_ud, this) == ZN_OK)
{
client = s;
acceptHandler = h;
return true;
}
return false;
}
inline bool TcpSocket::initialize(EventLoopPtr const& summer)
{ return (tcp = zn_newtcp(summer->S)) != nullptr; }
inline bool TcpSocket::doClose()
{ return zn_closetcp(tcp) == ZN_OK; }
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)
{
TcpSocket *ts = static_cast<TcpSocket*>(ud);
OnConnectHandler h(std::move(ts->connectHandler));
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, connect_cb, this) == ZN_OK)
{
connectHandler = h;
return true;
}
return false;
}
static inline void send_cb(void *ud, zn_Tcp *tcp, unsigned err, unsigned count)
{
TcpSocket *ts = static_cast<TcpSocket*>(ud);
OnSendHandler h(std::move(ts->sendHandler));
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 = h;
return true;
}
return false;
}
static inline void recv_cb(void *ud, zn_Tcp *tcp, unsigned err, unsigned count)
{
TcpSocket *ts = static_cast<TcpSocket*>(ud);
OnRecvHandler h(std::move(ts->recvHandler));
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 = h;
return true;
}
return false;
}
inline bool UdpSocket::initialize(EventLoopPtr const& summer, std::string const& ip, unsigned short port)
{ return (udp = zn_newudp(summer->S, ip.c_str(), port)) != nullptr; }
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; }
static inline void recvfrom_cb(void *ud, zn_Udp *udp, unsigned err, unsigned count, const char *ip, unsigned port)
{
UdpSocket *us = static_cast<UdpSocket*>(ud);
OnRecvFromHandler h(std::move(us->recvFromHandler));
h(static_cast<NetErrorCode>(err), ip, port, count);
}
inline bool UdpSocket::doRecv(char* buf, unsigned len, OnRecvFromHandler&& h)
{
if (zn_recvfrom(udp, buf, len, recvfrom_cb, this) == ZN_OK)
{
recvFromHandler = h;
return true;
}
return false;
}
class GlobalEnv {
GlobalEnv() { zn_initialize(); }
~GlobalEnv() { zn_deinitialize(); }
friend inline GlobalEnv& globalEnv() {
static GlobalEnv env;
return env;
}
};
ZNPP_NS_END
#endif /* ZNET_HPP_INCLUDED */
/* cc: flags+='-std=c++11' */