Comparison

net/server_event.lua @ 8728:41c959c5c84b

Fix spelling throughout the codebase [codespell]
author Kim Alvefur <zash@zash.se>
date Sun, 04 Feb 2018 01:51:25 +0100
parent 8530:075df839c110
child 8751:469f78fe1c3e
comparison
equal deleted inserted replaced
8727:285075a27f28 8728:41c959c5c84b
3 3
4 server.lua based on lua/libevent by blastbeat 4 server.lua based on lua/libevent by blastbeat
5 5
6 notes: 6 notes:
7 -- when using luaevent, never register 2 or more EV_READ at one socket, same for EV_WRITE 7 -- when using luaevent, never register 2 or more EV_READ at one socket, same for EV_WRITE
8 -- you cant even register a new EV_READ/EV_WRITE callback inside another one 8 -- you can't even register a new EV_READ/EV_WRITE callback inside another one
9 -- to do some of the above, use timeout events or something what will called from outside 9 -- to do some of the above, use timeout events or something what will called from outside
10 -- dont let garbagecollect eventcallbacks, as long they are running 10 -- don't let garbagecollect eventcallbacks, as long they are running
11 -- when using luasec, there are 4 cases of timeout errors: wantread or wantwrite during reading or writing 11 -- when using luasec, there are 4 cases of timeout errors: wantread or wantwrite during reading or writing
12 12
13 --]] 13 --]]
14 -- luacheck: ignore 212/self 431/err 211/ret 14 -- luacheck: ignore 212/self 431/err 211/ret
15 15
155 self.conn, err = ssl.wrap( self.conn, self._sslctx ) 155 self.conn, err = ssl.wrap( self.conn, self._sslctx )
156 if err then 156 if err then
157 self.fatalerror = err 157 self.fatalerror = err
158 self.conn = nil -- cannot be used anymore 158 self.conn = nil -- cannot be used anymore
159 if call_onconnect then 159 if call_onconnect then
160 self.ondisconnect = nil -- dont call this when client isnt really connected 160 self.ondisconnect = nil -- don't call this when client isn't really connected
161 end 161 end
162 self:_close() 162 self:_close()
163 debug( "fatal error while ssl wrapping:", err ) 163 debug( "fatal error while ssl wrapping:", err )
164 return false 164 return false
165 end 165 end
198 self.fatalerror = err 198 self.fatalerror = err
199 end 199 end
200 end 200 end
201 if self.fatalerror then 201 if self.fatalerror then
202 if call_onconnect then 202 if call_onconnect then
203 self.ondisconnect = nil -- dont call this when client isnt really connected 203 self.ondisconnect = nil -- don't call this when client isn't really connected
204 end 204 end
205 self:_close() 205 self:_close()
206 debug( "handshake failed because:", self.fatalerror ) 206 debug( "handshake failed because:", self.fatalerror )
207 self.eventhandshake = nil 207 self.eventhandshake = nil
208 return -1 208 return -1
227 _ = self.eventstarthandshake and self.eventstarthandshake:close( ) 227 _ = self.eventstarthandshake and self.eventstarthandshake:close( )
228 _ = self.eventconnect and self.eventconnect:close( ) 228 _ = self.eventconnect and self.eventconnect:close( )
229 _ = self.eventsession and self.eventsession:close( ) 229 _ = self.eventsession and self.eventsession:close( )
230 _ = self.eventwritetimeout and self.eventwritetimeout:close( ) 230 _ = self.eventwritetimeout and self.eventwritetimeout:close( )
231 _ = self.eventreadtimeout and self.eventreadtimeout:close( ) 231 _ = self.eventreadtimeout and self.eventreadtimeout:close( )
232 -- call ondisconnect listener (wont be the case if handshake failed on connect) 232 -- call ondisconnect listener (won't be the case if handshake failed on connect)
233 _ = self.ondisconnect and self:ondisconnect( self.fatalerror ~= "client to close" and self.fatalerror) 233 _ = self.ondisconnect and self:ondisconnect( self.fatalerror ~= "client to close" and self.fatalerror)
234 _ = self.conn and self.conn:close( ) -- close connection 234 _ = self.conn and self.conn:close( ) -- close connection
235 _ = self._server and self._server:counter(-1); 235 _ = self._server and self._server:counter(-1);
236 self.eventread, self.eventwrite = nil, nil 236 self.eventread, self.eventwrite = nil, nil
237 self.eventstarthandshake, self.eventhandshake, self.eventclose = nil, nil, nil 237 self.eventstarthandshake, self.eventhandshake, self.eventclose = nil, nil, nil
515 end 515 end
516 end 516 end
517 interface.writebuffer = { t_concat(interface.writebuffer) } 517 interface.writebuffer = { t_concat(interface.writebuffer) }
518 local succ, err, byte = interface.conn:send( interface.writebuffer[1], 1, interface.writebufferlen ) 518 local succ, err, byte = interface.conn:send( interface.writebuffer[1], 1, interface.writebufferlen )
519 --vdebug( "write data:", interface.writebuffer, "error:", err, "part:", byte ) 519 --vdebug( "write data:", interface.writebuffer, "error:", err, "part:", byte )
520 if succ then -- writing succesful 520 if succ then -- writing successful
521 interface.writebuffer[1] = nil 521 interface.writebuffer[1] = nil
522 interface.writebufferlen = 0 522 interface.writebufferlen = 0
523 interface:ondrain(); 523 interface:ondrain();
524 if interface.fatalerror then 524 if interface.fatalerror then
525 debug "closing client after writing" 525 debug "closing client after writing"
544 interface:_close() 544 interface:_close()
545 interface.eventwritetimeout = nil 545 interface.eventwritetimeout = nil
546 return -1; 546 return -1;
547 end 547 end
548 interface.eventwritetimeout = addevent( base, nil, EV_TIMEOUT, callback, cfg.WRITE_TIMEOUT ) -- reg a new timeout event 548 interface.eventwritetimeout = addevent( base, nil, EV_TIMEOUT, callback, cfg.WRITE_TIMEOUT ) -- reg a new timeout event
549 debug( "wantread during write attempt, reg it in readcallback but dont know what really happens next..." ) 549 debug( "wantread during write attempt, reg it in readcallback but don't know what really happens next..." )
550 -- hopefully this works with luasec; its simply not possible to use 2 different write events on a socket in luaevent 550 -- hopefully this works with luasec; its simply not possible to use 2 different write events on a socket in luaevent
551 return -1 551 return -1
552 end 552 end
553 return EV_WRITE, cfg.WRITE_TIMEOUT 553 return EV_WRITE, cfg.WRITE_TIMEOUT
554 else -- connection was closed during writing or fatal error 554 else -- connection was closed during writing or fatal error
600 if not interface.eventwrite then -- register new write event if needed 600 if not interface.eventwrite then -- register new write event if needed
601 interface.eventwrite = addevent( base, interface.conn, EV_WRITE, interface.writecallback, cfg.WRITE_TIMEOUT ) 601 interface.eventwrite = addevent( base, interface.conn, EV_WRITE, interface.writecallback, cfg.WRITE_TIMEOUT )
602 end 602 end
603 interface.eventreadtimeout = addevent( base, nil, EV_TIMEOUT, 603 interface.eventreadtimeout = addevent( base, nil, EV_TIMEOUT,
604 function( ) interface:_close() end, cfg.READ_TIMEOUT) 604 function( ) interface:_close() end, cfg.READ_TIMEOUT)
605 debug( "wantwrite during read attempt, reg it in writecallback but dont know what really happens next..." ) 605 debug( "wantwrite during read attempt, reg it in writecallback but don't know what really happens next..." )
606 -- to be honest i dont know what happens next, if it is allowed to first read, the write etc... 606 -- to be honest i don't know what happens next, if it is allowed to first read, the write etc...
607 else -- connection was closed or fatal error 607 else -- connection was closed or fatal error
608 interface.fatalerror = err 608 interface.fatalerror = err
609 debug( "connection failed in read event:", interface.fatalerror ) 609 debug( "connection failed in read event:", interface.fatalerror )
610 interface:_close() 610 interface:_close()
611 interface.eventread = nil 611 interface.eventread = nil