lua-users home
lua-l archive

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


Hi,

>> That is a pretty simple thing. If I made a simple patch to
>> that effect, would you consider adopting it?
>
> It would depend on a series of factors. The most
> important is whether it can be done just by *adding* code,
> instead of changing code. I still believe it can.

I would add a few lines to the C source of luasocket.
These would be of no consequence whatsoever to users
who are not aware of the new feature.

> Can't you you change the __index table of the metatables for
> classes tcp{server}, tcp{client} and tcp{master} to
> something you can use to trap method calls? You can even
> save the previous methods as upvalues of the new ones.
> I haven't tried, but it looks like you might be able to do
> this without changing anything in LuaSocket.

Maybe I'm just too Lua-illiterate for this. The description
alone sounds horrible to me ;). With the previous version
of luasocket I did something roughly like this:

Provided inside luasocket C source:

  void (*created_socket)(lua_State *, int fd) = 0;
  void (*deleted_socket)(lua_State *, int fd) = 0;

Added a few lines to luasocket C source that called the above
hooks appropriately. Alltogether less than 20 trivial lines of
code.


In my application:

   void luasocket_created (lua_State *, int fd) {
     // added fd to my set of polled things.
     }
   void luasocket_deleted (lua_State *, int fd) {
     // removed fd from my set of polled things.
     }

Stored those functions in above hooks. In my mainloop:

   // When luasocket readable, call a registered
   // Lua method to handle it.

The key is that I need to hook the Lua sockets into my own
C++ written otherwise super-important and complex mainloop.

A little bit of additional Lua-C-Api glue code inside my
application is used for associating handling methods with
luasockets.

> The other factor would be how popular such a feature
> (assuming it can't be done already) would be.

This feature would be useful for anybody integrating
luasocket into a C/C++ application that runs its own
event loop.

I don't claim that the approach is the most elegant.
It works with the minimum change to luasocket's C source.

Regards,
Dirk