In response to Coda:
I don't agree that opening up file handle creation to the C side of Lua would mean we have to forgo an appropriate level of security. Lua does have a fair bit of type checking code to make sure its receiving file handles in the io library... The easiest way to fool it is to take any userdata you want and set the metatable to the "FILE*" table in the registry -- if you use the right io function you can cause a segfault if it looks at non-existent members of the userdata. This would be easier in Lua 5.1 because you have newproxy() to create a zero-sized userdata. :-) Still -- *SECURITY* :D
In response to Sean:
Unfortunately I am not too familiar with the ins and outs of using poll() or epoll() -- select() was the simplest for me to grasp and I did that by copying fd sets into tables and back. Don't have much more to say about that... :x
I was kind of a snob when I made my socket binding. I saw the io library and how friendly it was compared to recv() and send() and I desperately wanted to fool those functions into working on my sockets. I didn't want to wind up writing my own compatibility layer to the io functions or writing functions that were very similar -- I wanted to use THOSE functions with no shim. I thought if I could orient my bindings so that they took Lua file handles and derived the necessary other type (fd on Linux, HANDLE on Windows) then I would be walking down Easy Street :] For the most part it worked -- I have not had to duplicate any io functions and this means other libraries can operate on my sockets without knowing their not regular files. Otherwise they error just like an invalid operation on a file would.
What I did in code I haven't yet committed is I backed the Lua file handle table with my lsock networking table so if it can't find :recv() in the first it chains to the 2nd. It's sneaky but I'm not sure I'd do it in production because it means modifying the __index of the "FILE*" table.
I have actually [successfully] written to a UDP-packeted socket with io.write(), but I didn't do more experimenting to see if io.read() worked just as well. What I loved most about using Lua file handles is that if the underlying socket doesn't support an operation or has already disconnected or the operation fails for whatever reason, the io.read/write functions will just error with: return nil, strerror(errno), errno
My favorite part is that my error handling code matches how an error would be propagated in the io functions. I swear I got the biggest thrill out of seeing it fail to write to a socket sometimes.
I think I'll go back on what I said about :getfd(). I don't like fds being known to the Lua side of things, but a fair number of libraries expect that method to exist. It is somewhat common, but I think it only exists because people are representing files as different types (bad). :>
The main focus of my socket bindings is that I was trying to be as true to the C prototype as possible so it would be easy for beginners to take a C networking tutorial and translate it to Lua without much guessing or digging through documentation. Sometimes I feel like details about luasocket are hard to find...
I like your socket option code in net.c though, it's much friendlier than what I tried to do by adding every defined CONSTANT I could and having a get/setsockopt(). That's the only part of my code I really am not happy with yet. I still need to get it to compile in Visual Studio, though...
I'm jealous that you got netlua_interfaces() implemented ;-) I need to give lsock similar functionality. OH THE JOYS OF HAVING YET ANOTHER SOCKET LIBRARY :]
-peace-