Skip to content
This repository has been archived by the owner on Nov 20, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' of git://github.com/keplerproject/copas
Browse files Browse the repository at this point in the history
* 'master' of git://github.com/keplerproject/copas:
  Updated UDP functionality and accompanying documentation. Added receivefrom(), sendto() methods and updated the wrap() method to be UDP or TCP specific. Change is fully transparent to existing usage.
  Very minor update to step() function. To return a result based upon data handled or timeout. Fully transparent, no breaking of existing code. This tells the caller that it should asap resume IO (eg. next call to step()) because data is coming in, or alternatively it timed out and there is (some) time to do something else temporarily.
  Added possibility for UDP servers, based upon fix by Cuero Bugot. Added minor code to auto detect socket type (UDP/TCP), so all function signatures remain the same and handling is transparent. Also updated documentation.
  autoclose option, set to false to not close socket after handler finishes
  • Loading branch information
drahosp committed Aug 14, 2012
2 parents c83f82b + 1d1d827 commit ebb8378
Show file tree
Hide file tree
Showing 3 changed files with 234 additions and 101 deletions.
34 changes: 34 additions & 0 deletions doc/us/manual.html
Expand Up @@ -281,6 +281,40 @@ <h2><a name="using"></a>Using Copas with an existing server</h2>
<p>During the loop Copas' dispatcher accepts connections from clients and
automatically calls the corresponding handler functions.</p>

<h2><a name="udp"></a>Using UDP servers</h2>
<p>Copas may also be used for UDP servers. Here is an example;</p>
<pre class="example">
local port = 51034
local server = socket.udp()
server:setsockname("*",port)

function handler(skt)
skt = copas.wrap(skt)
print("UDP connection handler")

while true do
local s, err
print("receiving...")
s, err = skt:receive(2048)
if not s then
print("Receive error: ", err)
return
end
print("Received data, bytes:" , #s)
end
end

copas.addserver(server, handler, 1)
copas.loop()
</pre>
<p>For UDP sockets the <code>receivefrom()</code> and <code>sendto()</code>
methods are available, both for copas and when the socket is wrapped. These
methods cannot be used on TCP sockets.</p>
<p><strong>NOTE:</strong> When using the <code>copas.receive([size])</code> method
on a UDP socket, the <code>size</code> parameter is NOT optional as with regular
luasocket UDP sockets. This limitation is removed when the socket is wrapped
(it then defaults to 8192, the max UDP datagram size luasocket supports).</p>

<h2><a name="control"></a>Controlling Copas</h2>

<p>
Expand Down
3 changes: 3 additions & 0 deletions doc/us/reference.html
Expand Up @@ -96,6 +96,9 @@ <h2>Reference</h2>
handlers. When a server accepts a connection, Copas calls the
associated handler passing the client socket returned by
<code>socket.accept()</code>. The <code>timeout</code> parameter is optional.
It returns <code>false</code> when no data was handled (timeout) or
<code>true</code> if there was data handled (or alternatively nil + error
message in case of errors).
</dd>
</dl>

Expand Down

0 comments on commit ebb8378

Please sign in to comment.