Comparison

net/server_event.lua @ 6850:41de00647ad3

server_event: Move local references to various functions to top of file
author Kim Alvefur <zash@zash.se>
date Fri, 25 Sep 2015 17:02:17 +0200
parent 6849:0455b9686e16
child 6851:1f1bed8ebc41
comparison
equal deleted inserted replaced
6849:0455b9686e16 6850:41de00647ad3
30 CONNECT_TIMEOUT = 20, -- timeout in seconds for connection attempts 30 CONNECT_TIMEOUT = 20, -- timeout in seconds for connection attempts
31 CLEAR_DELAY = 5, -- seconds to wait for clearing interface list (and calling ondisconnect listeners) 31 CLEAR_DELAY = 5, -- seconds to wait for clearing interface list (and calling ondisconnect listeners)
32 DEBUG = true, -- show debug messages 32 DEBUG = true, -- show debug messages
33 } 33 }
34 34
35 local function use(x) return rawget(_G, x); end 35 local pairs = pairs
36 local pairs = use "pairs" 36 local select = select
37 local string = use "string" 37 local require = require
38 local select = use "select" 38 local tostring = tostring
39 local require = use "require" 39 local setmetatable = setmetatable
40 local tostring = use "tostring"
41 local coroutine = use "coroutine"
42 local setmetatable = use "setmetatable"
43 40
44 local t_insert = table.insert 41 local t_insert = table.insert
45 local t_concat = table.concat 42 local t_concat = table.concat
43 local s_sub = string.sub
44
45 local coroutine_wrap = coroutine.wrap
46 local coroutine_yield = coroutine.yield
46 47
47 local has_luasec, ssl = pcall ( require , "ssl" ) 48 local has_luasec, ssl = pcall ( require , "ssl" )
48 local socket = use "socket" or require "socket" 49 local socket = require "socket"
50 local event = require "luaevent.core"
51
52 local socket_gettime = socket.gettime
49 local getaddrinfo = socket.dns.getaddrinfo 53 local getaddrinfo = socket.dns.getaddrinfo
50 54
51 local log = require ("util.logger").init("socket") 55 local log = require ("util.logger").init("socket")
52 56
53 local function debug(...) 57 local function debug(...)
71 end 75 end
72 return z 76 return z
73 end 77 end
74 end )( ) 78 end )( )
75 79
76 local event = require "luaevent.core"
77 local base = event.new( ) 80 local base = event.new( )
81 local addevent = base.addevent
78 local EV_READ = event.EV_READ 82 local EV_READ = event.EV_READ
79 local EV_WRITE = event.EV_WRITE 83 local EV_WRITE = event.EV_WRITE
80 local EV_TIMEOUT = event.EV_TIMEOUT 84 local EV_TIMEOUT = event.EV_TIMEOUT
81 local EV_SIGNAL = event.EV_SIGNAL 85 local EV_SIGNAL = event.EV_SIGNAL
82 86
87 -- Client interface methods 91 -- Client interface methods
88 local interface_mt 92 local interface_mt
89 do 93 do
90 interface_mt = {}; interface_mt.__index = interface_mt; 94 interface_mt = {}; interface_mt.__index = interface_mt;
91 95
92 local addevent = base.addevent
93 local coroutine_wrap, coroutine_yield = coroutine.wrap,coroutine.yield
94 96
95 -- Private methods 97 -- Private methods
96 function interface_mt:_close() 98 function interface_mt:_close()
97 return self:_destroy(); 99 return self:_destroy();
98 end 100 end
439 441
440 -- End of client interface methods 442 -- End of client interface methods
441 443
442 local handleclient; 444 local handleclient;
443 do 445 do
444 local string_sub = string.sub -- caching table lookups
445 local addevent = base.addevent
446 local socket_gettime = socket.gettime
447 function handleclient( client, ip, port, server, pattern, listener, sslctx ) -- creates an client interface 446 function handleclient( client, ip, port, server, pattern, listener, sslctx ) -- creates an client interface
448 --vdebug("creating client interfacce...") 447 --vdebug("creating client interfacce...")
449 local interface = { 448 local interface = {
450 type = "client"; 449 type = "client";
451 conn = client; 450 conn = client;
525 end 524 end
526 interface.eventwrite = nil 525 interface.eventwrite = nil
527 return -1 526 return -1
528 elseif byte and (err == "timeout" or err == "wantwrite") then -- want write again 527 elseif byte and (err == "timeout" or err == "wantwrite") then -- want write again
529 --vdebug( "writebuffer is not empty:", err ) 528 --vdebug( "writebuffer is not empty:", err )
530 interface.writebuffer[1] = string_sub( interface.writebuffer[1], byte + 1, interface.writebufferlen ) -- new buffer 529 interface.writebuffer[1] = s_sub( interface.writebuffer[1], byte + 1, interface.writebufferlen ) -- new buffer
531 interface.writebufferlen = interface.writebufferlen - byte 530 interface.writebufferlen = interface.writebufferlen - byte
532 if "wantread" == err then -- happens only with luasec 531 if "wantread" == err then -- happens only with luasec
533 local callback = function( ) 532 local callback = function( )
534 interface:_close() 533 interface:_close()
535 interface.eventwritetimeout = nil 534 interface.eventwritetimeout = nil
753 local loop = function( ) -- starts the event loop 752 local loop = function( ) -- starts the event loop
754 base:loop( ) 753 base:loop( )
755 return "quitting"; 754 return "quitting";
756 end 755 end
757 756
758 local newevent = ( function( ) 757 local function newevent( ... )
759 local add = base.addevent 758 return addevent( base, ... )
760 return function( ... ) 759 end
761 return add( base, ... )
762 end
763 end )( )
764 760
765 local closeallservers = function( arg ) 761 local closeallservers = function( arg )
766 for item in pairs( interfacelist ) do 762 for item in pairs( interfacelist ) do
767 if item.type == "server" then 763 if item.type == "server" then
768 item:close( arg ) 764 item:close( arg )