moonbridge

view moonbridge_io.c @ 142:8cd9acda3853

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

Impressum / About Us