moonbridge
changeset 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 |
files | moonbridge_io.c |
line diff
1.1 --- a/moonbridge_io.c Mon Apr 06 22:58:51 2015 +0200 1.2 +++ b/moonbridge_io.c Tue Apr 07 00:47:30 2015 +0200 1.3 @@ -4,9 +4,6 @@ 1.4 #include <stdint.h> 1.5 #include <errno.h> 1.6 #include <string.h> 1.7 -#include <stdio.h> 1.8 -#include <time.h> 1.9 -#include <sys/time.h> 1.10 #include <sys/socket.h> 1.11 #include <sys/select.h> 1.12 #include <fcntl.h> 1.13 @@ -23,7 +20,6 @@ 1.14 char errmsg[MOONBR_IO_MAXSTRERRORLEN]; \ 1.15 strerror_r(errno, errmsg, MOONBR_IO_MAXSTRERRORLEN) 1.16 1.17 - 1.18 #define MOONBR_IO_HANDLE_MT_REGKEY "moonbridge_io_handle" 1.19 #define MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY "moonbridge_io_handle_public" 1.20 1.21 @@ -65,7 +61,7 @@ 1.22 } 1.23 } 1.24 1.25 -static int moonbr_io_read_impl(lua_State *L, int nonblocking) { 1.26 +static int moonbr_io_read_impl(lua_State *L, int nonblocking, int drain) { 1.27 moonbr_io_handle_t *handle; 1.28 lua_Integer maxread; 1.29 const char *terminatorstr; 1.30 @@ -73,7 +69,7 @@ 1.31 char terminator; 1.32 luaL_Buffer luabuf; 1.33 size_t luabufcnt = 0; 1.34 - int restcnt; 1.35 + int endcnt; 1.36 char *terminatorpos; 1.37 ssize_t result; 1.38 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY); 1.39 @@ -83,6 +79,7 @@ 1.40 luaL_argcheck(L, terminatorlen == 1, 3, "single byte expected"); 1.41 terminator = terminatorstr[0]; 1.42 } 1.43 + lua_settop(L, 1); /* return handle on drain, terminator string may be garbage collected */ 1.44 if (handle->fd < 0) luaL_error(L, "Attempt to read from a closed I/O handle"); 1.45 if (handle->readerr) { 1.46 lua_pushnil(L); 1.47 @@ -90,23 +87,25 @@ 1.48 return 2; 1.49 } 1.50 moonbr_io_handle_set_nonblocking(L, handle, nonblocking); 1.51 - luaL_buffinit(L, &luabuf); 1.52 + if (!drain) luaL_buffinit(L, &luabuf); 1.53 while (1) { 1.54 - restcnt = -1; 1.55 + endcnt = -1; 1.56 if (maxread > 0 && handle->readbufcnt >= maxread - luabufcnt) { 1.57 - restcnt = maxread - luabufcnt; 1.58 + endcnt = maxread - luabufcnt; 1.59 } else if (terminatorlen) { 1.60 terminatorpos = memchr(handle->readbuf, terminator, handle->readbufcnt); 1.61 - if (terminatorpos) restcnt = 1 + (terminatorpos - handle->readbuf); 1.62 + if (terminatorpos) endcnt = 1 + (terminatorpos - handle->readbuf); 1.63 } 1.64 - if (restcnt >= 0) { 1.65 - luaL_addlstring(&luabuf, handle->readbuf, restcnt); 1.66 - handle->readbufcnt -= restcnt; 1.67 - memmove(handle->readbuf, handle->readbuf + restcnt, handle->readbufcnt); 1.68 - luaL_pushresult(&luabuf); 1.69 + if (endcnt >= 0) { 1.70 + if (!drain) { 1.71 + luaL_addlstring(&luabuf, handle->readbuf, endcnt); 1.72 + luaL_pushresult(&luabuf); 1.73 + } 1.74 + handle->readbufcnt -= endcnt; 1.75 + memmove(handle->readbuf, handle->readbuf + endcnt, handle->readbufcnt); 1.76 return 1; 1.77 } 1.78 - luaL_addlstring(&luabuf, handle->readbuf, handle->readbufcnt); 1.79 + if (!drain) luaL_addlstring(&luabuf, handle->readbuf, handle->readbufcnt); 1.80 luabufcnt += handle->readbufcnt; 1.81 handle->readbufcnt = 0; 1.82 do { 1.83 @@ -122,18 +121,28 @@ 1.84 } 1.85 handle->readbufcnt += result; 1.86 } 1.87 - luaL_addlstring(&luabuf, handle->readbuf, handle->readbufcnt); 1.88 + if (!drain) { 1.89 + luaL_addlstring(&luabuf, handle->readbuf, handle->readbufcnt); 1.90 + luaL_pushresult(&luabuf); 1.91 + } 1.92 handle->readbufcnt = 0; 1.93 - luaL_pushresult(&luabuf); 1.94 return 1; 1.95 } 1.96 1.97 static int moonbr_io_read(lua_State *L) { 1.98 - return moonbr_io_read_impl(L, 0); 1.99 + return moonbr_io_read_impl(L, 0, 0); 1.100 } 1.101 1.102 static int moonbr_io_read_nb(lua_State *L) { 1.103 - return moonbr_io_read_impl(L, 1); 1.104 + return moonbr_io_read_impl(L, 1, 0); 1.105 +} 1.106 + 1.107 +static int moonbr_io_drain(lua_State *L) { 1.108 + return moonbr_io_read_impl(L, 0, 1); 1.109 +} 1.110 + 1.111 +static int moonbr_io_drain_nb(lua_State *L) { 1.112 + return moonbr_io_read_impl(L, 1, 1); 1.113 } 1.114 1.115 static int moonbr_io_write_impl(lua_State *L, int nonblocking, int flush) { 1.116 @@ -355,6 +364,8 @@ 1.117 static const struct luaL_Reg moonbr_io_handle_methods[] = { 1.118 {"read", moonbr_io_read}, 1.119 {"read_nb", moonbr_io_read_nb}, 1.120 + {"drain", moonbr_io_drain}, 1.121 + {"drain_nb", moonbr_io_drain_nb}, 1.122 {"write", moonbr_io_write}, 1.123 {"write_nb", moonbr_io_write_nb}, 1.124 {"flush", moonbr_io_flush},