aboutsummaryrefslogtreecommitdiff
path: root/src/tcp.c
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2004-01-18 00:04:20 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2004-01-18 00:04:20 +0000
commitc8d58798f0b0c789df5c566494112f81ac302432 (patch)
tree40c52eab0a4bf92636ebe13027d165f0b8452bb1 /src/tcp.c
parent02ef4e7daae6bef629dcb568052755fb85ed2efc (diff)
downloadluasocket-c8d58798f0b0c789df5c566494112f81ac302432.tar.gz
luasocket-c8d58798f0b0c789df5c566494112f81ac302432.tar.bz2
luasocket-c8d58798f0b0c789df5c566494112f81ac302432.zip
Trying to get connect-with-timeout to work. Darwin works...
Diffstat (limited to 'src/tcp.c')
-rw-r--r--src/tcp.c69
1 files changed, 32 insertions, 37 deletions
diff --git a/src/tcp.c b/src/tcp.c
index fe09e8c..74f32f4 100644
--- a/src/tcp.c
+++ b/src/tcp.c
@@ -63,7 +63,7 @@ static luaL_reg tcp[] = {
63static luaL_reg opt[] = { 63static luaL_reg opt[] = {
64 {"keepalive", opt_keepalive}, 64 {"keepalive", opt_keepalive},
65 {"reuseaddr", opt_reuseaddr}, 65 {"reuseaddr", opt_reuseaddr},
66 {"tcp-nodelay", opt_tcp_nodelay}, 66 {"tcp-nodelay", opt_tcp_nodelay},
67 {"linger", opt_linger}, 67 {"linger", opt_linger},
68 {NULL, NULL} 68 {NULL, NULL}
69}; 69};
@@ -200,32 +200,26 @@ static int meth_dirty(lua_State *L)
200\*-------------------------------------------------------------------------*/ 200\*-------------------------------------------------------------------------*/
201static int meth_accept(lua_State *L) 201static int meth_accept(lua_State *L)
202{ 202{
203 struct sockaddr_in addr;
204 socklen_t addr_len = sizeof(addr);
205 int err = IO_ERROR;
206 p_tcp server = (p_tcp) aux_checkclass(L, "tcp{server}", 1); 203 p_tcp server = (p_tcp) aux_checkclass(L, "tcp{server}", 1);
207 p_tm tm = &server->tm; 204 p_tm tm = &server->tm;
208 p_tcp client;
209 t_sock sock; 205 t_sock sock;
210 tm_markstart(tm); 206 tm_markstart(tm);
211 /* loop until connection accepted or timeout happens */ 207 const char *err = inet_tryaccept(&server->sock, tm, &sock);
212 while (err != IO_DONE) { 208 /* if successful, push client socket */
213 err = sock_accept(&server->sock, &sock, 209 if (!err) {
214 (SA *) &addr, &addr_len, tm_getfailure(tm)); 210 p_tcp clnt = lua_newuserdata(L, sizeof(t_tcp));
215 if (err == IO_CLOSED || (err == IO_TIMEOUT && !tm_getfailure(tm))) { 211 aux_setclass(L, "tcp{client}", -1);
216 lua_pushnil(L); 212 /* initialize structure fields */
217 io_pusherror(L, err); 213 clnt->sock = sock;
218 return 2; 214 io_init(&clnt->io, (p_send)sock_send, (p_recv)sock_recv, &clnt->sock);
219 } 215 tm_init(&clnt->tm, -1, -1);
216 buf_init(&clnt->buf, &clnt->io, &clnt->tm);
217 return 1;
218 } else {
219 lua_pushnil(L);
220 lua_pushstring(L, err);
221 return 2;
220 } 222 }
221 client = lua_newuserdata(L, sizeof(t_tcp));
222 aux_setclass(L, "tcp{client}", -1);
223 client->sock = sock;
224 /* initialize remaining structure fields */
225 io_init(&client->io, (p_send) sock_send, (p_recv) sock_recv, &client->sock);
226 tm_init(&client->tm, -1, -1);
227 buf_init(&client->buf, &client->io, &client->tm);
228 return 1;
229} 223}
230 224
231/*-------------------------------------------------------------------------*\ 225/*-------------------------------------------------------------------------*\
@@ -260,7 +254,7 @@ static int meth_connect(lua_State *L)
260 p_tm tm = &tcp->tm; 254 p_tm tm = &tcp->tm;
261 const char *err; 255 const char *err;
262 tm_markstart(tm); 256 tm_markstart(tm);
263 err = inet_tryconnect(&tcp->sock, address, port, tm_getfailure(tm)); 257 err = inet_tryconnect(&tcp->sock, tm, address, port);
264 if (err) { 258 if (err) {
265 lua_pushnil(L); 259 lua_pushnil(L);
266 lua_pushstring(L, err); 260 lua_pushstring(L, err);
@@ -283,7 +277,7 @@ static int meth_close(lua_State *L)
283} 277}
284 278
285/*-------------------------------------------------------------------------*\ 279/*-------------------------------------------------------------------------*\
286* Shuts the connection down 280* Shuts the connection down partially
287\*-------------------------------------------------------------------------*/ 281\*-------------------------------------------------------------------------*/
288static int meth_shutdown(lua_State *L) 282static int meth_shutdown(lua_State *L)
289{ 283{
@@ -341,22 +335,23 @@ static int meth_settimeout(lua_State *L)
341\*-------------------------------------------------------------------------*/ 335\*-------------------------------------------------------------------------*/
342int global_create(lua_State *L) 336int global_create(lua_State *L)
343{ 337{
344 const char *err; 338 t_sock sock;
345 /* allocate tcp object */ 339 const char *err = inet_trycreate(&sock, SOCK_STREAM);
346 p_tcp tcp = (p_tcp) lua_newuserdata(L, sizeof(t_tcp));
347 /* set its type as master object */
348 aux_setclass(L, "tcp{master}", -1);
349 /* try to allocate a system socket */ 340 /* try to allocate a system socket */
350 err = inet_trycreate(&tcp->sock, SOCK_STREAM); 341 if (!err) {
351 if (err) { /* get rid of object on stack and push error */ 342 /* allocate tcp object */
352 lua_pop(L, 1); 343 p_tcp tcp = (p_tcp) lua_newuserdata(L, sizeof(t_tcp));
344 tcp->sock = sock;
345 /* set its type as master object */
346 aux_setclass(L, "tcp{master}", -1);
347 /* initialize remaining structure fields */
348 io_init(&tcp->io, (p_send) sock_send, (p_recv) sock_recv, &tcp->sock);
349 tm_init(&tcp->tm, -1, -1);
350 buf_init(&tcp->buf, &tcp->io, &tcp->tm);
351 return 1;
352 } else {
353 lua_pushnil(L); 353 lua_pushnil(L);
354 lua_pushstring(L, err); 354 lua_pushstring(L, err);
355 return 2; 355 return 2;
356 } 356 }
357 /* initialize remaining structure fields */
358 io_init(&tcp->io, (p_send) sock_send, (p_recv) sock_recv, &tcp->sock);
359 tm_init(&tcp->tm, -1, -1);
360 buf_init(&tcp->buf, &tcp->io, &tcp->tm);
361 return 1;
362} 357}