net.server_select, net.server_event: Support for ondrain listener callback for when send buffer is empty

This commit is contained in:
Matthew Wild 2010-05-05 15:24:24 +01:00
parent 242d67b43e
commit 3c7eb58b08
2 changed files with 9 additions and 1 deletions

View file

@ -465,6 +465,8 @@ do
end end
function interface_mt:ontimeout() function interface_mt:ontimeout()
end end
function interface_mt:ondrain()
end
function interface_mt:onstatus() function interface_mt:onstatus()
debug("server.lua: Dummy onstatus()") debug("server.lua: Dummy onstatus()")
end end
@ -545,6 +547,7 @@ do
if succ then -- writing succesful if succ then -- writing succesful
interface.writebuffer = "" interface.writebuffer = ""
interface.writebufferlen = 0 interface.writebufferlen = 0
interface:ondrain();
if interface.fatalerror then if interface.fatalerror then
debug "closing client after writing" debug "closing client after writing"
interface:_close() -- close interface if needed interface:_close() -- close interface if needed

View file

@ -252,6 +252,7 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport
local dispatch = listeners.onincoming local dispatch = listeners.onincoming
local status = listeners.onstatus local status = listeners.onstatus
local disconnect = listeners.ondisconnect local disconnect = listeners.ondisconnect
local drain = listeners.ondrain
local bufferqueue = { } -- buffer array local bufferqueue = { } -- buffer array
local bufferqueuelen = 0 -- end of buffer array local bufferqueuelen = 0 -- end of buffer array
@ -284,6 +285,7 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport
dispatch = listeners.onincoming dispatch = listeners.onincoming
disconnect = listeners.ondisconnect disconnect = listeners.ondisconnect
status = listeners.onstatus status = listeners.onstatus
drain = listeners.ondrain
end end
handler.getstats = function( ) handler.getstats = function( )
return readtraffic, sendtraffic return readtraffic, sendtraffic
@ -437,7 +439,7 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport
end end
local _readbuffer = function( ) -- this function reads data local _readbuffer = function( ) -- this function reads data
local buffer, err, part = receive( socket, pattern ) -- receive buffer with "pattern" local buffer, err, part = receive( socket, pattern ) -- receive buffer with "pattern"
if not err or (err == "wantread" or err == "timeout") or (part and string_len(part) > 0) then -- received something if not err or (err == "wantread" or err == "timeout") then -- received something
local buffer = buffer or part or "" local buffer = buffer or part or ""
local len = string_len( buffer ) local len = string_len( buffer )
if len > maxreadlen then if len > maxreadlen then
@ -479,6 +481,9 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport
_sendlistlen = removesocket( _sendlist, socket, _sendlistlen ) -- delete socket from writelist _sendlistlen = removesocket( _sendlist, socket, _sendlistlen ) -- delete socket from writelist
_ = needtls and handler:starttls(nil, true) _ = needtls and handler:starttls(nil, true)
_writetimes[ handler ] = nil _writetimes[ handler ] = nil
if drain then
drain(handler)
end
_ = toclose and handler:close( ) _ = toclose and handler:close( )
return true return true
elseif byte and ( err == "timeout" or err == "wantwrite" ) then -- want write elseif byte and ( err == "timeout" or err == "wantwrite" ) then -- want write