moonbridge

view moonbridge_io.c @ 92:111b2469b753

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

Impressum / About Us