moonbridge

view moonbridge_io.c @ 91:6b26783f9323

write_nb returns total number of bytes buffered in case of block; Updated reference
author jbe
date Tue Apr 07 03:50:28 2015 +0200 (2015-04-07)
parents 54e6104c70a6
children 111b2469b753
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, strpos;
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 strpos = handle->writeqoff;
206 while (strpos < strlen) {
207 if (strlen - strpos < MOONBR_IO_WRITEBUFLEN - handle->writebufcnt) {
208 memcpy(handle->writebuf + handle->writebufcnt, str + strpos, strlen - strpos);
209 handle->writebufcnt += strlen - strpos;
210 break;
211 } else {
212 written = 0;
213 memcpy(handle->writebuf + handle->writebufcnt, str + strpos, MOONBR_IO_WRITEBUFLEN - handle->writebufcnt);
214 strpos += MOONBR_IO_WRITEBUFLEN - handle->writebufcnt;
215 while (written < MOONBR_IO_WRITEBUFLEN) {
216 result = write(handle->fd, handle->writebuf + written, MOONBR_IO_WRITEBUFLEN - written);
217 if (result < 0) {
218 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
219 if (written) {
220 handle->writebufcnt -= written;
221 memmove(handle->writebuf, handle->writebuf + written, handle->writebufcnt);
222 }
223 handle->writeqoff = strpos;
224 goto moonbr_io_write_impl_block;
225 } else if (errno != EINTR) {
226 moonbr_io_errmsg();
227 handle->writeerr = 1;
228 lua_pushnil(L);
229 lua_pushstring(L, errmsg);
230 return 2;
231 }
232 } else {
233 written += result;
234 handle->writeleft -= result;
235 }
236 }
237 handle->writebufcnt = 0;
238 }
239 }
240 handle->writeqoff = 0;
241 lua_pop(L, 1);
242 lua_pushnil(L);
243 lua_rawseti(L, -2, handle->writeqout++);
244 }
245 if (flush) {
246 written = 0;
247 while (written < handle->writebufcnt) {
248 result = write(handle->fd, handle->writebuf + written, handle->writebufcnt - written);
249 if (result < 0) {
250 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
251 if (written) {
252 handle->writebufcnt -= written;
253 memmove(handle->writebuf, handle->writebuf + written, handle->writebufcnt);
254 }
255 goto moonbr_io_write_impl_block;
256 } else if (errno != EINTR) {
257 moonbr_io_errmsg();
258 handle->writeerr = -1;
259 lua_pushnil(L);
260 lua_pushstring(L, errmsg);
261 return 2;
262 }
263 } else {
264 written += result;
265 handle->writeleft -= result;
266 }
267 }
268 handle->writebufcnt = 0;
269 if (nonblocking) lua_pushinteger(L, 0);
270 } else {
271 if (nonblocking) lua_pushinteger(L, handle->writeleft);
272 }
273 if (!nonblocking) lua_pushvalue(L, 1);
274 return 1;
275 moonbr_io_write_impl_block:
276 lua_pushinteger(L, handle->writeleft);
277 return 1;
278 }
280 static int moonbr_io_write(lua_State *L) {
281 return moonbr_io_write_impl(L, 0, 0);
282 }
284 static int moonbr_io_write_nb(lua_State *L) {
285 return moonbr_io_write_impl(L, 1, 0);
286 }
288 static int moonbr_io_flush(lua_State *L) {
289 return moonbr_io_write_impl(L, 0, 1);
290 }
292 static int moonbr_io_flush_nb(lua_State *L) {
293 return moonbr_io_write_impl(L, 1, 1);
294 }
296 static int moonbr_io_finish(lua_State *L) {
297 moonbr_io_handle_t *handle;
298 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
299 if (handle->fd < 0) luaL_error(L, "Attempt to finish a closed I/O handle");
300 if (handle->shutwr) luaL_error(L, "Attempt to finish a finished I/O handle");
301 handle->shutwr = 1;
302 if (handle->canshutdown) {
303 if (handle->writeleft) {
304 lua_pushcfunction(L, moonbr_io_flush);
305 lua_pushvalue(L, 1);
306 lua_call(L, 1, 2);
307 if (!lua_toboolean(L, -2)) return 2;
308 }
309 if (shutdown(handle->fd, SHUT_WR)) {
310 moonbr_io_errmsg();
311 lua_pushnil(L);
312 lua_pushstring(L, errmsg);
313 return 2;
314 }
315 }
316 lua_pushboolean(L, 1);
317 return 1;
318 }
320 static int moonbr_io_close(lua_State *L) {
321 moonbr_io_handle_t *handle;
322 int timeout;
323 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
324 timeout = luaL_optinteger(L, 2, -1);
325 if (handle->fd < 0) luaL_error(L, "Attempt to close a closed I/O handle");
326 if (timeout != 0) {
327 if (handle->writeleft) {
328 lua_pushcfunction(L, moonbr_io_flush);
329 lua_pushvalue(L, 1);
330 lua_call(L, 1, 2);
331 if (!lua_toboolean(L, -2)) {
332 close(handle->fd);
333 handle->fd = -1;
334 return 2;
335 }
336 }
337 moonbr_io_handle_set_linger(L, handle, timeout);
338 }
339 if (close(handle->fd)) {
340 moonbr_io_errmsg();
341 handle->fd = -1;
342 lua_pushnil(L);
343 lua_pushstring(L, errmsg);
344 return 2;
345 }
346 handle->fd = -1;
347 lua_pushboolean(L, 1);
348 return 1;
350 }
352 static int moonbr_io_reset(lua_State *L) {
353 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
354 lua_settop(L, 1);
355 lua_pushcfunction(L, moonbr_io_close);
356 lua_insert(L, 1);
357 lua_pushinteger(L, 0);
358 lua_call(L, 2, LUA_MULTRET);
359 return lua_gettop(L);
360 }
362 static int moonbr_io_gc(lua_State *L) {
363 moonbr_io_handle_t *handle;
364 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
365 if (handle->fd >= 0) {
366 lua_pushcfunction(L, moonbr_io_close);
367 lua_pushvalue(L, 1);
368 lua_pushinteger(L, 0);
369 lua_call(L, 2, 0);
370 }
371 return 0;
372 }
374 void moonbr_io_closehandle(lua_State *L, int idx, int timeout) {
375 moonbr_io_handle_t *handle;
376 handle = luaL_checkudata(L, idx, MOONBR_IO_HANDLE_MT_REGKEY);
377 if (handle->fd >= 0) {
378 lua_pushcfunction(L, moonbr_io_close);
379 lua_pushvalue(L, idx);
380 lua_pushinteger(L, timeout);
381 lua_call(L, 2, 0);
382 }
383 }
385 void moonbr_io_pushhandle(lua_State *L, int fd, int issocket, int canshutdown) {
386 moonbr_io_handle_t *handle;
387 handle = lua_newuserdata(L, sizeof(moonbr_io_handle_t));
388 handle->fd = fd;
389 handle->issocket = issocket;
390 handle->canshutdown = canshutdown;
391 handle->shutwr = 0;
392 handle->nonblocking = -1;
393 handle->readerr = 0;
394 handle->readbufcnt = 0;
395 handle->writeerr = 0;
396 handle->writeleft = 0;
397 handle->writeqin = 0;
398 handle->writeqout = 0;
399 handle->writeqoff = 0;
400 handle->writebufcnt = 0;
401 moonbr_io_handle_set_linger(L, handle, 0);
402 luaL_getmetatable(L, MOONBR_IO_HANDLE_MT_REGKEY);
403 lua_setmetatable(L, -2);
404 lua_newtable(L); // uservalue
405 lua_newtable(L);
406 lua_setfield(L, -2, "writebuf");
407 lua_newtable(L); // public
408 luaL_getmetatable(L, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
409 lua_setmetatable(L, -2);
410 lua_setfield(L, -2, "public");
411 lua_setuservalue(L, -2);
412 }
414 static int moonbr_io_handleindex(lua_State *L) {
415 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
416 lua_getuservalue(L, 1);
417 lua_getfield(L, -1, "public");
418 lua_pushvalue(L, 2);
419 lua_gettable(L, -2);
420 return 1;
421 }
423 static int moonbr_io_handlenewindex(lua_State *L) {
424 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
425 lua_getuservalue(L, 1);
426 lua_getfield(L, -1, "public");
427 lua_pushvalue(L, 2);
428 lua_pushvalue(L, 3);
429 lua_settable(L, -3);
430 return 0;
431 }
433 static const struct luaL_Reg moonbr_io_handle_methods[] = {
434 {"read", moonbr_io_read},
435 {"read_nb", moonbr_io_read_nb},
436 {"drain", moonbr_io_drain},
437 {"drain_nb", moonbr_io_drain_nb},
438 {"write", moonbr_io_write},
439 {"write_nb", moonbr_io_write_nb},
440 {"flush", moonbr_io_flush},
441 {"flush_nb", moonbr_io_flush_nb},
442 {"finish", moonbr_io_finish},
443 {"close", moonbr_io_close},
444 {"reset", moonbr_io_reset},
445 {NULL, NULL}
446 };
448 static const struct luaL_Reg moonbr_io_handle_metamethods[] = {
449 {"__index", moonbr_io_handleindex},
450 {"__newindex", moonbr_io_handlenewindex},
451 {"__gc", moonbr_io_gc},
452 {NULL, NULL}
453 };
455 static const struct luaL_Reg moonbr_io_module_funcs[] = {
456 {NULL, NULL}
457 };
459 int luaopen_moonbridge_io(lua_State *L) {
461 lua_newtable(L); // module
463 lua_newtable(L); // public metatable
464 lua_newtable(L); // handle methods
465 luaL_setfuncs(L, moonbr_io_handle_methods, 0);
466 lua_pushvalue(L, -1);
467 lua_setfield(L, -4, "handle");
468 lua_setfield(L, -2, "__index");
469 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
471 lua_newtable(L); // handle metatable
472 luaL_setfuncs(L, moonbr_io_handle_metamethods, 0);
473 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_MT_REGKEY);
475 luaL_setfuncs(L, moonbr_io_module_funcs, 0);
476 return 1;
478 }

Impressum / About Us