moonbridge

view moonbridge_io.c @ 140:9ca22af4d4b1

Added methods "read_call" and "read_yield"
author jbe
date Fri May 01 01:46:07 2015 +0200 (2015-05-01)
parents 2097b1835d96
children 845e3dce3774
line source
2 #if defined(__linux__)
3 #define _GNU_SOURCE
4 #endif
5 #include <stdlib.h>
6 #include <stdint.h>
7 #include <string.h>
8 #include <errno.h>
9 #include <unistd.h>
10 #include <signal.h>
11 #include <fcntl.h>
12 #include <sys/stat.h>
13 #include <sys/socket.h>
14 #include <sys/un.h>
15 #include <netinet/in.h>
16 #include <netinet/tcp.h>
17 #include <sys/select.h>
18 #include <time.h>
19 #include <netdb.h>
20 #include <arpa/inet.h>
22 #include <lua.h>
23 #include <lauxlib.h>
24 #include <lualib.h>
26 #define MOONBR_IO_MAXSTRERRORLEN 80
27 #define MOONBR_IO_READBUFLEN 4096
28 #define MOONBR_IO_WRITEBUFLEN 4096
30 #define MOONBR_IO_LISTEN_BACKLOG 1024
32 #define moonbr_io_errmsg() \
33 char errmsg[MOONBR_IO_MAXSTRERRORLEN]; \
34 strerror_r(errno, errmsg, MOONBR_IO_MAXSTRERRORLEN)
36 #define MOONBR_IO_HANDLE_MT_REGKEY "moonbridge_io_handle"
37 #define MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY "moonbridge_io_handle_public"
38 #define MOONBR_IO_LISTENER_MT_REGKEY "moonbridge_io_listener"
40 typedef struct {
41 int fd;
42 int issock;
43 sa_family_t addrfam;
44 int finished;
45 int closed;
46 int nonblocking;
47 int nopush;
48 int readerr;
49 int readbufin;
50 int readbufout;
51 int writeerr;
52 size_t writeleft;
53 size_t flushedleft;
54 #if LUA_VERSION_NUM >= 503
55 lua_Integer writeqin;
56 lua_Integer writeqout;
57 #else
58 int writeqin;
59 int writeqout;
60 #endif
61 size_t writeqoff;
62 int writebufin;
63 int writebufout;
64 char readbuf[MOONBR_IO_READBUFLEN];
65 char writebuf[MOONBR_IO_WRITEBUFLEN];
66 } moonbr_io_handle_t;
68 typedef struct {
69 int fd;
70 sa_family_t addrfam;
71 int nonblocking;
72 } moonbr_io_listener_t;
74 static int moonbr_io_yield(lua_State *L) {
75 return lua_yield(L, 0);
76 }
78 #if LUA_VERSION_NUM >= 503
79 static int moonbr_io_cont_returnall(lua_State *L, int status, lua_KContext ctx) {
80 #else
81 static int moonbr_io_cont_returnall(lua_State *L) {
82 #endif
83 return lua_gettop(L);
84 }
86 static void moonbr_io_handle_set_nonblocking(lua_State *L, moonbr_io_handle_t *handle, int nonblocking) {
87 int flags;
88 if (handle->nonblocking == nonblocking) return;
89 flags = fcntl(handle->fd, F_GETFL, 0);
90 if (flags == -1) {
91 moonbr_io_errmsg();
92 close(handle->fd);
93 handle->fd = -1;
94 handle->closed = 1;
95 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
96 }
97 if (nonblocking) flags |= O_NONBLOCK;
98 else flags &= ~O_NONBLOCK;
99 if (fcntl(handle->fd, F_SETFL, flags) == -1) {
100 moonbr_io_errmsg();
101 close(handle->fd);
102 handle->fd = -1;
103 handle->closed = 1;
104 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
105 }
106 handle->nonblocking = nonblocking;
107 }
109 static void moonbr_io_handle_set_linger(lua_State *L, moonbr_io_handle_t *handle, int timeout) {
110 struct linger lingerval = { 0, };
111 if (!handle->issock) return;
112 if (timeout >= 0) {
113 lingerval.l_onoff = 1;
114 lingerval.l_linger = timeout;
115 }
116 if (setsockopt(handle->fd, SOL_SOCKET, SO_LINGER, &lingerval, sizeof(lingerval))) {
117 moonbr_io_errmsg();
118 close(handle->fd);
119 handle->fd = -1;
120 handle->closed = 1;
121 luaL_error(L, "Unexpected error while setting SO_LINGER with setsockopt: %s", errmsg);
122 }
123 }
125 static inline void moonbr_io_handle_set_nopush(lua_State *L, moonbr_io_handle_t *handle, int nopush) {
126 #if defined(TCP_NOPUSH) || defined(TCP_CORK)
127 if (
128 !(handle->addrfam == AF_INET6 || handle->addrfam == AF_INET) ||
129 handle->nopush == nopush
130 ) return;
131 #if defined(TCP_NOPUSH)
132 if (setsockopt(handle->fd, IPPROTO_TCP, TCP_NOPUSH, &nopush, sizeof(nopush))) {
133 #elif defined(TCP_CORK)
134 if (setsockopt(handle->fd, IPPROTO_TCP, TCP_CORK, &nopush, sizeof(nopush))) {
135 #endif
136 moonbr_io_errmsg();
137 close(handle->fd);
138 handle->fd = -1;
139 handle->closed = 1;
140 #if defined(TCP_NOPUSH)
141 luaL_error(L, "Unexpected error while setting TCP_NOPUSH with setsockopt: %s", errmsg);
142 #elif defined(TCP_CORK)
143 luaL_error(L, "Unexpected error while setting TCP_CORK with setsockopt: %s", errmsg);
144 #endif
145 }
146 handle->nopush = nopush;
147 #else
148 #warning Neither TCP_NOPUSH nor TCP_CORK is available
149 #endif
150 }
152 static int moonbr_io_read_impl(lua_State *L, int nonblocking, int drain) {
153 moonbr_io_handle_t *handle;
154 lua_Integer maxread;
155 const char *terminatorstr;
156 size_t terminatorlen;
157 char terminator = 0; /* initialize to avoid compiler warning */
158 luaL_Buffer luabuf;
159 size_t luabufcnt = 0;
160 int remaining;
161 char *terminatorpos;
162 ssize_t bytesread;
163 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
164 maxread = luaL_optinteger(L, 2, 0);
165 terminatorstr = luaL_optlstring(L, 3, "", &terminatorlen);
166 if (terminatorlen) {
167 luaL_argcheck(L, terminatorlen == 1, 3, "single byte expected");
168 terminator = terminatorstr[0];
169 }
170 lua_settop(L, 1); /* return handle on drain, terminator string may be garbage collected */
171 if (handle->closed) luaL_error(L, "Attempt to read from a closed I/O handle");
172 if (handle->readerr) {
173 lua_pushnil(L);
174 lua_pushliteral(L, "Previous read error");
175 return 2;
176 }
177 handle->readerr = 1;
178 if (handle->fd < 0) goto moonbr_io_read_impl_eof; /* fake EOF to simulate shutdown */
179 moonbr_io_handle_set_nonblocking(L, handle, nonblocking);
180 if (!drain) luaL_buffinit(L, &luabuf);
181 while (1) {
182 remaining = -1;
183 if (
184 maxread > 0 &&
185 handle->readbufin - handle->readbufout >= (size_t)maxread - luabufcnt
186 ) {
187 remaining = maxread - luabufcnt;
188 } else if (terminatorlen) {
189 terminatorpos = memchr(
190 handle->readbuf + handle->readbufout,
191 terminator,
192 handle->readbufin - handle->readbufout
193 );
194 if (terminatorpos) remaining = 1 + (
195 terminatorpos - (handle->readbuf + handle->readbufout)
196 );
197 }
198 if (remaining >= 0) {
199 if (!drain) {
200 luaL_addlstring(
201 &luabuf,
202 handle->readbuf + handle->readbufout,
203 remaining
204 );
205 luaL_pushresult(&luabuf);
206 } else {
207 luaL_pushresult(&luabuf);
208 lua_pop(L, 1);
209 lua_pushinteger(L, luabufcnt + remaining);
210 }
211 handle->readbufout += remaining;
212 if (handle->readbufout == handle->readbufin) {
213 handle->readbufin = 0;
214 handle->readbufout =0;
215 }
216 handle->readerr = 0;
217 return 1;
218 }
219 if (!drain) luaL_addlstring(
220 &luabuf,
221 handle->readbuf + handle->readbufout,
222 handle->readbufin - handle->readbufout
223 );
224 luabufcnt += handle->readbufin - handle->readbufout;
225 do {
226 bytesread = read(handle->fd, handle->readbuf, MOONBR_IO_READBUFLEN);
227 } while (bytesread < 0 && (errno == EINTR));
228 if (
229 bytesread == 0 || (
230 nonblocking &&
231 bytesread < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)
232 )
233 ) {
234 handle->readbufin = 0;
235 handle->readbufout = 0;
236 if (!drain) {
237 luaL_pushresult(&luabuf);
238 if (!luabufcnt && bytesread == 0) {
239 lua_pop(L, 1);
240 moonbr_io_read_impl_eof:
241 lua_pushboolean(L, 0);
242 lua_pushliteral(L, "End of file");
243 handle->readerr = 0;
244 return 2;
245 }
246 } else {
247 if (!luabufcnt && bytesread == 0) lua_pushboolean(L, 1);
248 else lua_pushboolean(L, luabufcnt);
249 }
250 handle->readerr = 0;
251 return 1;
252 }
253 if (bytesread < 0) {
254 moonbr_io_errmsg();
255 lua_pushnil(L);
256 lua_pushstring(L, errmsg);
257 return 2;
258 }
259 handle->readbufin = bytesread;
260 handle->readbufout = 0;
261 }
262 }
264 static int moonbr_io_read(lua_State *L) {
265 return moonbr_io_read_impl(L, 0, 0);
266 }
268 static int moonbr_io_read_nb(lua_State *L) {
269 return moonbr_io_read_impl(L, 1, 0);
270 }
272 static int moonbr_io_drain(lua_State *L) {
273 return moonbr_io_read_impl(L, 0, 1);
274 }
276 static int moonbr_io_drain_nb(lua_State *L) {
277 return moonbr_io_read_impl(L, 1, 1);
278 }
280 #if LUA_VERSION_NUM >= 503
281 static int moonbr_io_read_cont(lua_State *L, int status, lua_KContext ctx) {
282 #else
283 static int moonbr_io_read_cont(lua_State *L) {
284 #endif
285 size_t len, remaining;
286 const char *terminatorstr = NULL;
287 size_t terminatorlen = 0;
288 const char *chunk;
289 size_t chunklen;
290 #if !(LUA_VERSION_NUM >= 503)
291 int ctx = 0;
292 lua_getctx(L, &ctx);
293 #endif
294 remaining = lua_tointeger(L, 3);
295 while (1) {
296 lua_pushcfunction(L, moonbr_io_read_nb);
297 lua_pushvalue(L, 1);
298 lua_pushvalue(L, 3);
299 lua_pushvalue(L, 4);
300 lua_call(L, 3, 2);
301 if (lua_isnil(L, -2)) {
302 return 2;
303 } else if (!lua_toboolean(L, -2)) {
304 if (ctx) {
305 lua_pushnil(L);
306 lua_pushliteral(L, "Unexpected EOF");
307 }
308 return 2;
309 }
310 len = lua_rawlen(L, -2);
311 if (!len) {
312 lua_pop(L, 2);
313 } else {
314 lua_pop(L, 1);
315 if (!terminatorstr) {
316 terminatorstr = lua_tolstring(L, 4, &terminatorlen);
317 if (!terminatorstr) terminatorstr = "";
318 }
319 if (terminatorlen) chunk = lua_tolstring(L, -1, &chunklen);
320 if (ctx == 0) {
321 lua_replace(L, 5);
322 ctx = 1;
323 } else if (ctx == 1) {
324 lua_pushvalue(L, 5);
325 lua_newtable(L);
326 lua_replace(L, 5);
327 lua_rawseti(L, 5, 2);
328 lua_rawseti(L, 5, 1);
329 ctx = 2;
330 } else {
331 lua_rawseti(L, 5, lua_rawlen(L, 5) + 1);
332 }
333 if (remaining) {
334 if (len >= remaining) break;
335 remaining -= len;
336 lua_pushinteger(L, remaining);
337 lua_replace(L, 3);
338 }
339 if (terminatorlen && chunk[chunklen-1] == terminatorstr[0]) break;
340 }
341 lua_pushvalue(L, 2);
342 lua_pushvalue(L, 1);
343 lua_pushliteral(L, "r");
344 lua_callk(L, 2, 0, ctx, moonbr_io_read_cont);
345 }
346 if (ctx == 1) {
347 lua_pushvalue(L, 5);
348 } else {
349 luaL_Buffer buf;
350 lua_Integer i, chunkcount;
351 chunkcount = lua_rawlen(L, 5);
352 luaL_buffinit(L, &buf);
353 for (i=1; i<=chunkcount && i>0; i++) {
354 lua_rawgeti(L, 5, i);
355 luaL_addvalue(&buf);
356 }
357 luaL_pushresult(&buf);
358 }
359 return 1;
360 }
362 static int moonbr_io_read_call(lua_State *L) {
363 lua_settop(L, 4);
364 lua_pushnil(L);
365 #if LUA_VERSION_NUM >= 503
366 return moonbr_io_read_cont(L, 0, 0);
367 #else
368 return moonbr_io_read_cont(L);
369 #endif
370 }
372 static int moonbr_io_read_yield(lua_State *L) {
373 int args;
374 lua_pushcfunction(L, moonbr_io_read_call);
375 lua_insert(L, 1);
376 args = lua_gettop(L);
377 lua_pushcfunction(L, moonbr_io_yield);
378 lua_insert(L, 3);
379 lua_callk(L, args, LUA_MULTRET, 0, moonbr_io_cont_returnall);
380 return lua_gettop(L);
381 }
383 static int moonbr_io_write_impl(lua_State *L, int nonblocking, int flush) {
384 moonbr_io_handle_t *handle;
385 int i, top;
386 const char *str;
387 size_t strlen;
388 ssize_t written;
389 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
390 if (handle->closed) luaL_error(L, "Attempt to write to a closed I/O handle");
391 if (handle->finished) luaL_error(L, "Attempt to write to a finished I/O handle");
392 if (handle->writeerr) {
393 lua_pushnil(L);
394 lua_pushliteral(L, "Previous write error");
395 return 2;
396 }
397 handle->writeerr = 1;
398 moonbr_io_handle_set_nonblocking(L, handle, nonblocking);
399 top = lua_gettop(L);
400 lua_getuservalue(L, 1);
401 lua_getfield(L, -1, "writequeue");
402 for (i=2; i<=top; i++) {
403 luaL_checklstring(L, i, &strlen);
404 lua_pushvalue(L, i);
405 lua_rawseti(L, -2, handle->writeqin++);
406 handle->writeleft += strlen;
407 }
408 if (flush) handle->flushedleft = handle->writeleft;
409 while (handle->writeqout != handle->writeqin) {
410 lua_rawgeti(L, -1, handle->writeqout);
411 str = lua_tolstring(L, -1, &strlen);
412 while (handle->writeqoff < strlen) {
413 if (
414 strlen - handle->writeqoff <
415 MOONBR_IO_WRITEBUFLEN - handle->writebufin
416 ) {
417 memcpy(
418 handle->writebuf + handle->writebufin,
419 str + handle->writeqoff,
420 strlen - handle->writeqoff
421 );
422 handle->writebufin += strlen - handle->writeqoff;
423 break;
424 } else {
425 memcpy(
426 handle->writebuf + handle->writebufin,
427 str + handle->writeqoff,
428 MOONBR_IO_WRITEBUFLEN - handle->writebufin
429 );
430 handle->writeqoff += MOONBR_IO_WRITEBUFLEN - handle->writebufin;
431 while (handle->writebufout < MOONBR_IO_WRITEBUFLEN) {
432 moonbr_io_handle_set_nopush(L, handle, 1);
433 written = write(
434 handle->fd,
435 handle->writebuf + handle->writebufout,
436 MOONBR_IO_WRITEBUFLEN - handle->writebufout
437 );
438 if (written < 0) {
439 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
440 goto moonbr_io_write_impl_block;
441 } else if (errno != EINTR) {
442 moonbr_io_errmsg();
443 lua_pushnil(L);
444 lua_pushstring(L, errmsg);
445 return 2;
446 }
447 } else {
448 handle->writebufout += written;
449 handle->writeleft -= written;
450 if (handle->flushedleft) {
451 if (written >= handle->flushedleft) {
452 handle->flushedleft = 0;
453 moonbr_io_handle_set_nopush(L, handle, 0);
454 } else {
455 handle->flushedleft -= written;
456 }
457 }
458 }
459 }
460 handle->writebufin = 0;
461 handle->writebufout = 0;
462 }
463 }
464 handle->writeqoff = 0;
465 lua_pop(L, 1);
466 lua_pushnil(L);
467 lua_rawseti(L, -2, handle->writeqout++);
468 }
469 while (handle->flushedleft) {
470 moonbr_io_handle_set_nopush(L, handle, 1);
471 written = write(
472 handle->fd,
473 handle->writebuf + handle->writebufout,
474 handle->writebufin - handle->writebufout
475 );
476 if (written < 0) {
477 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
478 goto moonbr_io_write_impl_block;
479 } else if (errno != EINTR) {
480 moonbr_io_errmsg();
481 lua_pushnil(L);
482 lua_pushstring(L, errmsg);
483 return 2;
484 }
485 } else {
486 handle->writebufout += written;
487 handle->writeleft -= written;
488 if (handle->flushedleft) {
489 if (written >= handle->flushedleft) {
490 handle->flushedleft = 0;
491 moonbr_io_handle_set_nopush(L, handle, 0);
492 } else {
493 handle->flushedleft -= written;
494 }
495 }
496 }
497 }
498 if (handle->writebufout == handle->writebufin) {
499 handle->writebufin = 0;
500 handle->writebufout = 0;
501 }
502 if (nonblocking) lua_pushinteger(L, 0);
503 else lua_pushvalue(L, 1);
504 handle->writeerr = 0;
505 return 1;
506 moonbr_io_write_impl_block:
507 lua_pushinteger(L, handle->writeleft);
508 handle->writeerr = 0;
509 return 1;
510 }
512 static int moonbr_io_write(lua_State *L) {
513 return moonbr_io_write_impl(L, 0, 0);
514 }
516 static int moonbr_io_write_nb(lua_State *L) {
517 return moonbr_io_write_impl(L, 1, 0);
518 }
520 static int moonbr_io_flush(lua_State *L) {
521 return moonbr_io_write_impl(L, 0, 1);
522 }
524 static int moonbr_io_flush_nb(lua_State *L) {
525 return moonbr_io_write_impl(L, 1, 1);
526 }
528 static int moonbr_io_finish(lua_State *L) {
529 moonbr_io_handle_t *handle;
530 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
531 if (handle->closed) luaL_error(L, "Attempt to finish a closed I/O handle");
532 if (handle->finished) luaL_error(L, "Attempt to finish a finished I/O handle");
533 if (handle->writeleft) {
534 lua_pushcfunction(L, moonbr_io_flush);
535 lua_pushvalue(L, 1);
536 if (lua_pcall(L, 1, 2, 0)) {
537 handle->finished = 1;
538 lua_error(L);
539 }
540 if (!lua_toboolean(L, -2)) {
541 handle->finished = 1;
542 return 2;
543 }
544 }
545 handle->finished = 1;
546 if (handle->addrfam == AF_INET6 || handle->addrfam == AF_INET) {
547 if (shutdown(handle->fd, SHUT_WR)) {
548 moonbr_io_errmsg();
549 lua_pushnil(L);
550 lua_pushstring(L, errmsg);
551 return 2;
552 }
553 } else {
554 if (close(handle->fd)) {
555 moonbr_io_errmsg();
556 handle->fd = -1;
557 lua_pushnil(L);
558 lua_pushstring(L, errmsg);
559 return 2;
560 }
561 handle->fd = -1; /* fake EOF on read */
562 }
563 lua_pushboolean(L, 1);
564 return 1;
565 }
567 static int moonbr_io_close_impl(lua_State *L, int reset) {
568 moonbr_io_handle_t *handle;
569 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
570 if (handle->closed) luaL_error(L, "Attempt to close a closed I/O handle");
571 if (!reset) {
572 if (handle->writeleft) {
573 lua_pushcfunction(L, moonbr_io_flush);
574 lua_pushvalue(L, 1);
575 if (lua_pcall(L, 1, 2, 0)) {
576 handle->closed = 1;
577 close(handle->fd);
578 handle->fd = -1;
579 lua_error(L);
580 }
581 handle->closed = 1;
582 if (!lua_toboolean(L, -2)) {
583 close(handle->fd);
584 handle->fd = -1;
585 return 2;
586 }
587 } else {
588 handle->closed = 1;
589 moonbr_io_handle_set_linger(L, handle, -1);
590 }
591 } else {
592 handle->closed = 1;
593 }
594 if (handle->fd >= 0) {
595 if (close(handle->fd)) {
596 moonbr_io_errmsg();
597 handle->fd = -1;
598 lua_pushnil(L);
599 lua_pushstring(L, errmsg);
600 return 2;
601 }
602 handle->fd = -1;
603 }
604 lua_pushboolean(L, 1);
605 return 1;
607 }
609 static int moonbr_io_close(lua_State *L) {
610 return moonbr_io_close_impl(L, 0);
611 }
613 static int moonbr_io_reset(lua_State *L) {
614 return moonbr_io_close_impl(L, 1);
615 }
617 static int moonbr_io_handlegc(lua_State *L) {
618 moonbr_io_handle_t *handle;
619 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
620 if (handle->fd >= 0) {
621 lua_pushcfunction(L, moonbr_io_close);
622 lua_pushvalue(L, 1);
623 lua_pushinteger(L, 0);
624 lua_call(L, 2, 0);
625 }
626 return 0;
627 }
629 void moonbr_io_closehandle(lua_State *L, int idx, int reset) {
630 moonbr_io_handle_t *handle;
631 handle = luaL_checkudata(L, idx, MOONBR_IO_HANDLE_MT_REGKEY);
632 if (!handle->closed) {
633 lua_pushcfunction(L, reset ? moonbr_io_reset : moonbr_io_close);
634 lua_pushvalue(L, idx < 0 ? idx-1 : idx);
635 lua_call(L, 1, 0);
636 }
637 }
639 void moonbr_io_pushhandle(lua_State *L, int fd) {
640 moonbr_io_handle_t *handle;
641 struct sockaddr addr;
642 socklen_t addrlen;
643 handle = lua_newuserdata(L, sizeof(moonbr_io_handle_t));
644 handle->fd = fd;
645 addrlen = sizeof(addr);
646 if (getsockname(fd, &addr, &addrlen)) {
647 if (errno != ENOTSOCK) {
648 moonbr_io_errmsg();
649 luaL_error(L, "Unexpected error when examining socket: %s", errmsg);
650 }
651 handle->issock = 0;
652 } else {
653 handle->issock = 1;
654 handle->addrfam = addr.sa_family;
655 }
656 handle->finished = 0;
657 handle->closed = 0;
658 handle->nonblocking = -1;
659 handle->nopush = -1;
660 handle->readerr = 0;
661 handle->readbufin = 0;
662 handle->readbufout = 0;
663 handle->writeerr = 0;
664 handle->writeleft = 0;
665 handle->flushedleft = 0;
666 handle->writeqin = 0;
667 handle->writeqout = 0;
668 handle->writeqoff = 0;
669 handle->writebufin = 0;
670 handle->writebufout = 0;
671 moonbr_io_handle_set_linger(L, handle, 0);
672 luaL_getmetatable(L, MOONBR_IO_HANDLE_MT_REGKEY);
673 lua_setmetatable(L, -2);
674 lua_newtable(L); // uservalue
675 lua_newtable(L);
676 lua_setfield(L, -2, "writequeue");
677 lua_newtable(L); // public
678 if (handle->addrfam == AF_INET6) {
679 struct sockaddr_in6 addr_in6;
680 char addrstrbuf[INET6_ADDRSTRLEN];
681 const char *addrstr;
682 addrlen = sizeof(addr_in6);
683 if (getsockname(fd, (struct sockaddr *)&addr_in6, &addrlen)) {
684 moonbr_io_errmsg();
685 luaL_error(L, "Could not determine local IP address/port: %s", errmsg);
686 }
687 if (addrlen > sizeof(addr_in6)) {
688 luaL_error(L, "Could not determine local IP address/port: buffer size exceeded");
689 }
690 addrstr = inet_ntop(AF_INET6, addr_in6.sin6_addr.s6_addr, addrstrbuf, sizeof(addrstrbuf));
691 if (!addrstr) {
692 moonbr_io_errmsg();
693 luaL_error(L, "Could not format local IP address: %s", errmsg);
694 } else {
695 lua_pushstring(L, addrstr);
696 lua_setfield(L, -2, "local_ip6");
697 }
698 lua_pushinteger(L, ntohs(addr_in6.sin6_port));
699 lua_setfield(L, -2, "local_tcpport");
700 if (getpeername(fd, (struct sockaddr *)&addr_in6, &addrlen)) {
701 moonbr_io_errmsg();
702 luaL_error(L, "Could not determine remote IP address/port: %s", errmsg);
703 }
704 if (addrlen > sizeof(addr_in6)) {
705 luaL_error(L, "Could not determine remote IP address/port: buffer size exceeded");
706 }
707 addrstr = inet_ntop(AF_INET6, addr_in6.sin6_addr.s6_addr, addrstrbuf, sizeof(addrstrbuf));
708 if (!addrstr) {
709 moonbr_io_errmsg();
710 luaL_error(L, "Could not format remote IP address: %s", errmsg);
711 } else {
712 lua_pushstring(L, addrstr);
713 lua_setfield(L, -2, "remote_ip6");
714 }
715 lua_pushinteger(L, ntohs(addr_in6.sin6_port));
716 lua_setfield(L, -2, "remote_tcpport");
717 } else if (handle->addrfam == AF_INET) {
718 struct sockaddr_in addr_in;
719 char addrstrbuf[INET_ADDRSTRLEN];
720 const char *addrstr;
721 addrlen = sizeof(addr_in);
722 if (getsockname(fd, (struct sockaddr *)&addr_in, &addrlen)) {
723 moonbr_io_errmsg();
724 luaL_error(L, "Could not determine local IP address/port: %s", errmsg);
725 }
726 if (addrlen > sizeof(addr_in)) {
727 luaL_error(L, "Could not determine local IP address/port: buffer size exceeded");
728 }
729 addrstr = inet_ntop(AF_INET, &addr_in.sin_addr.s_addr, addrstrbuf, sizeof(addrstrbuf));
730 if (!addrstr) {
731 moonbr_io_errmsg();
732 luaL_error(L, "Could not format local IP address: %s", errmsg);
733 } else {
734 lua_pushstring(L, addrstr);
735 lua_setfield(L, -2, "local_ip4");
736 }
737 lua_pushinteger(L, ntohs(addr_in.sin_port));
738 lua_setfield(L, -2, "local_tcpport");
739 if (getpeername(fd, (struct sockaddr *)&addr_in, &addrlen)) {
740 moonbr_io_errmsg();
741 luaL_error(L, "Could not determine remote IP address/port: %s", errmsg);
742 }
743 if (addrlen > sizeof(addr_in)) {
744 luaL_error(L, "Could not determine remote IP address/port: buffer size exceeded");
745 }
746 addrstr = inet_ntop(AF_INET, &addr_in.sin_addr.s_addr, addrstrbuf, sizeof(addrstrbuf));
747 if (!addrstr) {
748 moonbr_io_errmsg();
749 luaL_error(L, "Could not format remote IP address: %s", errmsg);
750 } else {
751 lua_pushstring(L, addrstr);
752 lua_setfield(L, -2, "remote_ip4");
753 }
754 lua_pushinteger(L, ntohs(addr_in.sin_port));
755 lua_setfield(L, -2, "remote_tcpport");
756 }
757 luaL_getmetatable(L, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
758 lua_setmetatable(L, -2);
759 lua_setfield(L, -2, "public");
760 lua_setuservalue(L, -2);
761 }
763 static int moonbr_io_handleindex(lua_State *L) {
764 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
765 lua_getuservalue(L, 1);
766 lua_getfield(L, -1, "public");
767 lua_pushvalue(L, 2);
768 lua_gettable(L, -2);
769 return 1;
770 }
772 static int moonbr_io_handlenewindex(lua_State *L) {
773 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
774 lua_getuservalue(L, 1);
775 lua_getfield(L, -1, "public");
776 lua_pushvalue(L, 2);
777 lua_pushvalue(L, 3);
778 lua_settable(L, -3);
779 return 0;
780 }
782 static int moonbr_io_localconnect_impl(lua_State *L, int nonblocking) {
783 const char *path;
784 struct sockaddr_un sockaddr = { .sun_family = AF_LOCAL };
785 const int path_maxlen = sizeof(struct sockaddr_un) - (
786 (void *)sockaddr.sun_path - (void *)&sockaddr
787 ) - 1; /* one byte for termination */
788 int sock;
789 path = luaL_checkstring(L, 1);
790 if (strlen(path) > path_maxlen) luaL_error(L, "Path too long; only %i characters allowed", path_maxlen);
791 strcpy(sockaddr.sun_path, path);
792 sock = socket(
793 PF_LOCAL,
794 SOCK_STREAM | SOCK_CLOEXEC | (nonblocking ? SOCK_NONBLOCK : 0),
795 0
796 );
797 if (sock < 0) {
798 moonbr_io_errmsg();
799 lua_pushnil(L);
800 lua_pushstring(L, errmsg);
801 return 2;
802 }
803 if (connect(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) {
804 if (!nonblocking && errno == EINTR) {
805 moonbr_io_errmsg();
806 close(sock);
807 lua_pushnil(L);
808 lua_pushstring(L, errmsg);
809 return 2;
810 } else if (!(nonblocking && (errno == EINPROGRESS || errno == EINTR))) {
811 moonbr_io_errmsg();
812 lua_pushnil(L);
813 lua_pushstring(L, errmsg);
814 return 2;
815 }
816 }
817 moonbr_io_pushhandle(L, sock);
818 return 1;
819 }
821 static int moonbr_io_localconnect(lua_State *L) {
822 return moonbr_io_localconnect_impl(L, 0);
823 }
825 static int moonbr_io_localconnect_nb(lua_State *L) {
826 return moonbr_io_localconnect_impl(L, 1);
827 }
829 static int moonbr_io_tcpconnect_impl(lua_State *L, int nonblocking) {
830 const char *host, *port;
831 struct addrinfo hints = { 0, };
832 struct addrinfo *res, *addrinfo;
833 int errcode;
834 int sock;
835 host = luaL_checkstring(L, 1);
836 port = luaL_checkstring(L, 2);
837 hints.ai_family = AF_UNSPEC;
838 hints.ai_socktype = SOCK_STREAM;
839 hints.ai_protocol = IPPROTO_TCP;
840 hints.ai_flags = AI_ADDRCONFIG;
841 errcode = getaddrinfo(host, port, &hints, &res);
842 if (errcode) {
843 freeaddrinfo(res);
844 if (errcode == EAI_SYSTEM) {
845 moonbr_io_errmsg();
846 lua_pushnil(L);
847 lua_pushfstring(L, "%s: %s", gai_strerror(errcode), errmsg);
848 } else {
849 lua_pushnil(L);
850 lua_pushstring(L, gai_strerror(errcode));
851 }
852 return 2;
853 }
854 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
855 if (addrinfo->ai_family == AF_INET6) goto moonbr_io_tcpconnect_found;
856 }
857 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
858 if (addrinfo->ai_family == AF_INET) goto moonbr_io_tcpconnect_found;
859 }
860 addrinfo = res;
861 moonbr_io_tcpconnect_found:
862 sock = socket(
863 addrinfo->ai_family, /* NOTE: not correctly using PF_* but AF_* constants here */
864 addrinfo->ai_socktype | SOCK_CLOEXEC | (nonblocking ? SOCK_NONBLOCK : 0),
865 addrinfo->ai_protocol
866 );
867 if (sock < 0) {
868 moonbr_io_errmsg();
869 freeaddrinfo(res);
870 lua_pushnil(L);
871 lua_pushstring(L, errmsg);
872 return 2;
873 }
874 if (connect(sock, addrinfo->ai_addr, addrinfo->ai_addrlen)) {
875 freeaddrinfo(res);
876 if (!nonblocking && errno == EINTR) {
877 moonbr_io_errmsg();
878 close(sock);
879 lua_pushnil(L);
880 lua_pushstring(L, errmsg);
881 return 2;
882 } else if (!(nonblocking && (errno == EINPROGRESS || errno == EINTR))) {
883 moonbr_io_errmsg();
884 lua_pushnil(L);
885 lua_pushstring(L, errmsg);
886 return 2;
887 }
888 } else {
889 freeaddrinfo(res);
890 }
891 moonbr_io_pushhandle(L, sock);
892 return 1;
893 }
895 static int moonbr_io_tcpconnect(lua_State *L) {
896 return moonbr_io_tcpconnect_impl(L, 0);
897 }
899 static int moonbr_io_tcpconnect_nb(lua_State *L) {
900 return moonbr_io_tcpconnect_impl(L, 1);
901 }
903 static int moonbr_io_locallisten(lua_State *L) {
904 moonbr_io_listener_t *listener;
905 const char *path;
906 struct stat sb;
907 struct sockaddr_un sockaddr = { .sun_family = AF_LOCAL };
908 const int path_maxlen = sizeof(struct sockaddr_un) - (
909 (void *)sockaddr.sun_path - (void *)&sockaddr
910 ) - 1; /* one byte for termination */
911 int sock;
912 path = luaL_checkstring(L, 1);
913 if (strlen(path) > path_maxlen) luaL_error(L, "Path too long; only %i characters allowed", path_maxlen);
914 strcpy(sockaddr.sun_path, path);
915 if (stat(path, &sb) == 0) {
916 if (S_ISSOCK(sb.st_mode)) unlink(path);
917 }
918 listener = lua_newuserdata(L, sizeof(moonbr_io_listener_t));
919 listener->fd = -1;
920 luaL_setmetatable(L, MOONBR_IO_LISTENER_MT_REGKEY);
921 sock = socket(
922 PF_LOCAL,
923 SOCK_STREAM | SOCK_CLOEXEC,
924 0
925 );
926 if (sock < 0) {
927 moonbr_io_errmsg();
928 lua_pushnil(L);
929 lua_pushstring(L, errmsg);
930 return 2;
931 }
932 if (bind(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) {
933 moonbr_io_errmsg();
934 close(sock);
935 lua_pushnil(L);
936 lua_pushstring(L, errmsg);
937 return 2;
938 }
939 if (listen(sock, MOONBR_IO_LISTEN_BACKLOG)) {
940 moonbr_io_errmsg();
941 close(sock);
942 lua_pushnil(L);
943 lua_pushstring(L, errmsg);
944 return 2;
945 }
946 listener->fd = sock;
947 listener->addrfam = AF_LOCAL;
948 listener->nonblocking = -1;
949 return 1;
950 }
952 static int moonbr_io_tcplisten(lua_State *L) {
953 moonbr_io_listener_t *listener;
954 const char *host, *port;
955 struct addrinfo hints = { 0, };
956 struct addrinfo *res, *addrinfo;
957 int errcode;
958 int sock;
959 host = luaL_optstring(L, 1, NULL);
960 port = luaL_checkstring(L, 2);
961 listener = lua_newuserdata(L, sizeof(moonbr_io_listener_t));
962 listener->fd = -1;
963 luaL_setmetatable(L, MOONBR_IO_LISTENER_MT_REGKEY);
964 hints.ai_family = AF_UNSPEC;
965 hints.ai_socktype = SOCK_STREAM;
966 hints.ai_protocol = IPPROTO_TCP;
967 hints.ai_flags = AI_ADDRCONFIG | AI_PASSIVE;
968 errcode = getaddrinfo(host, port, &hints, &res);
969 if (errcode) {
970 freeaddrinfo(res);
971 if (errcode == EAI_SYSTEM) {
972 moonbr_io_errmsg();
973 lua_pushnil(L);
974 lua_pushfstring(L, "%s: %s", gai_strerror(errcode), errmsg);
975 } else {
976 lua_pushnil(L);
977 lua_pushstring(L, gai_strerror(errcode));
978 }
979 return 2;
980 }
981 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
982 if (addrinfo->ai_family == AF_INET6) goto moonbr_io_tcpconnect_found;
983 }
984 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
985 if (addrinfo->ai_family == AF_INET) goto moonbr_io_tcpconnect_found;
986 }
987 addrinfo = res;
988 moonbr_io_tcpconnect_found:
989 listener->addrfam = addrinfo->ai_family;
990 sock = socket(
991 addrinfo->ai_family, /* NOTE: not correctly using PF_* but AF_* constants here */
992 addrinfo->ai_socktype | SOCK_CLOEXEC,
993 addrinfo->ai_protocol
994 );
995 if (sock < 0) {
996 moonbr_io_errmsg();
997 freeaddrinfo(res);
998 lua_pushnil(L);
999 lua_pushstring(L, errmsg);
1000 return 2;
1003 static const int reuseval = 1;
1004 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseval, sizeof(reuseval))) {
1005 moonbr_io_errmsg();
1006 freeaddrinfo(res);
1007 close(sock);
1008 lua_pushnil(L);
1009 lua_pushfstring(L, "Error while setting SO_REUSEADDR with setsockopt: %s", errmsg);
1010 return 2;
1013 if (bind(sock, addrinfo->ai_addr, addrinfo->ai_addrlen)) {
1014 moonbr_io_errmsg();
1015 freeaddrinfo(res);
1016 close(sock);
1017 lua_pushnil(L);
1018 lua_pushstring(L, errmsg);
1019 return 2;
1021 freeaddrinfo(res);
1022 if (listen(sock, MOONBR_IO_LISTEN_BACKLOG)) {
1023 moonbr_io_errmsg();
1024 close(sock);
1025 lua_pushnil(L);
1026 lua_pushstring(L, errmsg);
1027 return 2;
1029 listener->fd = sock;
1030 listener->nonblocking = -1;
1031 return 1;
1034 static int moonbr_io_accept_impl(lua_State *L, int nonblocking) {
1035 moonbr_io_listener_t *listener;
1036 int fd;
1037 listener = luaL_checkudata(L, 1, MOONBR_IO_LISTENER_MT_REGKEY);
1038 if (listener->fd < 0) luaL_error(L, "Attempt to use a closed listener");
1039 if (listener->nonblocking != nonblocking) {
1040 int flags;
1041 flags = fcntl(listener->fd, F_GETFL, 0);
1042 if (flags == -1) {
1043 moonbr_io_errmsg();
1044 close(listener->fd);
1045 listener->fd = -1;
1046 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
1048 if (nonblocking) flags |= O_NONBLOCK;
1049 else flags &= ~O_NONBLOCK;
1050 if (fcntl(listener->fd, F_SETFL, flags) == -1) {
1051 moonbr_io_errmsg();
1052 close(listener->fd);
1053 listener->fd = -1;
1054 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
1056 listener->nonblocking = nonblocking;
1058 while (1) {
1059 fd = accept4(listener->fd, NULL, NULL, SOCK_CLOEXEC);
1060 if (fd < 0) {
1061 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
1062 lua_pushboolean(L, 0);
1063 lua_pushliteral(L, "No incoming connection pending");
1064 return 2;
1065 } else if (errno != EINTR) {
1066 moonbr_io_errmsg();
1067 lua_pushnil(L);
1068 lua_pushstring(L, errmsg);
1069 return 2;
1071 } else {
1072 moonbr_io_pushhandle(L, fd);
1073 return 1;
1078 static int moonbr_io_accept(lua_State *L) {
1079 return moonbr_io_accept_impl(L, 0);
1082 static int moonbr_io_accept_nb(lua_State *L) {
1083 return moonbr_io_accept_impl(L, 1);
1086 static int moonbr_io_unlisten(lua_State *L) {
1087 moonbr_io_listener_t *listener;
1088 struct sockaddr_un addr;
1089 socklen_t addrlen;
1090 struct stat sb;
1091 listener = luaL_checkudata(L, 1, MOONBR_IO_LISTENER_MT_REGKEY);
1092 if (listener->fd < 0) luaL_error(L, "Attempt to close a closed listener");
1093 addrlen = sizeof(addr);
1094 if (getsockname(listener->fd, (struct sockaddr *)&addr, &addrlen)) addrlen = 0;
1095 if (close(listener->fd)) {
1096 moonbr_io_errmsg();
1097 listener->fd = -1;
1098 if (addrlen && addrlen <= sizeof(addr)) {
1099 if (stat(addr.sun_path, &sb) == 0) {
1100 if (S_ISSOCK(sb.st_mode)) unlink(addr.sun_path);
1103 lua_pushnil(L);
1104 lua_pushstring(L, errmsg);
1105 return 2;
1107 listener->fd = -1;
1108 if (addrlen && addrlen <= sizeof(addr)) {
1109 if (stat(addr.sun_path, &sb) == 0) {
1110 if (S_ISSOCK(sb.st_mode)) unlink(addr.sun_path);
1113 lua_pushboolean(L, 1);
1114 return 1;
1117 static int moonbr_io_listenergc(lua_State *L) {
1118 moonbr_io_listener_t *listener;
1119 listener = luaL_checkudata(L, 1, MOONBR_IO_LISTENER_MT_REGKEY);
1120 if (listener->fd >= 0) close(listener->fd);
1121 listener->fd = -1;
1122 return 0;
1125 static int moonbr_io_poll(lua_State *L) {
1126 moonbr_io_handle_t *handle;
1127 moonbr_io_listener_t *listener;
1128 int fd, isnum;
1129 int nfds = 0;
1130 fd_set readfds, writefds, exceptfds;
1131 struct timeval timeout = {0, };
1132 int status;
1133 FD_ZERO(&readfds);
1134 FD_ZERO(&writefds);
1135 FD_ZERO(&exceptfds);
1136 if (!lua_isnoneornil(L, 1)) {
1137 luaL_checktype(L, 1, LUA_TTABLE);
1138 for (lua_pushnil(L); lua_next(L, 1); lua_pop(L, 1)) {
1139 if (lua_toboolean(L, -1)) {
1140 handle = luaL_testudata(L, -2, MOONBR_IO_HANDLE_MT_REGKEY);
1141 if (handle) {
1142 if (handle->closed) luaL_error(L, "Attempt to poll a closed connection");
1143 fd = handle->fd;
1144 if (
1145 fd < 0 || /* fake EOF to simulate shutdown if fd < 0 */
1146 handle->readbufin != handle->readbufout /* data pending in buffer */
1147 ) {
1148 lua_pushboolean(L, 1);
1149 return 1;
1151 } else {
1152 listener = luaL_testudata(L, -2, MOONBR_IO_LISTENER_MT_REGKEY);
1153 if (listener) {
1154 fd = listener->fd;
1155 if (fd < 0) luaL_error(L, "Attempt to poll a closed listener");
1156 } else {
1157 fd = lua_tointegerx(L, -2, &isnum);
1158 if (!isnum) luaL_error(L, "Expected integer (file descriptor), I/O handle, or listener in table key");
1161 if (fd < 0 || fd >= FD_SETSIZE) luaL_error(L, "File descriptor out of valid range");
1162 FD_SET(fd, &readfds);
1163 if (fd+1 > nfds) nfds = fd+1;
1167 if (!lua_isnoneornil(L, 2)) {
1168 luaL_checktype(L, 2, LUA_TTABLE);
1169 for (lua_pushnil(L); lua_next(L, 2); lua_pop(L, 1)) {
1170 if (lua_toboolean(L, -1)) {
1171 handle = luaL_testudata(L, -2, MOONBR_IO_HANDLE_MT_REGKEY);
1172 if (handle) {
1173 if (handle->closed) luaL_error(L, "Attempt to poll a closed connection");
1174 if (handle->finished) luaL_error(L, "Attempt to write-poll a finished connection");
1175 fd = handle->fd;
1176 } else {
1177 listener = luaL_testudata(L, -2, MOONBR_IO_LISTENER_MT_REGKEY);
1178 if (listener) luaL_error(L, "Attempt to write-poll a listener");
1179 fd = lua_tointegerx(L, -2, &isnum);
1180 if (!isnum) luaL_error(L, "Expected integer (file descriptor) or I/O handle in table key");
1182 if (fd < 0 || fd >= FD_SETSIZE) luaL_error(L, "File descriptor out of valid range");
1183 FD_SET(fd, &writefds);
1184 if (fd+1 > nfds) nfds = fd+1;
1188 if (!lua_isnoneornil(L, 3)) {
1189 lua_Number n;
1190 n = lua_tonumberx(L, 3, &isnum);
1191 if (isnum && n>=0 && n<100000000) {
1192 timeout.tv_sec = n;
1193 timeout.tv_usec = 1e6 * (n - timeout.tv_sec);
1194 } else {
1195 luaL_argcheck(L, 0, 3, "not a valid timeout");
1197 status = select(nfds, &readfds, &writefds, &exceptfds, &timeout);
1198 } else {
1199 status = select(nfds, &readfds, &writefds, &exceptfds, NULL);
1201 if (status == -1) {
1202 if (errno == EINTR) {
1203 lua_pushboolean(L, 0);
1204 lua_pushliteral(L, "Signal received while polling file descriptors");
1205 return 2;
1206 } else {
1207 moonbr_io_errmsg();
1208 return luaL_error(L, "Unexpected error during \"select\" system call: %s", errmsg);
1210 } else if (status == 0) {
1211 lua_pushboolean(L, 0);
1212 lua_pushliteral(L, "Timeout while polling file descriptors");
1213 return 2;
1214 } else {
1215 lua_pushboolean(L, 1);
1216 return 1;
1220 static int moonbr_io_timeref(lua_State *L) {
1221 lua_Number sub;
1222 struct timespec tp;
1223 sub = luaL_optnumber(L, 1, 0);
1224 if (clock_gettime(CLOCK_MONOTONIC, &tp)) {
1225 return luaL_error(L, "Could not access CLOCK_MONOTONIC");
1227 lua_pushnumber(L, tp.tv_sec + tp.tv_nsec / 1.0e9 - sub);
1228 return 1;
1231 static const struct luaL_Reg moonbr_io_handle_methods[] = {
1232 {"read", moonbr_io_read},
1233 {"read_nb", moonbr_io_read_nb},
1234 {"read_call", moonbr_io_read_call},
1235 {"read_yield", moonbr_io_read_yield},
1236 {"drain", moonbr_io_drain},
1237 {"drain_nb", moonbr_io_drain_nb},
1238 {"write", moonbr_io_write},
1239 {"write_nb", moonbr_io_write_nb},
1240 {"flush", moonbr_io_flush},
1241 {"flush_nb", moonbr_io_flush_nb},
1242 {"finish", moonbr_io_finish},
1243 {"close", moonbr_io_close},
1244 {"reset", moonbr_io_reset},
1245 {NULL, NULL}
1246 };
1248 static const struct luaL_Reg moonbr_io_handle_metamethods[] = {
1249 {"__index", moonbr_io_handleindex},
1250 {"__newindex", moonbr_io_handlenewindex},
1251 {"__gc", moonbr_io_handlegc},
1252 {NULL, NULL}
1253 };
1255 static const struct luaL_Reg moonbr_io_listener_methods[] = {
1256 {"accept", moonbr_io_accept},
1257 {"accept_nb", moonbr_io_accept_nb},
1258 {"close", moonbr_io_unlisten},
1259 {NULL, NULL}
1260 };
1262 static const struct luaL_Reg moonbr_io_listener_metamethods[] = {
1263 {"__gc", moonbr_io_listenergc},
1264 {NULL, NULL}
1265 };
1267 static const struct luaL_Reg moonbr_io_module_funcs[] = {
1268 {"localconnect", moonbr_io_localconnect},
1269 {"localconnect_nb", moonbr_io_localconnect_nb},
1270 {"tcpconnect", moonbr_io_tcpconnect},
1271 {"tcpconnect_nb", moonbr_io_tcpconnect_nb},
1272 {"locallisten", moonbr_io_locallisten},
1273 {"tcplisten", moonbr_io_tcplisten},
1274 {"poll", moonbr_io_poll},
1275 {"timeref", moonbr_io_timeref},
1276 {NULL, NULL}
1277 };
1279 int luaopen_moonbridge_io(lua_State *L) {
1281 signal(SIGPIPE, SIG_IGN); /* generate I/O errors instead of signal 13 */
1283 lua_newtable(L); // module
1285 lua_newtable(L); // public metatable
1286 lua_newtable(L); // handle methods
1287 luaL_setfuncs(L, moonbr_io_handle_methods, 0);
1288 lua_pushvalue(L, -1);
1289 lua_setfield(L, -4, "prototype_handle");
1290 lua_setfield(L, -2, "__index");
1291 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
1293 lua_newtable(L); // handle metatable
1294 luaL_setfuncs(L, moonbr_io_handle_metamethods, 0);
1295 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_MT_REGKEY);
1297 lua_newtable(L); // listener metatable
1298 luaL_setfuncs(L, moonbr_io_listener_metamethods, 0);
1299 lua_newtable(L); // listener methods
1300 luaL_setfuncs(L, moonbr_io_listener_methods, 0);
1301 lua_pushvalue(L, -1);
1302 lua_setfield(L, -4, "prototype_listener");
1303 lua_setfield(L, -2, "__index");
1304 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_LISTENER_MT_REGKEY);
1306 luaL_setfuncs(L, moonbr_io_module_funcs, 0);
1307 return 1;

Impressum / About Us