lua-users home
lua-l archive

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


Hi,

I never received so many requests in one message, but hopefully I will
get away with not writing extra code. :o) Turns out you
can do most of your stuff in Lua, the Lua C API and the mini
LuaSocket API.

> - Install hook functions which are called whenever a socket is
>   created or closed, like:

The creation callback can be done by encapsulating the socket
creation functions. These are mainly socket.tcp and socket.udp and you
can replace both directly in the socket namespace.
Server:accept() also "creates" a socket and is harder to encapsulate,
but see bellow for an idea.

For the close callback, you need to encapsulate both the any:close()
method and the garbage collector metamethod. Since you are already
encapsulating the socket creation functions, you might as well return
your own object, and encapsulate all socket objects in, say, a table.
Your object can forward methods to the LuaSoket object and trap the ones
you want to trap.

>   BTW, t_sock is int, even in Windows, where it is declared HANDLE,
>   it is still int., therefore rather:

Right, but HANDLE could be a pointer, and a pointer does not necessarily
fit in an int.

> - A C-function to detect if at a specific stack index there is a socket.
>   You have those internally, but I'd need to use them too.

Compiling LuaSocket with LUASOCKET_DEBUG defines a field "class" that
contains the class of the socket object. You can use that from Lua, just
like my test scripts do.  Otherwise, if you really want to do it in C,
take a look at auxiliar.c and you will see how I do it.

> - Access the socket descriptor inside such a userdata object at a
>   specific position in the stack.

If the object really has a descriptor, it will be in group
"select{able}", and will have a "getfd" method that does just that.
See the select.c module to see how it uses that.

> - A C-function to push a socket userdata object, when I have an "int fd".

This is kind of hard, because the methods depend on the kind of socket
you are dealing with. However, if you know it is, say, a TCP socket,
then you can just take a look at tcp.c (global_create) and see how it is
done.

> What do you think? Could you publish these hooks and functions?

You are free to "patch" LuaSocket to your needs, but I am not sure you
will really need to interface so deeply with it.  Let me know if you
simply cannot make your code work with the above ideas.

Regards,
Diego.

PS: I am on my way to Brazil right now, so I will be out of e-mail for a
few days.