Comparison

net/server_event.lua @ 6782:ec172dbe9d14

net.server_{select,event}: Don't rely on LuaSocket and LuaSec being present in the globals table
author Kim Alvefur <zash@zash.se>
date Mon, 23 Feb 2015 12:04:43 +0100
parent 6382:57d23c26039b
child 6791:e813e8cf6046
child 6816:36367fb1ecf9
comparison
equal deleted inserted replaced
6781:05cd80ec107c 6782:ec172dbe9d14
42 local setmetatable = use "setmetatable" 42 local setmetatable = use "setmetatable"
43 43
44 local t_insert = table.insert 44 local t_insert = table.insert
45 local t_concat = table.concat 45 local t_concat = table.concat
46 46
47 local ssl = use "ssl" 47 local has_luasec, ssl = pcall ( require , "ssl" )
48 local socket = use "socket" or require "socket" 48 local socket = use "socket" or require "socket"
49 49
50 local log = require ("util.logger").init("socket") 50 local log = require ("util.logger").init("socket")
51 51
52 local function debug(...) 52 local function debug(...)
134 self.fatalerror = "connection timeout" 134 self.fatalerror = "connection timeout"
135 self:ontimeout() -- call timeout listener 135 self:ontimeout() -- call timeout listener
136 self:_close() 136 self:_close()
137 debug( "new connection failed. id:", self.id, "error:", self.fatalerror ) 137 debug( "new connection failed. id:", self.id, "error:", self.fatalerror )
138 else 138 else
139 if plainssl and ssl then -- start ssl session 139 if plainssl and has_luasec then -- start ssl session
140 self:starttls(self._sslctx, true) 140 self:starttls(self._sslctx, true)
141 else -- normal connection 141 else -- normal connection
142 self:_start_session(true) 142 self:_start_session(true)
143 end 143 end
144 debug( "new connection established. id:", self.id ) 144 debug( "new connection established. id:", self.id )
510 _ip = ip, _port = port, _server = server, _pattern = pattern, 510 _ip = ip, _port = port, _server = server, _pattern = pattern,
511 _serverport = (server and server:port() or nil), 511 _serverport = (server and server:port() or nil),
512 _sslctx = sslctx; -- parameters 512 _sslctx = sslctx; -- parameters
513 _usingssl = false; -- client is using ssl; 513 _usingssl = false; -- client is using ssl;
514 } 514 }
515 if not ssl then interface.starttls = false; end 515 if not has_luasec then interface.starttls = false; end
516 interface.id = tostring(interface):match("%x+$"); 516 interface.id = tostring(interface):match("%x+$");
517 interface.writecallback = function( event ) -- called on write events 517 interface.writecallback = function( event ) -- called on write events
518 --vdebug( "new client write event, id/ip/port:", interface, ip, port ) 518 --vdebug( "new client write event, id/ip/port:", interface, ip, port )
519 if interface.nowriting or ( interface.fatalerror and ( "client to close" ~= interface.fatalerror ) ) then -- leave this event 519 if interface.nowriting or ( interface.fatalerror and ( "client to close" ~= interface.fatalerror ) ) then -- leave this event
520 --vdebug( "leaving this event because:", interface.nowriting or interface.fatalerror ) 520 --vdebug( "leaving this event because:", interface.nowriting or interface.fatalerror )
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 ssl and sslctx then 698 if has_luasec and sslctx 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>");
723 debug( "creating server socket on "..addr.." port "..port.." failed:", err ) 723 debug( "creating server socket on "..addr.." port "..port.." failed:", err )
724 return nil, err 724 return nil, err
725 end 725 end
726 local sslctx 726 local sslctx
727 if sslcfg then 727 if sslcfg then
728 if not ssl then 728 if not has_luasec then
729 debug "fatal error: luasec not found" 729 debug "fatal error: luasec not found"
730 return nil, "luasec not found" 730 return nil, "luasec not found"
731 end 731 end
732 sslctx, err = sslcfg 732 sslctx, err = sslcfg
733 if err then 733 if err then
764 return nil, err 764 return nil, err
765 end 765 end
766 end 766 end
767 local sslctx 767 local sslctx
768 if sslcfg then -- handle ssl/new context 768 if sslcfg then -- handle ssl/new context
769 if not ssl then 769 if not has_luasec then
770 debug "need luasec, but not available" 770 debug "need luasec, but not available"
771 return nil, "luasec not found" 771 return nil, "luasec not found"
772 end 772 end
773 sslctx, err = sslcfg 773 sslctx, err = sslcfg
774 if err then 774 if err then