diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-01-18 00:04:20 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-01-18 00:04:20 +0000 |
commit | c8d58798f0b0c789df5c566494112f81ac302432 (patch) | |
tree | 40c52eab0a4bf92636ebe13027d165f0b8452bb1 /src/udp.c | |
parent | 02ef4e7daae6bef629dcb568052755fb85ed2efc (diff) | |
download | luasocket-c8d58798f0b0c789df5c566494112f81ac302432.tar.gz luasocket-c8d58798f0b0c789df5c566494112f81ac302432.tar.bz2 luasocket-c8d58798f0b0c789df5c566494112f81ac302432.zip |
Trying to get connect-with-timeout to work. Darwin works...
Diffstat (limited to 'src/udp.c')
-rw-r--r-- | src/udp.c | 64 |
1 files changed, 46 insertions, 18 deletions
@@ -29,6 +29,7 @@ static int meth_getpeername(lua_State *L); | |||
29 | static int meth_setsockname(lua_State *L); | 29 | 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_shutdown(lua_State *L); | ||
32 | static int meth_setoption(lua_State *L); | 33 | static int meth_setoption(lua_State *L); |
33 | static int meth_settimeout(lua_State *L); | 34 | static int meth_settimeout(lua_State *L); |
34 | static int meth_fd(lua_State *L); | 35 | static int meth_fd(lua_State *L); |
@@ -53,6 +54,7 @@ static luaL_reg udp[] = { | |||
53 | {"receivefrom", meth_receivefrom}, | 54 | {"receivefrom", meth_receivefrom}, |
54 | {"settimeout", meth_settimeout}, | 55 | {"settimeout", meth_settimeout}, |
55 | {"close", meth_close}, | 56 | {"close", meth_close}, |
57 | {"shutdown", meth_shutdown}, | ||
56 | {"setoption", meth_setoption}, | 58 | {"setoption", meth_setoption}, |
57 | {"__gc", meth_close}, | 59 | {"__gc", meth_close}, |
58 | {"fd", meth_fd}, | 60 | {"fd", meth_fd}, |
@@ -110,7 +112,7 @@ static int meth_send(lua_State *L) | |||
110 | int err; | 112 | int err; |
111 | const char *data = luaL_checklstring(L, 2, &count); | 113 | const char *data = luaL_checklstring(L, 2, &count); |
112 | tm_markstart(tm); | 114 | tm_markstart(tm); |
113 | err = sock_send(&udp->sock, data, count, &sent, tm_getsuccess(tm)); | 115 | err = sock_send(&udp->sock, data, count, &sent, tm_get(tm)); |
114 | if (err == IO_DONE) lua_pushnumber(L, sent); | 116 | if (err == IO_DONE) lua_pushnumber(L, sent); |
115 | else lua_pushnil(L); | 117 | else lua_pushnil(L); |
116 | /* a 'closed' error on an unconnected means the target address was not | 118 | /* a 'closed' error on an unconnected means the target address was not |
@@ -139,7 +141,7 @@ static int meth_sendto(lua_State *L) | |||
139 | addr.sin_port = htons(port); | 141 | addr.sin_port = htons(port); |
140 | tm_markstart(tm); | 142 | tm_markstart(tm); |
141 | err = sock_sendto(&udp->sock, data, count, &sent, | 143 | err = sock_sendto(&udp->sock, data, count, &sent, |
142 | (SA *) &addr, sizeof(addr), tm_getsuccess(tm)); | 144 | (SA *) &addr, sizeof(addr), tm_get(tm)); |
143 | if (err == IO_DONE) lua_pushnumber(L, sent); | 145 | if (err == IO_DONE) lua_pushnumber(L, sent); |
144 | else lua_pushnil(L); | 146 | else lua_pushnil(L); |
145 | /* a 'closed' error on an unconnected means the target address was not | 147 | /* a 'closed' error on an unconnected means the target address was not |
@@ -160,7 +162,7 @@ static int meth_receive(lua_State *L) | |||
160 | p_tm tm = &udp->tm; | 162 | p_tm tm = &udp->tm; |
161 | count = MIN(count, sizeof(buffer)); | 163 | count = MIN(count, sizeof(buffer)); |
162 | tm_markstart(tm); | 164 | tm_markstart(tm); |
163 | err = sock_recv(&udp->sock, buffer, count, &got, tm_getsuccess(tm)); | 165 | err = sock_recv(&udp->sock, buffer, count, &got, tm_get(tm)); |
164 | if (err == IO_DONE) lua_pushlstring(L, buffer, got); | 166 | if (err == IO_DONE) lua_pushlstring(L, buffer, got); |
165 | else lua_pushnil(L); | 167 | else lua_pushnil(L); |
166 | io_pusherror(L, err); | 168 | io_pusherror(L, err); |
@@ -182,7 +184,7 @@ static int meth_receivefrom(lua_State *L) | |||
182 | tm_markstart(tm); | 184 | tm_markstart(tm); |
183 | count = MIN(count, sizeof(buffer)); | 185 | count = MIN(count, sizeof(buffer)); |
184 | err = sock_recvfrom(&udp->sock, buffer, count, &got, | 186 | err = sock_recvfrom(&udp->sock, buffer, count, &got, |
185 | (SA *) &addr, &addr_len, tm_getsuccess(tm)); | 187 | (SA *) &addr, &addr_len, tm_get(tm)); |
186 | if (err == IO_DONE) { | 188 | if (err == IO_DONE) { |
187 | lua_pushlstring(L, buffer, got); | 189 | lua_pushlstring(L, buffer, got); |
188 | lua_pushstring(L, inet_ntoa(addr.sin_addr)); | 190 | lua_pushstring(L, inet_ntoa(addr.sin_addr)); |
@@ -341,8 +343,7 @@ static int meth_setpeername(lua_State *L) | |||
341 | unsigned short port = connecting ? | 343 | unsigned short port = connecting ? |
342 | (unsigned short) luaL_checknumber(L, 3) : | 344 | (unsigned short) luaL_checknumber(L, 3) : |
343 | (unsigned short) luaL_optnumber(L, 3, 0); | 345 | (unsigned short) luaL_optnumber(L, 3, 0); |
344 | const char *err; | 346 | const char *err = inet_tryconnect(&udp->sock, tm, address, port); |
345 | err = inet_tryconnect(&udp->sock, address, port, tm_getfailure(tm)); | ||
346 | if (err) { | 347 | if (err) { |
347 | lua_pushnil(L); | 348 | lua_pushnil(L); |
348 | lua_pushstring(L, err); | 349 | lua_pushstring(L, err); |
@@ -366,6 +367,33 @@ static int meth_close(lua_State *L) | |||
366 | } | 367 | } |
367 | 368 | ||
368 | /*-------------------------------------------------------------------------*\ | 369 | /*-------------------------------------------------------------------------*\ |
370 | * Shuts the connection down partially | ||
371 | \*-------------------------------------------------------------------------*/ | ||
372 | static int meth_shutdown(lua_State *L) | ||
373 | { | ||
374 | p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); | ||
375 | const char *how = luaL_optstring(L, 2, "both"); | ||
376 | switch (how[0]) { | ||
377 | case 'b': | ||
378 | if (strcmp(how, "both")) goto error; | ||
379 | sock_shutdown(&udp->sock, 2); | ||
380 | break; | ||
381 | case 's': | ||
382 | if (strcmp(how, "send")) goto error; | ||
383 | sock_shutdown(&udp->sock, 1); | ||
384 | break; | ||
385 | case 'r': | ||
386 | if (strcmp(how, "receive")) goto error; | ||
387 | sock_shutdown(&udp->sock, 0); | ||
388 | break; | ||
389 | } | ||
390 | return 0; | ||
391 | error: | ||
392 | luaL_argerror(L, 2, "invalid shutdown method"); | ||
393 | return 0; | ||
394 | } | ||
395 | |||
396 | /*-------------------------------------------------------------------------*\ | ||
369 | * Turns a master object into a server object | 397 | * Turns a master object into a server object |
370 | \*-------------------------------------------------------------------------*/ | 398 | \*-------------------------------------------------------------------------*/ |
371 | static int meth_setsockname(lua_State *L) | 399 | static int meth_setsockname(lua_State *L) |
@@ -391,21 +419,21 @@ static int meth_setsockname(lua_State *L) | |||
391 | \*-------------------------------------------------------------------------*/ | 419 | \*-------------------------------------------------------------------------*/ |
392 | int global_create(lua_State *L) | 420 | int global_create(lua_State *L) |
393 | { | 421 | { |
394 | const char *err; | 422 | t_sock sock; |
395 | /* allocate udp object */ | 423 | const char *err = inet_trycreate(&sock, SOCK_DGRAM); |
396 | p_udp udp = (p_udp) lua_newuserdata(L, sizeof(t_udp)); | ||
397 | /* set its type as master object */ | ||
398 | aux_setclass(L, "udp{unconnected}", -1); | ||
399 | /* try to allocate a system socket */ | 424 | /* try to allocate a system socket */ |
400 | err = inet_trycreate(&udp->sock, SOCK_DGRAM); | 425 | if (!err) { |
401 | if (err) { | 426 | /* allocate tcp object */ |
402 | /* get rid of object on stack and push error */ | 427 | p_udp udp = (p_udp) lua_newuserdata(L, sizeof(t_udp)); |
403 | lua_pop(L, 1); | 428 | udp->sock = sock; |
429 | /* set its type as master object */ | ||
430 | aux_setclass(L, "udp{unconnected}", -1); | ||
431 | /* initialize remaining structure fields */ | ||
432 | tm_init(&udp->tm, -1, -1); | ||
433 | return 1; | ||
434 | } else { | ||
404 | lua_pushnil(L); | 435 | lua_pushnil(L); |
405 | lua_pushstring(L, err); | 436 | lua_pushstring(L, err); |
406 | return 2; | 437 | return 2; |
407 | } | 438 | } |
408 | /* initialize timeout management */ | ||
409 | tm_init(&udp->tm, -1, -1); | ||
410 | return 1; | ||
411 | } | 439 | } |