moonbridge

view moonbridge_io.c @ 97:0561abcc68ee

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

Impressum / About Us