-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlua_uv_tcp.cpp
218 lines (189 loc) · 6.03 KB
/
lua_uv_tcp.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
#include "luaopen_uv.h"
#if defined(__MINGW32__) || defined(_MSC_VER)
#include <inet_net_pton.h>
#include <inet_ntop.h>
#define uv_inet_pton ares_inet_pton
#define uv_inet_ntop ares_inet_ntop
#else /* __POSIX__ */
#include <arpa/inet.h>
#define uv_inet_pton inet_pton
#define uv_inet_ntop inet_ntop
#endif
int lua_uv_tcp(lua_State * L) {
uv_loop_t * loop = lua_uv_loop(L);
uv_tcp_t * t = (uv_tcp_t *)lua_newuserdata(L, sizeof(uv_tcp_t));
uv_tcp_init(loop, t);
luaL_getmetatable(L, classname<uv_tcp_t>());
lua_setmetatable(L, -2);
lua_uv_ok(L);
return 1;
}
int lua_uv_tcp_nodelay(lua_State * L) {
uv_tcp_t * t = lua_generic_object<uv_tcp_t>(L, 1);
int enable = lua_toboolean(L, 2);
int delay = lua_tointeger(L, 3);
uv_tcp_keepalive(t, enable, delay);
lua_uv_ok(L);
return 0;
}
int lua_uv_tcp_keepalive(lua_State * L) {
uv_tcp_t * t = lua_generic_object<uv_tcp_t>(L, 1);
int enable = lua_toboolean(L, 2);
uv_tcp_nodelay(t, enable);
lua_uv_ok(L);
return 0;
}
int lua_uv_tcp_bind(lua_State * L) {
uv_tcp_t * t = lua_generic_object<uv_tcp_t>(L, 1);
const char * host = luaL_checkstring(L, 2);
int port = luaL_checkinteger(L, 3);
struct sockaddr_in addr = uv_ip4_addr(host, port);
uv_tcp_bind(t, addr);
lua_uv_ok(L);
return 0;
}
int lua_uv_tcp_bind6(lua_State * L) {
uv_tcp_t * t = lua_generic_object<uv_tcp_t>(L, 1);
const char * host = luaL_checkstring(L, 2);
int port = luaL_checkinteger(L, 3);
struct sockaddr_in6 addr = uv_ip6_addr(host, port);
uv_tcp_bind6(t, addr);
lua_uv_ok(L);
return 0;
}
int lua_uv_tcp_getsockname(lua_State * L) {
uv_tcp_t * t = lua_generic_object<uv_tcp_t>(L, 1);
struct sockaddr_storage name;
int namelen = sizeof(name);
uv_tcp_getsockname(t, (struct sockaddr*)(&name), &namelen);
lua_uv_ok(L);
lua_newtable(L);
if (name.ss_family == AF_INET) {
lua_pushstring(L, "IPv4");
lua_setfield(L, -2, "family");
struct sockaddr_in* addr = (struct sockaddr_in*)&name;
char ip[INET_ADDRSTRLEN];
uv_inet_ntop(AF_INET, &(addr->sin_addr), ip, INET6_ADDRSTRLEN);
lua_pushstring(L, ip);
lua_setfield(L, -2, "address");
lua_pushnumber(L, ntohs(addr->sin_port));
lua_setfield(L, -2, "port");
} else if (name.ss_family == AF_INET6) {
lua_pushstring(L, "IPv6");
lua_setfield(L, -2, "family");
struct sockaddr_in6* addr = (struct sockaddr_in6*)&name;
char ip[INET6_ADDRSTRLEN];
uv_inet_ntop(AF_INET6, &(addr->sin6_addr), ip, INET6_ADDRSTRLEN);
lua_pushstring(L, ip);
lua_setfield(L, -2, "address");
lua_pushnumber(L, ntohs(addr->sin6_port));
lua_setfield(L, -2, "port");
}
return 1;
}
int lua_uv_tcp_getpeername(lua_State * L) {
uv_tcp_t * t = lua_generic_object<uv_tcp_t>(L, 1);
struct sockaddr_storage name;
int namelen = sizeof(name);
uv_tcp_getpeername(t, (struct sockaddr*)(&name), &namelen);
lua_uv_ok(L);
lua_newtable(L);
if (name.ss_family == AF_INET) {
lua_pushstring(L, "IPv4");
lua_setfield(L, -2, "family");
struct sockaddr_in* addr = (struct sockaddr_in*)&name;
char ip[INET_ADDRSTRLEN];
uv_inet_ntop(AF_INET, &(addr->sin_addr), ip, INET6_ADDRSTRLEN);
lua_pushstring(L, ip);
lua_setfield(L, -2, "address");
lua_pushnumber(L, ntohs(addr->sin_port));
lua_setfield(L, -2, "port");
} else if (name.ss_family == AF_INET6) {
lua_pushstring(L, "IPv6");
lua_setfield(L, -2, "family");
struct sockaddr_in6* addr = (struct sockaddr_in6*)&name;
char ip[INET6_ADDRSTRLEN];
uv_inet_ntop(AF_INET6, &(addr->sin6_addr), ip, INET6_ADDRSTRLEN);
lua_pushstring(L, ip);
lua_setfield(L, -2, "address");
lua_pushnumber(L, ntohs(addr->sin6_port));
lua_setfield(L, -2, "port");
}
return 1;
}
void lua_uv_tcp_connect_resume(uv_connect_t * req, int status) {
dprintf("lua_uv_tcp_connect_resume\n");
lua_State * L = (lua_State *)req->data;
callback_resume_after(L);
lua_pushinteger(L, status);
Lua(L).resume(1);
}
int lua_uv_tcp_connect(lua_State * L) {
uv_tcp_t * t = lua_generic_object<uv_tcp_t>(L, 1);
const char * host = luaL_checkstring(L, 2);
int port = luaL_checkinteger(L, 3);
struct sockaddr_in addr = uv_ip4_addr(host, port);
// are we in a coroutine?
if (lua_pushthread(L)) {
luaL_error(L, "must be called from a coroutine");
// // not a coroutine; make a blocking call:
// uv_connect_t req;
// uv_tcp_connect(&req, t, addr, NULL);
// lua_uv_ok(L);
// return 0;
} else {
uv_connect_t * req = callback_resume_before<uv_connect_t>(L, 1);
uv_tcp_connect(req, t, addr, lua_uv_tcp_connect_resume);
return lua_yield(L, 2);
}
return 1;
}
int lua_uv_tcp_connect6(lua_State * L) {
uv_tcp_t * t = lua_generic_object<uv_tcp_t>(L, 1);
const char * host = luaL_checkstring(L, 2);
int port = luaL_checkinteger(L, 3);
struct sockaddr_in6 addr = uv_ip6_addr(host, port);
// are we in a coroutine?
if (lua_pushthread(L)) {
luaL_error(L, "must be called from a coroutine");
// // not a coroutine; make a blocking call:
// uv_connect_t req;
// uv_tcp_connect6(&req, t, addr, NULL);
// lua_uv_ok(L);
// return 0;
} else {
uv_connect_t * req = callback_resume_before<uv_connect_t>(L, 1);
uv_tcp_connect6(req, t, addr, lua_uv_tcp_connect_resume);
return lua_yield(L, 2);
}
return 1;
}
int lua_uv_tcp___gc(lua_State * L) {
dprintf("lua_uv_tcp___gc\n");
lua_uv_handle_close(L);
return 0;
}
// bind loop as an upvalue of all the methods:
#define LUA_UV_CLOSURE(PREFIX, NAME) \
lua_pushvalue(L, loopidx); \
lua_pushcclosure(L, lua_##PREFIX##_##NAME, 1); \
lua_setfield(L, -2, #NAME);
void init_tcp_metatable(lua_State * L, int loopidx) {
luaL_newmetatable(L, classname<uv_tcp_t>());
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, lua_generic___tostring<uv_tcp_t>);
lua_setfield(L, -2, "__tostring");
lua_pushcfunction(L, lua_uv_tcp___gc);
lua_setfield(L, -2, "__gc");
LUA_UV_CLOSURE(uv_tcp, nodelay);
LUA_UV_CLOSURE(uv_tcp, keepalive);
LUA_UV_CLOSURE(uv_tcp, bind);
LUA_UV_CLOSURE(uv_tcp, bind6);
LUA_UV_CLOSURE(uv_tcp, getsockname);
LUA_UV_CLOSURE(uv_tcp, getpeername);
LUA_UV_CLOSURE(uv_tcp, connect);
LUA_UV_CLOSURE(uv_tcp, connect6);
lua_super_metatable(L, -1, classname<uv_stream_t>());
lua_pop(L, 1);
}