Comparison

net/server_select.lua @ 3002:d1f864ed44c0

net.server_select, net.server_event: Support for ondrain listener callback for when send buffer is empty
author Matthew Wild <mwild1@gmail.com>
date Wed, 05 May 2010 15:24:24 +0100
parent 3001:fa00c6f563b4
child 3003:48871a549b4c
comparison
equal deleted inserted replaced
3001:fa00c6f563b4 3002:d1f864ed44c0
250 local ssl 250 local ssl
251 251
252 local dispatch = listeners.onincoming 252 local dispatch = listeners.onincoming
253 local status = listeners.onstatus 253 local status = listeners.onstatus
254 local disconnect = listeners.ondisconnect 254 local disconnect = listeners.ondisconnect
255 local drain = listeners.ondrain
255 256
256 local bufferqueue = { } -- buffer array 257 local bufferqueue = { } -- buffer array
257 local bufferqueuelen = 0 -- end of buffer array 258 local bufferqueuelen = 0 -- end of buffer array
258 259
259 local toclose 260 local toclose
282 end 283 end
283 handler.setlistener = function( self, listeners ) 284 handler.setlistener = function( self, listeners )
284 dispatch = listeners.onincoming 285 dispatch = listeners.onincoming
285 disconnect = listeners.ondisconnect 286 disconnect = listeners.ondisconnect
286 status = listeners.onstatus 287 status = listeners.onstatus
288 drain = listeners.ondrain
287 end 289 end
288 handler.getstats = function( ) 290 handler.getstats = function( )
289 return readtraffic, sendtraffic 291 return readtraffic, sendtraffic
290 end 292 end
291 handler.ssl = function( ) 293 handler.ssl = function( )
435 end 437 end
436 return noread, nosend 438 return noread, nosend
437 end 439 end
438 local _readbuffer = function( ) -- this function reads data 440 local _readbuffer = function( ) -- this function reads data
439 local buffer, err, part = receive( socket, pattern ) -- receive buffer with "pattern" 441 local buffer, err, part = receive( socket, pattern ) -- receive buffer with "pattern"
440 if not err or (err == "wantread" or err == "timeout") or (part and string_len(part) > 0) then -- received something 442 if not err or (err == "wantread" or err == "timeout") then -- received something
441 local buffer = buffer or part or "" 443 local buffer = buffer or part or ""
442 local len = string_len( buffer ) 444 local len = string_len( buffer )
443 if len > maxreadlen then 445 if len > maxreadlen then
444 disconnect( handler, "receive buffer exceeded" ) 446 disconnect( handler, "receive buffer exceeded" )
445 handler:close( true ) 447 handler:close( true )
477 bufferqueuelen = 0 479 bufferqueuelen = 0
478 bufferlen = 0 480 bufferlen = 0
479 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen ) -- delete socket from writelist 481 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen ) -- delete socket from writelist
480 _ = needtls and handler:starttls(nil, true) 482 _ = needtls and handler:starttls(nil, true)
481 _writetimes[ handler ] = nil 483 _writetimes[ handler ] = nil
484 if drain then
485 drain(handler)
486 end
482 _ = toclose and handler:close( ) 487 _ = toclose and handler:close( )
483 return true 488 return true
484 elseif byte and ( err == "timeout" or err == "wantwrite" ) then -- want write 489 elseif byte and ( err == "timeout" or err == "wantwrite" ) then -- want write
485 buffer = string_sub( buffer, byte + 1, bufferlen ) -- new buffer 490 buffer = string_sub( buffer, byte + 1, bufferlen ) -- new buffer
486 bufferqueue[ 1 ] = buffer -- insert new buffer in queue 491 bufferqueue[ 1 ] = buffer -- insert new buffer in queue