lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


duck escribió:
In your examples, you mention:

    --  I hacked LuaSocket to simplify this but,
    --  in order to use a stock distribution of LuaSocket,
    --  I'm going to use code based in a kind suggestion
    --  from Diego Nehab.

Any chance of posting the patches you wrote here so I can have a look at them? I don't mind the bind-listen-close trick but it would be nice to avoid having to open listneing sockets unnecessarily. Wiould help to avoid
endpoint firewall nuisances and complaints from admins about "large amounts
of socket activity on your PC" :-)

Use the attached patch with care because I had to update it from "tcp.c,v 1.31" to "tcp.c,v 1.41".
I had no time to test it with the current LuaSocket release.

Best regards,
Daniel

--- tcp.c	2006-04-27 00:23:21.000000000 -0300
+++ hack-tcp.c	2007-05-19 13:41:42.000000000 -0300
@@ -3,6 +3,8 @@
 * LuaSocket toolkit
 *
 * RCS ID: $Id: tcp.c,v 1.41 2005/10/07 04:40:59 diego Exp $
+*
+* attach/detach hack by Daniel Quintela
 \*=========================================================================*/
 #include <string.h> 
 
@@ -19,6 +21,8 @@
 * Internal function prototypes
 \*=========================================================================*/
 static int global_create(lua_State *L);
+static int global_attach(lua_State *L);	// attach/detach hack
+static int global_attachserver(lua_State *L);	// attach/detach hack
 static int meth_connect(lua_State *L);
 static int meth_listen(lua_State *L);
 static int meth_bind(lua_State *L);
@@ -31,6 +35,7 @@
 static int meth_receive(lua_State *L);
 static int meth_accept(lua_State *L);
 static int meth_close(lua_State *L);
+static int meth_detach(lua_State *L);	// attach/detach hack
 static int meth_setoption(lua_State *L);
 static int meth_settimeout(lua_State *L);
 static int meth_getfd(lua_State *L);
@@ -60,6 +65,7 @@
     {"setsockname", meth_bind},
     {"settimeout",  meth_settimeout},
     {"shutdown",    meth_shutdown},
+    {"detach",      meth_detach},	// attach/detach hack
     {NULL,          NULL}
 };
 
@@ -75,6 +81,8 @@
 /* functions in library namespace */
 static luaL_reg func[] = {
     {"tcp", global_create},
+    {"attach", global_attach},	// attach/detach hack
+    {"attachserver", global_attachserver},	// attach/detach hack
     {NULL, NULL}
 };
 
@@ -237,6 +245,17 @@
 }
 
 /*-------------------------------------------------------------------------*\
+* Detaches socket used by object - attach/detach hack
+\*-------------------------------------------------------------------------*/
+static int meth_detach(lua_State *L)
+{
+    p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1);
+    tcp->sock = SOCK_INVALID;
+    lua_pushnumber(L, 1);
+    return 1;
+}
+
+/*-------------------------------------------------------------------------*\
 * Puts the sockt in listen mode
 \*-------------------------------------------------------------------------*/
 static int meth_listen(lua_State *L)
@@ -337,3 +356,38 @@
         return 2;
     }
 }
+
+/*-------------------------------------------------------------------------*\
+* Attaches a client tcp object - attach/detach hack
+\*-------------------------------------------------------------------------*/
+int global_attach(lua_State *L)
+{
+	t_socket skt = ( t_socket) luaL_checknumber(L, 1);
+    p_tcp client = lua_newuserdata(L, sizeof(t_tcp));
+    auxiliar_setclass(L, "tcp{client}", -1);
+    client->sock = skt;
+    /* initialize remaining structure fields */
+    io_init(&client->io, (p_send) socket_send, (p_recv) socket_recv,
+		(p_error) socket_ioerror, &client->sock);
+    timeout_init(&client->tm, -1, -1);
+    buffer_init(&client->buf, &client->io, &client->tm);
+    return 1;
+}
+
+/*-------------------------------------------------------------------------*\
+* Attaches a server tcp object - attach/detach hack
+\*-------------------------------------------------------------------------*/
+int global_attachserver(lua_State *L)
+{
+	t_socket skt = ( t_socket) luaL_checknumber(L, 1);
+    p_tcp server = lua_newuserdata(L, sizeof(t_tcp));
+    auxiliar_setclass(L, "tcp{server}", -1);
+    server->sock = skt;
+    /* initialize remaining structure fields */
+    io_init(&server->io, (p_send) socket_send, (p_recv) socket_recv,
+		(p_error) socket_ioerror, &server->sock);
+    timeout_init(&server->tm, -1, -1);
+    buffer_init(&server->buf, &server->io, &server->tm);
+    return 1;
+}
+