diff options
Diffstat (limited to 'src/tcp.c')
-rw-r--r-- | src/tcp.c | 114 |
1 files changed, 57 insertions, 57 deletions
@@ -40,7 +40,7 @@ static int meth_dirty(lua_State *L); | |||
40 | /* tcp object methods */ | 40 | /* tcp object methods */ |
41 | static luaL_reg tcp[] = { | 41 | static luaL_reg tcp[] = { |
42 | {"__gc", meth_close}, | 42 | {"__gc", meth_close}, |
43 | {"__tostring", aux_tostring}, | 43 | {"__tostring", auxiliar_tostring}, |
44 | {"accept", meth_accept}, | 44 | {"accept", meth_accept}, |
45 | {"bind", meth_bind}, | 45 | {"bind", meth_bind}, |
46 | {"close", meth_close}, | 46 | {"close", meth_close}, |
@@ -84,13 +84,13 @@ static luaL_reg func[] = { | |||
84 | int tcp_open(lua_State *L) | 84 | int tcp_open(lua_State *L) |
85 | { | 85 | { |
86 | /* create classes */ | 86 | /* create classes */ |
87 | aux_newclass(L, "tcp{master}", tcp); | 87 | auxiliar_newclass(L, "tcp{master}", tcp); |
88 | aux_newclass(L, "tcp{client}", tcp); | 88 | auxiliar_newclass(L, "tcp{client}", tcp); |
89 | aux_newclass(L, "tcp{server}", tcp); | 89 | auxiliar_newclass(L, "tcp{server}", tcp); |
90 | /* create class groups */ | 90 | /* create class groups */ |
91 | aux_add2group(L, "tcp{master}", "tcp{any}"); | 91 | auxiliar_add2group(L, "tcp{master}", "tcp{any}"); |
92 | aux_add2group(L, "tcp{client}", "tcp{any}"); | 92 | auxiliar_add2group(L, "tcp{client}", "tcp{any}"); |
93 | aux_add2group(L, "tcp{server}", "tcp{any}"); | 93 | auxiliar_add2group(L, "tcp{server}", "tcp{any}"); |
94 | /* define library functions */ | 94 | /* define library functions */ |
95 | luaL_openlib(L, NULL, func, 0); | 95 | luaL_openlib(L, NULL, func, 0); |
96 | return 0; | 96 | return 0; |
@@ -103,23 +103,23 @@ int tcp_open(lua_State *L) | |||
103 | * Just call buffered IO methods | 103 | * Just call buffered IO methods |
104 | \*-------------------------------------------------------------------------*/ | 104 | \*-------------------------------------------------------------------------*/ |
105 | static int meth_send(lua_State *L) { | 105 | static int meth_send(lua_State *L) { |
106 | p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1); | 106 | p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{client}", 1); |
107 | return buf_meth_send(L, &tcp->buf); | 107 | return buffer_meth_send(L, &tcp->buf); |
108 | } | 108 | } |
109 | 109 | ||
110 | static int meth_receive(lua_State *L) { | 110 | static int meth_receive(lua_State *L) { |
111 | p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1); | 111 | p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{client}", 1); |
112 | return buf_meth_receive(L, &tcp->buf); | 112 | return buffer_meth_receive(L, &tcp->buf); |
113 | } | 113 | } |
114 | 114 | ||
115 | static int meth_getstats(lua_State *L) { | 115 | static int meth_getstats(lua_State *L) { |
116 | p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1); | 116 | p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{client}", 1); |
117 | return buf_meth_getstats(L, &tcp->buf); | 117 | return buffer_meth_getstats(L, &tcp->buf); |
118 | } | 118 | } |
119 | 119 | ||
120 | static int meth_setstats(lua_State *L) { | 120 | static int meth_setstats(lua_State *L) { |
121 | p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1); | 121 | p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{client}", 1); |
122 | return buf_meth_setstats(L, &tcp->buf); | 122 | return buffer_meth_setstats(L, &tcp->buf); |
123 | } | 123 | } |
124 | 124 | ||
125 | /*-------------------------------------------------------------------------*\ | 125 | /*-------------------------------------------------------------------------*\ |
@@ -127,7 +127,7 @@ static int meth_setstats(lua_State *L) { | |||
127 | \*-------------------------------------------------------------------------*/ | 127 | \*-------------------------------------------------------------------------*/ |
128 | static int meth_setoption(lua_State *L) | 128 | static int meth_setoption(lua_State *L) |
129 | { | 129 | { |
130 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); | 130 | p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); |
131 | return opt_meth_setoption(L, opt, &tcp->sock); | 131 | return opt_meth_setoption(L, opt, &tcp->sock); |
132 | } | 132 | } |
133 | 133 | ||
@@ -136,7 +136,7 @@ static int meth_setoption(lua_State *L) | |||
136 | \*-------------------------------------------------------------------------*/ | 136 | \*-------------------------------------------------------------------------*/ |
137 | static int meth_getfd(lua_State *L) | 137 | static int meth_getfd(lua_State *L) |
138 | { | 138 | { |
139 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); | 139 | p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); |
140 | lua_pushnumber(L, (int) tcp->sock); | 140 | lua_pushnumber(L, (int) tcp->sock); |
141 | return 1; | 141 | return 1; |
142 | } | 142 | } |
@@ -144,15 +144,15 @@ static int meth_getfd(lua_State *L) | |||
144 | /* this is very dangerous, but can be handy for those that are brave enough */ | 144 | /* this is very dangerous, but can be handy for those that are brave enough */ |
145 | static int meth_setfd(lua_State *L) | 145 | static int meth_setfd(lua_State *L) |
146 | { | 146 | { |
147 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); | 147 | p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); |
148 | tcp->sock = (t_sock) luaL_checknumber(L, 2); | 148 | tcp->sock = (t_socket) luaL_checknumber(L, 2); |
149 | return 0; | 149 | return 0; |
150 | } | 150 | } |
151 | 151 | ||
152 | static int meth_dirty(lua_State *L) | 152 | static int meth_dirty(lua_State *L) |
153 | { | 153 | { |
154 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); | 154 | p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); |
155 | lua_pushboolean(L, !buf_isempty(&tcp->buf)); | 155 | lua_pushboolean(L, !buffer_isempty(&tcp->buf)); |
156 | return 1; | 156 | return 1; |
157 | } | 157 | } |
158 | 158 | ||
@@ -162,25 +162,25 @@ static int meth_dirty(lua_State *L) | |||
162 | \*-------------------------------------------------------------------------*/ | 162 | \*-------------------------------------------------------------------------*/ |
163 | static int meth_accept(lua_State *L) | 163 | static int meth_accept(lua_State *L) |
164 | { | 164 | { |
165 | p_tcp server = (p_tcp) aux_checkclass(L, "tcp{server}", 1); | 165 | p_tcp server = (p_tcp) auxiliar_checkclass(L, "tcp{server}", 1); |
166 | p_tm tm = tm_markstart(&server->tm); | 166 | p_timeout tm = timeout_markstart(&server->tm); |
167 | t_sock sock; | 167 | t_socket sock; |
168 | int err = sock_accept(&server->sock, &sock, NULL, NULL, tm); | 168 | int err = socket_accept(&server->sock, &sock, NULL, NULL, tm); |
169 | /* if successful, push client socket */ | 169 | /* if successful, push client socket */ |
170 | if (err == IO_DONE) { | 170 | if (err == IO_DONE) { |
171 | p_tcp clnt = (p_tcp) lua_newuserdata(L, sizeof(t_tcp)); | 171 | p_tcp clnt = (p_tcp) lua_newuserdata(L, sizeof(t_tcp)); |
172 | aux_setclass(L, "tcp{client}", -1); | 172 | auxiliar_setclass(L, "tcp{client}", -1); |
173 | /* initialize structure fields */ | 173 | /* initialize structure fields */ |
174 | sock_setnonblocking(&sock); | 174 | socket_setnonblocking(&sock); |
175 | clnt->sock = sock; | 175 | clnt->sock = sock; |
176 | io_init(&clnt->io, (p_send) sock_send, (p_recv) sock_recv, | 176 | io_init(&clnt->io, (p_send) socket_send, (p_recv) socket_recv, |
177 | (p_error) sock_ioerror, &clnt->sock); | 177 | (p_error) socket_ioerror, &clnt->sock); |
178 | tm_init(&clnt->tm, -1, -1); | 178 | timeout_init(&clnt->tm, -1, -1); |
179 | buf_init(&clnt->buf, &clnt->io, &clnt->tm); | 179 | buffer_init(&clnt->buf, &clnt->io, &clnt->tm); |
180 | return 1; | 180 | return 1; |
181 | } else { | 181 | } else { |
182 | lua_pushnil(L); | 182 | lua_pushnil(L); |
183 | lua_pushstring(L, sock_strerror(err)); | 183 | lua_pushstring(L, socket_strerror(err)); |
184 | return 2; | 184 | return 2; |
185 | } | 185 | } |
186 | } | 186 | } |
@@ -190,7 +190,7 @@ static int meth_accept(lua_State *L) | |||
190 | \*-------------------------------------------------------------------------*/ | 190 | \*-------------------------------------------------------------------------*/ |
191 | static int meth_bind(lua_State *L) | 191 | static int meth_bind(lua_State *L) |
192 | { | 192 | { |
193 | p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{master}", 1); | 193 | p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{master}", 1); |
194 | const char *address = luaL_checkstring(L, 2); | 194 | const char *address = luaL_checkstring(L, 2); |
195 | unsigned short port = (unsigned short) luaL_checknumber(L, 3); | 195 | unsigned short port = (unsigned short) luaL_checknumber(L, 3); |
196 | const char *err = inet_trybind(&tcp->sock, address, port); | 196 | const char *err = inet_trybind(&tcp->sock, address, port); |
@@ -208,13 +208,13 @@ static int meth_bind(lua_State *L) | |||
208 | \*-------------------------------------------------------------------------*/ | 208 | \*-------------------------------------------------------------------------*/ |
209 | static int meth_connect(lua_State *L) | 209 | static int meth_connect(lua_State *L) |
210 | { | 210 | { |
211 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); | 211 | p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); |
212 | const char *address = luaL_checkstring(L, 2); | 212 | const char *address = luaL_checkstring(L, 2); |
213 | unsigned short port = (unsigned short) luaL_checknumber(L, 3); | 213 | unsigned short port = (unsigned short) luaL_checknumber(L, 3); |
214 | p_tm tm = tm_markstart(&tcp->tm); | 214 | p_timeout tm = timeout_markstart(&tcp->tm); |
215 | const char *err = inet_tryconnect(&tcp->sock, address, port, tm); | 215 | const char *err = inet_tryconnect(&tcp->sock, address, port, tm); |
216 | /* have to set the class even if it failed due to non-blocking connects */ | 216 | /* have to set the class even if it failed due to non-blocking connects */ |
217 | aux_setclass(L, "tcp{client}", 1); | 217 | auxiliar_setclass(L, "tcp{client}", 1); |
218 | if (err) { | 218 | if (err) { |
219 | lua_pushnil(L); | 219 | lua_pushnil(L); |
220 | lua_pushstring(L, err); | 220 | lua_pushstring(L, err); |
@@ -230,8 +230,8 @@ static int meth_connect(lua_State *L) | |||
230 | \*-------------------------------------------------------------------------*/ | 230 | \*-------------------------------------------------------------------------*/ |
231 | static int meth_close(lua_State *L) | 231 | static int meth_close(lua_State *L) |
232 | { | 232 | { |
233 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); | 233 | p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); |
234 | sock_destroy(&tcp->sock); | 234 | socket_destroy(&tcp->sock); |
235 | lua_pushnumber(L, 1); | 235 | lua_pushnumber(L, 1); |
236 | return 1; | 236 | return 1; |
237 | } | 237 | } |
@@ -241,16 +241,16 @@ static int meth_close(lua_State *L) | |||
241 | \*-------------------------------------------------------------------------*/ | 241 | \*-------------------------------------------------------------------------*/ |
242 | static int meth_listen(lua_State *L) | 242 | static int meth_listen(lua_State *L) |
243 | { | 243 | { |
244 | p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{master}", 1); | 244 | p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{master}", 1); |
245 | int backlog = (int) luaL_optnumber(L, 2, 32); | 245 | int backlog = (int) luaL_optnumber(L, 2, 32); |
246 | int err = sock_listen(&tcp->sock, backlog); | 246 | int err = socket_listen(&tcp->sock, backlog); |
247 | if (err != IO_DONE) { | 247 | if (err != IO_DONE) { |
248 | lua_pushnil(L); | 248 | lua_pushnil(L); |
249 | lua_pushstring(L, sock_strerror(err)); | 249 | lua_pushstring(L, socket_strerror(err)); |
250 | return 2; | 250 | return 2; |
251 | } | 251 | } |
252 | /* turn master object into a server object */ | 252 | /* turn master object into a server object */ |
253 | aux_setclass(L, "tcp{server}", 1); | 253 | auxiliar_setclass(L, "tcp{server}", 1); |
254 | lua_pushnumber(L, 1); | 254 | lua_pushnumber(L, 1); |
255 | return 1; | 255 | return 1; |
256 | } | 256 | } |
@@ -260,20 +260,20 @@ static int meth_listen(lua_State *L) | |||
260 | \*-------------------------------------------------------------------------*/ | 260 | \*-------------------------------------------------------------------------*/ |
261 | static int meth_shutdown(lua_State *L) | 261 | static int meth_shutdown(lua_State *L) |
262 | { | 262 | { |
263 | p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1); | 263 | p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{client}", 1); |
264 | const char *how = luaL_optstring(L, 2, "both"); | 264 | const char *how = luaL_optstring(L, 2, "both"); |
265 | switch (how[0]) { | 265 | switch (how[0]) { |
266 | case 'b': | 266 | case 'b': |
267 | if (strcmp(how, "both")) goto error; | 267 | if (strcmp(how, "both")) goto error; |
268 | sock_shutdown(&tcp->sock, 2); | 268 | socket_shutdown(&tcp->sock, 2); |
269 | break; | 269 | break; |
270 | case 's': | 270 | case 's': |
271 | if (strcmp(how, "send")) goto error; | 271 | if (strcmp(how, "send")) goto error; |
272 | sock_shutdown(&tcp->sock, 1); | 272 | socket_shutdown(&tcp->sock, 1); |
273 | break; | 273 | break; |
274 | case 'r': | 274 | case 'r': |
275 | if (strcmp(how, "receive")) goto error; | 275 | if (strcmp(how, "receive")) goto error; |
276 | sock_shutdown(&tcp->sock, 0); | 276 | socket_shutdown(&tcp->sock, 0); |
277 | break; | 277 | break; |
278 | } | 278 | } |
279 | lua_pushnumber(L, 1); | 279 | lua_pushnumber(L, 1); |
@@ -288,13 +288,13 @@ error: | |||
288 | \*-------------------------------------------------------------------------*/ | 288 | \*-------------------------------------------------------------------------*/ |
289 | static int meth_getpeername(lua_State *L) | 289 | static int meth_getpeername(lua_State *L) |
290 | { | 290 | { |
291 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); | 291 | p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); |
292 | return inet_meth_getpeername(L, &tcp->sock); | 292 | return inet_meth_getpeername(L, &tcp->sock); |
293 | } | 293 | } |
294 | 294 | ||
295 | static int meth_getsockname(lua_State *L) | 295 | static int meth_getsockname(lua_State *L) |
296 | { | 296 | { |
297 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); | 297 | p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); |
298 | return inet_meth_getsockname(L, &tcp->sock); | 298 | return inet_meth_getsockname(L, &tcp->sock); |
299 | } | 299 | } |
300 | 300 | ||
@@ -303,8 +303,8 @@ static int meth_getsockname(lua_State *L) | |||
303 | \*-------------------------------------------------------------------------*/ | 303 | \*-------------------------------------------------------------------------*/ |
304 | static int meth_settimeout(lua_State *L) | 304 | static int meth_settimeout(lua_State *L) |
305 | { | 305 | { |
306 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); | 306 | p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); |
307 | return tm_meth_settimeout(L, &tcp->tm); | 307 | return timeout_meth_settimeout(L, &tcp->tm); |
308 | } | 308 | } |
309 | 309 | ||
310 | /*=========================================================================*\ | 310 | /*=========================================================================*\ |
@@ -315,21 +315,21 @@ static int meth_settimeout(lua_State *L) | |||
315 | \*-------------------------------------------------------------------------*/ | 315 | \*-------------------------------------------------------------------------*/ |
316 | static int global_create(lua_State *L) | 316 | static int global_create(lua_State *L) |
317 | { | 317 | { |
318 | t_sock sock; | 318 | t_socket sock; |
319 | const char *err = inet_trycreate(&sock, SOCK_STREAM); | 319 | const char *err = inet_trycreate(&sock, SOCK_STREAM); |
320 | /* try to allocate a system socket */ | 320 | /* try to allocate a system socket */ |
321 | if (!err) { | 321 | if (!err) { |
322 | /* allocate tcp object */ | 322 | /* allocate tcp object */ |
323 | p_tcp tcp = (p_tcp) lua_newuserdata(L, sizeof(t_tcp)); | 323 | p_tcp tcp = (p_tcp) lua_newuserdata(L, sizeof(t_tcp)); |
324 | /* set its type as master object */ | 324 | /* set its type as master object */ |
325 | aux_setclass(L, "tcp{master}", -1); | 325 | auxiliar_setclass(L, "tcp{master}", -1); |
326 | /* initialize remaining structure fields */ | 326 | /* initialize remaining structure fields */ |
327 | sock_setnonblocking(&sock); | 327 | socket_setnonblocking(&sock); |
328 | tcp->sock = sock; | 328 | tcp->sock = sock; |
329 | io_init(&tcp->io, (p_send) sock_send, (p_recv) sock_recv, | 329 | io_init(&tcp->io, (p_send) socket_send, (p_recv) socket_recv, |
330 | (p_error) sock_ioerror, &tcp->sock); | 330 | (p_error) socket_ioerror, &tcp->sock); |
331 | tm_init(&tcp->tm, -1, -1); | 331 | timeout_init(&tcp->tm, -1, -1); |
332 | buf_init(&tcp->buf, &tcp->io, &tcp->tm); | 332 | buffer_init(&tcp->buf, &tcp->io, &tcp->tm); |
333 | return 1; | 333 | return 1; |
334 | } else { | 334 | } else { |
335 | lua_pushnil(L); | 335 | lua_pushnil(L); |