[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: RE: Asynchronous socket library?
- From: "John Dunn" <John_Dunn@...>
- Date: Tue, 24 Mar 2009 13:22:53 -0700
> firstname.lastname@example.org] On Behalf Of Rob Kendrick
> This sounds almost precisely like what LuaSocket provides. It's a
> higher-level interface than Berkley (very easy to get going with),
> supports non-blocking calls (ie, select), doesn't require you to use
> co-routines, and works on Windows as well as Linux.
The problem is more with the way we plug lua into our application. The
user can define any number of scripts, each of which is loaded into
their own lua_State. The script itself is executed once on startup, and
the user can attach handlers to variable changes ( defined in our own
control engine ) or timer ticks. Script initialization and event
callback have to finish in a predetermined amount of timer or the script
is killed. A script might look something like
-- set the input value to the output, whenever the input changes
function OnInputChanged( control )
Controls.Outputs.Value = control.Value
-- attach to first input control
Controls.Inputs.EventHandler = OnInputChanged
We also have a timer available -
-- increment the output value each tick
Controls.Outputs.Value = Controls.Outputs.Value + 1
Timer.EventHandler = OnTimerTick
-- run timer every 1 sec
So the script is really an initialization script that attaches a bunch
of handlers to events - it's sorta similar to what scripting a GUI might
look like. The handlers are called from C using lua_pcall.
The user could implement a state machine in a timer handler which used
LuaSockets and select so it could return quickly. I've also played with
using coroutines inside our timer handler and that works as well.
while true do
co = coroutine.create(OnTimer)
Timer.EventHandler = function() coroutine.resume(co) end
My main issues with these approaches is that the script would look
pretty ugly and if the user passed the wrong arguments to the Socket
calls it would be possible to 'wedge' the script. The nice thing about
the truly asynchronous model is the user can't screw things up.