Introduction to Copas
Copas is a dispatcher that can help a lot in the creation of request/response servers based on LuaSocket.
Assuming you know how to implement the desired protocol, the first thing you have to do is create a server socket to receive the client connections. To do this you have to bind a host and a port using
server = socket.bind(host, port)
Then you have to create a handler function that implements the desired protocol.
The handler function will be called with a socket for each client connection
and you can use copas.send()
and copas.receive()
on that socket to
exchange data with the client.
For example, a simple echo handler would be:
function echoHandler(skt) while true local data = copas.receive(skt) if data == "quit" then break end copas.send(skt, data) end end
If all you will do with the socket is send and receive data, you may alternatively use
copas.wrap()
to let your code more close to the LuaSocket way of life:
function echoHandler(skt) skt = copas.wrap(skt) while true local data = skt:receive() if data == "quit" then break end skt:send(data) end end
To register the server socket and associate it with the corresponding handler we do:
copas.addserver(server, EchoHandler)
And to start Copas and all the registered servers we call:
copas.loop()
As long as every handler uses Copas's send
and receive
,
simultaneous connections will be handled transparently by Copas.
Why use Copas?
In a basic LuaSocket server usually there is a dispatcher loop like the one below:
server = socket.bind(host, port) while true skt = server:accept() handle(skt) end
where handle
is a function that implements the server protocol using LuaSocket's
socket functions:
function handle(skt) ... -- gets some data from the client - "the request" reqdata = skt:receive(pattern) ... -- sends some data to the client - "the response" skt:send(respdata) ... end
The problem with that approach is that the dispatcher loop is doing a busy wait
and can handle just one connection at a time. To solve the busy waiting we can
use LuaSocket's socket.select()
, like in:
server = socket.bind(host, port) reading = {server} while true input = socket.select(reading) skt = input:accept() handle(skt) end
While this helps our CPU usage, the server is still accepting only one client connection at a time. To handle more than one client the server must be able to multitask, and the solution usually involves some kind of threads.
The dispatcher loop then becomes something like:
server = socket.bind(host, port) reading = {server} while true input = socket.select(reading) skt = input:accept() newthread(handle(skt)) end
where newthread
is able to create a new thread that executes
independently the handler function.
The use of threads in the new loop solves the multitasking problem but may create another. Some platforms does not offer multithreading or maybe you don't want to use threads at all.
If that is the case, using Lua's coroutines may help a lot, and that's exactly what Copas does. Copas implements the dispatcher loop using coroutines so the handlers can multitask without the use of threads.
Using Copas with an existing server
If you already have a running server using some dispatcher like the previous ones, migrating to Copas is quite simple, usually consisting of just three steps.
First each server socket and its handler function have to be registered with Copas:
server = socket.bind(host, port) copas.addserver(server, handle)
Secondly the server handler has to be changed to use Copas. One solution
is to use Copas send
and receive
functions:
function handle(skt) ... -- gets some data from the client - "the request" reqdata = copas.receive(skt, pattern) ... -- sends some data to the client - "the response" copas.send(skt, respdata) ... end
the alternative is to wrap the received socket:
function handle(skt) skt = copas.wrap(skt) -- now skt behaves like a LuaSocket socket but uses Copas' ... -- gets some data from the client - "the request" reqdata = skt:receive(pattern) ... -- sends some data to the client - "the response" skt:send(respdata) ... end
Finally, to run the dispatcher infinite loop you just call:
copas.loop()
During the loop Copas' dispatcher accepts connections from clients and automatically calls the corresponding handler functions.
If you do not want copas to enter an infinite loop (maybe you have to
respond to events from other sources, such as an user interface), you should
have your own loop and call copas.step()
at each iteration of
the loop:
while condition do copas.step() -- processing for other events here end