Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed autoclose not closing udp sockets. added docs for autoclose #26

Merged
merged 1 commit into from
Mar 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/us/reference.html
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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