Software /
code /
prosody
Changeset
2057:ee2929e620bb
server.lua: Add addsocket(), refactor all code to use it, fixes nasty socket object leak - moral of the story is to not duplicate code, and not prematurely optimise. But you knew that already didn't you?
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Mon, 26 Oct 2009 23:29:04 +0000 |
parents | 2050:adc1b92b8c6b |
children | 2058:77244fd4ca99 |
files | net/server.lua |
diffstat | 1 files changed, 18 insertions(+), 21 deletions(-) [+] |
line wrap: on
line diff
--- a/net/server.lua Thu Oct 22 04:47:40 2009 +0500 +++ b/net/server.lua Mon Oct 26 23:29:04 2009 +0000 @@ -407,9 +407,7 @@ handler.write = idfalse -- dont write anymore return false elseif socket and not _sendlist[ socket ] then - _sendlistlen = _sendlistlen + 1 - _sendlist[ _sendlistlen ] = socket - _sendlist[ socket ] = _sendlistlen + _sendlistlen = addsocket(_sendlist, socket, _sendlistlen) end bufferqueuelen = bufferqueuelen + 1 bufferqueue[ bufferqueuelen ] = data @@ -457,9 +455,7 @@ handler.write = write if noread then noread = false - _readlistlen = _readlistlen + 1 - _readlist[ socket ] = _readlistlen - _readlist[ _readlistlen ] = socket + _readlistlen = addsocket(_readlist, socket, _readlistlen) _readtimes[ handler ] = _currenttime end if nosend then @@ -550,16 +546,15 @@ handler.readbuffer = _readbuffer -- when handshake is done, replace the handshake function with regular functions handler.sendbuffer = _sendbuffer _ = status and status( handler, "ssl-handshake-complete" ) + _readlistlen = addsocket(_readlist, socket, _readlistlen) return true else out_put( "server.lua: error during ssl handshake: ", tostring(err) ) if err == "wantwrite" and not wrote then - _sendlistlen = _sendlistlen + 1 - _sendlist[ _sendlistlen ] = client + _sendlistlen = addsocket(_sendlist, client, _sendlistlen) wrote = true elseif err == "wantread" and not read then - _readlistlen = _readlistlen + 1 - _readlist [ _readlistlen ] = client + _readlistlen = addsocket(_readlist, client, _readlistlen) read = true else break; @@ -619,9 +614,7 @@ shutdown = id _socketlist[ socket ] = handler - _readlistlen = _readlistlen + 1 - _readlist[ _readlistlen ] = socket - _readlist[ socket ] = _readlistlen + _readlistlen = addsocket(_readlist, socket, _readlistlen) -- remove traces of the old socket @@ -653,9 +646,7 @@ shutdown = ( ssl and id ) or socket.shutdown _socketlist[ socket ] = handler - _readlistlen = _readlistlen + 1 - _readlist[ _readlistlen ] = socket - _readlist[ socket ] = _readlistlen + _readlistlen = addsocket(_readlist, socket, _readlistlen) return handler, socket end @@ -667,6 +658,15 @@ return false end +addsocket = function( list, socket, len ) + if not list[ socket ] then + len = len + 1 + list[ len ] = socket + list[ socket ] = len + end + return len; +end + removesocket = function( list, socket, len ) -- this function removes sockets from a list ( copied from copas ) local pos = list[ socket ] if pos then @@ -721,8 +721,7 @@ return nil, err end server:settimeout( 0 ) - _readlistlen = _readlistlen + 1 - _readlist[ _readlistlen ] = server + _readlistlen = addsocket(_readlist, server, _readlistlen) _server[ port ] = handler _socketlist[ server ] = handler out_put( "server.lua: new server listener on '", addr, ":", port, "'" ) @@ -844,9 +843,7 @@ local wrapclient = function( socket, ip, serverport, listeners, pattern, sslctx, startssl ) local handler = wrapconnection( nil, listeners, socket, ip, serverport, "clientport", pattern, sslctx, startssl ) _socketlist[ socket ] = handler - _sendlistlen = _sendlistlen + 1 - _sendlist[ _sendlistlen ] = socket - _sendlist[ socket ] = _sendlistlen + _sendlistlen = addsocket(_sendlist, socket, _sendlistlen) return handler, socket end