Skip to content

Commit

Permalink
fixed autoclose not closing udp sockets. added docs for autoclose
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieske committed Mar 6, 2015
1 parent a76c8a9 commit 4e3111a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
7 changes: 7 additions & 0 deletions doc/us/reference.html
Expand Up @@ -84,6 +84,13 @@ <h2>Reference</h2>
registered handlers as long as it uses the Copas socket functions.
</dd>

<dt><strong><code>copas.autoclose</code></strong></dt>
<dd>Constant that controls whether sockets are automatically closed.<br />
When a TCP handler function completes and terminates, then the client
socket will be automatically closed when <code>copas.autoclose</code> is
truthy (neither <code>nil</code> nor <code>false</code>).
</dd>

<dt><strong><code>copas.finished()</code></strong></dt>
<dd>Checks whether anything remains to be done.<br />
Returns <code>false</code> when the sockets lists for reading and writing
Expand Down
15 changes: 11 additions & 4 deletions src/copas/copas.lua
Expand Up @@ -189,6 +189,11 @@ local _writing = newset() -- sockets currently being written
-------------------------------------------------------------------------------
-- Coroutine based socket I/O functions.
-------------------------------------------------------------------------------

local function isTCP(socket)
return string.sub(tostring(socket),1,3) ~= "udp"
end

-- reads a pattern from a client and yields to the reading set on timeouts
-- UDP: a UDP socket expects a second argument to be a number, so it MUST
-- be provided as the 'pattern' below defaults to a string. Will throw a
Expand Down Expand Up @@ -397,7 +402,9 @@ local function _doTick (co, skt, ...)
new_q:push (res, co)
else
if not ok then pcall (_errhandlers [co] or _deferror, res, co, skt) end
if skt and copas.autoclose then skt:close() end --TODO: should UDP socket be closed???? will close server!, should then also be removed from the _servers and _reading list!
if skt and copas.autoclose and isTCP(skt) then
skt:close() -- do not auto-close UDP sockets, as the handler socket is also the server socket
end
_errhandlers [co] = nil
end
end
Expand Down Expand Up @@ -440,10 +447,10 @@ local function addUDPserver(server, handler, timeout)
end

function copas.addserver(server, handler, timeout)
if string.sub(tostring(server),1,3) == "udp" then
addUDPserver(server, handler, timeout)
else
if isTCP(server) then
addTCPserver(server, handler, timeout)
else
addUDPserver(server, handler, timeout)
end
end

Expand Down

0 comments on commit 4e3111a

Please sign in to comment.