| 245 | |
| 246 | #define LUA_READ_STEP 8192 |
| 247 | static int l_socket_receive(lua_State *L) { |
| 248 | p_tcp self = (p_tcp) checktype(L, 1, SOCKET_CONN); |
| 249 | p_tcp handle = (p_tcp) checktype(L, 2, SOCKET_CONN); |
| 250 | size_t len = luaL_checknumber(L, 3); |
| 251 | char buf[LUA_READ_STEP]; |
| 252 | const char *err = NULL; |
| 253 | int received; |
| 254 | size_t got = 0, step = 0; |
| 255 | luaL_Buffer b; |
| 256 | |
| 257 | luaL_buffinit(L, &b); |
| 258 | do { |
| 259 | step = (LUA_READ_STEP < len - got ? LUA_READ_STEP : len - got); |
| 260 | err = tcp_raw_receive(&handle->sock, buf, step, self->timeout, &received); |
| 261 | if (err == NULL) { |
| 262 | luaL_addlstring(&b, buf, received); |
| 263 | got += received; |
| 264 | } |
| 265 | } while (err == NULL && got < len); |
| 266 | |
| 267 | if (err) { |
| 268 | lua_pushnil(L); |
| 269 | lua_pushstring(L, err); |
| 270 | return 2; |
| 271 | } |
| 272 | luaL_pushresult(&b); |
| 273 | return 1; |
| 274 | } |
| 275 | |
| 276 | // settimeout(timeout) |
| 277 | static int l_socket_settimeout(lua_State *L) { |
nothing calls this directly
no test coverage detected