[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: LuaSocket, LTN12 and coroutines: how to?
- From: PA <petite.abeille@...>
- Date: Sat, 29 Jan 2005 00:03:44 +0100
I'm trying to wrap my head on how to write a simple server in Lua.
Unfortunately, I haven't managed to find any relevant code example so
Does anyone have some pointers to show how to put all this together?
In the meantime, I'm digging in LuaSocket documentation.
Setting up a server seems to be quite trivial:
local aSocket = require( "socket" )
local aServer = aSocket.try( aSocket.bind( "*", 1110 ) )
while ( true )
local aConnection = aServer:accept()
aConnection:settimeout( 10 )
-- and now what?
- What should I use to get the input? This?
local aLine, anException = aConnection:receive()
Or a LTN12 source perhaps?
local aSource = aSocket.source( "until-closed", aConnection )
Or do I need to roll my own to avoid blocking as shown in PIL's
function receive (connection)
connection:timeout(0) -- do not block
local s, status = connection:receive(2^10)
if status == "timeout" then
return s, status
Or a mix of the above? I'm already quite confused :P
- What should I do for the output? See question for input :)
- How do I tie the above to a coroutine for asynchronous processing?
I'm endlessly staring at Chapter 9.4 - Non-Preemptive Multithreading in
PIL, but I don't quite see what to do :P
Should I simply try something like this?
local aRoutine = coroutine.create( aMagicFunction( aSource ) )
someRoutines.insert( aRoutine )
And let the "dispatcher" loop through all those coroutines as shown in
Some guidance much appreciated :))
PA, Onnay Equitursay