moonbridge

view moonbridge_io.c @ 95:719f83c7fea4

Correctly cache nonblocking status of socket in I/O library; Use TCP_NOPUSH/TCP_CORK when applicable
author jbe
date Wed Apr 08 01:12:03 2015 +0200 (2015-04-08)
parents de3982f17d05
children fdc1bb710544
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>
13 #include <lua.h>
14 #include <lauxlib.h>
15 #include <lualib.h>
17 #define MOONBR_IO_MAXSTRERRORLEN 80
18 #define MOONBR_IO_READBUFLEN 4096
19 #define MOONBR_IO_WRITEBUFLEN 4096
21 #define moonbr_io_errmsg() \
22 char errmsg[MOONBR_IO_MAXSTRERRORLEN]; \
23 strerror_r(errno, errmsg, MOONBR_IO_MAXSTRERRORLEN)
25 #define MOONBR_IO_HANDLE_MT_REGKEY "moonbridge_io_handle"
26 #define MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY "moonbridge_io_handle_public"
28 typedef struct {
29 int fd;
30 int isnetwork;
31 int finished;
32 int closed;
33 int nonblocking;
34 int nopush;
35 int readerr;
36 int readbufcnt;
37 int writeerr;
38 size_t writeleft;
39 #if LUA_VERSION_NUM >= 503
40 lua_Integer writeqin;
41 lua_Integer writeqout;
42 #else
43 int writeqin;
44 int writeqout;
45 #endif
46 size_t writeqoff;
47 int writebufcnt;
48 char readbuf[MOONBR_IO_READBUFLEN];
49 char writebuf[MOONBR_IO_WRITEBUFLEN];
50 } moonbr_io_handle_t;
52 static void moonbr_io_handle_set_nonblocking(lua_State *L, moonbr_io_handle_t *handle, int nonblocking) {
53 int flags;
54 if (handle->nonblocking == nonblocking) return;
55 flags = fcntl(handle->fd, F_GETFL, 0);
56 if (flags == -1) {
57 moonbr_io_errmsg();
58 handle->nonblocking = -1;
59 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
60 }
61 if (nonblocking) flags |= O_NONBLOCK;
62 else flags &= ~O_NONBLOCK;
63 if (fcntl(handle->fd, F_SETFL, flags) == -1) {
64 moonbr_io_errmsg();
65 handle->nonblocking = -1;
66 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
67 }
68 handle->nonblocking = nonblocking;
69 }
71 static void moonbr_io_handle_set_linger(lua_State *L, moonbr_io_handle_t *handle, int timeout) {
72 struct linger lingerval = { 0, };
73 if (!handle->isnetwork) return;
74 if (timeout >= 0) {
75 lingerval.l_onoff = 1;
76 lingerval.l_linger = timeout;
77 }
78 if (setsockopt(handle->fd, SOL_SOCKET, SO_LINGER, &lingerval, sizeof(lingerval))) {
79 moonbr_io_errmsg();
80 luaL_error(L, "Unexpected error while setting SO_LINGER with setsockopt: %s", errmsg);
81 }
82 }
84 static void moonbr_io_handle_set_nopush(lua_State *L, moonbr_io_handle_t *handle, int nopush) {
85 if (!handle->isnetwork || handle->nopush == nopush) return;
86 #if defined(TCP_CORK) && !defined(TCP_NOPUSH)
87 if (setsockopt(handle->fd, IPPROTO_TCP, TCP_CORK, &nopush, sizeof(nopush))) {
88 moonbr_io_errmsg();
89 handle->nopush = -1;
90 luaL_error(L, "Unexpected error while setting TCP_CORK with setsockopt: %s", errmsg);
91 }
92 #else
93 if (setsockopt(handle->fd, IPPROTO_TCP, TCP_NOPUSH, &nopush, sizeof(nopush))) {
94 moonbr_io_errmsg();
95 handle->nopush = -1;
96 luaL_error(L, "Unexpected error while setting TCP_NOPUSH with setsockopt: %s", errmsg);
97 }
98 #endif
99 handle->nopush = nopush;
100 }
102 static int moonbr_io_read_impl(lua_State *L, int nonblocking, int drain) {
103 moonbr_io_handle_t *handle;
104 lua_Integer maxread;
105 const char *terminatorstr;
106 size_t terminatorlen;
107 char terminator;
108 luaL_Buffer luabuf;
109 size_t luabufcnt = 0;
110 int endcnt;
111 char *terminatorpos;
112 ssize_t result;
113 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
114 maxread = luaL_optinteger(L, 2, 0);
115 terminatorstr = luaL_optlstring(L, 3, "", &terminatorlen);
116 if (terminatorlen) {
117 luaL_argcheck(L, terminatorlen == 1, 3, "single byte expected");
118 terminator = terminatorstr[0];
119 }
120 lua_settop(L, 1); /* return handle on drain, terminator string may be garbage collected */
121 if (handle->closed) luaL_error(L, "Attempt to read from a closed I/O handle");
122 if (handle->fd < 0) goto moonbr_io_read_impl_eof; /* fake EOF to simulate shutdown */
123 if (handle->readerr) {
124 lua_pushnil(L);
125 lua_pushliteral(L, "Previous read error");
126 return 2;
127 }
128 moonbr_io_handle_set_nonblocking(L, handle, nonblocking);
129 if (!drain) luaL_buffinit(L, &luabuf);
130 while (1) {
131 endcnt = -1;
132 if (maxread > 0 && handle->readbufcnt >= maxread - luabufcnt) {
133 endcnt = maxread - luabufcnt;
134 } else if (terminatorlen) {
135 terminatorpos = memchr(handle->readbuf, terminator, handle->readbufcnt);
136 if (terminatorpos) endcnt = 1 + (terminatorpos - handle->readbuf);
137 }
138 if (endcnt >= 0) {
139 if (!drain) {
140 luaL_addlstring(&luabuf, handle->readbuf, endcnt);
141 luaL_pushresult(&luabuf);
142 } else {
143 luabufcnt += handle->readbufcnt;
144 lua_pushinteger(L, luabufcnt);
145 }
146 handle->readbufcnt -= endcnt;
147 memmove(handle->readbuf, handle->readbuf + endcnt, handle->readbufcnt);
148 return 1;
149 }
150 if (!drain) luaL_addlstring(&luabuf, handle->readbuf, handle->readbufcnt);
151 luabufcnt += handle->readbufcnt;
152 handle->readbufcnt = 0;
153 do {
154 result = read(handle->fd, handle->readbuf, MOONBR_IO_READBUFLEN);
155 } while (result < 0 && (errno == EINTR));
156 if (result == 0 || (nonblocking && result < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))) break;
157 if (result < 0) {
158 moonbr_io_errmsg();
159 handle->readerr = 1;
160 lua_pushnil(L);
161 lua_pushstring(L, errmsg);
162 return 2;
163 }
164 handle->readbufcnt += result;
165 }
166 if (!drain) {
167 luaL_addlstring(&luabuf, handle->readbuf, handle->readbufcnt);
168 luaL_pushresult(&luabuf);
169 }
170 luabufcnt += handle->readbufcnt;
171 handle->readbufcnt = 0;
172 if (!drain) {
173 if (!luabufcnt && result == 0) {
174 moonbr_io_read_impl_eof:
175 lua_pushboolean(L, 0);
176 lua_pushliteral(L, "End of file");
177 return 2;
178 }
179 } else {
180 if (!luabufcnt && result == 0) lua_pushboolean(L, 1);
181 else lua_pushboolean(L, luabufcnt);
182 }
183 return 1;
184 }
186 static int moonbr_io_read(lua_State *L) {
187 return moonbr_io_read_impl(L, 0, 0);
188 }
190 static int moonbr_io_read_nb(lua_State *L) {
191 return moonbr_io_read_impl(L, 1, 0);
192 }
194 static int moonbr_io_drain(lua_State *L) {
195 return moonbr_io_read_impl(L, 0, 1);
196 }
198 static int moonbr_io_drain_nb(lua_State *L) {
199 return moonbr_io_read_impl(L, 1, 1);
200 }
202 static int moonbr_io_write_impl(lua_State *L, int nonblocking, int flush) {
203 moonbr_io_handle_t *handle;
204 int i, top;
205 const char *str;
206 size_t strlen;
207 size_t written;
208 ssize_t result;
209 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
210 if (handle->closed) luaL_error(L, "Attempt to write to a closed I/O handle");
211 if (handle->finished) luaL_error(L, "Attempt to write to a finished I/O handle");
212 if (handle->writeerr) {
213 lua_pushnil(L);
214 lua_pushliteral(L, "Previous write error");
215 return 2;
216 }
217 moonbr_io_handle_set_nonblocking(L, handle, nonblocking);
218 top = lua_gettop(L);
219 lua_getuservalue(L, 1);
220 lua_getfield(L, -1, "writebuf");
221 for (i=2; i<=top; i++) {
222 luaL_checklstring(L, i, &strlen);
223 lua_pushvalue(L, i);
224 lua_rawseti(L, -2, handle->writeqin++);
225 handle->writeleft += strlen;
226 }
227 while (handle->writeqout != handle->writeqin) {
228 lua_rawgeti(L, -1, handle->writeqout);
229 str = lua_tolstring(L, -1, &strlen);
230 while (handle->writeqoff < strlen) {
231 if (strlen - handle->writeqoff < MOONBR_IO_WRITEBUFLEN - handle->writebufcnt) {
232 memcpy(handle->writebuf + handle->writebufcnt, str + handle->writeqoff, strlen - handle->writeqoff);
233 handle->writebufcnt += strlen - handle->writeqoff;
234 break;
235 } else {
236 moonbr_io_handle_set_nopush(L, handle,
237 (flush || handle->writeleft == MOONBR_IO_WRITEBUFLEN) ?
238 0 : 1
239 );
240 written = 0;
241 memcpy(handle->writebuf + handle->writebufcnt, str + handle->writeqoff, MOONBR_IO_WRITEBUFLEN - handle->writebufcnt);
242 handle->writeqoff += MOONBR_IO_WRITEBUFLEN - handle->writebufcnt;
243 while (written < MOONBR_IO_WRITEBUFLEN) {
244 result = write(handle->fd, handle->writebuf + written, MOONBR_IO_WRITEBUFLEN - written);
245 if (result < 0) {
246 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
247 if (written) {
248 handle->writebufcnt -= written;
249 memmove(handle->writebuf, handle->writebuf + written, handle->writebufcnt);
250 goto moonbr_io_write_impl_continue;
251 }
252 goto moonbr_io_write_impl_block;
253 } else if (errno != EINTR) {
254 moonbr_io_errmsg();
255 handle->writeerr = 1;
256 lua_pushnil(L);
257 lua_pushstring(L, errmsg);
258 return 2;
259 }
260 }
261 written += result;
262 handle->writeleft -= result;
263 }
264 handle->writebufcnt = 0;
265 }
266 moonbr_io_write_impl_continue:;
267 }
268 handle->writeqoff = 0;
269 lua_pop(L, 1);
270 lua_pushnil(L);
271 lua_rawseti(L, -2, handle->writeqout++);
272 }
273 if (flush) {
274 written = 0;
275 while (written < handle->writebufcnt) {
276 result = write(handle->fd, handle->writebuf + written, handle->writebufcnt - written);
277 if (result < 0) {
278 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
279 if (written) {
280 handle->writebufcnt -= written;
281 memmove(handle->writebuf, handle->writebuf + written, handle->writebufcnt);
282 }
283 goto moonbr_io_write_impl_block;
284 } else if (errno != EINTR) {
285 moonbr_io_errmsg();
286 handle->writeerr = -1;
287 lua_pushnil(L);
288 lua_pushstring(L, errmsg);
289 return 2;
290 }
291 } else {
292 written += result;
293 handle->writeleft -= result;
294 }
295 }
296 handle->writebufcnt = 0;
297 if (nonblocking) lua_pushinteger(L, 0);
298 } else {
299 if (nonblocking) lua_pushinteger(L, handle->writeleft);
300 }
301 if (!nonblocking) lua_pushvalue(L, 1);
302 return 1;
303 moonbr_io_write_impl_block:
304 lua_pushinteger(L, handle->writeleft);
305 return 1;
306 }
308 static int moonbr_io_write(lua_State *L) {
309 return moonbr_io_write_impl(L, 0, 0);
310 }
312 static int moonbr_io_write_nb(lua_State *L) {
313 return moonbr_io_write_impl(L, 1, 0);
314 }
316 static int moonbr_io_flush(lua_State *L) {
317 return moonbr_io_write_impl(L, 0, 1);
318 }
320 static int moonbr_io_flush_nb(lua_State *L) {
321 return moonbr_io_write_impl(L, 1, 1);
322 }
324 static int moonbr_io_finish(lua_State *L) {
325 moonbr_io_handle_t *handle;
326 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
327 if (handle->closed) luaL_error(L, "Attempt to finish a closed I/O handle");
328 if (handle->finished) luaL_error(L, "Attempt to finish a finished I/O handle");
329 if (handle->writeleft) {
330 lua_pushcfunction(L, moonbr_io_flush);
331 lua_pushvalue(L, 1);
332 lua_call(L, 1, 2);
333 if (!lua_toboolean(L, -2)) {
334 handle->finished = 1;
335 return 2;
336 }
337 }
338 handle->finished = 1;
339 if (handle->isnetwork) {
340 if (shutdown(handle->fd, SHUT_WR)) {
341 moonbr_io_errmsg();
342 lua_pushnil(L);
343 lua_pushstring(L, errmsg);
344 return 2;
345 }
346 } else {
347 if (close(handle->fd)) {
348 moonbr_io_errmsg();
349 handle->fd = -1;
350 lua_pushnil(L);
351 lua_pushstring(L, errmsg);
352 return 2;
353 }
354 handle->fd = -1; /* fake EOF on read */
355 }
356 lua_pushboolean(L, 1);
357 return 1;
358 }
360 static int moonbr_io_close_impl(lua_State *L, int reset) {
361 moonbr_io_handle_t *handle;
362 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
363 if (handle->closed) luaL_error(L, "Attempt to close a closed I/O handle");
364 if (!reset) {
365 if (handle->writeleft) {
366 lua_pushcfunction(L, moonbr_io_flush);
367 lua_pushvalue(L, 1);
368 lua_call(L, 1, 2);
369 if (!lua_toboolean(L, -2)) {
370 close(handle->fd);
371 handle->fd = -1;
372 return 2;
373 }
374 }
375 moonbr_io_handle_set_linger(L, handle, -1);
376 }
377 if (handle->fd >= 0) {
378 if (close(handle->fd)) {
379 moonbr_io_errmsg();
380 handle->fd = -1;
381 lua_pushnil(L);
382 lua_pushstring(L, errmsg);
383 return 2;
384 }
385 }
386 handle->fd = -1;
387 lua_pushboolean(L, 1);
388 return 1;
390 }
392 static int moonbr_io_close(lua_State *L) {
393 return moonbr_io_close_impl(L, 0);
394 }
396 static int moonbr_io_reset(lua_State *L) {
397 return moonbr_io_close_impl(L, 1);
398 }
400 static int moonbr_io_gc(lua_State *L) {
401 moonbr_io_handle_t *handle;
402 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
403 if (handle->fd >= 0) {
404 lua_pushcfunction(L, moonbr_io_close);
405 lua_pushvalue(L, 1);
406 lua_pushinteger(L, 0);
407 lua_call(L, 2, 0);
408 }
409 return 0;
410 }
412 void moonbr_io_closehandle(lua_State *L, int idx, int timeout) {
413 moonbr_io_handle_t *handle;
414 handle = luaL_checkudata(L, idx, MOONBR_IO_HANDLE_MT_REGKEY);
415 if (handle->fd >= 0) {
416 lua_pushcfunction(L, moonbr_io_close);
417 lua_pushvalue(L, idx);
418 lua_pushinteger(L, timeout);
419 lua_call(L, 2, 0);
420 }
421 }
423 void moonbr_io_pushhandle(lua_State *L, int fd, int isnetwork) {
424 moonbr_io_handle_t *handle;
425 handle = lua_newuserdata(L, sizeof(moonbr_io_handle_t));
426 handle->fd = fd;
427 handle->isnetwork = isnetwork;
428 handle->finished = 0;
429 handle->closed = 0;
430 handle->nonblocking = -1;
431 handle->nopush = -1;
432 handle->readerr = 0;
433 handle->readbufcnt = 0;
434 handle->writeerr = 0;
435 handle->writeleft = 0;
436 handle->writeqin = 0;
437 handle->writeqout = 0;
438 handle->writeqoff = 0;
439 handle->writebufcnt = 0;
440 moonbr_io_handle_set_linger(L, handle, 0);
441 luaL_getmetatable(L, MOONBR_IO_HANDLE_MT_REGKEY);
442 lua_setmetatable(L, -2);
443 lua_newtable(L); // uservalue
444 lua_newtable(L);
445 lua_setfield(L, -2, "writebuf");
446 lua_newtable(L); // public
447 luaL_getmetatable(L, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
448 lua_setmetatable(L, -2);
449 lua_setfield(L, -2, "public");
450 lua_setuservalue(L, -2);
451 }
453 static int moonbr_io_handleindex(lua_State *L) {
454 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
455 lua_getuservalue(L, 1);
456 lua_getfield(L, -1, "public");
457 lua_pushvalue(L, 2);
458 lua_gettable(L, -2);
459 return 1;
460 }
462 static int moonbr_io_handlenewindex(lua_State *L) {
463 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
464 lua_getuservalue(L, 1);
465 lua_getfield(L, -1, "public");
466 lua_pushvalue(L, 2);
467 lua_pushvalue(L, 3);
468 lua_settable(L, -3);
469 return 0;
470 }
472 static const struct luaL_Reg moonbr_io_handle_methods[] = {
473 {"read", moonbr_io_read},
474 {"read_nb", moonbr_io_read_nb},
475 {"drain", moonbr_io_drain},
476 {"drain_nb", moonbr_io_drain_nb},
477 {"write", moonbr_io_write},
478 {"write_nb", moonbr_io_write_nb},
479 {"flush", moonbr_io_flush},
480 {"flush_nb", moonbr_io_flush_nb},
481 {"finish", moonbr_io_finish},
482 {"close", moonbr_io_close},
483 {"reset", moonbr_io_reset},
484 {NULL, NULL}
485 };
487 static const struct luaL_Reg moonbr_io_handle_metamethods[] = {
488 {"__index", moonbr_io_handleindex},
489 {"__newindex", moonbr_io_handlenewindex},
490 {"__gc", moonbr_io_gc},
491 {NULL, NULL}
492 };
494 static const struct luaL_Reg moonbr_io_module_funcs[] = {
495 {NULL, NULL}
496 };
498 int luaopen_moonbridge_io(lua_State *L) {
500 lua_newtable(L); // module
502 lua_newtable(L); // public metatable
503 lua_newtable(L); // handle methods
504 luaL_setfuncs(L, moonbr_io_handle_methods, 0);
505 lua_pushvalue(L, -1);
506 lua_setfield(L, -4, "handle");
507 lua_setfield(L, -2, "__index");
508 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
510 lua_newtable(L); // handle metatable
511 luaL_setfuncs(L, moonbr_io_handle_metamethods, 0);
512 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_MT_REGKEY);
514 luaL_setfuncs(L, moonbr_io_module_funcs, 0);
515 return 1;
517 }

Impressum / About Us