moonbridge

view moonbridge_io.c @ 144:e7fac0918f9c

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

Impressum / About Us