-
Notifications
You must be signed in to change notification settings - Fork 2
/
UDP.cpp
356 lines (331 loc) · 7.73 KB
/
UDP.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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include "Socket.h"
/*
class UDPSocket
constructor()
close()
setblocking(nonblocking: boolean)
setbroadcast(isbroadcast: boolean) Broadcast default to false.
listen([ip: string], port: int) Address default to 0.0.0.0.
connect(ip: string, port: int)
send(data: string)
recv([maxsize: int]): string. Default to 4KB.
sendto([ip: string], port: int, data: string) If ip is empty, default to 255.255.255.255 (broadcast)
recvfrom(): data: string, ip: string, port: int
*/
int udp_socket_dtor(lua_State* L)
{
auto s = lua_checkblock<SocketData>(L, 1, "LuaEngineUDPSocket");
if (s->fd > 0)
{
closesocket(s->fd);
s->fd = -1;
}
s->~SocketData();
return 0;
}
int udp_socket_close(lua_State* L)
{
auto s = lua_checkblock<SocketData>(L, 1, "LuaEngineUDPSocket");
if (s->fd > 0)
{
closesocket(s->fd);
s->fd = -1;
}
return 0;
}
int udp_socket_send(lua_State* L)
{
auto s = lua_checkblock<SocketData>(L, 1, "LuaEngineUDPSocket");
size_t sz;
const char* str = luaL_checklstring(L, 2, &sz);
int ret = send(s->fd, str, sz, 0);
if (ret < 0)
{
int errcode = WSAGetLastError();
if (s->nonblocking && errcode == WSAEWOULDBLOCK)
{
lua_pushboolean(L, true);
return 1;
}
put_winerror(L, errcode, "send");
return lua_error(L);
}
return 0;
}
int udp_socket_recv(lua_State* L)
{
auto s = lua_checkblock<SocketData>(L, 1, "LuaEngineUDPSocket");
int ret = recv(s->fd, s->data.data(), s->buffsz, 0);
if (ret < 0)
{
int errcode = WSAGetLastError();
if (s->nonblocking && errcode == WSAEWOULDBLOCK)
{
return 0;
}
put_winerror(L, errcode, "recv");
return lua_error(L);
}
lua_pushlstring(L, s->data.data(), ret);
return 1;
}
int udp_socket_sendto(lua_State* L)
{
auto s = lua_checkblock<SocketData>(L, 1, "LuaEngineUDPSocket");
const char* ip = NULL;
int port;
size_t sz;
const char* str = NULL;
if (lua_type(L, 2) == LUA_TSTRING)
{
ip = luaL_checkstring(L, 2);
port = luaL_checkinteger(L, 3);
str = luaL_checklstring(L, 4, &sz);
}
else
{
port = luaL_checkinteger(L, 2);
str = luaL_checklstring(L, 3, &sz);
}
sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
if (ip && strcmp(ip, "255.255.255.255"))
{
if (inet_pton(AF_INET, ip, &addr.sin_addr) < 0)
{
int errcode = WSAGetLastError();
put_winerror(L, errcode, "inet_pton");
return lua_error(L);
}
}
else
{
addr.sin_addr.s_addr = INADDR_BROADCAST;
}
int ret = sendto(s->fd, str, sz, 0, (const sockaddr*)&addr, sizeof(addr));
if (ret < 0)
{
int errcode = WSAGetLastError();
if (s->nonblocking && errcode == WSAEWOULDBLOCK)
{
lua_pushboolean(L, true);
return 1;
}
put_winerror(L, errcode, "sendto");
return lua_error(L);
}
return 0;
}
int udp_socket_recvfrom(lua_State* L)
{
auto s = lua_checkblock<SocketData>(L, 1, "LuaEngineUDPSocket");
sockaddr_in addr;
int addrlen = sizeof(addr);
int ret = recvfrom(s->fd, s->data.data(), s->buffsz, 0, (sockaddr*)&addr, &addrlen);
if (ret < 0)
{
int errcode = WSAGetLastError();
if (s->nonblocking && errcode == WSAEWOULDBLOCK)
{
return 0;
}
put_winerror(L, errcode, "recvfrom");
return lua_error(L);
}
char buffer[1024] = { 0 };
if (inet_ntop(AF_INET, &addr.sin_addr, buffer, 1024) < 0)
{
int errcode = WSAGetLastError();
put_winerror(L, errcode, "inet_ntop");
return lua_error(L);
}
lua_pushlstring(L, s->data.data(), ret);
lua_pushstring(L, buffer);
lua_pushinteger(L, ntohs(addr.sin_port));
return 3;
}
int udp_socket_connect(lua_State* L)
{
auto s = lua_checkblock<SocketData>(L, 1, "LuaEngineUDPSocket");
const char* ip = luaL_checkstring(L, 2);
int port = luaL_checkinteger(L, 3);
sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
if (inet_pton(AF_INET, ip, &addr.sin_addr) < 0)
{
int errcode = WSAGetLastError();
put_winerror(L, errcode, "inet_pton");
return lua_error(L);
}
addr.sin_port = htons(port);
int ret = connect(s->fd, (const sockaddr*)&addr, sizeof(addr));
if (ret < 0)
{
int errcode = WSAGetLastError();
if (s->nonblocking && errcode == WSAEWOULDBLOCK)
{
lua_pushboolean(L, true);
return 1;
}
put_winerror(L, errcode, "connect");
return lua_error(L);
}
return 0;
}
int udp_socket_setblocking(lua_State* L)
{
auto s = lua_checkblock<SocketData>(L, 1, "LuaEngineUDPSocket");
luaL_checkany(L, 2);
bool blocking = lua_toboolean(L, 2);
if (blocking != s->nonblocking)
{
return 0;
}
if (blocking) // set to blocking
{
u_long arg = 0;
int ret = ioctlsocket(s->fd, FIONBIO, &arg);
if (ret)
{
int errcode = WSAGetLastError();
put_winerror(L, errcode, "ioctlsocket");
return lua_error(L);
}
}
else // set to nonblocking
{
u_long arg = 1;
int ret = ioctlsocket(s->fd, FIONBIO, &arg);
if (ret)
{
int errcode = WSAGetLastError();
put_winerror(L, errcode, "ioctlsocket");
return lua_error(L);
}
}
return 0;
}
int udp_socket_setbroadcast(lua_State* L)
{
auto s = lua_checkblock<SocketData>(L, 1, "LuaEngineUDPSocket");
socklen_t opt = 0;
if (!lua_isnone(L, 2) && lua_toboolean(L, 2))
{
opt = 1;
}
int ret = setsockopt(s->fd, SOL_SOCKET, SO_BROADCAST, (const char*)&opt, sizeof(opt));
if (ret < 0)
{
int errcode = WSAGetLastError();
put_winerror(L, errcode, "setsockopt");
return lua_error(L);
}
return 0;
}
int udp_socket_listen(lua_State* L)
{
auto s = lua_checkblock<SocketData>(L, 1, "LuaEngineUDPSocket");
const char* ip = NULL;
int port;
if (lua_type(L, 2) == LUA_TSTRING)
{
ip = luaL_checkstring(L, 2);
port = luaL_checkinteger(L, 3);
}
else
{
port = luaL_checkinteger(L, 2);
}
sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
if (ip && strcmp(ip, "0.0.0.0"))
{
if (inet_pton(AF_INET, ip, &addr.sin_addr) < 0)
{
int errcode = WSAGetLastError();
put_winerror(L, errcode, "inet_pton");
return lua_error(L);
}
}
else
{
addr.sin_addr.s_addr = INADDR_ANY;
}
addr.sin_port = htons(port);
int ret = bind(s->fd, (const sockaddr*)&addr, sizeof(addr));
if (ret < 0)
{
int errcode = WSAGetLastError();
put_winerror(L, errcode, "bind");
return lua_error(L);
}
return 0;
}
void put_udp_socket(lua_State* L, int fd, bool nonblocking)
{
auto s = new (lua_newblock<SocketData>(L)) SocketData;
if (luaL_newmetatable(L, "LuaEngineUDPSocket"))
{
lua_setfield_function(L, "__gc", udp_socket_dtor);
lua_newtable(L);
lua_setfield_function(L, "close", udp_socket_close);
lua_setfield_function(L, "send", udp_socket_send);
lua_setfield_function(L, "recv", udp_socket_recv);
lua_setfield_function(L, "sendto", udp_socket_sendto);
lua_setfield_function(L, "recvfrom", udp_socket_recvfrom);
lua_setfield_function(L, "listen", udp_socket_listen);
lua_setfield_function(L, "connect", udp_socket_connect);
lua_setfield_function(L, "setblocking", udp_socket_setblocking);
lua_setfield_function(L, "setbroadcast", udp_socket_setbroadcast);
lua_setfield(L, -2, "__index");
}
lua_setmetatable(L, -2);
s->fd = fd;
s->nonblocking = nonblocking;
s->buffsz = 4096;
s->data.resize(s->buffsz);
}
int udp_socket_new(lua_State* L)
{
bool nonblocking = false;
if (!lua_isnone(L, 1))
{
if (lua_toboolean(L, 1))
{
nonblocking = true;
}
}
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0)
{
int errcode = WSAGetLastError();
put_winerror(L, errcode, "socket");
return lua_error(L);
}
if (nonblocking)
{
u_long arg = 1;
int ret = ioctlsocket(fd, FIONBIO, &arg);
if (ret)
{
int errcode = WSAGetLastError();
closesocket(fd);
put_winerror(L, errcode, "ioctlsocket");
return lua_error(L);
}
}
put_udp_socket(L, fd, nonblocking);
return 1;
}
void InitUDPSocket(lua_State* L)
{
lua_getglobal(L, "package");
lua_getfield(L, -1, "loaded");
lua_pushcfunction(L, udp_socket_new);
lua_setfield(L, -2, "UDPSocket");
lua_pop(L, 2);
}