Reference
NOTE: Some functions require DNS lookups, which is handled internally
by LuaSocket. This is being done in a blocking manner. Hence every function
that accepts a hostname as an argument (e.g. tcp:connect(),
udp:sendto(), etc.) is potentially blocking on the DNS resolving part.
So either provide IP addresses (assuming the underlying OS will detect those and resolve
locally, non-blocking) or accept that the lookup might block.
Copas dispatcher main functions
The group of functions 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 new
serverand itshandlerto the dispatcher using an optionaltimeout.
serveris a LuaSocket server socket created usingsocket.bind().
handleris a function that receives a LuaSocket client socket and handles the communication with that client.
timeoutis the timeout for blocking I/O in seconds. The handler will be executed in parallel with other threads and the registered handlers as long as it uses the Copas socket functions. copas.addthread(func [, ...])- Adds a function as a new coroutine/thread to the dispatcher. The optional
parameters will be passed to the function
func.
The thread will be executed in parallel with other threads and the registered handlers as long as it uses the Copas socket/sleep functions. It returns the created coroutine. copas.autoclose- Constant that controls whether sockets are automatically closed.
When a TCP handler function completes and terminates, then the client socket will be automatically closed whencopas.autocloseis truthy (neithernilnorfalse). copas.finished()- Checks whether anything remains to be done.
Returnsfalsewhen the socket lists for reading and writing are empty and there is not another (sleeping) task to execute. NOTE: when tasks or sockets have been scheduled/setup this function will returntrueeven if the loop has not yet started. See alsocopas.running. copas.loop([timeout])- Starts the Copas 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 by
socket.accept(). Thetimeoutparameter is optional, and is passed to thecopas.step()function. The loop returns whencopas.finished() == true. copas.removeserver(skt [, keep_open])- Removes a server socket from the Copas scheduler.
By default, the socket will be closed to allow the socket to be reused right away after
removing the server. If
keep_openistrue, the socket is removed from the scheduler but it is not closed. copas.running- A flag set to
truewhencopas.loop()starts, and reset tofalsewhen the loop exits. See alsocopas.finished(). 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 by
socket.accept(). Thetimeoutparameter is optional. It returnsfalsewhen no data was handled (timeout) ortrueif there was data handled (or alternatively nil + error message in case of errors). NOTE: thecopas.runningflag will not automatically be (un)set. So when using your own main loop, consider manually setting the flag. copas.setErrorHandler(func)- Sets the error handling function for the current thread. Any errors will be forwarded to the handler, which will receive the error, coroutine, and socket as arguments.
Non-blocking data exchange and timer/sleep functions
These are used by the handler functions to exchange data with
the clients, and by threads registered with addthread to
exchange data with other services.
copas.connect(skt, address, port)- Connects and transforms a master socket to a client just like LuaSocket
socket:connect(). The Copas version does not block and allows the multitasking of the other handlers and threads. copas.dohandshake(skt, sslparams)- Performs an ssl handshake on an already connected TCP client socket. It returns the new ssl-socket on success, or throws an error on failure.
copas.flush(skt)- (deprecated)
copas.handler(connhandler [, sslparams])- Wraps the
connhandlerfunction. Returns a new function that wraps the client socket, and (ifsslparamsis provided) performs the ssl handshake, before callingconnhandler. copas.receive(skt [, pattern])(TCP) orcopas.receive(size)(UDP)- Reads data from a client socket according to a pattern just like LuaSocket
socket:receive(). The Copas version does not block and allows the multitasking of the other handlers and threads. Note: for UDP sockets thesizeparameter is NOT optional. For the wrapped functionsocket:receive([size])it is optional again. copas.receivefrom(skt [, size])- Reads data from a UDP socket just like LuaSocket
socket:receivefrom(). The Copas version does not block and allows the multitasking of the other handlers and threads. copas.send(skt, data [, i [, j]])(TCP) orcopas.send(skt, datagram)(UDP)- Sends data to a client socket just like
socket:send(). The Copas version is buffered and does not block, allowing the multitasking of the other handlers and threads. copas.sendto(skt, datagram, ip, port)- Sends data over a UDP socket just like LuaSocket
socket:sendto(). The Copas version does not block and allows the multitasking of the other handlers and threads. copas.sleep([sleeptime])- Pauses the current co-routine. Parameter
sleeptime(in seconds) is optional and defaults to 0. Ifsleeptime < 0then it will sleep until explicitly woken by a call tocopas.wakeup(). copas.wakeup(co)cois the coroutine to wakeup.copas.wrap(skt [, sslparams] )- Wraps a LuaSocket socket and returns a Copas socket that implements LuaSocket's API
but use Copas' methods like
copas.send()andcopas.receive()automatically. If thesslparamsis provided, then a call to the wrappedskt:connect()method will automatically include the handshake (and in that caseconnect()might throw an error instead of returning nil+error, seecopas.dohandshake()). To use ssl with defaults; provide an empty table.
High level request functions
The last ones are the higher level client functions to perform requests to (remote) servers.
copas.http.request(url [, body]) or
copas.http.request(requestparams)requestparams.redirect == "all"
copas.ftp.put(url, content) or
copas.ftp.put(requestparams)copas.ftp.get(url) or
copas.ftp.get(requestparams)copas.smtp.send(msgparams)copas.smtp.message(msgt)socket.smtp.message, provided so the copas.smtp
module is a drop-in replacement for the socket.smtp module
copas.limit.new(max)max number
of running tasks. Eg. 100 http requests, in a set with max == 10, then no more than
10 requests will be performed simultaneously. Only when a request finishes, the next will be started.
limitset:addthread(func [, ...])copas.addthread, except that it operates within the limits of
the set of running tasks.
limitset:wait()