moonbridge

annotate moonbridge_io.c @ 84:8a6e2a80fcad

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

Impressum / About Us