lua-users home
lua-l archive

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


It was thus said that the Great Jonathan Castello once stated:
> 
> I sent this a few days ago, but I haven't seen any replies yet. I
> understand that people might just not know, but if someone could just give
> me a pointer to where I might find my answers (or even just tell me you
> don't know!), that would be fine.
> 
> Thanks again, and apologies for reposting,~Jonathan Castello
> 
> > I'm using non-blocking sockets with LuaSocket under Win32, and I find
> > myself looking for a way to tell if a connect() call failed. I can use
> > socket.select(nil, {sock}, 0) to see if the socket has connected
> > successfully, but I don't see any way to tell if it failed. Looking over
> > the WinSock documentation, I notice an 'exceptfds' parameter that comes
> > after the read and write tables, but LuaSocket doesn't appear to use it
> > at all (its source shows NULL used instead). The WinSock documentation
> > says that if a nonblocking socket failed to connect, passing it in as
> > part of 'exceptfds' would return the socket as part of that list.
> > 
> > Does anyone have any suggestions for how to work around this? Or better,
> > can anyone explain why 'exceptfds' is ignored in LuaSocket? I can't use
> > blocking sockets due to the environment I'm writing this script for (as
> > a plugin to a single-threaded process), and I don't want to lock up the
> > rest of the environment with blocking calls.

  I've been working on something similar under Linux, but have decided *not*
to use LuaSocket but to roll my own (the program is basically the skeleton
of a Unix daemon where the socket code is handled in C, and passes each
connection off to a Lua thread).  

  I can see two solutions for you:

	1. fix LuaSocket to use the exceptfds

	2. do something similar to what I did and handle the sockets in
	   C/C++ and handle the logic in Lua (although it took me several
	   long days to get it working how I wanted it to work).

  I can send you what I have, but it's Linux specific (uses epoll()) and has
a few deficiencies, and is meant more for incoming connections than outgoing
connections, but it does allow one to write code like:

	-- implement an echo service

	function main(socket)
	  while true do
	    socket:write(socket:read("*a"))
	  end
	end

and each connection is its own Lua thread.  It couldn't hurt to look at it.  

  Maybe.

  -spc (I didn't use LuaSocket because I wanted to learn how to embed Lua in
	an application and I had the skeleton of a daemon already lying 
	around ... )