Comparison

net/server_event.lua @ 2561:e7bec4072c61

net.server_event: Update for compatibility with new server API
author Matthew Wild <mwild1@gmail.com>
date Sun, 31 Jan 2010 19:58:52 +0000
parent 2535:75bb31b6c53f
child 2599:cc6164d85024
comparison
equal deleted inserted replaced
2560:56063f825199 2561:e7bec4072c61
216 else 216 else
217 onsomething = self.onsslconnection 217 onsomething = self.onsslconnection
218 end 218 end
219 self:_start_session( onsomething ) 219 self:_start_session( onsomething )
220 debug( "ssl handshake done" ) 220 debug( "ssl handshake done" )
221 self:onstatus("ssl-handshake-complete");
221 self.eventhandshake = nil 222 self.eventhandshake = nil
222 return -1 223 return -1
223 end 224 end
224 debug( "error during ssl handshake:", err ) 225 debug( "error during ssl handshake:", err )
225 if err == "wantwrite" then 226 if err == "wantwrite" then
387 388
388 function interface_mt:set_send(new_send) 389 function interface_mt:set_send(new_send)
389 -- No-op, we always use the underlying connection's send 390 -- No-op, we always use the underlying connection's send
390 end 391 end
391 392
392 function interface_mt:starttls() 393 function interface_mt:starttls(sslctx)
393 debug( "try to start ssl at client id:", self.id ) 394 debug( "try to start ssl at client id:", self.id )
394 local err 395 local err
395 if not self._sslctx then -- no ssl available 396 self._sslctx = sslctx;
396 err = "no ssl context available" 397 if self._usingssl then -- startssl was already called
397 elseif self._usingssl then -- startssl was already called
398 err = "ssl already active" 398 err = "ssl already active"
399 end 399 end
400 if err then 400 if err then
401 debug( "error:", err ) 401 debug( "error:", err )
402 return nil, err 402 return nil, err
425 end 425 end
426 return false, "setoption not implemented"; 426 return false, "setoption not implemented";
427 end 427 end
428 428
429 function interface_mt:setlistener(listener) 429 function interface_mt:setlistener(listener)
430 self.onconnect, self.ondisconnect, self.onincoming, self.ontimeout 430 self.onconnect, self.ondisconnect, self.onincoming, self.ontimeout, self.onstatus
431 = listener.onconnect, listener.ondisconnect, listener.onincoming, listener.ontimeout; 431 = listener.onconnect, listener.ondisconnect, listener.onincoming, listener.ontimeout, listener.onstatus;
432 end 432 end
433 433
434 -- Stub handlers 434 -- Stub handlers
435 function interface_mt:onconnect() 435 function interface_mt:onconnect()
436 end 436 end
437 function interface_mt:onincoming() 437 function interface_mt:onincoming()
438 end 438 end
439 function interface_mt:ondisconnect() 439 function interface_mt:ondisconnect()
440 end 440 end
441 function interface_mt:ontimeout() 441 function interface_mt:ontimeout()
442 end
443 function interface_mt:onstatus()
444 debug("server.lua: Dummy onstatus()")
442 end 445 end
443 end 446 end
444 447
445 -- End of client interface methods 448 -- End of client interface methods
446 449
464 receive = client.receive; 467 receive = client.receive;
465 onconnect = listener.onconnect; -- will be called when client disconnects 468 onconnect = listener.onconnect; -- will be called when client disconnects
466 ondisconnect = listener.ondisconnect; -- will be called when client disconnects 469 ondisconnect = listener.ondisconnect; -- will be called when client disconnects
467 onincoming = listener.onincoming; -- will be called when client sends data 470 onincoming = listener.onincoming; -- will be called when client sends data
468 ontimeout = listener.ontimeout; -- called when fatal socket timeout occurs 471 ontimeout = listener.ontimeout; -- called when fatal socket timeout occurs
472 onstatus = listener.onstatus; -- called for status changes (e.g. of SSL/TLS)
469 eventread = false, eventwrite = false, eventclose = false, 473 eventread = false, eventwrite = false, eventclose = false,
470 eventhandshake = false, eventstarthandshake = false; -- event handler 474 eventhandshake = false, eventstarthandshake = false; -- event handler
471 eventconnect = false, eventsession = false; -- more event handler... 475 eventconnect = false, eventsession = false; -- more event handler...
472 eventwritetimeout = false; -- even more event handler... 476 eventwritetimeout = false; -- even more event handler...
473 eventreadtimeout = false; 477 eventreadtimeout = false;
483 _ip = ip, _port = port, _server = server, _pattern = pattern, 487 _ip = ip, _port = port, _server = server, _pattern = pattern,
484 _serverport = (server and server:port() or nil), 488 _serverport = (server and server:port() or nil),
485 _sslctx = sslctx; -- parameters 489 _sslctx = sslctx; -- parameters
486 _usingssl = false; -- client is using ssl; 490 _usingssl = false; -- client is using ssl;
487 } 491 }
488 if not sslctx then
489 interface.starttls = false -- don't allow TLS
490 end
491 interface.id = tostring(interface):match("%x+$"); 492 interface.id = tostring(interface):match("%x+$");
492 interface.writecallback = function( event ) -- called on write events 493 interface.writecallback = function( event ) -- called on write events
493 --vdebug( "new client write event, id/ip/port:", interface, ip, port ) 494 --vdebug( "new client write event, id/ip/port:", interface, ip, port )
494 if interface.nowriting or ( interface.fatalerror and ( "client to close" ~= interface.fatalerror ) ) then -- leave this event 495 if interface.nowriting or ( interface.fatalerror and ( "client to close" ~= interface.fatalerror ) ) then -- leave this event
495 --vdebug( "leaving this event because:", interface.nowriting or interface.fatalerror ) 496 --vdebug( "leaving this event because:", interface.nowriting or interface.fatalerror )
622 end 623 end
623 end 624 end
624 625
625 local handleserver 626 local handleserver
626 do 627 do
627 function handleserver( server, addr, port, pattern, listener, sslctx, startssl ) -- creates an server interface 628 function handleserver( server, addr, port, pattern, listener, sslctx ) -- creates an server interface
628 debug "creating server interface..." 629 debug "creating server interface..."
629 local interface = { 630 local interface = {
630 _connections = 0; 631 _connections = 0;
631 632
632 conn = server; 633 conn = server;
667 end 668 end
668 local ip, port = client:getpeername( ) 669 local ip, port = client:getpeername( )
669 interface._connections = interface._connections + 1 -- increase connection count 670 interface._connections = interface._connections + 1 -- increase connection count
670 local clientinterface = handleclient( client, ip, port, interface, pattern, listener, nil, sslctx ) 671 local clientinterface = handleclient( client, ip, port, interface, pattern, listener, nil, sslctx )
671 --vdebug( "client id:", clientinterface, "startssl:", startssl ) 672 --vdebug( "client id:", clientinterface, "startssl:", startssl )
672 if startssl then 673 if sslctx then
673 clientinterface:starttls() 674 clientinterface:starttls(sslctx)
674 else 675 else
675 clientinterface:_start_session( clientinterface.onconnect ) 676 clientinterface:_start_session( clientinterface.onconnect )
676 end 677 end
677 debug( "accepted incoming client connection from:", ip, port ) 678 debug( "accepted incoming client connection from:", ip, port )
678 client, err = server:accept() -- try to accept again 679 client, err = server:accept() -- try to accept again