moonbridge

annotate moonbridge_io.c @ 96:fdc1bb710544

Allow full write buffer without forcing TCP PSH
author jbe
date Wed Apr 08 01:22:20 2015 +0200 (2015-04-08)
parents 719f83c7fea4
children 0561abcc68ee
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 <sys/socket.h>
jbe@79 8 #include <sys/select.h>
jbe@81 9 #include <fcntl.h>
jbe@95 10 #include <netinet/in.h>
jbe@95 11 #include <netinet/tcp.h>
jbe@79 12
jbe@79 13 #include <lua.h>
jbe@79 14 #include <lauxlib.h>
jbe@79 15 #include <lualib.h>
jbe@79 16
jbe@80 17 #define MOONBR_IO_MAXSTRERRORLEN 80
jbe@85 18 #define MOONBR_IO_READBUFLEN 4096
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@79 25 #define MOONBR_IO_HANDLE_MT_REGKEY "moonbridge_io_handle"
jbe@79 26 #define MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY "moonbridge_io_handle_public"
jbe@79 27
jbe@79 28 typedef struct {
jbe@79 29 int fd;
jbe@95 30 int isnetwork;
jbe@94 31 int finished;
jbe@94 32 int closed;
jbe@81 33 int nonblocking;
jbe@95 34 int nopush;
jbe@85 35 int readerr;
jbe@85 36 int readbufcnt;
jbe@81 37 int writeerr;
jbe@83 38 size_t writeleft;
jbe@83 39 #if LUA_VERSION_NUM >= 503
jbe@83 40 lua_Integer writeqin;
jbe@83 41 lua_Integer writeqout;
jbe@83 42 #else
jbe@83 43 int writeqin;
jbe@83 44 int writeqout;
jbe@83 45 #endif
jbe@83 46 size_t writeqoff;
jbe@81 47 int writebufcnt;
jbe@85 48 char readbuf[MOONBR_IO_READBUFLEN];
jbe@80 49 char writebuf[MOONBR_IO_WRITEBUFLEN];
jbe@79 50 } moonbr_io_handle_t;
jbe@79 51
jbe@81 52 static void moonbr_io_handle_set_nonblocking(lua_State *L, moonbr_io_handle_t *handle, int nonblocking) {
jbe@95 53 int flags;
jbe@95 54 if (handle->nonblocking == nonblocking) return;
jbe@95 55 flags = fcntl(handle->fd, F_GETFL, 0);
jbe@95 56 if (flags == -1) {
jbe@95 57 moonbr_io_errmsg();
jbe@96 58 close(handle->fd);
jbe@96 59 handle->fd = -1;
jbe@96 60 handle->closed = 1;
jbe@95 61 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
jbe@81 62 }
jbe@95 63 if (nonblocking) flags |= O_NONBLOCK;
jbe@95 64 else flags &= ~O_NONBLOCK;
jbe@95 65 if (fcntl(handle->fd, F_SETFL, flags) == -1) {
jbe@95 66 moonbr_io_errmsg();
jbe@96 67 close(handle->fd);
jbe@96 68 handle->fd = -1;
jbe@96 69 handle->closed = 1;
jbe@95 70 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
jbe@95 71 }
jbe@95 72 handle->nonblocking = nonblocking;
jbe@81 73 }
jbe@81 74
jbe@87 75 static void moonbr_io_handle_set_linger(lua_State *L, moonbr_io_handle_t *handle, int timeout) {
jbe@87 76 struct linger lingerval = { 0, };
jbe@95 77 if (!handle->isnetwork) return;
jbe@87 78 if (timeout >= 0) {
jbe@87 79 lingerval.l_onoff = 1;
jbe@87 80 lingerval.l_linger = timeout;
jbe@87 81 }
jbe@87 82 if (setsockopt(handle->fd, SOL_SOCKET, SO_LINGER, &lingerval, sizeof(lingerval))) {
jbe@87 83 moonbr_io_errmsg();
jbe@96 84 close(handle->fd);
jbe@96 85 handle->fd = -1;
jbe@96 86 handle->closed = 1;
jbe@95 87 luaL_error(L, "Unexpected error while setting SO_LINGER with setsockopt: %s", errmsg);
jbe@87 88 }
jbe@87 89 }
jbe@87 90
jbe@95 91 static void moonbr_io_handle_set_nopush(lua_State *L, moonbr_io_handle_t *handle, int nopush) {
jbe@96 92 #if defined(TCP_NOPUSH) || defined(TCP_CORK)
jbe@95 93 if (!handle->isnetwork || handle->nopush == nopush) return;
jbe@96 94 #if defined(TCP_NOPUSH)
jbe@96 95 if (setsockopt(handle->fd, IPPROTO_TCP, TCP_NOPUSH, &nopush, sizeof(nopush))) {
jbe@96 96 moonbr_io_errmsg();
jbe@96 97 close(handle->fd);
jbe@96 98 handle->fd = -1;
jbe@96 99 handle->closed = 1;
jbe@96 100 luaL_error(L, "Unexpected error while setting TCP_NOPUSH with setsockopt: %s", errmsg);
jbe@96 101 }
jbe@96 102 #elif defined(TCP_CORK)
jbe@95 103 if (setsockopt(handle->fd, IPPROTO_TCP, TCP_CORK, &nopush, sizeof(nopush))) {
jbe@95 104 moonbr_io_errmsg();
jbe@96 105 close(handle->fd);
jbe@96 106 handle->fd = -1;
jbe@96 107 handle->closed = 1;
jbe@95 108 luaL_error(L, "Unexpected error while setting TCP_CORK with setsockopt: %s", errmsg);
jbe@95 109 }
jbe@95 110 #endif
jbe@95 111 handle->nopush = nopush;
jbe@96 112 #else
jbe@96 113 #warning Neither TCP_NOPUSH nor TCP_CORK is available
jbe@96 114 #endif
jbe@95 115 }
jbe@95 116
jbe@86 117 static int moonbr_io_read_impl(lua_State *L, int nonblocking, int drain) {
jbe@85 118 moonbr_io_handle_t *handle;
jbe@85 119 lua_Integer maxread;
jbe@85 120 const char *terminatorstr;
jbe@85 121 size_t terminatorlen;
jbe@85 122 char terminator;
jbe@85 123 luaL_Buffer luabuf;
jbe@85 124 size_t luabufcnt = 0;
jbe@86 125 int endcnt;
jbe@85 126 char *terminatorpos;
jbe@85 127 ssize_t result;
jbe@85 128 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@85 129 maxread = luaL_optinteger(L, 2, 0);
jbe@85 130 terminatorstr = luaL_optlstring(L, 3, "", &terminatorlen);
jbe@85 131 if (terminatorlen) {
jbe@85 132 luaL_argcheck(L, terminatorlen == 1, 3, "single byte expected");
jbe@85 133 terminator = terminatorstr[0];
jbe@85 134 }
jbe@86 135 lua_settop(L, 1); /* return handle on drain, terminator string may be garbage collected */
jbe@94 136 if (handle->closed) luaL_error(L, "Attempt to read from a closed I/O handle");
jbe@94 137 if (handle->fd < 0) goto moonbr_io_read_impl_eof; /* fake EOF to simulate shutdown */
jbe@85 138 if (handle->readerr) {
jbe@85 139 lua_pushnil(L);
jbe@85 140 lua_pushliteral(L, "Previous read error");
jbe@85 141 return 2;
jbe@85 142 }
jbe@85 143 moonbr_io_handle_set_nonblocking(L, handle, nonblocking);
jbe@86 144 if (!drain) luaL_buffinit(L, &luabuf);
jbe@85 145 while (1) {
jbe@86 146 endcnt = -1;
jbe@85 147 if (maxread > 0 && handle->readbufcnt >= maxread - luabufcnt) {
jbe@86 148 endcnt = maxread - luabufcnt;
jbe@85 149 } else if (terminatorlen) {
jbe@85 150 terminatorpos = memchr(handle->readbuf, terminator, handle->readbufcnt);
jbe@86 151 if (terminatorpos) endcnt = 1 + (terminatorpos - handle->readbuf);
jbe@85 152 }
jbe@86 153 if (endcnt >= 0) {
jbe@86 154 if (!drain) {
jbe@86 155 luaL_addlstring(&luabuf, handle->readbuf, endcnt);
jbe@86 156 luaL_pushresult(&luabuf);
jbe@90 157 } else {
jbe@90 158 luabufcnt += handle->readbufcnt;
jbe@90 159 lua_pushinteger(L, luabufcnt);
jbe@86 160 }
jbe@86 161 handle->readbufcnt -= endcnt;
jbe@86 162 memmove(handle->readbuf, handle->readbuf + endcnt, handle->readbufcnt);
jbe@85 163 return 1;
jbe@85 164 }
jbe@86 165 if (!drain) luaL_addlstring(&luabuf, handle->readbuf, handle->readbufcnt);
jbe@85 166 luabufcnt += handle->readbufcnt;
jbe@85 167 handle->readbufcnt = 0;
jbe@85 168 do {
jbe@85 169 result = read(handle->fd, handle->readbuf, MOONBR_IO_READBUFLEN);
jbe@85 170 } while (result < 0 && (errno == EINTR));
jbe@85 171 if (result == 0 || (nonblocking && result < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))) break;
jbe@85 172 if (result < 0) {
jbe@85 173 moonbr_io_errmsg();
jbe@85 174 handle->readerr = 1;
jbe@85 175 lua_pushnil(L);
jbe@85 176 lua_pushstring(L, errmsg);
jbe@85 177 return 2;
jbe@85 178 }
jbe@85 179 handle->readbufcnt += result;
jbe@85 180 }
jbe@86 181 if (!drain) {
jbe@86 182 luaL_addlstring(&luabuf, handle->readbuf, handle->readbufcnt);
jbe@86 183 luaL_pushresult(&luabuf);
jbe@86 184 }
jbe@90 185 luabufcnt += handle->readbufcnt;
jbe@85 186 handle->readbufcnt = 0;
jbe@90 187 if (!drain) {
jbe@90 188 if (!luabufcnt && result == 0) {
jbe@94 189 moonbr_io_read_impl_eof:
jbe@90 190 lua_pushboolean(L, 0);
jbe@90 191 lua_pushliteral(L, "End of file");
jbe@90 192 return 2;
jbe@90 193 }
jbe@90 194 } else {
jbe@90 195 if (!luabufcnt && result == 0) lua_pushboolean(L, 1);
jbe@90 196 else lua_pushboolean(L, luabufcnt);
jbe@90 197 }
jbe@85 198 return 1;
jbe@85 199 }
jbe@85 200
jbe@85 201 static int moonbr_io_read(lua_State *L) {
jbe@86 202 return moonbr_io_read_impl(L, 0, 0);
jbe@85 203 }
jbe@85 204
jbe@85 205 static int moonbr_io_read_nb(lua_State *L) {
jbe@86 206 return moonbr_io_read_impl(L, 1, 0);
jbe@86 207 }
jbe@86 208
jbe@86 209 static int moonbr_io_drain(lua_State *L) {
jbe@86 210 return moonbr_io_read_impl(L, 0, 1);
jbe@86 211 }
jbe@86 212
jbe@86 213 static int moonbr_io_drain_nb(lua_State *L) {
jbe@86 214 return moonbr_io_read_impl(L, 1, 1);
jbe@85 215 }
jbe@85 216
jbe@81 217 static int moonbr_io_write_impl(lua_State *L, int nonblocking, int flush) {
jbe@80 218 moonbr_io_handle_t *handle;
jbe@80 219 int i, top;
jbe@80 220 const char *str;
jbe@92 221 size_t strlen;
jbe@80 222 size_t written;
jbe@80 223 ssize_t result;
jbe@80 224 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@94 225 if (handle->closed) luaL_error(L, "Attempt to write to a closed I/O handle");
jbe@94 226 if (handle->finished) luaL_error(L, "Attempt to write to a finished I/O handle");
jbe@81 227 if (handle->writeerr) {
jbe@80 228 lua_pushnil(L);
jbe@80 229 lua_pushliteral(L, "Previous write error");
jbe@80 230 return 2;
jbe@80 231 }
jbe@81 232 moonbr_io_handle_set_nonblocking(L, handle, nonblocking);
jbe@84 233 top = lua_gettop(L);
jbe@81 234 lua_getuservalue(L, 1);
jbe@81 235 lua_getfield(L, -1, "writebuf");
jbe@84 236 for (i=2; i<=top; i++) {
jbe@84 237 luaL_checklstring(L, i, &strlen);
jbe@84 238 lua_pushvalue(L, i);
jbe@84 239 lua_rawseti(L, -2, handle->writeqin++);
jbe@84 240 handle->writeleft += strlen;
jbe@81 241 }
jbe@83 242 while (handle->writeqout != handle->writeqin) {
jbe@83 243 lua_rawgeti(L, -1, handle->writeqout);
jbe@81 244 str = lua_tolstring(L, -1, &strlen);
jbe@92 245 while (handle->writeqoff < strlen) {
jbe@96 246 if (strlen - handle->writeqoff <= MOONBR_IO_WRITEBUFLEN - handle->writebufcnt) {
jbe@92 247 memcpy(handle->writebuf + handle->writebufcnt, str + handle->writeqoff, strlen - handle->writeqoff);
jbe@92 248 handle->writebufcnt += strlen - handle->writeqoff;
jbe@80 249 break;
jbe@80 250 } else {
jbe@96 251 moonbr_io_handle_set_nopush(L, handle, flush ? 0 : 1);
jbe@80 252 written = 0;
jbe@92 253 memcpy(handle->writebuf + handle->writebufcnt, str + handle->writeqoff, MOONBR_IO_WRITEBUFLEN - handle->writebufcnt);
jbe@92 254 handle->writeqoff += MOONBR_IO_WRITEBUFLEN - handle->writebufcnt;
jbe@80 255 while (written < MOONBR_IO_WRITEBUFLEN) {
jbe@80 256 result = write(handle->fd, handle->writebuf + written, MOONBR_IO_WRITEBUFLEN - written);
jbe@80 257 if (result < 0) {
jbe@81 258 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
jbe@81 259 if (written) {
jbe@81 260 handle->writebufcnt -= written;
jbe@81 261 memmove(handle->writebuf, handle->writebuf + written, handle->writebufcnt);
jbe@92 262 goto moonbr_io_write_impl_continue;
jbe@81 263 }
jbe@81 264 goto moonbr_io_write_impl_block;
jbe@81 265 } else if (errno != EINTR) {
jbe@80 266 moonbr_io_errmsg();
jbe@81 267 handle->writeerr = 1;
jbe@80 268 lua_pushnil(L);
jbe@80 269 lua_pushstring(L, errmsg);
jbe@80 270 return 2;
jbe@80 271 }
jbe@80 272 }
jbe@92 273 written += result;
jbe@92 274 handle->writeleft -= result;
jbe@80 275 }
jbe@81 276 handle->writebufcnt = 0;
jbe@80 277 }
jbe@92 278 moonbr_io_write_impl_continue:;
jbe@80 279 }
jbe@81 280 handle->writeqoff = 0;
jbe@81 281 lua_pop(L, 1);
jbe@81 282 lua_pushnil(L);
jbe@83 283 lua_rawseti(L, -2, handle->writeqout++);
jbe@80 284 }
jbe@81 285 if (flush) {
jbe@81 286 written = 0;
jbe@81 287 while (written < handle->writebufcnt) {
jbe@81 288 result = write(handle->fd, handle->writebuf + written, handle->writebufcnt - written);
jbe@81 289 if (result < 0) {
jbe@81 290 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
jbe@81 291 if (written) {
jbe@81 292 handle->writebufcnt -= written;
jbe@81 293 memmove(handle->writebuf, handle->writebuf + written, handle->writebufcnt);
jbe@81 294 }
jbe@81 295 goto moonbr_io_write_impl_block;
jbe@81 296 } else if (errno != EINTR) {
jbe@81 297 moonbr_io_errmsg();
jbe@81 298 handle->writeerr = -1;
jbe@81 299 lua_pushnil(L);
jbe@81 300 lua_pushstring(L, errmsg);
jbe@81 301 return 2;
jbe@81 302 }
jbe@81 303 } else {
jbe@81 304 written += result;
jbe@81 305 handle->writeleft -= result;
jbe@81 306 }
jbe@81 307 }
jbe@81 308 handle->writebufcnt = 0;
jbe@82 309 if (nonblocking) lua_pushinteger(L, 0);
jbe@81 310 } else {
jbe@91 311 if (nonblocking) lua_pushinteger(L, handle->writeleft);
jbe@81 312 }
jbe@82 313 if (!nonblocking) lua_pushvalue(L, 1);
jbe@80 314 return 1;
jbe@81 315 moonbr_io_write_impl_block:
jbe@91 316 lua_pushinteger(L, handle->writeleft);
jbe@81 317 return 1;
jbe@81 318 }
jbe@81 319
jbe@81 320 static int moonbr_io_write(lua_State *L) {
jbe@81 321 return moonbr_io_write_impl(L, 0, 0);
jbe@81 322 }
jbe@81 323
jbe@81 324 static int moonbr_io_write_nb(lua_State *L) {
jbe@81 325 return moonbr_io_write_impl(L, 1, 0);
jbe@80 326 }
jbe@80 327
jbe@80 328 static int moonbr_io_flush(lua_State *L) {
jbe@81 329 return moonbr_io_write_impl(L, 0, 1);
jbe@81 330 }
jbe@81 331
jbe@81 332 static int moonbr_io_flush_nb(lua_State *L) {
jbe@81 333 return moonbr_io_write_impl(L, 1, 1);
jbe@80 334 }
jbe@80 335
jbe@88 336 static int moonbr_io_finish(lua_State *L) {
jbe@88 337 moonbr_io_handle_t *handle;
jbe@88 338 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@94 339 if (handle->closed) luaL_error(L, "Attempt to finish a closed I/O handle");
jbe@94 340 if (handle->finished) luaL_error(L, "Attempt to finish a finished I/O handle");
jbe@94 341 if (handle->writeleft) {
jbe@94 342 lua_pushcfunction(L, moonbr_io_flush);
jbe@94 343 lua_pushvalue(L, 1);
jbe@94 344 lua_call(L, 1, 2);
jbe@94 345 if (!lua_toboolean(L, -2)) {
jbe@94 346 handle->finished = 1;
jbe@94 347 return 2;
jbe@88 348 }
jbe@94 349 }
jbe@94 350 handle->finished = 1;
jbe@95 351 if (handle->isnetwork) {
jbe@88 352 if (shutdown(handle->fd, SHUT_WR)) {
jbe@88 353 moonbr_io_errmsg();
jbe@88 354 lua_pushnil(L);
jbe@88 355 lua_pushstring(L, errmsg);
jbe@88 356 return 2;
jbe@88 357 }
jbe@94 358 } else {
jbe@94 359 if (close(handle->fd)) {
jbe@94 360 moonbr_io_errmsg();
jbe@94 361 handle->fd = -1;
jbe@94 362 lua_pushnil(L);
jbe@94 363 lua_pushstring(L, errmsg);
jbe@94 364 return 2;
jbe@94 365 }
jbe@94 366 handle->fd = -1; /* fake EOF on read */
jbe@88 367 }
jbe@88 368 lua_pushboolean(L, 1);
jbe@88 369 return 1;
jbe@88 370 }
jbe@88 371
jbe@94 372 static int moonbr_io_close_impl(lua_State *L, int reset) {
jbe@83 373 moonbr_io_handle_t *handle;
jbe@83 374 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@94 375 if (handle->closed) luaL_error(L, "Attempt to close a closed I/O handle");
jbe@94 376 if (!reset) {
jbe@87 377 if (handle->writeleft) {
jbe@87 378 lua_pushcfunction(L, moonbr_io_flush);
jbe@87 379 lua_pushvalue(L, 1);
jbe@87 380 lua_call(L, 1, 2);
jbe@87 381 if (!lua_toboolean(L, -2)) {
jbe@87 382 close(handle->fd);
jbe@87 383 handle->fd = -1;
jbe@87 384 return 2;
jbe@87 385 }
jbe@83 386 }
jbe@94 387 moonbr_io_handle_set_linger(L, handle, -1);
jbe@83 388 }
jbe@94 389 if (handle->fd >= 0) {
jbe@94 390 if (close(handle->fd)) {
jbe@94 391 moonbr_io_errmsg();
jbe@94 392 handle->fd = -1;
jbe@94 393 lua_pushnil(L);
jbe@94 394 lua_pushstring(L, errmsg);
jbe@94 395 return 2;
jbe@94 396 }
jbe@83 397 }
jbe@83 398 handle->fd = -1;
jbe@83 399 lua_pushboolean(L, 1);
jbe@83 400 return 1;
jbe@84 401
jbe@83 402 }
jbe@83 403
jbe@94 404 static int moonbr_io_close(lua_State *L) {
jbe@94 405 return moonbr_io_close_impl(L, 0);
jbe@94 406 }
jbe@94 407
jbe@84 408 static int moonbr_io_reset(lua_State *L) {
jbe@94 409 return moonbr_io_close_impl(L, 1);
jbe@84 410 }
jbe@84 411
jbe@88 412 static int moonbr_io_gc(lua_State *L) {
jbe@88 413 moonbr_io_handle_t *handle;
jbe@88 414 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@88 415 if (handle->fd >= 0) {
jbe@88 416 lua_pushcfunction(L, moonbr_io_close);
jbe@88 417 lua_pushvalue(L, 1);
jbe@88 418 lua_pushinteger(L, 0);
jbe@88 419 lua_call(L, 2, 0);
jbe@88 420 }
jbe@88 421 return 0;
jbe@88 422 }
jbe@88 423
jbe@88 424 void moonbr_io_closehandle(lua_State *L, int idx, int timeout) {
jbe@88 425 moonbr_io_handle_t *handle;
jbe@88 426 handle = luaL_checkudata(L, idx, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@88 427 if (handle->fd >= 0) {
jbe@88 428 lua_pushcfunction(L, moonbr_io_close);
jbe@88 429 lua_pushvalue(L, idx);
jbe@88 430 lua_pushinteger(L, timeout);
jbe@88 431 lua_call(L, 2, 0);
jbe@88 432 }
jbe@88 433 }
jbe@88 434
jbe@95 435 void moonbr_io_pushhandle(lua_State *L, int fd, int isnetwork) {
jbe@79 436 moonbr_io_handle_t *handle;
jbe@79 437 handle = lua_newuserdata(L, sizeof(moonbr_io_handle_t));
jbe@79 438 handle->fd = fd;
jbe@95 439 handle->isnetwork = isnetwork;
jbe@94 440 handle->finished = 0;
jbe@94 441 handle->closed = 0;
jbe@81 442 handle->nonblocking = -1;
jbe@95 443 handle->nopush = -1;
jbe@85 444 handle->readerr = 0;
jbe@85 445 handle->readbufcnt = 0;
jbe@81 446 handle->writeerr = 0;
jbe@81 447 handle->writeleft = 0;
jbe@83 448 handle->writeqin = 0;
jbe@83 449 handle->writeqout = 0;
jbe@81 450 handle->writeqoff = 0;
jbe@81 451 handle->writebufcnt = 0;
jbe@87 452 moonbr_io_handle_set_linger(L, handle, 0);
jbe@79 453 luaL_getmetatable(L, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@79 454 lua_setmetatable(L, -2);
jbe@79 455 lua_newtable(L); // uservalue
jbe@81 456 lua_newtable(L);
jbe@81 457 lua_setfield(L, -2, "writebuf");
jbe@79 458 lua_newtable(L); // public
jbe@79 459 luaL_getmetatable(L, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
jbe@79 460 lua_setmetatable(L, -2);
jbe@79 461 lua_setfield(L, -2, "public");
jbe@79 462 lua_setuservalue(L, -2);
jbe@79 463 }
jbe@79 464
jbe@79 465 static int moonbr_io_handleindex(lua_State *L) {
jbe@80 466 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@79 467 lua_getuservalue(L, 1);
jbe@79 468 lua_getfield(L, -1, "public");
jbe@79 469 lua_pushvalue(L, 2);
jbe@79 470 lua_gettable(L, -2);
jbe@79 471 return 1;
jbe@79 472 }
jbe@79 473
jbe@79 474 static int moonbr_io_handlenewindex(lua_State *L) {
jbe@80 475 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@79 476 lua_getuservalue(L, 1);
jbe@79 477 lua_getfield(L, -1, "public");
jbe@79 478 lua_pushvalue(L, 2);
jbe@79 479 lua_pushvalue(L, 3);
jbe@79 480 lua_settable(L, -3);
jbe@79 481 return 0;
jbe@79 482 }
jbe@79 483
jbe@79 484 static const struct luaL_Reg moonbr_io_handle_methods[] = {
jbe@85 485 {"read", moonbr_io_read},
jbe@85 486 {"read_nb", moonbr_io_read_nb},
jbe@86 487 {"drain", moonbr_io_drain},
jbe@86 488 {"drain_nb", moonbr_io_drain_nb},
jbe@80 489 {"write", moonbr_io_write},
jbe@81 490 {"write_nb", moonbr_io_write_nb},
jbe@80 491 {"flush", moonbr_io_flush},
jbe@81 492 {"flush_nb", moonbr_io_flush_nb},
jbe@88 493 {"finish", moonbr_io_finish},
jbe@87 494 {"close", moonbr_io_close},
jbe@85 495 {"reset", moonbr_io_reset},
jbe@79 496 {NULL, NULL}
jbe@79 497 };
jbe@79 498
jbe@79 499 static const struct luaL_Reg moonbr_io_handle_metamethods[] = {
jbe@79 500 {"__index", moonbr_io_handleindex},
jbe@79 501 {"__newindex", moonbr_io_handlenewindex},
jbe@88 502 {"__gc", moonbr_io_gc},
jbe@79 503 {NULL, NULL}
jbe@79 504 };
jbe@79 505
jbe@79 506 static const struct luaL_Reg moonbr_io_module_funcs[] = {
jbe@79 507 {NULL, NULL}
jbe@79 508 };
jbe@79 509
jbe@79 510 int luaopen_moonbridge_io(lua_State *L) {
jbe@79 511
jbe@80 512 lua_newtable(L); // module
jbe@80 513
jbe@79 514 lua_newtable(L); // public metatable
jbe@79 515 lua_newtable(L); // handle methods
jbe@79 516 luaL_setfuncs(L, moonbr_io_handle_methods, 0);
jbe@80 517 lua_pushvalue(L, -1);
jbe@80 518 lua_setfield(L, -4, "handle");
jbe@79 519 lua_setfield(L, -2, "__index");
jbe@79 520 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
jbe@79 521
jbe@79 522 lua_newtable(L); // handle metatable
jbe@79 523 luaL_setfuncs(L, moonbr_io_handle_metamethods, 0);
jbe@79 524 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@79 525
jbe@79 526 luaL_setfuncs(L, moonbr_io_module_funcs, 0);
jbe@79 527 return 1;
jbe@79 528
jbe@79 529 }
jbe@79 530

Impressum / About Us