Software /
code /
prosody
Comparison
net/server_event.lua @ 2240:373e3fd8b862
net.server_event: Add addclient/wrapclient compatible with server_select, DNS and s2s should now work with libevent \o/
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 26 Nov 2009 22:25:13 +0000 |
parent | 2239:c050074d453a |
child | 2303:ecbd68fc46db |
comparison
equal
deleted
inserted
replaced
2239:c050074d453a | 2240:373e3fd8b862 |
---|---|
691 debug( "new server created with id:", tostring(interface)) | 691 debug( "new server created with id:", tostring(interface)) |
692 return interface | 692 return interface |
693 end | 693 end |
694 end )( ) | 694 end )( ) |
695 | 695 |
696 local wrapclient = ( function( ) | 696 local addclient, wrapclient |
697 return function( client, addr, serverport, listener, pattern, localaddr, localport, sslcfg, startssl ) | 697 do |
698 debug( "try to connect to:", addr, serverport, "with parameters:", pattern, localaddr, localport, sslcfg, startssl ) | 698 function wrapclient( client, ip, port, listeners, pattern, sslctx, startssl ) |
699 local sslctx | 699 local interface = handleclient( client, ip, port, nil, pattern, listeners, sslctx ) |
700 if sslcfg then -- handle ssl/new context | 700 interface:_start_session() |
701 if not ssl then | 701 return interface |
702 debug "need luasec, but not available" | 702 --function handleclient( client, ip, port, server, pattern, listener, _, sslctx ) -- creates an client interface |
703 return nil, "luasec not found" | 703 end |
704 end | 704 |
705 sslctx, err = ssl.newcontext( sslcfg ) | 705 function addclient( addr, serverport, listener, pattern, localaddr, localport, sslcfg, startssl ) |
706 if err then | |
707 debug( "cannot create new ssl context:", err ) | |
708 return nil, err | |
709 end | |
710 end | |
711 end | |
712 end )( ) | |
713 | |
714 local addclient = ( function( ) | |
715 return function( addr, serverport, listener, pattern, localaddr, localport, sslcfg, startssl ) | |
716 local client, err = socket.tcp() -- creating new socket | 706 local client, err = socket.tcp() -- creating new socket |
717 if not client then | 707 if not client then |
718 debug( "cannot create socket:", err ) | 708 debug( "cannot create socket:", err ) |
719 return nil, err | 709 return nil, err |
720 end | 710 end |
724 if not res then | 714 if not res then |
725 debug( "cannot bind client:", err ) | 715 debug( "cannot bind client:", err ) |
726 return nil, err | 716 return nil, err |
727 end | 717 end |
728 end | 718 end |
719 local sslctx | |
720 if sslcfg then -- handle ssl/new context | |
721 if not ssl then | |
722 debug "need luasec, but not available" | |
723 return nil, "luasec not found" | |
724 end | |
725 sslctx, err = ssl.newcontext( sslcfg ) | |
726 if err then | |
727 debug( "cannot create new ssl context:", err ) | |
728 return nil, err | |
729 end | |
730 end | |
729 local res, err = client:connect( addr, serverport ) -- connect | 731 local res, err = client:connect( addr, serverport ) -- connect |
730 if res or ( err == "timeout" ) then | 732 if res or ( err == "timeout" ) then |
731 local ip, port = client:getsockname( ) | 733 local ip, port = client:getsockname( ) |
732 local server = function( ) | 734 local server = function( ) |
733 return nil, "this is a dummy server interface" | 735 return nil, "this is a dummy server interface" |
734 end | 736 end |
735 local interface = handleclient( client, ip, port, server, pattern, listener, sslctx ) | 737 local interface = wrapclient( client, ip, serverport, listeners, pattern, sslctx, startssl ) |
736 interface:_start_connection( startssl ) | 738 interface:_start_connection( startssl ) |
737 debug( "new connection id:", interface ) | 739 debug( "new connection id:", interface.id ) |
738 return interface, err | 740 return interface, err |
739 else | 741 else |
740 debug( "new connection failed:", err ) | 742 debug( "new connection failed:", err ) |
741 return nil, err | 743 return nil, err |
742 end | 744 end |
743 return wrapclient( client, addr, serverport, listener, pattern, localaddr, localport, sslcfg, startssl ) | 745 end |
744 end | 746 end |
745 end )( ) | 747 |
746 | 748 |
747 local loop = function( ) -- starts the event loop | 749 local loop = function( ) -- starts the event loop |
748 return base:loop( ) | 750 return base:loop( ) |
749 end | 751 end |
750 | 752 |