Finished implementation of LuaSocket 2.0 alpha on Linux.
Some testing still needed.
This commit is contained in:
parent
f330540576
commit
71f6bb60bf
41 changed files with 700 additions and 339 deletions
61
src/udp.c
61
src/udp.c
|
@ -1,5 +1,6 @@
|
|||
/*=========================================================================*\
|
||||
* UDP object
|
||||
* LuaSocket toolkit
|
||||
*
|
||||
* RCS ID: $Id$
|
||||
\*=========================================================================*/
|
||||
|
@ -13,7 +14,6 @@
|
|||
#include "auxiliar.h"
|
||||
#include "socket.h"
|
||||
#include "inet.h"
|
||||
#include "error.h"
|
||||
#include "udp.h"
|
||||
|
||||
/*=========================================================================*\
|
||||
|
@ -29,9 +29,12 @@ static int meth_getpeername(lua_State *L);
|
|||
static int meth_setsockname(lua_State *L);
|
||||
static int meth_setpeername(lua_State *L);
|
||||
static int meth_close(lua_State *L);
|
||||
static int meth_setoption(lua_State *L);
|
||||
static int meth_timeout(lua_State *L);
|
||||
static int meth_fd(lua_State *L);
|
||||
static int meth_dirty(lua_State *L);
|
||||
static int opt_dontroute(lua_State *L);
|
||||
static int opt_broadcast(lua_State *L);
|
||||
|
||||
/* udp object methods */
|
||||
static luaL_reg udp[] = {
|
||||
|
@ -45,11 +48,20 @@ static luaL_reg udp[] = {
|
|||
{"receivefrom", meth_receivefrom},
|
||||
{"timeout", meth_timeout},
|
||||
{"close", meth_close},
|
||||
{"setoption", meth_setoption},
|
||||
{"__gc", meth_close},
|
||||
{"fd", meth_fd},
|
||||
{"dirty", meth_dirty},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
/* socket options */
|
||||
static luaL_reg opt[] = {
|
||||
{"dontroute", opt_dontroute},
|
||||
{"broadcast", opt_broadcast},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
/* functions in library namespace */
|
||||
static luaL_reg func[] = {
|
||||
{"udp", global_create},
|
||||
|
@ -91,7 +103,9 @@ static int meth_send(lua_State *L)
|
|||
err = sock_send(&udp->sock, data, count, &sent, tm_get(tm));
|
||||
if (err == IO_DONE) lua_pushnumber(L, sent);
|
||||
else lua_pushnil(L);
|
||||
error_push(L, err);
|
||||
/* a 'closed' error on an unconnected means the target address was not
|
||||
* accepted by the transport layer */
|
||||
io_pusherror(L, err == IO_CLOSED ? IO_REFUSED : err);
|
||||
return 2;
|
||||
}
|
||||
|
||||
|
@ -118,7 +132,9 @@ static int meth_sendto(lua_State *L)
|
|||
(SA *) &addr, sizeof(addr), tm_get(tm));
|
||||
if (err == IO_DONE) lua_pushnumber(L, sent);
|
||||
else lua_pushnil(L);
|
||||
error_push(L, err == IO_CLOSED ? IO_REFUSED : err);
|
||||
/* a 'closed' error on an unconnected means the target address was not
|
||||
* accepted by the transport layer */
|
||||
io_pusherror(L, err == IO_CLOSED ? IO_REFUSED : err);
|
||||
return 2;
|
||||
}
|
||||
|
||||
|
@ -137,7 +153,7 @@ static int meth_receive(lua_State *L)
|
|||
err = sock_recv(&udp->sock, buffer, count, &got, tm_get(tm));
|
||||
if (err == IO_DONE) lua_pushlstring(L, buffer, got);
|
||||
else lua_pushnil(L);
|
||||
error_push(L, err);
|
||||
io_pusherror(L, err);
|
||||
return 2;
|
||||
}
|
||||
|
||||
|
@ -164,7 +180,7 @@ static int meth_receivefrom(lua_State *L)
|
|||
return 3;
|
||||
} else {
|
||||
lua_pushnil(L);
|
||||
error_push(L, err);
|
||||
io_pusherror(L, err);
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
@ -174,14 +190,14 @@ static int meth_receivefrom(lua_State *L)
|
|||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_fd(lua_State *L)
|
||||
{
|
||||
p_udp udp = (p_udp) aux_checkclass(L, "udp{any}", 1);
|
||||
p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1);
|
||||
lua_pushnumber(L, udp->sock);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int meth_dirty(lua_State *L)
|
||||
{
|
||||
p_udp udp = (p_udp) aux_checkclass(L, "udp{any}", 1);
|
||||
p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1);
|
||||
(void) udp;
|
||||
lua_pushboolean(L, 0);
|
||||
return 1;
|
||||
|
@ -202,6 +218,37 @@ static int meth_getsockname(lua_State *L)
|
|||
return inet_meth_getsockname(L, &udp->sock);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Option handlers
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int meth_setoption(lua_State *L)
|
||||
{
|
||||
return aux_meth_setoption(L, opt);
|
||||
}
|
||||
|
||||
static int opt_boolean(lua_State *L, int level, int name)
|
||||
{
|
||||
p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1);
|
||||
int val = aux_checkboolean(L, 2);
|
||||
if (setsockopt(udp->sock, level, name, (char *) &val, sizeof(val)) < 0) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, "setsockopt failed");
|
||||
return 2;
|
||||
}
|
||||
lua_pushnumber(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int opt_dontroute(lua_State *L)
|
||||
{
|
||||
return opt_boolean(L, SOL_SOCKET, SO_DONTROUTE);
|
||||
}
|
||||
|
||||
static int opt_broadcast(lua_State *L)
|
||||
{
|
||||
return opt_boolean(L, SOL_SOCKET, SO_BROADCAST);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Just call tm methods
|
||||
\*-------------------------------------------------------------------------*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue