moonbridge
view moonbridge_io.c @ 90:54e6104c70a6
Proper return values for drain functions
author | jbe |
---|---|
date | Tue Apr 07 02:52:41 2015 +0200 (2015-04-07) |
parents | fca51922b708 |
children | 6b26783f9323 |
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 - handle->writebufcnt);
272 }
273 if (!nonblocking) lua_pushvalue(L, 1);
274 return 1;
275 moonbr_io_write_impl_block:
276 if (flush) lua_pushinteger(L, handle->writeleft);
277 else lua_pushinteger(L, handle->writeleft - handle->writebufcnt);
278 return 1;
279 }
281 static int moonbr_io_write(lua_State *L) {
282 return moonbr_io_write_impl(L, 0, 0);
283 }
285 static int moonbr_io_write_nb(lua_State *L) {
286 return moonbr_io_write_impl(L, 1, 0);
287 }
289 static int moonbr_io_flush(lua_State *L) {
290 return moonbr_io_write_impl(L, 0, 1);
291 }
293 static int moonbr_io_flush_nb(lua_State *L) {
294 return moonbr_io_write_impl(L, 1, 1);
295 }
297 static int moonbr_io_finish(lua_State *L) {
298 moonbr_io_handle_t *handle;
299 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
300 if (handle->fd < 0) luaL_error(L, "Attempt to finish a closed I/O handle");
301 if (handle->shutwr) luaL_error(L, "Attempt to finish a finished I/O handle");
302 handle->shutwr = 1;
303 if (handle->canshutdown) {
304 if (handle->writeleft) {
305 lua_pushcfunction(L, moonbr_io_flush);
306 lua_pushvalue(L, 1);
307 lua_call(L, 1, 2);
308 if (!lua_toboolean(L, -2)) return 2;
309 }
310 if (shutdown(handle->fd, SHUT_WR)) {
311 moonbr_io_errmsg();
312 lua_pushnil(L);
313 lua_pushstring(L, errmsg);
314 return 2;
315 }
316 }
317 lua_pushboolean(L, 1);
318 return 1;
319 }
321 static int moonbr_io_close(lua_State *L) {
322 moonbr_io_handle_t *handle;
323 int timeout;
324 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
325 timeout = luaL_optinteger(L, 2, -1);
326 if (handle->fd < 0) luaL_error(L, "Attempt to close a closed I/O handle");
327 if (timeout != 0) {
328 if (handle->writeleft) {
329 lua_pushcfunction(L, moonbr_io_flush);
330 lua_pushvalue(L, 1);
331 lua_call(L, 1, 2);
332 if (!lua_toboolean(L, -2)) {
333 close(handle->fd);
334 handle->fd = -1;
335 return 2;
336 }
337 }
338 moonbr_io_handle_set_linger(L, handle, timeout);
339 }
340 if (close(handle->fd)) {
341 moonbr_io_errmsg();
342 handle->fd = -1;
343 lua_pushnil(L);
344 lua_pushstring(L, errmsg);
345 return 2;
346 }
347 handle->fd = -1;
348 lua_pushboolean(L, 1);
349 return 1;
351 }
353 static int moonbr_io_reset(lua_State *L) {
354 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
355 lua_settop(L, 1);
356 lua_pushcfunction(L, moonbr_io_close);
357 lua_insert(L, 1);
358 lua_pushinteger(L, 0);
359 lua_call(L, 2, LUA_MULTRET);
360 return lua_gettop(L);
361 }
363 static int moonbr_io_gc(lua_State *L) {
364 moonbr_io_handle_t *handle;
365 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
366 if (handle->fd >= 0) {
367 lua_pushcfunction(L, moonbr_io_close);
368 lua_pushvalue(L, 1);
369 lua_pushinteger(L, 0);
370 lua_call(L, 2, 0);
371 }
372 return 0;
373 }
375 void moonbr_io_closehandle(lua_State *L, int idx, int timeout) {
376 moonbr_io_handle_t *handle;
377 handle = luaL_checkudata(L, idx, MOONBR_IO_HANDLE_MT_REGKEY);
378 if (handle->fd >= 0) {
379 lua_pushcfunction(L, moonbr_io_close);
380 lua_pushvalue(L, idx);
381 lua_pushinteger(L, timeout);
382 lua_call(L, 2, 0);
383 }
384 }
386 void moonbr_io_pushhandle(lua_State *L, int fd, int issocket, int canshutdown) {
387 moonbr_io_handle_t *handle;
388 handle = lua_newuserdata(L, sizeof(moonbr_io_handle_t));
389 handle->fd = fd;
390 handle->issocket = issocket;
391 handle->canshutdown = canshutdown;
392 handle->shutwr = 0;
393 handle->nonblocking = -1;
394 handle->readerr = 0;
395 handle->readbufcnt = 0;
396 handle->writeerr = 0;
397 handle->writeleft = 0;
398 handle->writeqin = 0;
399 handle->writeqout = 0;
400 handle->writeqoff = 0;
401 handle->writebufcnt = 0;
402 moonbr_io_handle_set_linger(L, handle, 0);
403 luaL_getmetatable(L, MOONBR_IO_HANDLE_MT_REGKEY);
404 lua_setmetatable(L, -2);
405 lua_newtable(L); // uservalue
406 lua_newtable(L);
407 lua_setfield(L, -2, "writebuf");
408 lua_newtable(L); // public
409 luaL_getmetatable(L, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
410 lua_setmetatable(L, -2);
411 lua_setfield(L, -2, "public");
412 lua_setuservalue(L, -2);
413 }
415 static int moonbr_io_handleindex(lua_State *L) {
416 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
417 lua_getuservalue(L, 1);
418 lua_getfield(L, -1, "public");
419 lua_pushvalue(L, 2);
420 lua_gettable(L, -2);
421 return 1;
422 }
424 static int moonbr_io_handlenewindex(lua_State *L) {
425 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
426 lua_getuservalue(L, 1);
427 lua_getfield(L, -1, "public");
428 lua_pushvalue(L, 2);
429 lua_pushvalue(L, 3);
430 lua_settable(L, -3);
431 return 0;
432 }
434 static const struct luaL_Reg moonbr_io_handle_methods[] = {
435 {"read", moonbr_io_read},
436 {"read_nb", moonbr_io_read_nb},
437 {"drain", moonbr_io_drain},
438 {"drain_nb", moonbr_io_drain_nb},
439 {"write", moonbr_io_write},
440 {"write_nb", moonbr_io_write_nb},
441 {"flush", moonbr_io_flush},
442 {"flush_nb", moonbr_io_flush_nb},
443 {"finish", moonbr_io_finish},
444 {"close", moonbr_io_close},
445 {"reset", moonbr_io_reset},
446 {NULL, NULL}
447 };
449 static const struct luaL_Reg moonbr_io_handle_metamethods[] = {
450 {"__index", moonbr_io_handleindex},
451 {"__newindex", moonbr_io_handlenewindex},
452 {"__gc", moonbr_io_gc},
453 {NULL, NULL}
454 };
456 static const struct luaL_Reg moonbr_io_module_funcs[] = {
457 {NULL, NULL}
458 };
460 int luaopen_moonbridge_io(lua_State *L) {
462 lua_newtable(L); // module
464 lua_newtable(L); // public metatable
465 lua_newtable(L); // handle methods
466 luaL_setfuncs(L, moonbr_io_handle_methods, 0);
467 lua_pushvalue(L, -1);
468 lua_setfield(L, -4, "handle");
469 lua_setfield(L, -2, "__index");
470 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
472 lua_newtable(L); // handle metatable
473 luaL_setfuncs(L, moonbr_io_handle_metamethods, 0);
474 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_MT_REGKEY);
476 luaL_setfuncs(L, moonbr_io_module_funcs, 0);
477 return 1;
479 }