Persistent Soap Connection

Hi everyone,

Interesting project. I think that it will probably work. Probably, yes, it’s best to do this outside of the LuaUPnP process so that you are not at risk of breaking it with an apparent deadlock. (But still, why not try it inside a plugin first? If you’re writing the code in Lua anyway, you won’t have wasted that code if and when you have to move it to a daemon.)

The thing that’s going to be most tricky is doing it all in a single thread. Running as a daemon doesn’t absolve you of this requirement: Lua is single-threaded by design and doesn’t give you access to fork() either. The UPnP Event Proxy faced this too, faking the two network connections it has to do by cooperatively switching back and forth between listening on UDP and sending notifications to LuaUPnP on localhost3480. For this reason, although you can happily set the LuaSocket timeout to infinite (I think this is the default), I wouldn’t recommend it, because it will prevent other processes (say, a plugin running in LuaUPnP) from talking to the daemon.

If you really, really need to fork a separate process, then doing os.execute(“some-shell-script”) where some-shell-script backgrounds with & does work (it’s what the UPnP Event Proxy wrapper implementation does). But be sure to close your file descriptors manually, because os.execute() doesn’t set the close-on-exec flag, so your shell will inherit all of the parent process’s open sockets.

You may have to sidestep the LuaSocket http.* functions, because they do tend to close the socket after sending the request. http.request() is written in pure Lua on top of socket.tcp(), so you’ve got the code to refer to and can alter your own version as you need to keep the connection alive. Writing cooperative socket-conversation code that successfully avoids deadlock is “fun”. In a single thread, “lots of fun”.

I’ve licensed the UPnP Event Proxy code the same as Lua itself (MIT) so you’re invited to snaffle whatever code from there you like. You may be interested in the XML parsing code in particular. As for the rest, ask me. I’m not a world expert on SOAP, but I think I have a pretty good handle on LuaSocket, and I have HTTP down pat (I wrote a RESTful web service for a commercial product in my day job).