moonbridge

view moonbridge_io.c @ 89:c4fd976d9537

Register I/O library as moonbridge_io (and not mapping functions into io)
author jbe
date Tue Apr 07 02:52:24 2015 +0200 (2015-04-07)
parents fca51922b708
children 54e6104c70a6
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 }
119 handle->readbufcnt -= endcnt;
120 memmove(handle->readbuf, handle->readbuf + endcnt, handle->readbufcnt);
121 return 1;
122 }
123 if (!drain) luaL_addlstring(&luabuf, handle->readbuf, handle->readbufcnt);
124 luabufcnt += handle->readbufcnt;
125 handle->readbufcnt = 0;
126 do {
127 result = read(handle->fd, handle->readbuf, MOONBR_IO_READBUFLEN);
128 } while (result < 0 && (errno == EINTR));
129 if (result == 0 || (nonblocking && result < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))) break;
130 if (result < 0) {
131 moonbr_io_errmsg();
132 handle->readerr = 1;
133 lua_pushnil(L);
134 lua_pushstring(L, errmsg);
135 return 2;
136 }
137 handle->readbufcnt += result;
138 }
139 if (!drain) {
140 luaL_addlstring(&luabuf, handle->readbuf, handle->readbufcnt);
141 luaL_pushresult(&luabuf);
142 }
143 handle->readbufcnt = 0;
144 return 1;
145 }
147 static int moonbr_io_read(lua_State *L) {
148 return moonbr_io_read_impl(L, 0, 0);
149 }
151 static int moonbr_io_read_nb(lua_State *L) {
152 return moonbr_io_read_impl(L, 1, 0);
153 }
155 static int moonbr_io_drain(lua_State *L) {
156 return moonbr_io_read_impl(L, 0, 1);
157 }
159 static int moonbr_io_drain_nb(lua_State *L) {
160 return moonbr_io_read_impl(L, 1, 1);
161 }
163 static int moonbr_io_write_impl(lua_State *L, int nonblocking, int flush) {
164 moonbr_io_handle_t *handle;
165 int i, top;
166 const char *str;
167 size_t strlen, strpos;
168 size_t written;
169 ssize_t result;
170 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
171 if (handle->fd < 0) luaL_error(L, "Attempt to write to a closed I/O handle");
172 if (handle->shutwr) luaL_error(L, "Attempt to write to a finished I/O handle");
173 if (handle->writeerr) {
174 lua_pushnil(L);
175 lua_pushliteral(L, "Previous write error");
176 return 2;
177 }
178 moonbr_io_handle_set_nonblocking(L, handle, nonblocking);
179 top = lua_gettop(L);
180 lua_getuservalue(L, 1);
181 lua_getfield(L, -1, "writebuf");
182 for (i=2; i<=top; i++) {
183 luaL_checklstring(L, i, &strlen);
184 lua_pushvalue(L, i);
185 lua_rawseti(L, -2, handle->writeqin++);
186 handle->writeleft += strlen;
187 }
188 while (handle->writeqout != handle->writeqin) {
189 lua_rawgeti(L, -1, handle->writeqout);
190 str = lua_tolstring(L, -1, &strlen);
191 strpos = handle->writeqoff;
192 while (strpos < strlen) {
193 if (strlen - strpos < MOONBR_IO_WRITEBUFLEN - handle->writebufcnt) {
194 memcpy(handle->writebuf + handle->writebufcnt, str + strpos, strlen - strpos);
195 handle->writebufcnt += strlen - strpos;
196 break;
197 } else {
198 written = 0;
199 memcpy(handle->writebuf + handle->writebufcnt, str + strpos, MOONBR_IO_WRITEBUFLEN - handle->writebufcnt);
200 strpos += MOONBR_IO_WRITEBUFLEN - handle->writebufcnt;
201 while (written < MOONBR_IO_WRITEBUFLEN) {
202 result = write(handle->fd, handle->writebuf + written, MOONBR_IO_WRITEBUFLEN - written);
203 if (result < 0) {
204 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
205 if (written) {
206 handle->writebufcnt -= written;
207 memmove(handle->writebuf, handle->writebuf + written, handle->writebufcnt);
208 }
209 handle->writeqoff = strpos;
210 goto moonbr_io_write_impl_block;
211 } else if (errno != EINTR) {
212 moonbr_io_errmsg();
213 handle->writeerr = 1;
214 lua_pushnil(L);
215 lua_pushstring(L, errmsg);
216 return 2;
217 }
218 } else {
219 written += result;
220 handle->writeleft -= result;
221 }
222 }
223 handle->writebufcnt = 0;
224 }
225 }
226 handle->writeqoff = 0;
227 lua_pop(L, 1);
228 lua_pushnil(L);
229 lua_rawseti(L, -2, handle->writeqout++);
230 }
231 if (flush) {
232 written = 0;
233 while (written < handle->writebufcnt) {
234 result = write(handle->fd, handle->writebuf + written, handle->writebufcnt - written);
235 if (result < 0) {
236 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
237 if (written) {
238 handle->writebufcnt -= written;
239 memmove(handle->writebuf, handle->writebuf + written, handle->writebufcnt);
240 }
241 goto moonbr_io_write_impl_block;
242 } else if (errno != EINTR) {
243 moonbr_io_errmsg();
244 handle->writeerr = -1;
245 lua_pushnil(L);
246 lua_pushstring(L, errmsg);
247 return 2;
248 }
249 } else {
250 written += result;
251 handle->writeleft -= result;
252 }
253 }
254 handle->writebufcnt = 0;
255 if (nonblocking) lua_pushinteger(L, 0);
256 } else {
257 if (nonblocking) lua_pushinteger(L, handle->writeleft - handle->writebufcnt);
258 }
259 if (!nonblocking) lua_pushvalue(L, 1);
260 return 1;
261 moonbr_io_write_impl_block:
262 if (flush) lua_pushinteger(L, handle->writeleft);
263 else lua_pushinteger(L, handle->writeleft - handle->writebufcnt);
264 return 1;
265 }
267 static int moonbr_io_write(lua_State *L) {
268 return moonbr_io_write_impl(L, 0, 0);
269 }
271 static int moonbr_io_write_nb(lua_State *L) {
272 return moonbr_io_write_impl(L, 1, 0);
273 }
275 static int moonbr_io_flush(lua_State *L) {
276 return moonbr_io_write_impl(L, 0, 1);
277 }
279 static int moonbr_io_flush_nb(lua_State *L) {
280 return moonbr_io_write_impl(L, 1, 1);
281 }
283 static int moonbr_io_finish(lua_State *L) {
284 moonbr_io_handle_t *handle;
285 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
286 if (handle->fd < 0) luaL_error(L, "Attempt to finish a closed I/O handle");
287 if (handle->shutwr) luaL_error(L, "Attempt to finish a finished I/O handle");
288 handle->shutwr = 1;
289 if (handle->canshutdown) {
290 if (handle->writeleft) {
291 lua_pushcfunction(L, moonbr_io_flush);
292 lua_pushvalue(L, 1);
293 lua_call(L, 1, 2);
294 if (!lua_toboolean(L, -2)) return 2;
295 }
296 if (shutdown(handle->fd, SHUT_WR)) {
297 moonbr_io_errmsg();
298 lua_pushnil(L);
299 lua_pushstring(L, errmsg);
300 return 2;
301 }
302 }
303 lua_pushboolean(L, 1);
304 return 1;
305 }
307 static int moonbr_io_close(lua_State *L) {
308 moonbr_io_handle_t *handle;
309 int timeout;
310 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
311 timeout = luaL_optinteger(L, 2, -1);
312 if (handle->fd < 0) luaL_error(L, "Attempt to close a closed I/O handle");
313 if (timeout != 0) {
314 if (handle->writeleft) {
315 lua_pushcfunction(L, moonbr_io_flush);
316 lua_pushvalue(L, 1);
317 lua_call(L, 1, 2);
318 if (!lua_toboolean(L, -2)) {
319 close(handle->fd);
320 handle->fd = -1;
321 return 2;
322 }
323 }
324 moonbr_io_handle_set_linger(L, handle, timeout);
325 }
326 if (close(handle->fd)) {
327 moonbr_io_errmsg();
328 handle->fd = -1;
329 lua_pushnil(L);
330 lua_pushstring(L, errmsg);
331 return 2;
332 }
333 handle->fd = -1;
334 lua_pushboolean(L, 1);
335 return 1;
337 }
339 static int moonbr_io_reset(lua_State *L) {
340 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
341 lua_settop(L, 1);
342 lua_pushcfunction(L, moonbr_io_close);
343 lua_insert(L, 1);
344 lua_pushinteger(L, 0);
345 lua_call(L, 2, LUA_MULTRET);
346 return lua_gettop(L);
347 }
349 static int moonbr_io_gc(lua_State *L) {
350 moonbr_io_handle_t *handle;
351 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
352 if (handle->fd >= 0) {
353 lua_pushcfunction(L, moonbr_io_close);
354 lua_pushvalue(L, 1);
355 lua_pushinteger(L, 0);
356 lua_call(L, 2, 0);
357 }
358 return 0;
359 }
361 void moonbr_io_closehandle(lua_State *L, int idx, int timeout) {
362 moonbr_io_handle_t *handle;
363 handle = luaL_checkudata(L, idx, MOONBR_IO_HANDLE_MT_REGKEY);
364 if (handle->fd >= 0) {
365 lua_pushcfunction(L, moonbr_io_close);
366 lua_pushvalue(L, idx);
367 lua_pushinteger(L, timeout);
368 lua_call(L, 2, 0);
369 }
370 }
372 void moonbr_io_pushhandle(lua_State *L, int fd, int issocket, int canshutdown) {
373 moonbr_io_handle_t *handle;
374 handle = lua_newuserdata(L, sizeof(moonbr_io_handle_t));
375 handle->fd = fd;
376 handle->issocket = issocket;
377 handle->canshutdown = canshutdown;
378 handle->shutwr = 0;
379 handle->nonblocking = -1;
380 handle->readerr = 0;
381 handle->readbufcnt = 0;
382 handle->writeerr = 0;
383 handle->writeleft = 0;
384 handle->writeqin = 0;
385 handle->writeqout = 0;
386 handle->writeqoff = 0;
387 handle->writebufcnt = 0;
388 moonbr_io_handle_set_linger(L, handle, 0);
389 luaL_getmetatable(L, MOONBR_IO_HANDLE_MT_REGKEY);
390 lua_setmetatable(L, -2);
391 lua_newtable(L); // uservalue
392 lua_newtable(L);
393 lua_setfield(L, -2, "writebuf");
394 lua_newtable(L); // public
395 luaL_getmetatable(L, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
396 lua_setmetatable(L, -2);
397 lua_setfield(L, -2, "public");
398 lua_setuservalue(L, -2);
399 }
401 static int moonbr_io_handleindex(lua_State *L) {
402 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
403 lua_getuservalue(L, 1);
404 lua_getfield(L, -1, "public");
405 lua_pushvalue(L, 2);
406 lua_gettable(L, -2);
407 return 1;
408 }
410 static int moonbr_io_handlenewindex(lua_State *L) {
411 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
412 lua_getuservalue(L, 1);
413 lua_getfield(L, -1, "public");
414 lua_pushvalue(L, 2);
415 lua_pushvalue(L, 3);
416 lua_settable(L, -3);
417 return 0;
418 }
420 static const struct luaL_Reg moonbr_io_handle_methods[] = {
421 {"read", moonbr_io_read},
422 {"read_nb", moonbr_io_read_nb},
423 {"drain", moonbr_io_drain},
424 {"drain_nb", moonbr_io_drain_nb},
425 {"write", moonbr_io_write},
426 {"write_nb", moonbr_io_write_nb},
427 {"flush", moonbr_io_flush},
428 {"flush_nb", moonbr_io_flush_nb},
429 {"finish", moonbr_io_finish},
430 {"close", moonbr_io_close},
431 {"reset", moonbr_io_reset},
432 {NULL, NULL}
433 };
435 static const struct luaL_Reg moonbr_io_handle_metamethods[] = {
436 {"__index", moonbr_io_handleindex},
437 {"__newindex", moonbr_io_handlenewindex},
438 {"__gc", moonbr_io_gc},
439 {NULL, NULL}
440 };
442 static const struct luaL_Reg moonbr_io_module_funcs[] = {
443 {NULL, NULL}
444 };
446 int luaopen_moonbridge_io(lua_State *L) {
448 lua_newtable(L); // module
450 lua_newtable(L); // public metatable
451 lua_newtable(L); // handle methods
452 luaL_setfuncs(L, moonbr_io_handle_methods, 0);
453 lua_pushvalue(L, -1);
454 lua_setfield(L, -4, "handle");
455 lua_setfield(L, -2, "__index");
456 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
458 lua_newtable(L); // handle metatable
459 luaL_setfuncs(L, moonbr_io_handle_metamethods, 0);
460 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_MT_REGKEY);
462 luaL_setfuncs(L, moonbr_io_module_funcs, 0);
463 return 1;
465 }

Impressum / About Us