moonbridge

view moonbridge_io.c @ 96:fdc1bb710544

Allow full write buffer without forcing TCP PSH
author jbe
date Wed Apr 08 01:22:20 2015 +0200 (2015-04-08)
parents 719f83c7fea4
children 0561abcc68ee
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, flush ? 0 : 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 written = 0;
287 while (written < handle->writebufcnt) {
288 result = write(handle->fd, handle->writebuf + written, handle->writebufcnt - written);
289 if (result < 0) {
290 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
291 if (written) {
292 handle->writebufcnt -= written;
293 memmove(handle->writebuf, handle->writebuf + written, handle->writebufcnt);
294 }
295 goto moonbr_io_write_impl_block;
296 } else if (errno != EINTR) {
297 moonbr_io_errmsg();
298 handle->writeerr = -1;
299 lua_pushnil(L);
300 lua_pushstring(L, errmsg);
301 return 2;
302 }
303 } else {
304 written += result;
305 handle->writeleft -= result;
306 }
307 }
308 handle->writebufcnt = 0;
309 if (nonblocking) lua_pushinteger(L, 0);
310 } else {
311 if (nonblocking) lua_pushinteger(L, handle->writeleft);
312 }
313 if (!nonblocking) lua_pushvalue(L, 1);
314 return 1;
315 moonbr_io_write_impl_block:
316 lua_pushinteger(L, handle->writeleft);
317 return 1;
318 }
320 static int moonbr_io_write(lua_State *L) {
321 return moonbr_io_write_impl(L, 0, 0);
322 }
324 static int moonbr_io_write_nb(lua_State *L) {
325 return moonbr_io_write_impl(L, 1, 0);
326 }
328 static int moonbr_io_flush(lua_State *L) {
329 return moonbr_io_write_impl(L, 0, 1);
330 }
332 static int moonbr_io_flush_nb(lua_State *L) {
333 return moonbr_io_write_impl(L, 1, 1);
334 }
336 static int moonbr_io_finish(lua_State *L) {
337 moonbr_io_handle_t *handle;
338 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
339 if (handle->closed) luaL_error(L, "Attempt to finish a closed I/O handle");
340 if (handle->finished) luaL_error(L, "Attempt to finish a finished I/O handle");
341 if (handle->writeleft) {
342 lua_pushcfunction(L, moonbr_io_flush);
343 lua_pushvalue(L, 1);
344 lua_call(L, 1, 2);
345 if (!lua_toboolean(L, -2)) {
346 handle->finished = 1;
347 return 2;
348 }
349 }
350 handle->finished = 1;
351 if (handle->isnetwork) {
352 if (shutdown(handle->fd, SHUT_WR)) {
353 moonbr_io_errmsg();
354 lua_pushnil(L);
355 lua_pushstring(L, errmsg);
356 return 2;
357 }
358 } else {
359 if (close(handle->fd)) {
360 moonbr_io_errmsg();
361 handle->fd = -1;
362 lua_pushnil(L);
363 lua_pushstring(L, errmsg);
364 return 2;
365 }
366 handle->fd = -1; /* fake EOF on read */
367 }
368 lua_pushboolean(L, 1);
369 return 1;
370 }
372 static int moonbr_io_close_impl(lua_State *L, int reset) {
373 moonbr_io_handle_t *handle;
374 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
375 if (handle->closed) luaL_error(L, "Attempt to close a closed I/O handle");
376 if (!reset) {
377 if (handle->writeleft) {
378 lua_pushcfunction(L, moonbr_io_flush);
379 lua_pushvalue(L, 1);
380 lua_call(L, 1, 2);
381 if (!lua_toboolean(L, -2)) {
382 close(handle->fd);
383 handle->fd = -1;
384 return 2;
385 }
386 }
387 moonbr_io_handle_set_linger(L, handle, -1);
388 }
389 if (handle->fd >= 0) {
390 if (close(handle->fd)) {
391 moonbr_io_errmsg();
392 handle->fd = -1;
393 lua_pushnil(L);
394 lua_pushstring(L, errmsg);
395 return 2;
396 }
397 }
398 handle->fd = -1;
399 lua_pushboolean(L, 1);
400 return 1;
402 }
404 static int moonbr_io_close(lua_State *L) {
405 return moonbr_io_close_impl(L, 0);
406 }
408 static int moonbr_io_reset(lua_State *L) {
409 return moonbr_io_close_impl(L, 1);
410 }
412 static int moonbr_io_gc(lua_State *L) {
413 moonbr_io_handle_t *handle;
414 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
415 if (handle->fd >= 0) {
416 lua_pushcfunction(L, moonbr_io_close);
417 lua_pushvalue(L, 1);
418 lua_pushinteger(L, 0);
419 lua_call(L, 2, 0);
420 }
421 return 0;
422 }
424 void moonbr_io_closehandle(lua_State *L, int idx, int timeout) {
425 moonbr_io_handle_t *handle;
426 handle = luaL_checkudata(L, idx, MOONBR_IO_HANDLE_MT_REGKEY);
427 if (handle->fd >= 0) {
428 lua_pushcfunction(L, moonbr_io_close);
429 lua_pushvalue(L, idx);
430 lua_pushinteger(L, timeout);
431 lua_call(L, 2, 0);
432 }
433 }
435 void moonbr_io_pushhandle(lua_State *L, int fd, int isnetwork) {
436 moonbr_io_handle_t *handle;
437 handle = lua_newuserdata(L, sizeof(moonbr_io_handle_t));
438 handle->fd = fd;
439 handle->isnetwork = isnetwork;
440 handle->finished = 0;
441 handle->closed = 0;
442 handle->nonblocking = -1;
443 handle->nopush = -1;
444 handle->readerr = 0;
445 handle->readbufcnt = 0;
446 handle->writeerr = 0;
447 handle->writeleft = 0;
448 handle->writeqin = 0;
449 handle->writeqout = 0;
450 handle->writeqoff = 0;
451 handle->writebufcnt = 0;
452 moonbr_io_handle_set_linger(L, handle, 0);
453 luaL_getmetatable(L, MOONBR_IO_HANDLE_MT_REGKEY);
454 lua_setmetatable(L, -2);
455 lua_newtable(L); // uservalue
456 lua_newtable(L);
457 lua_setfield(L, -2, "writebuf");
458 lua_newtable(L); // public
459 luaL_getmetatable(L, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
460 lua_setmetatable(L, -2);
461 lua_setfield(L, -2, "public");
462 lua_setuservalue(L, -2);
463 }
465 static int moonbr_io_handleindex(lua_State *L) {
466 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
467 lua_getuservalue(L, 1);
468 lua_getfield(L, -1, "public");
469 lua_pushvalue(L, 2);
470 lua_gettable(L, -2);
471 return 1;
472 }
474 static int moonbr_io_handlenewindex(lua_State *L) {
475 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
476 lua_getuservalue(L, 1);
477 lua_getfield(L, -1, "public");
478 lua_pushvalue(L, 2);
479 lua_pushvalue(L, 3);
480 lua_settable(L, -3);
481 return 0;
482 }
484 static const struct luaL_Reg moonbr_io_handle_methods[] = {
485 {"read", moonbr_io_read},
486 {"read_nb", moonbr_io_read_nb},
487 {"drain", moonbr_io_drain},
488 {"drain_nb", moonbr_io_drain_nb},
489 {"write", moonbr_io_write},
490 {"write_nb", moonbr_io_write_nb},
491 {"flush", moonbr_io_flush},
492 {"flush_nb", moonbr_io_flush_nb},
493 {"finish", moonbr_io_finish},
494 {"close", moonbr_io_close},
495 {"reset", moonbr_io_reset},
496 {NULL, NULL}
497 };
499 static const struct luaL_Reg moonbr_io_handle_metamethods[] = {
500 {"__index", moonbr_io_handleindex},
501 {"__newindex", moonbr_io_handlenewindex},
502 {"__gc", moonbr_io_gc},
503 {NULL, NULL}
504 };
506 static const struct luaL_Reg moonbr_io_module_funcs[] = {
507 {NULL, NULL}
508 };
510 int luaopen_moonbridge_io(lua_State *L) {
512 lua_newtable(L); // module
514 lua_newtable(L); // public metatable
515 lua_newtable(L); // handle methods
516 luaL_setfuncs(L, moonbr_io_handle_methods, 0);
517 lua_pushvalue(L, -1);
518 lua_setfield(L, -4, "handle");
519 lua_setfield(L, -2, "__index");
520 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
522 lua_newtable(L); // handle metatable
523 luaL_setfuncs(L, moonbr_io_handle_metamethods, 0);
524 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_MT_REGKEY);
526 luaL_setfuncs(L, moonbr_io_module_funcs, 0);
527 return 1;
529 }

Impressum / About Us