From ceb278adf666b2d3259408245f1c3a96031b17d7 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 6 Apr 2021 16:46:14 +0800 Subject: [PATCH] Add shutdown to client_socket for testing --- lualib-src/lua-clientsocket.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/lualib-src/lua-clientsocket.c b/lualib-src/lua-clientsocket.c index 25189032..532abed2 100644 --- a/lualib-src/lua-clientsocket.c +++ b/lualib-src/lua-clientsocket.c @@ -185,6 +185,40 @@ lreadstdin(lua_State *L) { return 1; } +static int +lshutdown(lua_State *L) { + int fd = luaL_checkinteger(L,1); + const char *mode = luaL_checkstring(L,2); + int v = 0; + int i; + int read = 1; + int write = 2; + for (i=0;mode[i];i++) { + switch(mode[i]) { + case 'r': + v |= read; + break; + case 'w': + v |= write; + break; + default: + return luaL_error(L, "Invalid mode %c", mode[i]); + } + } + if (v == 0) { + return luaL_error(L, "mode should be r or/and w"); + } + if (v == read) + v = SHUT_RD; + else if (v == write) + v = SHUT_WR; + else + v = SHUT_RDWR; + printf("SHUTDOWN %d %d\n", fd, v); + shutdown(fd, v); + return 0; +} + LUAMOD_API int luaopen_client_socket(lua_State *L) { luaL_checkversion(L); @@ -192,6 +226,7 @@ luaopen_client_socket(lua_State *L) { { "connect", lconnect }, { "recv", lrecv }, { "send", lsend }, + { "shutdown", lshutdown }, { "close", lclose }, { "usleep", lusleep }, { NULL, NULL },