moonbridge

view moonbridge_io.c @ 106:8fce76ef321f

Added moonbridge_io.poll(...)
author jbe
date Wed Apr 08 20:38:18 2015 +0200 (2015-04-08)
parents 11d03470ef19
children 06d965df8a0c
line source
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <stdint.h>
5 #include <errno.h>
6 #include <string.h>
7 #include <sys/socket.h>
8 #include <sys/select.h>
9 #include <fcntl.h>
10 #include <netinet/in.h>
11 #include <netinet/tcp.h>
12 #include <sys/types.h>
13 #include <netdb.h>
15 #include <lua.h>
16 #include <lauxlib.h>
17 #include <lualib.h>
19 #define MOONBR_IO_MAXSTRERRORLEN 80
20 #define MOONBR_IO_READBUFLEN 4096
21 #define MOONBR_IO_WRITEBUFLEN 4096
23 #define moonbr_io_errmsg() \
24 char errmsg[MOONBR_IO_MAXSTRERRORLEN]; \
25 strerror_r(errno, errmsg, MOONBR_IO_MAXSTRERRORLEN)
27 #define MOONBR_IO_HANDLE_MT_REGKEY "moonbridge_io_handle"
28 #define MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY "moonbridge_io_handle_public"
30 typedef struct {
31 int fd;
32 int isnetwork;
33 int finished;
34 int closed;
35 int nonblocking;
36 int nopush;
37 int readerr;
38 int readbufin;
39 int readbufout;
40 int writeerr;
41 size_t writeleft;
42 #if LUA_VERSION_NUM >= 503
43 lua_Integer writeqin;
44 lua_Integer writeqout;
45 #else
46 int writeqin;
47 int writeqout;
48 #endif
49 size_t writeqoff;
50 int writebufin;
51 int writebufout;
52 char readbuf[MOONBR_IO_READBUFLEN];
53 char writebuf[MOONBR_IO_WRITEBUFLEN];
54 } moonbr_io_handle_t;
56 static void moonbr_io_handle_set_nonblocking(lua_State *L, moonbr_io_handle_t *handle, int nonblocking) {
57 int flags;
58 if (handle->nonblocking == nonblocking) return;
59 flags = fcntl(handle->fd, F_GETFL, 0);
60 if (flags == -1) {
61 moonbr_io_errmsg();
62 close(handle->fd);
63 handle->fd = -1;
64 handle->closed = 1;
65 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
66 }
67 if (nonblocking) flags |= O_NONBLOCK;
68 else flags &= ~O_NONBLOCK;
69 if (fcntl(handle->fd, F_SETFL, flags) == -1) {
70 moonbr_io_errmsg();
71 close(handle->fd);
72 handle->fd = -1;
73 handle->closed = 1;
74 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
75 }
76 handle->nonblocking = nonblocking;
77 }
79 static void moonbr_io_handle_set_linger(lua_State *L, moonbr_io_handle_t *handle, int timeout) {
80 struct linger lingerval = { 0, };
81 if (!handle->isnetwork) return;
82 if (timeout >= 0) {
83 lingerval.l_onoff = 1;
84 lingerval.l_linger = timeout;
85 }
86 if (setsockopt(handle->fd, SOL_SOCKET, SO_LINGER, &lingerval, sizeof(lingerval))) {
87 moonbr_io_errmsg();
88 close(handle->fd);
89 handle->fd = -1;
90 handle->closed = 1;
91 luaL_error(L, "Unexpected error while setting SO_LINGER with setsockopt: %s", errmsg);
92 }
93 }
95 static void moonbr_io_handle_set_nopush(lua_State *L, moonbr_io_handle_t *handle, int nopush) {
96 #if defined(TCP_NOPUSH) || defined(TCP_CORK)
97 if (!handle->isnetwork || handle->nopush == nopush) return;
98 #if defined(TCP_NOPUSH)
99 if (setsockopt(handle->fd, IPPROTO_TCP, TCP_NOPUSH, &nopush, sizeof(nopush))) {
100 #elif defined(TCP_CORK)
101 if (setsockopt(handle->fd, IPPROTO_TCP, TCP_CORK, &nopush, sizeof(nopush))) {
102 #endif
103 moonbr_io_errmsg();
104 close(handle->fd);
105 handle->fd = -1;
106 handle->closed = 1;
107 #if defined(TCP_NOPUSH)
108 luaL_error(L, "Unexpected error while setting TCP_NOPUSH with setsockopt: %s", errmsg);
109 #elif defined(TCP_CORK)
110 luaL_error(L, "Unexpected error while setting TCP_CORK with setsockopt: %s", errmsg);
111 #endif
112 }
113 handle->nopush = nopush;
114 #else
115 #warning Neither TCP_NOPUSH nor TCP_CORK is available
116 #endif
117 }
119 static int moonbr_io_read_impl(lua_State *L, int nonblocking, int drain) {
120 moonbr_io_handle_t *handle;
121 lua_Integer maxread;
122 const char *terminatorstr;
123 size_t terminatorlen;
124 char terminator;
125 luaL_Buffer luabuf;
126 size_t luabufcnt = 0;
127 int remaining;
128 char *terminatorpos;
129 ssize_t bytesread;
130 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
131 maxread = luaL_optinteger(L, 2, 0);
132 terminatorstr = luaL_optlstring(L, 3, "", &terminatorlen);
133 if (terminatorlen) {
134 luaL_argcheck(L, terminatorlen == 1, 3, "single byte expected");
135 terminator = terminatorstr[0];
136 }
137 lua_settop(L, 1); /* return handle on drain, terminator string may be garbage collected */
138 if (handle->closed) luaL_error(L, "Attempt to read from a closed I/O handle");
139 if (handle->readerr) {
140 lua_pushnil(L);
141 lua_pushliteral(L, "Previous read error");
142 return 2;
143 }
144 handle->readerr = 1;
145 if (handle->fd < 0) goto moonbr_io_read_impl_eof; /* fake EOF to simulate shutdown */
146 moonbr_io_handle_set_nonblocking(L, handle, nonblocking);
147 if (!drain) luaL_buffinit(L, &luabuf);
148 while (1) {
149 remaining = -1;
150 if (
151 maxread > 0 &&
152 handle->readbufin - handle->readbufout >= (size_t)maxread - luabufcnt
153 ) {
154 remaining = maxread - luabufcnt;
155 } else if (terminatorlen) {
156 terminatorpos = memchr(
157 handle->readbuf + handle->readbufout,
158 terminator,
159 handle->readbufin - handle->readbufout
160 );
161 if (terminatorpos) remaining = 1 + (
162 terminatorpos - (handle->readbuf + handle->readbufout)
163 );
164 }
165 if (remaining >= 0) {
166 if (!drain) {
167 luaL_addlstring(
168 &luabuf,
169 handle->readbuf + handle->readbufout,
170 remaining
171 );
172 luaL_pushresult(&luabuf);
173 } else {
174 luaL_pushresult(&luabuf);
175 lua_pop(L, 1);
176 lua_pushinteger(L, luabufcnt + remaining);
177 }
178 handle->readbufout += remaining;
179 if (handle->readbufout == handle->readbufin) {
180 handle->readbufin = 0;
181 handle->readbufout =0;
182 }
183 handle->readerr = 0;
184 return 1;
185 }
186 if (!drain) luaL_addlstring(
187 &luabuf,
188 handle->readbuf + handle->readbufout,
189 handle->readbufin - handle->readbufout
190 );
191 luabufcnt += handle->readbufin - handle->readbufout;
192 do {
193 bytesread = read(handle->fd, handle->readbuf, MOONBR_IO_READBUFLEN);
194 } while (bytesread < 0 && (errno == EINTR));
195 if (
196 bytesread == 0 || (
197 nonblocking &&
198 bytesread < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)
199 )
200 ) {
201 handle->readbufin = 0;
202 handle->readbufout = 0;
203 if (!drain) {
204 luaL_pushresult(&luabuf);
205 if (!luabufcnt && bytesread == 0) {
206 lua_pop(L, 1);
207 moonbr_io_read_impl_eof:
208 lua_pushboolean(L, 0);
209 lua_pushliteral(L, "End of file");
210 handle->readerr = 0;
211 return 2;
212 }
213 } else {
214 if (!luabufcnt && bytesread == 0) lua_pushboolean(L, 1);
215 else lua_pushboolean(L, luabufcnt);
216 }
217 handle->readerr = 0;
218 return 1;
219 }
220 if (bytesread < 0) {
221 moonbr_io_errmsg();
222 lua_pushnil(L);
223 lua_pushstring(L, errmsg);
224 return 2;
225 }
226 handle->readbufin = bytesread;
227 handle->readbufout = 0;
228 }
229 }
231 static int moonbr_io_read(lua_State *L) {
232 return moonbr_io_read_impl(L, 0, 0);
233 }
235 static int moonbr_io_read_nb(lua_State *L) {
236 return moonbr_io_read_impl(L, 1, 0);
237 }
239 static int moonbr_io_drain(lua_State *L) {
240 return moonbr_io_read_impl(L, 0, 1);
241 }
243 static int moonbr_io_drain_nb(lua_State *L) {
244 return moonbr_io_read_impl(L, 1, 1);
245 }
247 static int moonbr_io_write_impl(lua_State *L, int nonblocking, int flush) {
248 moonbr_io_handle_t *handle;
249 int i, top;
250 const char *str;
251 size_t strlen;
252 ssize_t written;
253 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
254 if (handle->closed) luaL_error(L, "Attempt to write to a closed I/O handle");
255 if (handle->finished) luaL_error(L, "Attempt to write to a finished I/O handle");
256 if (handle->writeerr) {
257 lua_pushnil(L);
258 lua_pushliteral(L, "Previous write error");
259 return 2;
260 }
261 handle->writeerr = 1;
262 moonbr_io_handle_set_nonblocking(L, handle, nonblocking);
263 top = lua_gettop(L);
264 lua_getuservalue(L, 1);
265 lua_getfield(L, -1, "writequeue");
266 for (i=2; i<=top; i++) {
267 luaL_checklstring(L, i, &strlen);
268 lua_pushvalue(L, i);
269 lua_rawseti(L, -2, handle->writeqin++);
270 handle->writeleft += strlen;
271 }
272 while (handle->writeqout != handle->writeqin) {
273 lua_rawgeti(L, -1, handle->writeqout);
274 str = lua_tolstring(L, -1, &strlen);
275 while (handle->writeqoff < strlen) {
276 if (
277 strlen - handle->writeqoff <=
278 MOONBR_IO_WRITEBUFLEN - handle->writebufin
279 ) {
280 memcpy(
281 handle->writebuf + handle->writebufin,
282 str + handle->writeqoff,
283 strlen - handle->writeqoff
284 );
285 handle->writebufin += strlen - handle->writeqoff;
286 break;
287 } else {
288 moonbr_io_handle_set_nopush(L, handle, 1);
289 memcpy(
290 handle->writebuf + handle->writebufin,
291 str + handle->writeqoff,
292 MOONBR_IO_WRITEBUFLEN - handle->writebufin
293 );
294 handle->writeqoff += MOONBR_IO_WRITEBUFLEN - handle->writebufin;
295 while (handle->writebufout < MOONBR_IO_WRITEBUFLEN) {
296 written = write(
297 handle->fd,
298 handle->writebuf + handle->writebufout,
299 MOONBR_IO_WRITEBUFLEN - handle->writebufout
300 );
301 if (written < 0) {
302 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
303 goto moonbr_io_write_impl_block;
304 } else if (errno != EINTR) {
305 moonbr_io_errmsg();
306 lua_pushnil(L);
307 lua_pushstring(L, errmsg);
308 return 2;
309 }
310 } else {
311 handle->writebufout += written;
312 handle->writeleft -= written;
313 }
314 }
315 handle->writebufin = 0;
316 handle->writebufout = 0;
317 }
318 }
319 handle->writeqoff = 0;
320 lua_pop(L, 1);
321 lua_pushnil(L);
322 lua_rawseti(L, -2, handle->writeqout++);
323 }
324 if (flush) {
325 moonbr_io_handle_set_nopush(L, handle, 0);
326 while (handle->writebufout < handle->writebufin) {
327 written = write(
328 handle->fd,
329 handle->writebuf + handle->writebufout,
330 handle->writebufin - handle->writebufout
331 );
332 if (written < 0) {
333 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
334 goto moonbr_io_write_impl_block;
335 } else if (errno != EINTR) {
336 moonbr_io_errmsg();
337 lua_pushnil(L);
338 lua_pushstring(L, errmsg);
339 return 2;
340 }
341 } else {
342 handle->writebufout += written;
343 handle->writeleft -= written;
344 }
345 }
346 handle->writebufin = 0;
347 handle->writebufout = 0;
348 }
349 if (nonblocking) lua_pushinteger(L, 0);
350 else lua_pushvalue(L, 1);
351 handle->writeerr = 0;
352 return 1;
353 moonbr_io_write_impl_block:
354 lua_pushinteger(L, handle->writeleft);
355 handle->writeerr = 0;
356 return 1;
357 }
359 static int moonbr_io_write(lua_State *L) {
360 return moonbr_io_write_impl(L, 0, 0);
361 }
363 static int moonbr_io_write_nb(lua_State *L) {
364 return moonbr_io_write_impl(L, 1, 0);
365 }
367 static int moonbr_io_flush(lua_State *L) {
368 return moonbr_io_write_impl(L, 0, 1);
369 }
371 static int moonbr_io_flush_nb(lua_State *L) {
372 return moonbr_io_write_impl(L, 1, 1);
373 }
375 static int moonbr_io_finish(lua_State *L) {
376 moonbr_io_handle_t *handle;
377 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
378 if (handle->closed) luaL_error(L, "Attempt to finish a closed I/O handle");
379 if (handle->finished) luaL_error(L, "Attempt to finish a finished I/O handle");
380 if (handle->writeleft) {
381 lua_pushcfunction(L, moonbr_io_flush);
382 lua_pushvalue(L, 1);
383 lua_call(L, 1, 2);
384 if (!lua_toboolean(L, -2)) {
385 handle->finished = 1;
386 return 2;
387 }
388 }
389 handle->finished = 1;
390 if (handle->isnetwork) {
391 if (shutdown(handle->fd, SHUT_WR)) {
392 moonbr_io_errmsg();
393 lua_pushnil(L);
394 lua_pushstring(L, errmsg);
395 return 2;
396 }
397 } else {
398 if (close(handle->fd)) {
399 moonbr_io_errmsg();
400 handle->fd = -1;
401 lua_pushnil(L);
402 lua_pushstring(L, errmsg);
403 return 2;
404 }
405 handle->fd = -1; /* fake EOF on read */
406 }
407 lua_pushboolean(L, 1);
408 return 1;
409 }
411 static int moonbr_io_close_impl(lua_State *L, int reset) {
412 moonbr_io_handle_t *handle;
413 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
414 if (handle->closed) luaL_error(L, "Attempt to close a closed I/O handle");
415 if (!reset) {
416 if (handle->writeleft) {
417 lua_pushcfunction(L, moonbr_io_flush);
418 lua_pushvalue(L, 1);
419 lua_call(L, 1, 2);
420 if (!lua_toboolean(L, -2)) {
421 close(handle->fd);
422 handle->fd = -1;
423 return 2;
424 }
425 }
426 moonbr_io_handle_set_linger(L, handle, -1);
427 }
428 if (handle->fd >= 0) {
429 if (close(handle->fd)) {
430 moonbr_io_errmsg();
431 handle->fd = -1;
432 lua_pushnil(L);
433 lua_pushstring(L, errmsg);
434 return 2;
435 }
436 handle->fd = -1;
437 }
438 lua_pushboolean(L, 1);
439 return 1;
441 }
443 static int moonbr_io_close(lua_State *L) {
444 return moonbr_io_close_impl(L, 0);
445 }
447 static int moonbr_io_reset(lua_State *L) {
448 return moonbr_io_close_impl(L, 1);
449 }
451 static int moonbr_io_gc(lua_State *L) {
452 moonbr_io_handle_t *handle;
453 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
454 if (handle->fd >= 0) {
455 lua_pushcfunction(L, moonbr_io_close);
456 lua_pushvalue(L, 1);
457 lua_pushinteger(L, 0);
458 lua_call(L, 2, 0);
459 }
460 return 0;
461 }
463 void moonbr_io_closehandle(lua_State *L, int idx, int reset) {
464 moonbr_io_handle_t *handle;
465 handle = luaL_checkudata(L, idx, MOONBR_IO_HANDLE_MT_REGKEY);
466 if (!handle->closed) {
467 lua_pushcfunction(L, reset ? moonbr_io_reset : moonbr_io_close);
468 lua_pushvalue(L, idx < 0 ? idx-1 : idx);
469 lua_call(L, 1, 0);
470 }
471 }
473 void moonbr_io_pushhandle(lua_State *L, int fd, int isnetwork) {
474 moonbr_io_handle_t *handle;
475 handle = lua_newuserdata(L, sizeof(moonbr_io_handle_t));
476 handle->fd = fd;
477 handle->isnetwork = isnetwork;
478 handle->finished = 0;
479 handle->closed = 0;
480 handle->nonblocking = -1;
481 handle->nopush = -1;
482 handle->readerr = 0;
483 handle->readbufin = 0;
484 handle->readbufout = 0;
485 handle->writeerr = 0;
486 handle->writeleft = 0;
487 handle->writeqin = 0;
488 handle->writeqout = 0;
489 handle->writeqoff = 0;
490 handle->writebufin = 0;
491 handle->writebufout = 0;
492 moonbr_io_handle_set_linger(L, handle, 0);
493 luaL_getmetatable(L, MOONBR_IO_HANDLE_MT_REGKEY);
494 lua_setmetatable(L, -2);
495 lua_newtable(L); // uservalue
496 lua_newtable(L);
497 lua_setfield(L, -2, "writequeue");
498 lua_newtable(L); // public
499 luaL_getmetatable(L, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
500 lua_setmetatable(L, -2);
501 lua_setfield(L, -2, "public");
502 lua_setuservalue(L, -2);
503 }
505 static int moonbr_io_handleindex(lua_State *L) {
506 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
507 lua_getuservalue(L, 1);
508 lua_getfield(L, -1, "public");
509 lua_pushvalue(L, 2);
510 lua_gettable(L, -2);
511 return 1;
512 }
514 static int moonbr_io_handlenewindex(lua_State *L) {
515 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
516 lua_getuservalue(L, 1);
517 lua_getfield(L, -1, "public");
518 lua_pushvalue(L, 2);
519 lua_pushvalue(L, 3);
520 lua_settable(L, -3);
521 return 0;
522 }
524 static int moonbr_io_tcpconnect_impl(lua_State *L, int nonblocking) {
525 const char *host, *port;
526 struct addrinfo hints = { 0, };
527 struct addrinfo *res, *addrinfo;
528 int errcode;
529 int sock;
530 host = luaL_checkstring(L, 1);
531 port = luaL_checkstring(L, 2);
532 hints.ai_family = AF_UNSPEC;
533 hints.ai_socktype = SOCK_STREAM;
534 hints.ai_protocol = IPPROTO_TCP;
535 hints.ai_flags = AI_ADDRCONFIG;
536 errcode = getaddrinfo(host, port, &hints, &res);
537 if (errcode) {
538 freeaddrinfo(res);
539 if (errcode == EAI_SYSTEM) {
540 moonbr_io_errmsg();
541 lua_pushnil(L);
542 lua_pushfstring(L, "%s: %s", gai_strerror(errcode), errmsg);
543 } else {
544 lua_pushnil(L);
545 lua_pushstring(L, gai_strerror(errcode));
546 }
547 return 2;
548 }
549 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
550 if (addrinfo->ai_family == PF_INET6) goto moonbr_io_tcpconnect_found;
551 }
552 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
553 if (addrinfo->ai_family == PF_INET) goto moonbr_io_tcpconnect_found;
554 }
555 addrinfo = res;
556 moonbr_io_tcpconnect_found:
557 sock = socket(
558 addrinfo->ai_family,
559 addrinfo->ai_socktype | SOCK_CLOEXEC | (nonblocking ? SOCK_NONBLOCK : 0),
560 addrinfo->ai_protocol
561 );
562 freeaddrinfo(res);
563 if (sock < 0) {
564 moonbr_io_errmsg();
565 lua_pushnil(L);
566 lua_pushstring(L, errmsg);
567 }
568 if (connect(sock, addrinfo->ai_addr, addrinfo->ai_addrlen)) {
569 if (!nonblocking && errno == EINTR) {
570 moonbr_io_errmsg();
571 close(sock);
572 lua_pushnil(L);
573 lua_pushstring(L, errmsg);
574 return 2;
575 } else if (!(nonblocking && (errno == EINPROGRESS || errno == EINTR))) {
576 moonbr_io_errmsg();
577 lua_pushnil(L);
578 lua_pushstring(L, errmsg);
579 return 2;
580 }
581 }
582 moonbr_io_pushhandle(L, sock, 1);
583 return 1;
584 }
586 static int moonbr_io_tcpconnect(lua_State *L) {
587 return moonbr_io_tcpconnect_impl(L, 0);
588 }
590 static int moonbr_io_tcpconnect_nb(lua_State *L) {
591 return moonbr_io_tcpconnect_impl(L, 1);
592 }
594 static int moonbr_io_poll(lua_State *L) {
595 moonbr_io_handle_t *handle;
596 int fd, isnum;
597 int nfds = 0;
598 fd_set readfds, writefds, exceptfds;
599 struct timeval timeout = {0, };
600 int status;
601 FD_ZERO(&readfds);
602 FD_ZERO(&writefds);
603 FD_ZERO(&exceptfds);
604 if (!lua_isnoneornil(L, 1)) {
605 luaL_checktype(L, 1, LUA_TTABLE);
606 for (lua_pushnil(L); lua_next(L, 1); lua_pop(L, 1)) {
607 if (lua_toboolean(L, -1)) {
608 handle = luaL_testudata(L, -2, MOONBR_IO_HANDLE_MT_REGKEY);
609 if (handle) {
610 fd = handle->fd;
611 if (fd < 0) luaL_error(L, "Handle in illegal state");
612 } else {
613 fd = lua_tointegerx(L, -2, &isnum);
614 if (!isnum) luaL_error(L, "File descriptor is not an integer");
615 }
616 FD_SET(fd, &readfds);
617 if (fd+1 > nfds) nfds = fd+1;
618 }
619 }
620 }
621 if (!lua_isnoneornil(L, 2)) {
622 luaL_checktype(L, 2, LUA_TTABLE);
623 for (lua_pushnil(L); lua_next(L, 2); lua_pop(L, 1)) {
624 if (lua_toboolean(L, -1)) {
625 handle = luaL_testudata(L, -2, MOONBR_IO_HANDLE_MT_REGKEY);
626 if (handle) {
627 fd = handle->fd;
628 if (fd < 0) luaL_error(L, "Handle in illegal state");
629 } else {
630 fd = lua_tointegerx(L, -2, &isnum);
631 if (!isnum) luaL_error(L, "File descriptor is not an integer");
632 }
633 FD_SET(fd, &writefds);
634 if (fd+1 > nfds) nfds = fd+1;
635 }
636 }
637 }
638 if (!lua_isnoneornil(L, 3)) {
639 lua_Number n;
640 n = lua_tonumberx(L, 3, &isnum);
641 if (isnum && n>=0 && n<100000000) {
642 timeout.tv_sec = n;
643 timeout.tv_usec = 1e6 * (n - timeout.tv_sec);
644 } else {
645 luaL_argcheck(L, 0, 3, "not a valid timeout");
646 }
647 status = select(nfds, &readfds, &writefds, &exceptfds, &timeout);
648 } else {
649 status = select(nfds, &readfds, &writefds, &exceptfds, NULL);
650 }
651 if (status == -1) {
652 if (errno == EINTR) {
653 lua_pushboolean(L, 0);
654 lua_pushliteral(L, "Signal received while polling file descriptors");
655 return 2;
656 } else {
657 moonbr_io_errmsg();
658 return luaL_error(L, "Unexpected error during \"select\" system call: %s", errmsg);
659 }
660 } else if (status == 0) {
661 lua_pushboolean(L, 0);
662 lua_pushliteral(L, "Timeout while polling file descriptors");
663 return 2;
664 } else {
665 lua_pushboolean(L, 1);
666 return 1;
667 }
668 }
670 static const struct luaL_Reg moonbr_io_handle_methods[] = {
671 {"read", moonbr_io_read},
672 {"read_nb", moonbr_io_read_nb},
673 {"drain", moonbr_io_drain},
674 {"drain_nb", moonbr_io_drain_nb},
675 {"write", moonbr_io_write},
676 {"write_nb", moonbr_io_write_nb},
677 {"flush", moonbr_io_flush},
678 {"flush_nb", moonbr_io_flush_nb},
679 {"finish", moonbr_io_finish},
680 {"close", moonbr_io_close},
681 {"reset", moonbr_io_reset},
682 {NULL, NULL}
683 };
685 static const struct luaL_Reg moonbr_io_handle_metamethods[] = {
686 {"__index", moonbr_io_handleindex},
687 {"__newindex", moonbr_io_handlenewindex},
688 {"__gc", moonbr_io_gc},
689 {NULL, NULL}
690 };
692 static const struct luaL_Reg moonbr_io_module_funcs[] = {
693 {"tcpconnect", moonbr_io_tcpconnect},
694 {"tcpconnect_nb", moonbr_io_tcpconnect_nb},
695 {"poll", moonbr_io_poll},
696 {NULL, NULL}
697 };
699 int luaopen_moonbridge_io(lua_State *L) {
701 lua_newtable(L); // module
703 lua_newtable(L); // public metatable
704 lua_newtable(L); // handle methods
705 luaL_setfuncs(L, moonbr_io_handle_methods, 0);
706 lua_pushvalue(L, -1);
707 lua_setfield(L, -4, "handle");
708 lua_setfield(L, -2, "__index");
709 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
711 lua_newtable(L); // handle metatable
712 luaL_setfuncs(L, moonbr_io_handle_metamethods, 0);
713 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_MT_REGKEY);
715 luaL_setfuncs(L, moonbr_io_module_funcs, 0);
716 return 1;
718 }

Impressum / About Us