moonbridge

view moonbridge_io.c @ 83:697cdf8e2000

Code cleanup and fixes in optionally non-blocking write/write_nb method; Allow to mark I/O handles as closed
author jbe
date Mon Apr 06 15:02:15 2015 +0200 (2015-04-06)
parents 069ee3f5ab17
children 8a6e2a80fcad
line source
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <stdint.h>
5 #include <errno.h>
6 #include <string.h>
7 #include <stdio.h>
8 #include <time.h>
9 #include <sys/time.h>
10 #include <sys/socket.h>
11 #include <sys/select.h>
12 #include <fcntl.h>
14 #include <lua.h>
15 #include <lauxlib.h>
16 #include <lualib.h>
18 #define MOONBR_IO_MAXSTRERRORLEN 80
19 #define MOONBR_IO_WRITEBUFLEN 4096
21 #define moonbr_io_errmsg() \
22 char errmsg[MOONBR_IO_MAXSTRERRORLEN]; \
23 strerror_r(errno, errmsg, MOONBR_IO_MAXSTRERRORLEN)
26 #define MOONBR_IO_HANDLE_MT_REGKEY "moonbridge_io_handle"
27 #define MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY "moonbridge_io_handle_public"
29 typedef struct {
30 int fd;
31 int nonblocking;
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 writebuf[MOONBR_IO_WRITEBUFLEN];
44 } moonbr_io_handle_t;
46 int moonbr_io_isclosed(lua_State *L, int idx) {
47 moonbr_io_handle_t *handle;
48 handle = luaL_checkudata(L, idx, MOONBR_IO_HANDLE_MT_REGKEY);
49 return handle->fd < 0;
50 }
52 void moonbr_io_markclosed(lua_State *L, int idx) {
53 moonbr_io_handle_t *handle;
54 handle = luaL_checkudata(L, idx, MOONBR_IO_HANDLE_MT_REGKEY);
55 handle->fd = -1;
56 }
58 static void moonbr_io_handle_set_nonblocking(lua_State *L, moonbr_io_handle_t *handle, int nonblocking) {
59 if (handle->nonblocking != nonblocking) {
60 int flags;
61 flags = fcntl(handle->fd, F_GETFL, 0);
62 if (flags == -1) {
63 moonbr_io_errmsg();
64 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
65 }
66 if (nonblocking) flags |= O_NONBLOCK;
67 else flags &= ~O_NONBLOCK;
68 if (fcntl(handle->fd, F_SETFL, flags) == -1) {
69 moonbr_io_errmsg();
70 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
71 }
72 }
73 }
75 static int moonbr_io_write_impl(lua_State *L, int nonblocking, int flush) {
76 moonbr_io_handle_t *handle;
77 int i, top;
78 const char *str;
79 size_t strlen, strpos;
80 size_t written;
81 ssize_t result;
82 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
83 if (handle->fd < 0) luaL_error(L, "Attempt to write to a closed I/O handle");
84 if (handle->writeerr) {
85 lua_pushnil(L);
86 lua_pushliteral(L, "Previous write error");
87 return 2;
88 }
89 moonbr_io_handle_set_nonblocking(L, handle, nonblocking);
90 if (!flush) top = lua_gettop(L);
91 lua_getuservalue(L, 1);
92 lua_getfield(L, -1, "writebuf");
93 if (!flush) {
94 for (i=2; i<=top; i++) {
95 luaL_checklstring(L, i, &strlen);
96 lua_pushvalue(L, i);
97 lua_rawseti(L, -2, handle->writeqin++);
98 handle->writeleft += strlen;
99 }
100 }
101 while (handle->writeqout != handle->writeqin) {
102 lua_rawgeti(L, -1, handle->writeqout);
103 str = lua_tolstring(L, -1, &strlen);
104 strpos = handle->writeqoff;
105 while (strpos < strlen) {
106 if (strlen - strpos < MOONBR_IO_WRITEBUFLEN - handle->writebufcnt) {
107 memcpy(handle->writebuf + handle->writebufcnt, str + strpos, strlen - strpos);
108 handle->writebufcnt += strlen - strpos;
109 break;
110 } else {
111 written = 0;
112 memcpy(handle->writebuf + handle->writebufcnt, str + strpos, MOONBR_IO_WRITEBUFLEN - handle->writebufcnt);
113 strpos += MOONBR_IO_WRITEBUFLEN - handle->writebufcnt;
114 while (written < MOONBR_IO_WRITEBUFLEN) {
115 result = write(handle->fd, handle->writebuf + written, MOONBR_IO_WRITEBUFLEN - written);
116 if (result < 0) {
117 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
118 if (written) {
119 handle->writebufcnt -= written;
120 memmove(handle->writebuf, handle->writebuf + written, handle->writebufcnt);
121 }
122 handle->writeqoff = strpos;
123 goto moonbr_io_write_impl_block;
124 } else if (errno != EINTR) {
125 moonbr_io_errmsg();
126 handle->writeerr = 1;
127 lua_pushnil(L);
128 lua_pushstring(L, errmsg);
129 return 2;
130 }
131 } else {
132 written += result;
133 handle->writeleft -= result;
134 }
135 }
136 handle->writebufcnt = 0;
137 }
138 }
139 handle->writeqoff = 0;
140 lua_pop(L, 1);
141 lua_pushnil(L);
142 lua_rawseti(L, -2, handle->writeqout++);
143 }
144 if (flush) {
145 written = 0;
146 while (written < handle->writebufcnt) {
147 result = write(handle->fd, handle->writebuf + written, handle->writebufcnt - written);
148 if (result < 0) {
149 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
150 if (written) {
151 handle->writebufcnt -= written;
152 memmove(handle->writebuf, handle->writebuf + written, handle->writebufcnt);
153 }
154 goto moonbr_io_write_impl_block;
155 } else if (errno != EINTR) {
156 moonbr_io_errmsg();
157 handle->writeerr = -1;
158 lua_pushnil(L);
159 lua_pushstring(L, errmsg);
160 return 2;
161 }
162 } else {
163 written += result;
164 handle->writeleft -= result;
165 }
166 }
167 handle->writebufcnt = 0;
168 if (nonblocking) lua_pushinteger(L, 0);
169 } else {
170 if (nonblocking) lua_pushinteger(L, handle->writeleft - handle->writebufcnt);
171 }
172 if (!nonblocking) lua_pushvalue(L, 1);
173 return 1;
174 moonbr_io_write_impl_block:
175 if (flush) lua_pushinteger(L, handle->writeleft);
176 else lua_pushinteger(L, handle->writeleft - handle->writebufcnt);
177 return 1;
178 }
180 static int moonbr_io_write(lua_State *L) {
181 return moonbr_io_write_impl(L, 0, 0);
182 }
184 static int moonbr_io_write_nb(lua_State *L) {
185 return moonbr_io_write_impl(L, 1, 0);
186 }
188 static int moonbr_io_flush(lua_State *L) {
189 return moonbr_io_write_impl(L, 0, 1);
190 }
192 static int moonbr_io_flush_nb(lua_State *L) {
193 return moonbr_io_write_impl(L, 1, 1);
194 }
196 static int moonbr_io_close(lua_State *L) {
197 moonbr_io_handle_t *handle;
198 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
199 if (handle->fd < 0) luaL_error(L, "Attempt to close a closed I/O handle");
200 if (handle->writeleft) {
201 lua_pushcfunction(L, moonbr_io_flush);
202 lua_pushvalue(L, 1);
203 lua_call(L, 1, 2);
204 if (!lua_toboolean(L, -2)) {
205 close(handle->fd);
206 handle->fd = -1;
207 return 2;
208 }
209 }
210 if (close(handle->fd)) {
211 moonbr_io_errmsg();
212 handle->fd = -1;
213 lua_pushnil(L);
214 lua_pushstring(L, errmsg);
215 return 2;
216 }
217 handle->fd = -1;
218 lua_pushboolean(L, 1);
219 return 1;
220 }
222 void moonbr_io_pushhandle(lua_State *L, int fd, int gc_idx) {
223 moonbr_io_handle_t *handle;
224 handle = lua_newuserdata(L, sizeof(moonbr_io_handle_t));
225 handle->fd = fd;
226 handle->nonblocking = -1;
227 handle->writeerr = 0;
228 handle->writeleft = 0;
229 handle->writeqin = 0;
230 handle->writeqout = 0;
231 handle->writeqoff = 0;
232 handle->writebufcnt = 0;
233 luaL_getmetatable(L, MOONBR_IO_HANDLE_MT_REGKEY);
234 lua_setmetatable(L, -2);
235 lua_newtable(L); // uservalue
236 lua_newtable(L);
237 lua_setfield(L, -2, "writebuf");
238 if (gc_idx) lua_pushvalue(L, gc_idx);
239 else lua_pushcfunction(L, moonbr_io_close);
240 lua_setfield(L, -2, "gc");
241 lua_newtable(L); // public
242 luaL_getmetatable(L, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
243 lua_setmetatable(L, -2);
244 lua_setfield(L, -2, "public");
245 lua_setuservalue(L, -2);
246 }
248 static int moonbr_io_handleindex(lua_State *L) {
249 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
250 lua_getuservalue(L, 1);
251 lua_getfield(L, -1, "public");
252 lua_pushvalue(L, 2);
253 lua_gettable(L, -2);
254 return 1;
255 }
257 static int moonbr_io_handlenewindex(lua_State *L) {
258 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
259 lua_getuservalue(L, 1);
260 lua_getfield(L, -1, "public");
261 lua_pushvalue(L, 2);
262 lua_pushvalue(L, 3);
263 lua_settable(L, -3);
264 return 0;
265 }
267 static int moonbr_io_handlegc(lua_State *L) {
268 moonbr_io_handle_t *handle;
269 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
270 if (handle->fd >= 0) {
271 lua_getuservalue(L, 1);
272 lua_getfield(L, -1, "gc");
273 lua_pushvalue(L, 1);
274 lua_call(L, 1, 0);
275 }
276 return 0;
277 }
279 static int moonbr_io_getdummy(lua_State *L) {
280 moonbr_io_pushhandle(L, 2, 0);
281 return 1;
282 }
284 static const struct luaL_Reg moonbr_io_handle_methods[] = {
285 {"close", moonbr_io_close},
286 {"write", moonbr_io_write},
287 {"write_nb", moonbr_io_write_nb},
288 {"flush", moonbr_io_flush},
289 {"flush_nb", moonbr_io_flush_nb},
290 {NULL, NULL}
291 };
293 static const struct luaL_Reg moonbr_io_handle_metamethods[] = {
294 {"__index", moonbr_io_handleindex},
295 {"__newindex", moonbr_io_handlenewindex},
296 {"__gc", moonbr_io_handlegc},
297 {NULL, NULL}
298 };
300 static const struct luaL_Reg moonbr_io_module_funcs[] = {
301 {"getdummy", moonbr_io_getdummy},
302 {NULL, NULL}
303 };
305 int luaopen_moonbridge_io(lua_State *L) {
307 lua_newtable(L); // module
309 lua_newtable(L); // public metatable
310 lua_newtable(L); // handle methods
311 luaL_setfuncs(L, moonbr_io_handle_methods, 0);
312 lua_pushvalue(L, -1);
313 lua_setfield(L, -4, "handle");
314 lua_setfield(L, -2, "__index");
315 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
317 lua_newtable(L); // handle metatable
318 luaL_setfuncs(L, moonbr_io_handle_metamethods, 0);
319 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_MT_REGKEY);
321 luaL_setfuncs(L, moonbr_io_module_funcs, 0);
322 return 1;
324 }

Impressum / About Us