Reference
Copas functions are separated in two groups.
The first group is relative to the use of the dispatcher itself and are used to register servers and to execute the main loop of Copas:
-
copas.addserver(server, handler[, timeout])
Adds a newserver
and itshandler
to the dispatcher using an optionaltimeout
.
server
is a LuaSocket server socket created usingsocket.bind()
.
handler
is a function that receives a LuaSocket client socket and handles the communication with that client.
timeout
is the timeout for blocking I/O in seconds. -
copas.loop(timeout)
Starts the Copas infinite loop accepting client connections for the registered servers and handling those connections with the corresponding handlers. Every time a server accepts a connection, Copas calls the associated handler passing the client socket returned byaccept()
. Thetimeout
parameter is optional. -
copas.step(timeout)
Executes one copas iteration accepting client connections for the registered servers and handling those connections with the corresponding handlers. When a server accepts a connection, Copas calls the associated handler passing the client socket returned byaccept()
. Thetimeout
parameter is optional.
The second group is used by the handler functions to exchange data with the clients:
-
copas.flush(skt)
Flushes a client write buffer.copas.flush()
is called from time to time bycopas.loop()
but it may be necessary to call it from the handler function. -
copas.receive(skt, pattern)
Reads data from a client socket according to a pattern just like LuaSocketsocket:receive()
. The Copas version does not block and allows the multitasking of the other handlers. -
copas.send(skt, data)
Sends data to a client socket just likesocket:send()
. The Copas version is buffered and does not block, allowing the multitasking of the other handlers. -
copas.wrap(skt)
Wraps a LuaSocket socket and returns a Copas socket that implements LuaSocket's API but use Copas' methodscopas.send()
andcopas.receive()
automatically.