diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2003-08-16 00:06:04 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2003-08-16 00:06:04 +0000 |
commit | c51d4acf1c2a8675a3bb043e799ff4390cef47d6 (patch) | |
tree | 345b71aa70b50c964a58980461e147a260fa6e0b /src | |
parent | 3099704affa1fda195eb8f3934f6c1f18bf1f706 (diff) | |
download | luasocket-c51d4acf1c2a8675a3bb043e799ff4390cef47d6.tar.gz luasocket-c51d4acf1c2a8675a3bb043e799ff4390cef47d6.tar.bz2 luasocket-c51d4acf1c2a8675a3bb043e799ff4390cef47d6.zip |
Adjusted a few inconsistencies with the manual.
Diffstat (limited to 'src')
-rw-r--r-- | src/ftp.lua | 4 | ||||
-rw-r--r-- | src/http.lua | 2 | ||||
-rw-r--r-- | src/inet.c | 29 | ||||
-rw-r--r-- | src/smtp.lua | 12 | ||||
-rw-r--r-- | src/tcp.c | 164 | ||||
-rw-r--r-- | src/timeout.c | 2 | ||||
-rw-r--r-- | src/timeout.h | 2 | ||||
-rw-r--r-- | src/udp.c | 10 | ||||
-rw-r--r-- | src/usocket.c | 6 | ||||
-rw-r--r-- | src/wsocket.c | 6 |
10 files changed, 128 insertions, 109 deletions
diff --git a/src/ftp.lua b/src/ftp.lua index 9d75d2a..25226a6 100644 --- a/src/ftp.lua +++ b/src/ftp.lua | |||
@@ -224,7 +224,7 @@ function Private.port(control) | |||
224 | local server, ctl_ip | 224 | local server, ctl_ip |
225 | ctl_ip, answer = control:getsockname() | 225 | ctl_ip, answer = control:getsockname() |
226 | server, answer = socket.bind(ctl_ip, 0) | 226 | server, answer = socket.bind(ctl_ip, 0) |
227 | server:timeout(Public.TIMEOUT) | 227 | server:settimeout(Public.TIMEOUT) |
228 | local ip, p, ph, pl | 228 | local ip, p, ph, pl |
229 | ip, p = server:getsockname() | 229 | ip, p = server:getsockname() |
230 | pl = math.mod(p, 256) | 230 | pl = math.mod(p, 256) |
@@ -404,7 +404,7 @@ function Private.open(parsed) | |||
404 | local control, err = socket.connect(parsed.host, parsed.port) | 404 | local control, err = socket.connect(parsed.host, parsed.port) |
405 | if not control then return nil, err end | 405 | if not control then return nil, err end |
406 | -- make sure we don't block forever | 406 | -- make sure we don't block forever |
407 | control:timeout(Public.TIMEOUT) | 407 | control:settimeout(Public.TIMEOUT) |
408 | -- check greeting | 408 | -- check greeting |
409 | local code, answer = Private.greet(control) | 409 | local code, answer = Private.greet(control) |
410 | if not code then return nil, answer end | 410 | if not code then return nil, answer end |
diff --git a/src/http.lua b/src/http.lua index 4ef2c87..212e8f6 100644 --- a/src/http.lua +++ b/src/http.lua | |||
@@ -553,7 +553,7 @@ function Public.request_cb(request, response) | |||
553 | sock, response.error = socket.connect(parsed.host, parsed.port) | 553 | sock, response.error = socket.connect(parsed.host, parsed.port) |
554 | if not sock then return response end | 554 | if not sock then return response end |
555 | -- set connection timeout so that we do not hang forever | 555 | -- set connection timeout so that we do not hang forever |
556 | sock:timeout(Public.TIMEOUT) | 556 | sock:settimeout(Public.TIMEOUT) |
557 | -- send request message | 557 | -- send request message |
558 | response.error = Private.send_request(sock, request.method, | 558 | response.error = Private.send_request(sock, request.method, |
559 | Private.request_uri(parsed), request.headers, request.body_cb) | 559 | Private.request_uri(parsed), request.headers, request.body_cb) |
@@ -20,6 +20,7 @@ static int inet_global_toip(lua_State *L); | |||
20 | static int inet_global_tohostname(lua_State *L); | 20 | static int inet_global_tohostname(lua_State *L); |
21 | static void inet_pushresolved(lua_State *L, struct hostent *hp); | 21 | static void inet_pushresolved(lua_State *L, struct hostent *hp); |
22 | 22 | ||
23 | /* DNS functions */ | ||
23 | static luaL_reg func[] = { | 24 | static luaL_reg func[] = { |
24 | { "toip", inet_global_toip }, | 25 | { "toip", inet_global_toip }, |
25 | { "tohostname", inet_global_tohostname }, | 26 | { "tohostname", inet_global_tohostname }, |
@@ -34,7 +35,19 @@ static luaL_reg func[] = { | |||
34 | \*-------------------------------------------------------------------------*/ | 35 | \*-------------------------------------------------------------------------*/ |
35 | void inet_open(lua_State *L) | 36 | void inet_open(lua_State *L) |
36 | { | 37 | { |
37 | luaL_openlib(L, LUASOCKET_LIBNAME, func, 0); | 38 | lua_pushstring(L, LUASOCKET_LIBNAME); |
39 | lua_gettable(L, LUA_GLOBALSINDEX); | ||
40 | if (lua_isnil(L, -1)) { | ||
41 | lua_pop(L, 1); | ||
42 | lua_newtable(L); | ||
43 | lua_pushstring(L, LUASOCKET_LIBNAME); | ||
44 | lua_pushvalue(L, -2); | ||
45 | lua_settable(L, LUA_GLOBALSINDEX); | ||
46 | } | ||
47 | lua_pushstring(L, "dns"); | ||
48 | lua_newtable(L); | ||
49 | luaL_openlib(L, NULL, func, 0); | ||
50 | lua_settable(L, -3); | ||
38 | lua_pop(L, 1); | 51 | lua_pop(L, 1); |
39 | } | 52 | } |
40 | 53 | ||
@@ -100,10 +113,11 @@ int inet_meth_getpeername(lua_State *L, p_sock ps) | |||
100 | socklen_t peer_len = sizeof(peer); | 113 | socklen_t peer_len = sizeof(peer); |
101 | if (getpeername(*ps, (SA *) &peer, &peer_len) < 0) { | 114 | if (getpeername(*ps, (SA *) &peer, &peer_len) < 0) { |
102 | lua_pushnil(L); | 115 | lua_pushnil(L); |
103 | return 1; | 116 | lua_pushstring(L, "getpeername failed"); |
117 | } else { | ||
118 | lua_pushstring(L, inet_ntoa(peer.sin_addr)); | ||
119 | lua_pushnumber(L, ntohs(peer.sin_port)); | ||
104 | } | 120 | } |
105 | lua_pushstring(L, inet_ntoa(peer.sin_addr)); | ||
106 | lua_pushnumber(L, ntohs(peer.sin_port)); | ||
107 | return 2; | 121 | return 2; |
108 | } | 122 | } |
109 | 123 | ||
@@ -116,10 +130,11 @@ int inet_meth_getsockname(lua_State *L, p_sock ps) | |||
116 | socklen_t local_len = sizeof(local); | 130 | socklen_t local_len = sizeof(local); |
117 | if (getsockname(*ps, (SA *) &local, &local_len) < 0) { | 131 | if (getsockname(*ps, (SA *) &local, &local_len) < 0) { |
118 | lua_pushnil(L); | 132 | lua_pushnil(L); |
119 | return 1; | 133 | lua_pushstring(L, "getsockname failed"); |
134 | } else { | ||
135 | lua_pushstring(L, inet_ntoa(local.sin_addr)); | ||
136 | lua_pushnumber(L, ntohs(local.sin_port)); | ||
120 | } | 137 | } |
121 | lua_pushstring(L, inet_ntoa(local.sin_addr)); | ||
122 | lua_pushnumber(L, ntohs(local.sin_port)); | ||
123 | return 2; | 138 | return 2; |
124 | } | 139 | } |
125 | 140 | ||
diff --git a/src/smtp.lua b/src/smtp.lua index 209825b..5249160 100644 --- a/src/smtp.lua +++ b/src/smtp.lua | |||
@@ -243,7 +243,7 @@ function Private.open(server) | |||
243 | -- connect to server and make sure we won't hang | 243 | -- connect to server and make sure we won't hang |
244 | local sock, err = socket.connect(server, Public.PORT) | 244 | local sock, err = socket.connect(server, Public.PORT) |
245 | if not sock then return nil, err end | 245 | if not sock then return nil, err end |
246 | sock:timeout(Public.TIMEOUT) | 246 | sock:settimeout(Public.TIMEOUT) |
247 | -- initial server greeting | 247 | -- initial server greeting |
248 | code, answer = Private.check_answer(sock, 220) | 248 | code, answer = Private.check_answer(sock, 220) |
249 | if not code then return nil, answer end | 249 | if not code then return nil, answer end |
@@ -305,10 +305,10 @@ end | |||
305 | ----------------------------------------------------------------------------- | 305 | ----------------------------------------------------------------------------- |
306 | function Public.mail(message) | 306 | function Public.mail(message) |
307 | local sock, err = Private.open(message.server) | 307 | local sock, err = Private.open(message.server) |
308 | if not sock then return err end | 308 | if not sock then return nil, err end |
309 | local code, answer = Private.send(sock, message) | 309 | local code, answer = Private.send(sock, message) |
310 | if not code then return answer end | 310 | if not code then return nil, answer end |
311 | code, answer = Private.close(sock) | 311 | code, answer = Private.close(sock) |
312 | if code then return nil end | 312 | if code then return 1 |
313 | return answer | 313 | else return nil, answer end |
314 | end | 314 | end |
@@ -29,10 +29,10 @@ static int meth_receive(lua_State *L); | |||
29 | static int meth_accept(lua_State *L); | 29 | static int meth_accept(lua_State *L); |
30 | static int meth_close(lua_State *L); | 30 | static int meth_close(lua_State *L); |
31 | static int meth_setoption(lua_State *L); | 31 | static int meth_setoption(lua_State *L); |
32 | static int meth_timeout(lua_State *L); | 32 | static int meth_settimeout(lua_State *L); |
33 | static int meth_fd(lua_State *L); | 33 | static int meth_fd(lua_State *L); |
34 | static int meth_dirty(lua_State *L); | 34 | static int meth_dirty(lua_State *L); |
35 | static int opt_nodelay(lua_State *L); | 35 | static int opt_tcp_nodelay(lua_State *L); |
36 | static int opt_keepalive(lua_State *L); | 36 | static int opt_keepalive(lua_State *L); |
37 | static int opt_linger(lua_State *L); | 37 | static int opt_linger(lua_State *L); |
38 | 38 | ||
@@ -47,7 +47,7 @@ static luaL_reg tcp[] = { | |||
47 | {"setsockname", meth_bind}, | 47 | {"setsockname", meth_bind}, |
48 | {"getpeername", meth_getpeername}, | 48 | {"getpeername", meth_getpeername}, |
49 | {"getsockname", meth_getsockname}, | 49 | {"getsockname", meth_getsockname}, |
50 | {"timeout", meth_timeout}, | 50 | {"settimeout", meth_settimeout}, |
51 | {"close", meth_close}, | 51 | {"close", meth_close}, |
52 | {"setoption", meth_setoption}, | 52 | {"setoption", meth_setoption}, |
53 | {"__gc", meth_close}, | 53 | {"__gc", meth_close}, |
@@ -59,7 +59,7 @@ static luaL_reg tcp[] = { | |||
59 | /* socket option handlers */ | 59 | /* socket option handlers */ |
60 | static luaL_reg opt[] = { | 60 | static luaL_reg opt[] = { |
61 | {"keepalive", opt_keepalive}, | 61 | {"keepalive", opt_keepalive}, |
62 | {"nodelay", opt_nodelay}, | 62 | {"tcp-nodelay", opt_tcp_nodelay}, |
63 | {"linger", opt_linger}, | 63 | {"linger", opt_linger}, |
64 | {NULL, NULL} | 64 | {NULL, NULL} |
65 | }; | 65 | }; |
@@ -83,8 +83,8 @@ void tcp_open(lua_State *L) | |||
83 | aux_add2group(L, "tcp{master}", "tcp{any}"); | 83 | aux_add2group(L, "tcp{master}", "tcp{any}"); |
84 | aux_add2group(L, "tcp{client}", "tcp{any}"); | 84 | aux_add2group(L, "tcp{client}", "tcp{any}"); |
85 | aux_add2group(L, "tcp{server}", "tcp{any}"); | 85 | aux_add2group(L, "tcp{server}", "tcp{any}"); |
86 | aux_add2group(L, "tcp{client}", "tcp{client, server}"); | 86 | aux_add2group(L, "tcp{client}", "tcp{client,server}"); |
87 | aux_add2group(L, "tcp{server}", "tcp{client, server}"); | 87 | aux_add2group(L, "tcp{server}", "tcp{client,server}"); |
88 | /* both server and client objects are selectable */ | 88 | /* both server and client objects are selectable */ |
89 | aux_add2group(L, "tcp{client}", "select{able}"); | 89 | aux_add2group(L, "tcp{client}", "select{able}"); |
90 | aux_add2group(L, "tcp{server}", "select{able}"); | 90 | aux_add2group(L, "tcp{server}", "select{able}"); |
@@ -121,7 +121,7 @@ static int meth_setoption(lua_State *L) | |||
121 | 121 | ||
122 | static int opt_boolean(lua_State *L, int level, int name) | 122 | static int opt_boolean(lua_State *L, int level, int name) |
123 | { | 123 | { |
124 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); | 124 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{client,server}", 1); |
125 | int val = aux_checkboolean(L, 2); | 125 | int val = aux_checkboolean(L, 2); |
126 | if (setsockopt(tcp->sock, level, name, (char *) &val, sizeof(val)) < 0) { | 126 | if (setsockopt(tcp->sock, level, name, (char *) &val, sizeof(val)) < 0) { |
127 | lua_pushnil(L); | 127 | lua_pushnil(L); |
@@ -132,8 +132,8 @@ static int opt_boolean(lua_State *L, int level, int name) | |||
132 | return 1; | 132 | return 1; |
133 | } | 133 | } |
134 | 134 | ||
135 | /* disables the Nagle algorithm */ | 135 | /* disables the Naggle algorithm */ |
136 | static int opt_nodelay(lua_State *L) | 136 | static int opt_tcp_nodelay(lua_State *L) |
137 | { | 137 | { |
138 | struct protoent *pe = getprotobyname("TCP"); | 138 | struct protoent *pe = getprotobyname("TCP"); |
139 | if (!pe) { | 139 | if (!pe) { |
@@ -155,13 +155,13 @@ int opt_linger(lua_State *L) | |||
155 | struct linger li; | 155 | struct linger li; |
156 | if (!lua_istable(L, 2)) | 156 | if (!lua_istable(L, 2)) |
157 | luaL_typerror(L, 2, lua_typename(L, LUA_TTABLE)); | 157 | luaL_typerror(L, 2, lua_typename(L, LUA_TTABLE)); |
158 | lua_pushstring(L, "onoff"); | 158 | lua_pushstring(L, "on"); |
159 | lua_gettable(L, 2); | 159 | lua_gettable(L, 2); |
160 | if (!lua_isnumber(L, -1)) luaL_argerror(L, 2, "invalid onoff field"); | 160 | if (!lua_isboolean(L, -1)) luaL_argerror(L, 2, "invalid 'on' field"); |
161 | li.l_onoff = (int) lua_tonumber(L, -1); | 161 | li.l_onoff = lua_toboolean(L, -1); |
162 | lua_pushstring(L, "linger"); | 162 | lua_pushstring(L, "timeout"); |
163 | lua_gettable(L, 2); | 163 | lua_gettable(L, 2); |
164 | if (!lua_isnumber(L, -1)) luaL_argerror(L, 2, "invalid linger field"); | 164 | if (!lua_isnumber(L, -1)) luaL_argerror(L, 2, "invalid 'timeout' field"); |
165 | li.l_linger = (int) lua_tonumber(L, -1); | 165 | li.l_linger = (int) lua_tonumber(L, -1); |
166 | if (setsockopt(tcp->sock, SOL_SOCKET, SO_LINGER, | 166 | if (setsockopt(tcp->sock, SOL_SOCKET, SO_LINGER, |
167 | (char *) &li, sizeof(li) < 0)) { | 167 | (char *) &li, sizeof(li) < 0)) { |
@@ -178,122 +178,122 @@ int opt_linger(lua_State *L) | |||
178 | \*-------------------------------------------------------------------------*/ | 178 | \*-------------------------------------------------------------------------*/ |
179 | static int meth_fd(lua_State *L) | 179 | static int meth_fd(lua_State *L) |
180 | { | 180 | { |
181 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{client, server}", 1); | 181 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{client,server}", 1); |
182 | lua_pushnumber(L, tcp->sock); | 182 | lua_pushnumber(L, tcp->sock); |
183 | return 1; | 183 | return 1; |
184 | } | 184 | } |
185 | 185 | ||
186 | static int meth_dirty(lua_State *L) | 186 | static int meth_dirty(lua_State *L) |
187 | { | 187 | { |
188 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{client, server}", 1); | 188 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{client,server}", 1); |
189 | lua_pushboolean(L, !buf_isempty(&tcp->buf)); | 189 | lua_pushboolean(L, !buf_isempty(&tcp->buf)); |
190 | return 1; | 190 | return 1; |
191 | } | 191 | } |
192 | 192 | ||
193 | /*-------------------------------------------------------------------------*\ | 193 | /*-------------------------------------------------------------------------*\ |
194 | * Just call inet methods | 194 | * Waits for and returns a client object attempting connection to the |
195 | \*-------------------------------------------------------------------------*/ | 195 | * server object |
196 | static int meth_getpeername(lua_State *L) | ||
197 | { | ||
198 | p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1); | ||
199 | return inet_meth_getpeername(L, &tcp->sock); | ||
200 | } | ||
201 | |||
202 | static int meth_getsockname(lua_State *L) | ||
203 | { | ||
204 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{client, server}", 1); | ||
205 | return inet_meth_getsockname(L, &tcp->sock); | ||
206 | } | ||
207 | |||
208 | /*-------------------------------------------------------------------------*\ | ||
209 | * Just call tm methods | ||
210 | \*-------------------------------------------------------------------------*/ | ||
211 | static int meth_timeout(lua_State *L) | ||
212 | { | ||
213 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); | ||
214 | return tm_meth_timeout(L, &tcp->tm); | ||
215 | } | ||
216 | |||
217 | /*-------------------------------------------------------------------------*\ | ||
218 | * Closes socket used by object | ||
219 | \*-------------------------------------------------------------------------*/ | 196 | \*-------------------------------------------------------------------------*/ |
220 | static int meth_close(lua_State *L) | 197 | static int meth_accept(lua_State *L) |
221 | { | 198 | { |
222 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); | 199 | struct sockaddr_in addr; |
223 | sock_destroy(&tcp->sock); | 200 | socklen_t addr_len = sizeof(addr); |
224 | return 0; | 201 | p_tcp server = (p_tcp) aux_checkclass(L, "tcp{server}", 1); |
202 | p_tm tm = &server->tm; | ||
203 | p_tcp client = lua_newuserdata(L, sizeof(t_tcp)); | ||
204 | tm_markstart(tm); | ||
205 | aux_setclass(L, "tcp{client}", -1); | ||
206 | for ( ;; ) { | ||
207 | sock_accept(&server->sock, &client->sock, | ||
208 | (SA *) &addr, &addr_len, tm_get(tm)); | ||
209 | if (client->sock == SOCK_INVALID) { | ||
210 | if (tm_get(tm) == 0) { | ||
211 | lua_pushnil(L); | ||
212 | io_pusherror(L, IO_TIMEOUT); | ||
213 | return 2; | ||
214 | } | ||
215 | } else break; | ||
216 | } | ||
217 | /* initialize remaining structure fields */ | ||
218 | io_init(&client->io, (p_send) sock_send, (p_recv) sock_recv, &client->sock); | ||
219 | tm_init(&client->tm, -1, -1); | ||
220 | buf_init(&client->buf, &client->io, &client->tm); | ||
221 | return 1; | ||
225 | } | 222 | } |
226 | 223 | ||
227 | /*-------------------------------------------------------------------------*\ | 224 | /*-------------------------------------------------------------------------*\ |
228 | * Turns a master tcp object into a client object. | 225 | * Turns a master object into a server object |
229 | \*-------------------------------------------------------------------------*/ | 226 | \*-------------------------------------------------------------------------*/ |
230 | static int meth_connect(lua_State *L) | 227 | static int meth_bind(lua_State *L) |
231 | { | 228 | { |
232 | p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{master}", 1); | 229 | p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{master}", 1); |
233 | const char *address = luaL_checkstring(L, 2); | 230 | const char *address = luaL_checkstring(L, 2); |
234 | unsigned short port = (unsigned short) luaL_checknumber(L, 3); | 231 | unsigned short port = (unsigned short) luaL_checknumber(L, 3); |
235 | const char *err = inet_tryconnect(&tcp->sock, address, port); | 232 | int backlog = (int) luaL_optnumber(L, 4, 1); |
233 | const char *err = inet_trybind(&tcp->sock, address, port, backlog); | ||
236 | if (err) { | 234 | if (err) { |
237 | lua_pushnil(L); | 235 | lua_pushnil(L); |
238 | lua_pushstring(L, err); | 236 | lua_pushstring(L, err); |
239 | return 2; | 237 | return 2; |
240 | } | 238 | } |
241 | /* turn master object into a client object */ | 239 | /* turn master object into a server object */ |
242 | aux_setclass(L, "tcp{client}", 1); | 240 | aux_setclass(L, "tcp{server}", 1); |
243 | lua_pushnumber(L, 1); | 241 | lua_pushnumber(L, 1); |
244 | return 1; | 242 | return 1; |
245 | } | 243 | } |
246 | 244 | ||
247 | /*-------------------------------------------------------------------------*\ | 245 | /*-------------------------------------------------------------------------*\ |
248 | * Turns a master object into a server object | 246 | * Turns a master tcp object into a client object. |
249 | \*-------------------------------------------------------------------------*/ | 247 | \*-------------------------------------------------------------------------*/ |
250 | static int meth_bind(lua_State *L) | 248 | static int meth_connect(lua_State *L) |
251 | { | 249 | { |
252 | p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{master}", 1); | 250 | p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{master}", 1); |
253 | const char *address = luaL_checkstring(L, 2); | 251 | const char *address = luaL_checkstring(L, 2); |
254 | unsigned short port = (unsigned short) luaL_checknumber(L, 3); | 252 | unsigned short port = (unsigned short) luaL_checknumber(L, 3); |
255 | int backlog = (int) luaL_optnumber(L, 4, 1); | 253 | const char *err = inet_tryconnect(&tcp->sock, address, port); |
256 | const char *err = inet_trybind(&tcp->sock, address, port, backlog); | ||
257 | if (err) { | 254 | if (err) { |
258 | lua_pushnil(L); | 255 | lua_pushnil(L); |
259 | lua_pushstring(L, err); | 256 | lua_pushstring(L, err); |
260 | return 2; | 257 | return 2; |
261 | } | 258 | } |
262 | /* turn master object into a server object */ | 259 | /* turn master object into a client object */ |
263 | aux_setclass(L, "tcp{server}", 1); | 260 | aux_setclass(L, "tcp{client}", 1); |
264 | lua_pushnumber(L, 1); | 261 | lua_pushnumber(L, 1); |
265 | return 1; | 262 | return 1; |
266 | } | 263 | } |
267 | 264 | ||
268 | /*-------------------------------------------------------------------------*\ | 265 | /*-------------------------------------------------------------------------*\ |
269 | * Waits for and returns a client object attempting connection to the | 266 | * Closes socket used by object |
270 | * server object | ||
271 | \*-------------------------------------------------------------------------*/ | 267 | \*-------------------------------------------------------------------------*/ |
272 | static int meth_accept(lua_State *L) | 268 | static int meth_close(lua_State *L) |
273 | { | 269 | { |
274 | struct sockaddr_in addr; | 270 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); |
275 | socklen_t addr_len = sizeof(addr); | 271 | sock_destroy(&tcp->sock); |
276 | p_tcp server = (p_tcp) aux_checkclass(L, "tcp{server}", 1); | 272 | return 0; |
277 | p_tm tm = &server->tm; | 273 | } |
278 | p_tcp client = lua_newuserdata(L, sizeof(t_tcp)); | 274 | |
279 | tm_markstart(tm); | 275 | /*-------------------------------------------------------------------------*\ |
280 | aux_setclass(L, "tcp{client}", -1); | 276 | * Just call inet methods |
281 | for ( ;; ) { | 277 | \*-------------------------------------------------------------------------*/ |
282 | sock_accept(&server->sock, &client->sock, | 278 | static int meth_getpeername(lua_State *L) |
283 | (SA *) &addr, &addr_len, tm_get(tm)); | 279 | { |
284 | if (client->sock == SOCK_INVALID) { | 280 | p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1); |
285 | if (tm_get(tm) == 0) { | 281 | return inet_meth_getpeername(L, &tcp->sock); |
286 | lua_pushnil(L); | 282 | } |
287 | io_pusherror(L, IO_TIMEOUT); | 283 | |
288 | return 2; | 284 | static int meth_getsockname(lua_State *L) |
289 | } | 285 | { |
290 | } else break; | 286 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{client,server}", 1); |
291 | } | 287 | return inet_meth_getsockname(L, &tcp->sock); |
292 | /* initialize remaining structure fields */ | 288 | } |
293 | io_init(&client->io, (p_send) sock_send, (p_recv) sock_recv, &client->sock); | 289 | |
294 | tm_init(&client->tm, -1, -1); | 290 | /*-------------------------------------------------------------------------*\ |
295 | buf_init(&client->buf, &client->io, &client->tm); | 291 | * Just call tm methods |
296 | return 1; | 292 | \*-------------------------------------------------------------------------*/ |
293 | static int meth_settimeout(lua_State *L) | ||
294 | { | ||
295 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{client,server}", 1); | ||
296 | return tm_meth_settimeout(L, &tcp->tm); | ||
297 | } | 297 | } |
298 | 298 | ||
299 | /*=========================================================================*\ | 299 | /*=========================================================================*\ |
diff --git a/src/timeout.c b/src/timeout.c index 6a30e3a..38d1135 100644 --- a/src/timeout.c +++ b/src/timeout.c | |||
@@ -125,7 +125,7 @@ void tm_open(lua_State *L) | |||
125 | * time: time out value in seconds | 125 | * time: time out value in seconds |
126 | * mode: "b" for block timeout, "t" for total timeout. (default: b) | 126 | * mode: "b" for block timeout, "t" for total timeout. (default: b) |
127 | \*-------------------------------------------------------------------------*/ | 127 | \*-------------------------------------------------------------------------*/ |
128 | int tm_meth_timeout(lua_State *L, p_tm tm) | 128 | int tm_meth_settimeout(lua_State *L, p_tm tm) |
129 | { | 129 | { |
130 | int ms = lua_isnil(L, 2) ? -1 : (int) (luaL_checknumber(L, 2)*1000.0); | 130 | int ms = lua_isnil(L, 2) ? -1 : (int) (luaL_checknumber(L, 2)*1000.0); |
131 | const char *mode = luaL_optstring(L, 3, "b"); | 131 | const char *mode = luaL_optstring(L, 3, "b"); |
diff --git a/src/timeout.h b/src/timeout.h index 32eb836..ef2f533 100644 --- a/src/timeout.h +++ b/src/timeout.h | |||
@@ -26,6 +26,6 @@ void tm_markstart(p_tm tm); | |||
26 | int tm_getstart(p_tm tm); | 26 | int tm_getstart(p_tm tm); |
27 | int tm_get(p_tm tm); | 27 | int tm_get(p_tm tm); |
28 | int tm_gettime(void); | 28 | int tm_gettime(void); |
29 | int tm_meth_timeout(lua_State *L, p_tm tm); | 29 | int tm_meth_settimeout(lua_State *L, p_tm tm); |
30 | 30 | ||
31 | #endif /* TM_H */ | 31 | #endif /* TM_H */ |
@@ -30,7 +30,7 @@ static int meth_setsockname(lua_State *L); | |||
30 | static int meth_setpeername(lua_State *L); | 30 | static int meth_setpeername(lua_State *L); |
31 | static int meth_close(lua_State *L); | 31 | static int meth_close(lua_State *L); |
32 | static int meth_setoption(lua_State *L); | 32 | static int meth_setoption(lua_State *L); |
33 | static int meth_timeout(lua_State *L); | 33 | static int meth_settimeout(lua_State *L); |
34 | static int meth_fd(lua_State *L); | 34 | static int meth_fd(lua_State *L); |
35 | static int meth_dirty(lua_State *L); | 35 | static int meth_dirty(lua_State *L); |
36 | static int opt_dontroute(lua_State *L); | 36 | static int opt_dontroute(lua_State *L); |
@@ -46,7 +46,7 @@ static luaL_reg udp[] = { | |||
46 | {"sendto", meth_sendto}, | 46 | {"sendto", meth_sendto}, |
47 | {"receive", meth_receive}, | 47 | {"receive", meth_receive}, |
48 | {"receivefrom", meth_receivefrom}, | 48 | {"receivefrom", meth_receivefrom}, |
49 | {"timeout", meth_timeout}, | 49 | {"settimeout", meth_settimeout}, |
50 | {"close", meth_close}, | 50 | {"close", meth_close}, |
51 | {"setoption", meth_setoption}, | 51 | {"setoption", meth_setoption}, |
52 | {"__gc", meth_close}, | 52 | {"__gc", meth_close}, |
@@ -252,10 +252,10 @@ static int opt_broadcast(lua_State *L) | |||
252 | /*-------------------------------------------------------------------------*\ | 252 | /*-------------------------------------------------------------------------*\ |
253 | * Just call tm methods | 253 | * Just call tm methods |
254 | \*-------------------------------------------------------------------------*/ | 254 | \*-------------------------------------------------------------------------*/ |
255 | static int meth_timeout(lua_State *L) | 255 | static int meth_settimeout(lua_State *L) |
256 | { | 256 | { |
257 | p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); | 257 | p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); |
258 | return tm_meth_timeout(L, &udp->tm); | 258 | return tm_meth_settimeout(L, &udp->tm); |
259 | } | 259 | } |
260 | 260 | ||
261 | /*-------------------------------------------------------------------------*\ | 261 | /*-------------------------------------------------------------------------*\ |
@@ -297,7 +297,7 @@ static int meth_close(lua_State *L) | |||
297 | \*-------------------------------------------------------------------------*/ | 297 | \*-------------------------------------------------------------------------*/ |
298 | static int meth_setsockname(lua_State *L) | 298 | static int meth_setsockname(lua_State *L) |
299 | { | 299 | { |
300 | p_udp udp = (p_udp) aux_checkclass(L, "udp{master}", 1); | 300 | p_udp udp = (p_udp) aux_checkclass(L, "udp{unconnected}", 1); |
301 | const char *address = luaL_checkstring(L, 2); | 301 | const char *address = luaL_checkstring(L, 2); |
302 | unsigned short port = (unsigned short) luaL_checknumber(L, 3); | 302 | unsigned short port = (unsigned short) luaL_checknumber(L, 3); |
303 | const char *err = inet_trybind(&udp->sock, address, port, -1); | 303 | const char *err = inet_trybind(&udp->sock, address, port, -1); |
diff --git a/src/usocket.c b/src/usocket.c index cdd550c..202238b 100644 --- a/src/usocket.c +++ b/src/usocket.c | |||
@@ -26,8 +26,10 @@ int sock_open(void) | |||
26 | \*-------------------------------------------------------------------------*/ | 26 | \*-------------------------------------------------------------------------*/ |
27 | void sock_destroy(p_sock ps) | 27 | void sock_destroy(p_sock ps) |
28 | { | 28 | { |
29 | close(*ps); | 29 | if (*ps != SOCK_INVALID) { |
30 | *ps = SOCK_INVALID; | 30 | close(*ps); |
31 | *ps = SOCK_INVALID; | ||
32 | } | ||
31 | } | 33 | } |
32 | 34 | ||
33 | /*-------------------------------------------------------------------------*\ | 35 | /*-------------------------------------------------------------------------*\ |
diff --git a/src/wsocket.c b/src/wsocket.c index 2ce828e..f9e1084 100644 --- a/src/wsocket.c +++ b/src/wsocket.c | |||
@@ -29,8 +29,10 @@ int sock_open(void) | |||
29 | \*-------------------------------------------------------------------------*/ | 29 | \*-------------------------------------------------------------------------*/ |
30 | void sock_destroy(p_sock ps) | 30 | void sock_destroy(p_sock ps) |
31 | { | 31 | { |
32 | closesocket(*ps); | 32 | if (*ps != SOCK_INVALID) { |
33 | *ps = SOCK_INVALID; | 33 | closesocket(*ps); |
34 | *ps = SOCK_INVALID; | ||
35 | } | ||
34 | } | 36 | } |
35 | 37 | ||
36 | /*-------------------------------------------------------------------------*\ | 38 | /*-------------------------------------------------------------------------*\ |