lua-users home
lua-l archive

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


Hello,

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 far :/

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?
end

First question:

- 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 Non-Preemptive Multithreading:

    function receive (connection)
      connection:timeout(0)   -- do not block
      local s, status = connection:receive(2^10)
      if status == "timeout" then
        coroutine.yield(connection)
      end
      return s, status
    end

Or a mix of the above? I'm already quite confused :P

Second question:

- What should I do for the output? See question for input :)

Third question:

- 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 PIL 9.4?

Some guidance much appreciated :))

Thanks!

Cheers

--
PA, Onnay Equitursay
http://alt.textdrive.com/