diff options
| -rw-r--r-- | src/tcp.c | 22 | ||||
| -rw-r--r-- | src/unix.c | 24 |
2 files changed, 9 insertions, 37 deletions
| @@ -308,27 +308,13 @@ static int meth_listen(lua_State *L) | |||
| 308 | \*-------------------------------------------------------------------------*/ | 308 | \*-------------------------------------------------------------------------*/ |
| 309 | static int meth_shutdown(lua_State *L) | 309 | static int meth_shutdown(lua_State *L) |
| 310 | { | 310 | { |
| 311 | /* SHUT_RD, SHUT_WR, SHUT_RDWR have the value 0, 1, 2, so we can use method index directly */ | ||
| 312 | static const char* methods[] = { "receive", "send", "both", NULL }; | ||
| 311 | p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{client}", 1); | 313 | p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{client}", 1); |
| 312 | const char *how = luaL_optstring(L, 2, "both"); | 314 | int how = luaL_checkoption(L, 2, "both", methods); |
| 313 | switch (how[0]) { | 315 | socket_shutdown(&tcp->sock, how); |
| 314 | case 'b': | ||
| 315 | if (strcmp(how, "both")) goto error; | ||
| 316 | socket_shutdown(&tcp->sock, 2); | ||
| 317 | break; | ||
| 318 | case 's': | ||
| 319 | if (strcmp(how, "send")) goto error; | ||
| 320 | socket_shutdown(&tcp->sock, 1); | ||
| 321 | break; | ||
| 322 | case 'r': | ||
| 323 | if (strcmp(how, "receive")) goto error; | ||
| 324 | socket_shutdown(&tcp->sock, 0); | ||
| 325 | break; | ||
| 326 | } | ||
| 327 | lua_pushnumber(L, 1); | 316 | lua_pushnumber(L, 1); |
| 328 | return 1; | 317 | return 1; |
| 329 | error: | ||
| 330 | luaL_argerror(L, 2, "invalid shutdown method"); | ||
| 331 | return 0; | ||
| 332 | } | 318 | } |
| 333 | 319 | ||
| 334 | /*-------------------------------------------------------------------------*\ | 320 | /*-------------------------------------------------------------------------*\ |
| @@ -292,27 +292,13 @@ static int meth_listen(lua_State *L) | |||
| 292 | \*-------------------------------------------------------------------------*/ | 292 | \*-------------------------------------------------------------------------*/ |
| 293 | static int meth_shutdown(lua_State *L) | 293 | static int meth_shutdown(lua_State *L) |
| 294 | { | 294 | { |
| 295 | p_unix un = (p_unix) auxiliar_checkclass(L, "unix{client}", 1); | 295 | /* SHUT_RD, SHUT_WR, SHUT_RDWR have the value 0, 1, 2, so we can use method index directly */ |
| 296 | const char *how = luaL_optstring(L, 2, "both"); | 296 | static const char* methods[] = { "receive", "send", "both", NULL }; |
| 297 | switch (how[0]) { | 297 | p_unix tcp = (p_unix) auxiliar_checkclass(L, "unix{client}", 1); |
| 298 | case 'b': | 298 | int how = luaL_checkoption(L, 2, "both", methods); |
| 299 | if (strcmp(how, "both")) goto error; | 299 | socket_shutdown(&tcp->sock, how); |
| 300 | socket_shutdown(&un->sock, 2); | ||
| 301 | break; | ||
| 302 | case 's': | ||
| 303 | if (strcmp(how, "send")) goto error; | ||
| 304 | socket_shutdown(&un->sock, 1); | ||
| 305 | break; | ||
| 306 | case 'r': | ||
| 307 | if (strcmp(how, "receive")) goto error; | ||
| 308 | socket_shutdown(&un->sock, 0); | ||
| 309 | break; | ||
| 310 | } | ||
| 311 | lua_pushnumber(L, 1); | 300 | lua_pushnumber(L, 1); |
| 312 | return 1; | 301 | return 1; |
| 313 | error: | ||
| 314 | luaL_argerror(L, 2, "invalid shutdown method"); | ||
| 315 | return 0; | ||
| 316 | } | 302 | } |
| 317 | 303 | ||
| 318 | /*-------------------------------------------------------------------------*\ | 304 | /*-------------------------------------------------------------------------*\ |
