moonbridge
view moonbridge_io.c @ 86:9fa3a36733ff
New I/O methods drain and drain_nb
author | jbe |
---|---|
date | Tue Apr 07 00:47:30 2015 +0200 (2015-04-07) |
parents | ef552870f1b6 |
children | 1d91c6eedf18 |
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 nonblocking;
30 int readerr;
31 int readbufcnt;
32 int writeerr;
33 size_t writeleft;
34 #if LUA_VERSION_NUM >= 503
35 lua_Integer writeqin;
36 lua_Integer writeqout;
37 #else
38 int writeqin;
39 int writeqout;
40 #endif
41 size_t writeqoff;
42 int writebufcnt;
43 char readbuf[MOONBR_IO_READBUFLEN];
44 char writebuf[MOONBR_IO_WRITEBUFLEN];
45 } moonbr_io_handle_t;
47 static void moonbr_io_handle_set_nonblocking(lua_State *L, moonbr_io_handle_t *handle, int nonblocking) {
48 if (handle->nonblocking != nonblocking) {
49 int flags;
50 flags = fcntl(handle->fd, F_GETFL, 0);
51 if (flags == -1) {
52 moonbr_io_errmsg();
53 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
54 }
55 if (nonblocking) flags |= O_NONBLOCK;
56 else flags &= ~O_NONBLOCK;
57 if (fcntl(handle->fd, F_SETFL, flags) == -1) {
58 moonbr_io_errmsg();
59 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
60 }
61 }
62 }
64 static int moonbr_io_read_impl(lua_State *L, int nonblocking, int drain) {
65 moonbr_io_handle_t *handle;
66 lua_Integer maxread;
67 const char *terminatorstr;
68 size_t terminatorlen;
69 char terminator;
70 luaL_Buffer luabuf;
71 size_t luabufcnt = 0;
72 int endcnt;
73 char *terminatorpos;
74 ssize_t result;
75 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
76 maxread = luaL_optinteger(L, 2, 0);
77 terminatorstr = luaL_optlstring(L, 3, "", &terminatorlen);
78 if (terminatorlen) {
79 luaL_argcheck(L, terminatorlen == 1, 3, "single byte expected");
80 terminator = terminatorstr[0];
81 }
82 lua_settop(L, 1); /* return handle on drain, terminator string may be garbage collected */
83 if (handle->fd < 0) luaL_error(L, "Attempt to read from a closed I/O handle");
84 if (handle->readerr) {
85 lua_pushnil(L);
86 lua_pushliteral(L, "Previous read error");
87 return 2;
88 }
89 moonbr_io_handle_set_nonblocking(L, handle, nonblocking);
90 if (!drain) luaL_buffinit(L, &luabuf);
91 while (1) {
92 endcnt = -1;
93 if (maxread > 0 && handle->readbufcnt >= maxread - luabufcnt) {
94 endcnt = maxread - luabufcnt;
95 } else if (terminatorlen) {
96 terminatorpos = memchr(handle->readbuf, terminator, handle->readbufcnt);
97 if (terminatorpos) endcnt = 1 + (terminatorpos - handle->readbuf);
98 }
99 if (endcnt >= 0) {
100 if (!drain) {
101 luaL_addlstring(&luabuf, handle->readbuf, endcnt);
102 luaL_pushresult(&luabuf);
103 }
104 handle->readbufcnt -= endcnt;
105 memmove(handle->readbuf, handle->readbuf + endcnt, handle->readbufcnt);
106 return 1;
107 }
108 if (!drain) luaL_addlstring(&luabuf, handle->readbuf, handle->readbufcnt);
109 luabufcnt += handle->readbufcnt;
110 handle->readbufcnt = 0;
111 do {
112 result = read(handle->fd, handle->readbuf, MOONBR_IO_READBUFLEN);
113 } while (result < 0 && (errno == EINTR));
114 if (result == 0 || (nonblocking && result < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))) break;
115 if (result < 0) {
116 moonbr_io_errmsg();
117 handle->readerr = 1;
118 lua_pushnil(L);
119 lua_pushstring(L, errmsg);
120 return 2;
121 }
122 handle->readbufcnt += result;
123 }
124 if (!drain) {
125 luaL_addlstring(&luabuf, handle->readbuf, handle->readbufcnt);
126 luaL_pushresult(&luabuf);
127 }
128 handle->readbufcnt = 0;
129 return 1;
130 }
132 static int moonbr_io_read(lua_State *L) {
133 return moonbr_io_read_impl(L, 0, 0);
134 }
136 static int moonbr_io_read_nb(lua_State *L) {
137 return moonbr_io_read_impl(L, 1, 0);
138 }
140 static int moonbr_io_drain(lua_State *L) {
141 return moonbr_io_read_impl(L, 0, 1);
142 }
144 static int moonbr_io_drain_nb(lua_State *L) {
145 return moonbr_io_read_impl(L, 1, 1);
146 }
148 static int moonbr_io_write_impl(lua_State *L, int nonblocking, int flush) {
149 moonbr_io_handle_t *handle;
150 int i, top;
151 const char *str;
152 size_t strlen, strpos;
153 size_t written;
154 ssize_t result;
155 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
156 if (handle->fd < 0) luaL_error(L, "Attempt to write to a closed I/O handle");
157 if (handle->writeerr) {
158 lua_pushnil(L);
159 lua_pushliteral(L, "Previous write error");
160 return 2;
161 }
162 moonbr_io_handle_set_nonblocking(L, handle, nonblocking);
163 top = lua_gettop(L);
164 lua_getuservalue(L, 1);
165 lua_getfield(L, -1, "writebuf");
166 for (i=2; i<=top; i++) {
167 luaL_checklstring(L, i, &strlen);
168 lua_pushvalue(L, i);
169 lua_rawseti(L, -2, handle->writeqin++);
170 handle->writeleft += strlen;
171 }
172 while (handle->writeqout != handle->writeqin) {
173 lua_rawgeti(L, -1, handle->writeqout);
174 str = lua_tolstring(L, -1, &strlen);
175 strpos = handle->writeqoff;
176 while (strpos < strlen) {
177 if (strlen - strpos < MOONBR_IO_WRITEBUFLEN - handle->writebufcnt) {
178 memcpy(handle->writebuf + handle->writebufcnt, str + strpos, strlen - strpos);
179 handle->writebufcnt += strlen - strpos;
180 break;
181 } else {
182 written = 0;
183 memcpy(handle->writebuf + handle->writebufcnt, str + strpos, MOONBR_IO_WRITEBUFLEN - handle->writebufcnt);
184 strpos += MOONBR_IO_WRITEBUFLEN - handle->writebufcnt;
185 while (written < MOONBR_IO_WRITEBUFLEN) {
186 result = write(handle->fd, handle->writebuf + written, MOONBR_IO_WRITEBUFLEN - written);
187 if (result < 0) {
188 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
189 if (written) {
190 handle->writebufcnt -= written;
191 memmove(handle->writebuf, handle->writebuf + written, handle->writebufcnt);
192 }
193 handle->writeqoff = strpos;
194 goto moonbr_io_write_impl_block;
195 } else if (errno != EINTR) {
196 moonbr_io_errmsg();
197 handle->writeerr = 1;
198 lua_pushnil(L);
199 lua_pushstring(L, errmsg);
200 return 2;
201 }
202 } else {
203 written += result;
204 handle->writeleft -= result;
205 }
206 }
207 handle->writebufcnt = 0;
208 }
209 }
210 handle->writeqoff = 0;
211 lua_pop(L, 1);
212 lua_pushnil(L);
213 lua_rawseti(L, -2, handle->writeqout++);
214 }
215 if (flush) {
216 written = 0;
217 while (written < handle->writebufcnt) {
218 result = write(handle->fd, handle->writebuf + written, handle->writebufcnt - written);
219 if (result < 0) {
220 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
221 if (written) {
222 handle->writebufcnt -= written;
223 memmove(handle->writebuf, handle->writebuf + written, handle->writebufcnt);
224 }
225 goto moonbr_io_write_impl_block;
226 } else if (errno != EINTR) {
227 moonbr_io_errmsg();
228 handle->writeerr = -1;
229 lua_pushnil(L);
230 lua_pushstring(L, errmsg);
231 return 2;
232 }
233 } else {
234 written += result;
235 handle->writeleft -= result;
236 }
237 }
238 handle->writebufcnt = 0;
239 if (nonblocking) lua_pushinteger(L, 0);
240 } else {
241 if (nonblocking) lua_pushinteger(L, handle->writeleft - handle->writebufcnt);
242 }
243 if (!nonblocking) lua_pushvalue(L, 1);
244 return 1;
245 moonbr_io_write_impl_block:
246 if (flush) lua_pushinteger(L, handle->writeleft);
247 else lua_pushinteger(L, handle->writeleft - handle->writebufcnt);
248 return 1;
249 }
251 static int moonbr_io_write(lua_State *L) {
252 return moonbr_io_write_impl(L, 0, 0);
253 }
255 static int moonbr_io_write_nb(lua_State *L) {
256 return moonbr_io_write_impl(L, 1, 0);
257 }
259 static int moonbr_io_flush(lua_State *L) {
260 return moonbr_io_write_impl(L, 0, 1);
261 }
263 static int moonbr_io_flush_nb(lua_State *L) {
264 return moonbr_io_write_impl(L, 1, 1);
265 }
267 static int moonbr_io_close_impl(lua_State *L, int clean) {
268 moonbr_io_handle_t *handle;
269 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
270 if (handle->fd < 0) luaL_error(L, "Attempt to close a closed I/O handle");
271 if (clean && handle->writeleft) {
272 lua_pushcfunction(L, moonbr_io_flush);
273 lua_pushvalue(L, 1);
274 lua_call(L, 1, 2);
275 if (!lua_toboolean(L, -2)) {
276 close(handle->fd);
277 handle->fd = -1;
278 return 2;
279 }
280 }
281 if (close(handle->fd)) {
282 moonbr_io_errmsg();
283 handle->fd = -1;
284 lua_pushnil(L);
285 lua_pushstring(L, errmsg);
286 return 2;
287 }
288 handle->fd = -1;
289 lua_pushboolean(L, 1);
290 return 1;
292 }
294 static int moonbr_io_reset(lua_State *L) {
295 return moonbr_io_close_impl(L, 0);
296 }
298 static int moonbr_io_close(lua_State *L) {
299 return moonbr_io_close_impl(L, 1);
300 }
302 void moonbr_io_pushhandle(lua_State *L, int fd, int issocket) {
303 moonbr_io_handle_t *handle;
304 handle = lua_newuserdata(L, sizeof(moonbr_io_handle_t));
305 handle->fd = fd;
306 handle->issocket = issocket;
307 handle->nonblocking = -1;
308 handle->readerr = 0;
309 handle->readbufcnt = 0;
310 handle->writeerr = 0;
311 handle->writeleft = 0;
312 handle->writeqin = 0;
313 handle->writeqout = 0;
314 handle->writeqoff = 0;
315 handle->writebufcnt = 0;
316 luaL_getmetatable(L, MOONBR_IO_HANDLE_MT_REGKEY);
317 lua_setmetatable(L, -2);
318 lua_newtable(L); // uservalue
319 lua_newtable(L);
320 lua_setfield(L, -2, "writebuf");
321 lua_newtable(L); // public
322 luaL_getmetatable(L, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
323 lua_setmetatable(L, -2);
324 lua_setfield(L, -2, "public");
325 lua_setuservalue(L, -2);
326 }
328 static int moonbr_io_handleindex(lua_State *L) {
329 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
330 lua_getuservalue(L, 1);
331 lua_getfield(L, -1, "public");
332 lua_pushvalue(L, 2);
333 lua_gettable(L, -2);
334 return 1;
335 }
337 static int moonbr_io_handlenewindex(lua_State *L) {
338 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
339 lua_getuservalue(L, 1);
340 lua_getfield(L, -1, "public");
341 lua_pushvalue(L, 2);
342 lua_pushvalue(L, 3);
343 lua_settable(L, -3);
344 return 0;
345 }
347 static int moonbr_io_handlegc(lua_State *L) {
348 moonbr_io_handle_t *handle;
349 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
350 if (handle->fd >= 0) {
351 lua_getuservalue(L, 1);
352 lua_getfield(L, -1, "gc");
353 lua_pushvalue(L, 1);
354 lua_call(L, 1, 0);
355 }
356 return 0;
357 }
359 static int moonbr_io_getdummy(lua_State *L) {
360 moonbr_io_pushhandle(L, 0, 0);
361 return 1;
362 }
364 static const struct luaL_Reg moonbr_io_handle_methods[] = {
365 {"read", moonbr_io_read},
366 {"read_nb", moonbr_io_read_nb},
367 {"drain", moonbr_io_drain},
368 {"drain_nb", moonbr_io_drain_nb},
369 {"write", moonbr_io_write},
370 {"write_nb", moonbr_io_write_nb},
371 {"flush", moonbr_io_flush},
372 {"flush_nb", moonbr_io_flush_nb},
373 {"reset", moonbr_io_reset},
374 {"close", moonbr_io_close},
375 {NULL, NULL}
376 };
378 static const struct luaL_Reg moonbr_io_handle_metamethods[] = {
379 {"__index", moonbr_io_handleindex},
380 {"__newindex", moonbr_io_handlenewindex},
381 {"__gc", moonbr_io_handlegc},
382 {NULL, NULL}
383 };
385 static const struct luaL_Reg moonbr_io_module_funcs[] = {
386 {"getdummy", moonbr_io_getdummy},
387 {NULL, NULL}
388 };
390 int luaopen_moonbridge_io(lua_State *L) {
392 lua_newtable(L); // module
394 lua_newtable(L); // public metatable
395 lua_newtable(L); // handle methods
396 luaL_setfuncs(L, moonbr_io_handle_methods, 0);
397 lua_pushvalue(L, -1);
398 lua_setfield(L, -4, "handle");
399 lua_setfield(L, -2, "__index");
400 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
402 lua_newtable(L); // handle metatable
403 luaL_setfuncs(L, moonbr_io_handle_metamethods, 0);
404 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_MT_REGKEY);
406 luaL_setfuncs(L, moonbr_io_module_funcs, 0);
407 return 1;
409 }