Software /
code /
prosody
Comparison
net/server_event.lua @ 6858:d8f8c0b2fda8
server_event: Normalize indentation
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 25 Sep 2015 17:43:21 +0200 |
parent | 6856:489f4ae291bf |
child | 6859:88a997e08b70 |
comparison
equal
deleted
inserted
replaced
6857:86fcc3fa1a97 | 6858:d8f8c0b2fda8 |
---|---|
96 function interface_mt:_close() | 96 function interface_mt:_close() |
97 return self:_destroy(); | 97 return self:_destroy(); |
98 end | 98 end |
99 | 99 |
100 function interface_mt:_start_connection(plainssl) -- should be called from addclient | 100 function interface_mt:_start_connection(plainssl) -- should be called from addclient |
101 local callback = function( event ) | 101 local callback = function( event ) |
102 if EV_TIMEOUT == event then -- timeout during connection | 102 if EV_TIMEOUT == event then -- timeout during connection |
103 self.fatalerror = "connection timeout" | 103 self.fatalerror = "connection timeout" |
104 self:ontimeout() -- call timeout listener | 104 self:ontimeout() -- call timeout listener |
105 self:_close() | 105 self:_close() |
106 debug( "new connection failed. id:", self.id, "error:", self.fatalerror ) | 106 debug( "new connection failed. id:", self.id, "error:", self.fatalerror ) |
107 else | 107 else |
108 if plainssl and has_luasec then -- start ssl session | 108 if plainssl and has_luasec then -- start ssl session |
109 self:starttls(self._sslctx, true) | 109 self:starttls(self._sslctx, true) |
110 else -- normal connection | 110 else -- normal connection |
111 self:_start_session(true) | 111 self:_start_session(true) |
112 end | 112 end |
113 debug( "new connection established. id:", self.id ) | 113 debug( "new connection established. id:", self.id ) |
114 end | 114 end |
115 self.eventconnect = nil | 115 self.eventconnect = nil |
116 return -1 | 116 return -1 |
117 end | 117 end |
118 self.eventconnect = addevent( base, self.conn, EV_WRITE, callback, cfg.CONNECT_TIMEOUT ) | 118 self.eventconnect = addevent( base, self.conn, EV_WRITE, callback, cfg.CONNECT_TIMEOUT ) |
119 return true | 119 return true |
120 end | 120 end |
121 function interface_mt:_start_session(call_onconnect) -- new session, for example after startssl | 121 function interface_mt:_start_session(call_onconnect) -- new session, for example after startssl |
122 if self.type == "client" then | 122 if self.type == "client" then |
123 local callback = function( ) | 123 local callback = function( ) |
124 self:_lock( false, false, false ) | 124 self:_lock( false, false, false ) |
137 self.eventread = addevent( base, self.conn, EV_READ, self.readcallback ) -- register callback | 137 self.eventread = addevent( base, self.conn, EV_READ, self.readcallback ) -- register callback |
138 end | 138 end |
139 return true | 139 return true |
140 end | 140 end |
141 function interface_mt:_start_ssl(call_onconnect) -- old socket will be destroyed, therefore we have to close read/write events first | 141 function interface_mt:_start_ssl(call_onconnect) -- old socket will be destroyed, therefore we have to close read/write events first |
142 --vdebug( "starting ssl session with client id:", self.id ) | 142 --vdebug( "starting ssl session with client id:", self.id ) |
143 local _ | 143 local _ |
144 _ = self.eventread and self.eventread:close( ) -- close events; this must be called outside of the event callbacks! | 144 _ = self.eventread and self.eventread:close( ) -- close events; this must be called outside of the event callbacks! |
145 _ = self.eventwrite and self.eventwrite:close( ) | |
146 self.eventread, self.eventwrite = nil, nil | |
147 local err | |
148 self.conn, err = ssl.wrap( self.conn, self._sslctx ) | |
149 if err then | |
150 self.fatalerror = err | |
151 self.conn = nil -- cannot be used anymore | |
152 if call_onconnect then | |
153 self.ondisconnect = nil -- dont call this when client isnt really connected | |
154 end | |
155 self:_close() | |
156 debug( "fatal error while ssl wrapping:", err ) | |
157 return false | |
158 end | |
159 self.conn:settimeout( 0 ) -- set non blocking | |
160 local handshakecallback = coroutine_wrap( | |
161 function( event ) | |
162 local _, err | |
163 local attempt = 0 | |
164 local maxattempt = cfg.MAX_HANDSHAKE_ATTEMPTS | |
165 while attempt < maxattempt do -- no endless loop | |
166 attempt = attempt + 1 | |
167 debug( "ssl handshake of client with id:"..tostring(self)..", attempt:"..attempt ) | |
168 if attempt > maxattempt then | |
169 self.fatalerror = "max handshake attempts exceeded" | |
170 elseif EV_TIMEOUT == event then | |
171 self.fatalerror = "timeout during handshake" | |
172 else | |
173 _, err = self.conn:dohandshake( ) | |
174 if not err then | |
175 self:_lock( false, false, false ) -- unlock the interface; sending, closing etc allowed | |
176 self.send = self.conn.send -- caching table lookups with new client object | |
177 self.receive = self.conn.receive | |
178 if not call_onconnect then -- trigger listener | |
179 self:onstatus("ssl-handshake-complete"); | |
180 end | |
181 self:_start_session( call_onconnect ) | |
182 debug( "ssl handshake done" ) | |
183 self.eventhandshake = nil | |
184 return -1 | |
185 end | |
186 if err == "wantwrite" then | |
187 event = EV_WRITE | |
188 elseif err == "wantread" then | |
189 event = EV_READ | |
190 else | |
191 debug( "ssl handshake error:", err ) | |
192 self.fatalerror = err | |
193 end | |
194 end | |
195 if self.fatalerror then | |
196 if call_onconnect then | |
197 self.ondisconnect = nil -- dont call this when client isnt really connected | |
198 end | |
199 self:_close() | |
200 debug( "handshake failed because:", self.fatalerror ) | |
201 self.eventhandshake = nil | |
202 return -1 | |
203 end | |
204 event = coroutine_yield( event, cfg.HANDSHAKE_TIMEOUT ) -- yield this monster... | |
205 end | |
206 end | |
207 ) | |
208 debug "starting handshake..." | |
209 self:_lock( false, true, true ) -- unlock read/write events, but keep interface locked | |
210 self.eventhandshake = addevent( base, self.conn, EV_READWRITE, handshakecallback, cfg.HANDSHAKE_TIMEOUT ) | |
211 return true | |
212 end | |
213 function interface_mt:_destroy() -- close this interface + events and call last listener | |
214 debug( "closing client with id:", self.id, self.fatalerror ) | |
215 self:_lock( true, true, true ) -- first of all, lock the interface to avoid further actions | |
216 local _ | |
217 _ = self.eventread and self.eventread:close( ) | |
218 if self.type == "client" then | |
145 _ = self.eventwrite and self.eventwrite:close( ) | 219 _ = self.eventwrite and self.eventwrite:close( ) |
220 _ = self.eventhandshake and self.eventhandshake:close( ) | |
221 _ = self.eventstarthandshake and self.eventstarthandshake:close( ) | |
222 _ = self.eventconnect and self.eventconnect:close( ) | |
223 _ = self.eventsession and self.eventsession:close( ) | |
224 _ = self.eventwritetimeout and self.eventwritetimeout:close( ) | |
225 _ = self.eventreadtimeout and self.eventreadtimeout:close( ) | |
226 _ = self.ondisconnect and self:ondisconnect( self.fatalerror ~= "client to close" and self.fatalerror) -- call ondisconnect listener (wont be the case if handshake failed on connect) | |
227 _ = self.conn and self.conn:close( ) -- close connection | |
228 _ = self._server and self._server:counter(-1); | |
146 self.eventread, self.eventwrite = nil, nil | 229 self.eventread, self.eventwrite = nil, nil |
147 local err | 230 self.eventstarthandshake, self.eventhandshake, self.eventclose = nil, nil, nil |
148 self.conn, err = ssl.wrap( self.conn, self._sslctx ) | 231 self.readcallback, self.writecallback = nil, nil |
149 if err then | 232 else |
150 self.fatalerror = err | 233 self.conn:close( ) |
151 self.conn = nil -- cannot be used anymore | 234 self.eventread, self.eventclose = nil, nil |
152 if call_onconnect then | 235 self.interface, self.readcallback = nil, nil |
153 self.ondisconnect = nil -- dont call this when client isnt really connected | 236 end |
154 end | 237 interfacelist[ self ] = nil |
155 self:_close() | 238 return true |
156 debug( "fatal error while ssl wrapping:", err ) | |
157 return false | |
158 end | |
159 self.conn:settimeout( 0 ) -- set non blocking | |
160 local handshakecallback = coroutine_wrap( | |
161 function( event ) | |
162 local _, err | |
163 local attempt = 0 | |
164 local maxattempt = cfg.MAX_HANDSHAKE_ATTEMPTS | |
165 while attempt < maxattempt do -- no endless loop | |
166 attempt = attempt + 1 | |
167 debug( "ssl handshake of client with id:"..tostring(self)..", attempt:"..attempt ) | |
168 if attempt > maxattempt then | |
169 self.fatalerror = "max handshake attempts exceeded" | |
170 elseif EV_TIMEOUT == event then | |
171 self.fatalerror = "timeout during handshake" | |
172 else | |
173 _, err = self.conn:dohandshake( ) | |
174 if not err then | |
175 self:_lock( false, false, false ) -- unlock the interface; sending, closing etc allowed | |
176 self.send = self.conn.send -- caching table lookups with new client object | |
177 self.receive = self.conn.receive | |
178 if not call_onconnect then -- trigger listener | |
179 self:onstatus("ssl-handshake-complete"); | |
180 end | |
181 self:_start_session( call_onconnect ) | |
182 debug( "ssl handshake done" ) | |
183 self.eventhandshake = nil | |
184 return -1 | |
185 end | |
186 if err == "wantwrite" then | |
187 event = EV_WRITE | |
188 elseif err == "wantread" then | |
189 event = EV_READ | |
190 else | |
191 debug( "ssl handshake error:", err ) | |
192 self.fatalerror = err | |
193 end | |
194 end | |
195 if self.fatalerror then | |
196 if call_onconnect then | |
197 self.ondisconnect = nil -- dont call this when client isnt really connected | |
198 end | |
199 self:_close() | |
200 debug( "handshake failed because:", self.fatalerror ) | |
201 self.eventhandshake = nil | |
202 return -1 | |
203 end | |
204 event = coroutine_yield( event, cfg.HANDSHAKE_TIMEOUT ) -- yield this monster... | |
205 end | |
206 end | |
207 ) | |
208 debug "starting handshake..." | |
209 self:_lock( false, true, true ) -- unlock read/write events, but keep interface locked | |
210 self.eventhandshake = addevent( base, self.conn, EV_READWRITE, handshakecallback, cfg.HANDSHAKE_TIMEOUT ) | |
211 return true | |
212 end | |
213 function interface_mt:_destroy() -- close this interface + events and call last listener | |
214 debug( "closing client with id:", self.id, self.fatalerror ) | |
215 self:_lock( true, true, true ) -- first of all, lock the interface to avoid further actions | |
216 local _ | |
217 _ = self.eventread and self.eventread:close( ) | |
218 if self.type == "client" then | |
219 _ = self.eventwrite and self.eventwrite:close( ) | |
220 _ = self.eventhandshake and self.eventhandshake:close( ) | |
221 _ = self.eventstarthandshake and self.eventstarthandshake:close( ) | |
222 _ = self.eventconnect and self.eventconnect:close( ) | |
223 _ = self.eventsession and self.eventsession:close( ) | |
224 _ = self.eventwritetimeout and self.eventwritetimeout:close( ) | |
225 _ = self.eventreadtimeout and self.eventreadtimeout:close( ) | |
226 _ = self.ondisconnect and self:ondisconnect( self.fatalerror ~= "client to close" and self.fatalerror) -- call ondisconnect listener (wont be the case if handshake failed on connect) | |
227 _ = self.conn and self.conn:close( ) -- close connection | |
228 _ = self._server and self._server:counter(-1); | |
229 self.eventread, self.eventwrite = nil, nil | |
230 self.eventstarthandshake, self.eventhandshake, self.eventclose = nil, nil, nil | |
231 self.readcallback, self.writecallback = nil, nil | |
232 else | |
233 self.conn:close( ) | |
234 self.eventread, self.eventclose = nil, nil | |
235 self.interface, self.readcallback = nil, nil | |
236 end | |
237 interfacelist[ self ] = nil | |
238 return true | |
239 end | 239 end |
240 | 240 |
241 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 |
242 self.nointerface, self.noreading, self.nowriting = nointerface, noreading, nowriting | 242 self.nointerface, self.noreading, self.nowriting = nointerface, noreading, nowriting |
243 return nointerface, noreading, nowriting | 243 return nointerface, noreading, nowriting |
244 end | 244 end |
245 | 245 |
246 --TODO: Deprecate | 246 --TODO: Deprecate |
247 function interface_mt:lock_read(switch) | 247 function interface_mt:lock_read(switch) |
248 if switch then | 248 if switch then |
390 end | 390 end |
391 if not self.eventwrite then | 391 if not self.eventwrite then |
392 self:_lock( true, true, true ) -- lock the interface, to not disturb the handshake | 392 self:_lock( true, true, true ) -- lock the interface, to not disturb the handshake |
393 self.eventstarthandshake = addevent( base, nil, EV_TIMEOUT, self.startsslcallback, 0 ) -- add event to start handshake | 393 self.eventstarthandshake = addevent( base, nil, EV_TIMEOUT, self.startsslcallback, 0 ) -- add event to start handshake |
394 else -- wait until writebuffer is empty | 394 else -- wait until writebuffer is empty |
395 self:_lock( true, true, false ) | 395 self:_lock( true, true, false ) |
396 debug "ssl session delayed until writebuffer is empty..." | 396 debug "ssl session delayed until writebuffer is empty..." |
397 end | 397 end |
398 self.starttls = false; | 398 self.starttls = false; |
399 return true | 399 return true |
400 end | 400 end |
401 | 401 |
402 function interface_mt:setoption(option, value) | 402 function interface_mt:setoption(option, value) |
403 if self.conn.setoption then | 403 if self.conn.setoption then |
404 return self.conn:setoption(option, value); | 404 return self.conn:setoption(option, value); |
751 end | 751 end |
752 end | 752 end |
753 | 753 |
754 local function setquitting(yes) | 754 local function setquitting(yes) |
755 if yes then | 755 if yes then |
756 -- Quit now | 756 -- Quit now |
757 closeallservers(); | 757 closeallservers(); |
758 base:loopexit(); | 758 base:loopexit(); |
759 end | 759 end |
760 end | 760 end |
761 | 761 |
762 local function get_backend() | 762 local function get_backend() |
763 return base:method(); | 763 return base:method(); |