Comparison

net/server_select.lua @ 3382:fea0f8e19e4c

net.server_select: Ensure that onconnect is called for server sockets as well as clients, rather than onincoming. Fixes mod_console traceback, issue #197
author Matthew Wild <mwild1@gmail.com>
date Fri, 16 Jul 2010 22:28:50 +0100
parent 3312:12fc7e005e8b
child 3398:abc4a52aef02
comparison
equal deleted inserted replaced
3381:28cb5ad72870 3382:fea0f8e19e4c
165 165
166 maxconnections = maxconnections or _maxclientsperserver 166 maxconnections = maxconnections or _maxclientsperserver
167 167
168 local connections = 0 168 local connections = 0
169 169
170 local dispatch, disconnect = listeners.onincoming, listeners.ondisconnect 170 local dispatch, disconnect = listeners.onconnect or listeners.onincoming, listeners.ondisconnect
171 171
172 local accept = socket.accept 172 local accept = socket.accept
173 173
174 --// public methods of the object //-- 174 --// public methods of the object //--
175 175
621 receive = socket.receive 621 receive = socket.receive
622 shutdown = ( ssl and id ) or socket.shutdown 622 shutdown = ( ssl and id ) or socket.shutdown
623 623
624 _socketlist[ socket ] = handler 624 _socketlist[ socket ] = handler
625 _readlistlen = addsocket(_readlist, socket, _readlistlen) 625 _readlistlen = addsocket(_readlist, socket, _readlistlen)
626 if listeners.onconnect then
627 _sendlistlen = addsocket(_sendlist, socket, _sendlistlen)
628 handler.sendbuffer = function ()
629 listeners.onconnect(handler);
630 handler.sendbuffer = _sendbuffer;
631 if bufferqueuelen > 0 then
632 return _sendbuffer();
633 end
634 end
635 end
636 return handler, socket 626 return handler, socket
637 end 627 end
638 628
639 id = function( ) 629 id = function( )
640 end 630 end
852 842
853 local wrapclient = function( socket, ip, serverport, listeners, pattern, sslctx ) 843 local wrapclient = function( socket, ip, serverport, listeners, pattern, sslctx )
854 local handler = wrapconnection( nil, listeners, socket, ip, serverport, "clientport", pattern, sslctx ) 844 local handler = wrapconnection( nil, listeners, socket, ip, serverport, "clientport", pattern, sslctx )
855 _socketlist[ socket ] = handler 845 _socketlist[ socket ] = handler
856 _sendlistlen = addsocket(_sendlist, socket, _sendlistlen) 846 _sendlistlen = addsocket(_sendlist, socket, _sendlistlen)
847 if listeners.onconnect then
848 -- When socket is writeable, call onconnect
849 local _sendbuffer = handler.sendbuffer;
850 handler.sendbuffer = function ()
851 listeners.onconnect(handler);
852 handler.sendbuffer = _sendbuffer;
853 -- If there was data with the incoming packet, handle it now.
854 if #handler:bufferqueue() > 0 then
855 return _sendbuffer();
856 end
857 end
858 end
857 return handler, socket 859 return handler, socket
858 end 860 end
859 861
860 local addclient = function( address, port, listeners, pattern, sslctx ) 862 local addclient = function( address, port, listeners, pattern, sslctx )
861 local client, err = luasocket.tcp( ) 863 local client, err = luasocket.tcp( )