Comparison

net/server_event.lua @ 9835:20bf5b47c1fb

net.server: New API for creating server listeners server.listen(interface, port, listeners, options);
author Kim Alvefur <zash@zash.se>
date Thu, 13 Sep 2018 21:16:37 +0200
parent 9581:1be99aedb0b3
child 9850:9a905888b96c
comparison
equal deleted inserted replaced
9834:a657df70cc31 9835:20bf5b47c1fb
647 setmetatable(interface, interface_mt) 647 setmetatable(interface, interface_mt)
648 interfacelist[ interface ] = true -- add to interfacelist 648 interfacelist[ interface ] = true -- add to interfacelist
649 return interface 649 return interface
650 end 650 end
651 651
652 local function handleserver( server, addr, port, pattern, listener, sslctx ) -- creates an server interface 652 local function handleserver( server, addr, port, pattern, listener, sslctx, startssl ) -- creates an server interface
653 debug "creating server interface..." 653 debug "creating server interface..."
654 local interface = { 654 local interface = {
655 _connections = 0; 655 _connections = 0;
656 656
657 type = "server"; 657 type = "server";
693 end 693 end
694 local client_ip, client_port = client:getpeername( ) 694 local client_ip, client_port = client:getpeername( )
695 interface._connections = interface._connections + 1 -- increase connection count 695 interface._connections = interface._connections + 1 -- increase connection count
696 local clientinterface = handleclient( client, client_ip, client_port, interface, pattern, listener, sslctx ) 696 local clientinterface = handleclient( client, client_ip, client_port, interface, pattern, listener, sslctx )
697 --vdebug( "client id:", clientinterface, "startssl:", startssl ) 697 --vdebug( "client id:", clientinterface, "startssl:", startssl )
698 if has_luasec and sslctx then 698 if has_luasec and startssl then
699 clientinterface:starttls(sslctx, true) 699 clientinterface:starttls(sslctx, true)
700 else 700 else
701 clientinterface:_start_session( true ) 701 clientinterface:_start_session( true )
702 end 702 end
703 debug( "accepted incoming client connection from:", client_ip or "<unknown IP>", client_port or "<unknown port>", "to", port or "<unknown port>"); 703 debug( "accepted incoming client connection from:", client_ip or "<unknown IP>", client_port or "<unknown port>", "to", port or "<unknown port>");
712 interfacelist[ interface ] = true 712 interfacelist[ interface ] = true
713 interface:_start_session() 713 interface:_start_session()
714 return interface 714 return interface
715 end 715 end
716 716
717 local function addserver( addr, port, listener, pattern, sslctx, startssl ) -- TODO: check arguments 717 local function listen(addr, port, listener, config)
718 --vdebug( "creating new tcp server with following parameters:", addr or "nil", port or "nil", sslctx or "nil", startssl or "nil") 718 config = config or {}
719 if sslctx and not has_luasec then 719 if config.sslctx and not has_luasec then
720 debug "fatal error: luasec not found" 720 debug "fatal error: luasec not found"
721 return nil, "luasec not found" 721 return nil, "luasec not found"
722 end 722 end
723 local server, err = socket.bind( addr, port, cfg.ACCEPT_QUEUE ) -- create server socket 723 local server, err = socket.bind( addr, port, cfg.ACCEPT_QUEUE ) -- create server socket
724 if not server then 724 if not server then
725 debug( "creating server socket on "..addr.." port "..port.." failed:", err ) 725 debug( "creating server socket on "..addr.." port "..port.." failed:", err )
726 return nil, err 726 return nil, err
727 end 727 end
728 local interface = handleserver( server, addr, port, pattern, listener, sslctx, startssl ) -- new server handler 728 local interface = handleserver( server, addr, port, config.read_size, listener, config.tls_ctx, config.tls_direct) -- new server handler
729 debug( "new server created with id:", tostring(interface)) 729 debug( "new server created with id:", tostring(interface))
730 return interface 730 return interface
731 end
732
733 local function addserver( addr, port, listener, pattern, sslctx ) -- TODO: check arguments
734 --vdebug( "creating new tcp server with following parameters:", addr or "nil", port or "nil", sslctx or "nil", startssl or "nil")
735 return listen( addr, port, listener, {
736 read_size = pattern,
737 tls_ctx = sslctx,
738 tls_direct = not not sslctx,
739 });
731 end 740 end
732 741
733 local function wrapclient( client, ip, port, listeners, pattern, sslctx ) 742 local function wrapclient( client, ip, port, listeners, pattern, sslctx )
734 local interface = handleclient( client, ip, port, nil, pattern, listeners, sslctx ) 743 local interface = handleclient( client, ip, port, nil, pattern, listeners, sslctx )
735 interface:_start_connection(sslctx) 744 interface:_start_connection(sslctx)
888 link = link, 897 link = link,
889 event = levent, 898 event = levent,
890 event_base = base, 899 event_base = base,
891 addevent = newevent, 900 addevent = newevent,
892 addserver = addserver, 901 addserver = addserver,
902 listen = listen,
893 addclient = addclient, 903 addclient = addclient,
894 wrapclient = wrapclient, 904 wrapclient = wrapclient,
895 setquitting = setquitting, 905 setquitting = setquitting,
896 closeall = closeallservers, 906 closeall = closeallservers,
897 get_backend = get_backend, 907 get_backend = get_backend,