Comparison

net/server_event.lua @ 6849:0455b9686e16

server_event: Replace implementation of ordered set with a simple table set
author Kim Alvefur <zash@zash.se>
date Fri, 25 Sep 2015 16:45:02 +0200
parent 6819:ffb2b5e31456
child 6850:41de00647ad3
comparison
equal deleted inserted replaced
6848:32327c80710b 6849:0455b9686e16
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 function use(x) return rawget(_G, x); end
36 local ipairs = use "ipairs" 36 local pairs = use "pairs"
37 local string = use "string" 37 local string = use "string"
38 local select = use "select" 38 local select = use "select"
39 local require = use "require" 39 local require = use "require"
40 local tostring = use "tostring" 40 local tostring = use "tostring"
41 local coroutine = use "coroutine" 41 local coroutine = use "coroutine"
80 local EV_TIMEOUT = event.EV_TIMEOUT 80 local EV_TIMEOUT = event.EV_TIMEOUT
81 local EV_SIGNAL = event.EV_SIGNAL 81 local EV_SIGNAL = event.EV_SIGNAL
82 82
83 local EV_READWRITE = bitor( EV_READ, EV_WRITE ) 83 local EV_READWRITE = bitor( EV_READ, EV_WRITE )
84 84
85 local interfacelist = ( function( ) -- holds the interfaces for sockets 85 local interfacelist = { }
86 local array = { }
87 local len = 0
88 return function( method, arg )
89 if "add" == method then
90 len = len + 1
91 array[ len ] = arg
92 arg:_position( len )
93 return len
94 elseif "delete" == method then
95 if len <= 0 then
96 return nil, "array is already empty"
97 end
98 local position = arg:_position() -- get position in array
99 if position ~= len then
100 local interface = array[ len ] -- get last interface
101 array[ position ] = interface -- copy it into free position
102 array[ len ] = nil -- free last position
103 interface:_position( position ) -- set new position in array
104 else -- free last position
105 array[ len ] = nil
106 end
107 len = len - 1
108 return len
109 else
110 return array
111 end
112 end
113 end )( )
114 86
115 -- Client interface methods 87 -- Client interface methods
116 local interface_mt 88 local interface_mt
117 do 89 do
118 interface_mt = {}; interface_mt.__index = interface_mt; 90 interface_mt = {}; interface_mt.__index = interface_mt;
119 91
120 local addevent = base.addevent 92 local addevent = base.addevent
121 local coroutine_wrap, coroutine_yield = coroutine.wrap,coroutine.yield 93 local coroutine_wrap, coroutine_yield = coroutine.wrap,coroutine.yield
122 94
123 -- Private methods 95 -- Private methods
124 function interface_mt:_position(new_position)
125 self.position = new_position or self.position
126 return self.position;
127 end
128 function interface_mt:_close() 96 function interface_mt:_close()
129 return self:_destroy(); 97 return self:_destroy();
130 end 98 end
131 99
132 function interface_mt:_start_connection(plainssl) -- should be called from addclient 100 function interface_mt:_start_connection(plainssl) -- should be called from addclient
264 else 232 else
265 self.conn:close( ) 233 self.conn:close( )
266 self.eventread, self.eventclose = nil, nil 234 self.eventread, self.eventclose = nil, nil
267 self.interface, self.readcallback = nil, nil 235 self.interface, self.readcallback = nil, nil
268 end 236 end
269 interfacelist( "delete", self ) 237 interfacelist[ self ] = nil
270 return true 238 return true
271 end 239 end
272 240
273 function interface_mt:_lock(nointerface, noreading, nowriting) -- lock or unlock this interface or events 241 function interface_mt:_lock(nointerface, noreading, nowriting) -- lock or unlock this interface or events
274 self.nointerface, self.noreading, self.nowriting = nointerface, noreading, nowriting 242 self.nointerface, self.noreading, self.nowriting = nointerface, noreading, nowriting
642 return EV_READ, cfg.READ_TIMEOUT 610 return EV_READ, cfg.READ_TIMEOUT
643 end 611 end
644 612
645 client:settimeout( 0 ) -- set non blocking 613 client:settimeout( 0 ) -- set non blocking
646 setmetatable(interface, interface_mt) 614 setmetatable(interface, interface_mt)
647 interfacelist( "add", interface ) -- add to interfacelist 615 interfacelist[ interface ] = true -- add to interfacelist
648 return interface 616 return interface
649 end 617 end
650 end 618 end
651 619
652 local handleserver 620 local handleserver
708 return EV_READ 676 return EV_READ
709 end 677 end
710 678
711 server:settimeout( 0 ) 679 server:settimeout( 0 )
712 setmetatable(interface, interface_mt) 680 setmetatable(interface, interface_mt)
713 interfacelist( "add", interface ) 681 interfacelist[ interface ] = true
714 interface:_start_session() 682 interface:_start_session()
715 return interface 683 return interface
716 end 684 end
717 end 685 end
718 686
793 return add( base, ... ) 761 return add( base, ... )
794 end 762 end
795 end )( ) 763 end )( )
796 764
797 local closeallservers = function( arg ) 765 local closeallservers = function( arg )
798 for _, item in ipairs( interfacelist( ) ) do 766 for item in pairs( interfacelist ) do
799 if item.type == "server" then 767 if item.type == "server" then
800 item:close( arg ) 768 item:close( arg )
801 end 769 end
802 end 770 end
803 end 771 end