Mercurial > prosody-hg
comparison net/server_event.lua @ 5776:bd0ff8ae98a8
Remove all trailing whitespace
| author | Florian Zeitz <florob@babelmonkeys.de> |
|---|---|
| date | Fri, 09 Aug 2013 17:48:21 +0200 |
| parents | 61521ce49aae |
| children | 451b4649fad2 7a5ddbaf758d |
comparison
equal
deleted
inserted
replaced
| 5775:a6c2b8933507 | 5776:bd0ff8ae98a8 |
|---|---|
| 113 | 113 |
| 114 -- Client interface methods | 114 -- Client interface methods |
| 115 local interface_mt | 115 local interface_mt |
| 116 do | 116 do |
| 117 interface_mt = {}; interface_mt.__index = interface_mt; | 117 interface_mt = {}; interface_mt.__index = interface_mt; |
| 118 | 118 |
| 119 local addevent = base.addevent | 119 local addevent = base.addevent |
| 120 local coroutine_wrap, coroutine_yield = coroutine.wrap,coroutine.yield | 120 local coroutine_wrap, coroutine_yield = coroutine.wrap,coroutine.yield |
| 121 | 121 |
| 122 -- Private methods | 122 -- Private methods |
| 123 function interface_mt:_position(new_position) | 123 function interface_mt:_position(new_position) |
| 124 self.position = new_position or self.position | 124 self.position = new_position or self.position |
| 125 return self.position; | 125 return self.position; |
| 126 end | 126 end |
| 127 function interface_mt:_close() | 127 function interface_mt:_close() |
| 128 return self:_destroy(); | 128 return self:_destroy(); |
| 129 end | 129 end |
| 130 | 130 |
| 131 function interface_mt:_start_connection(plainssl) -- should be called from addclient | 131 function interface_mt:_start_connection(plainssl) -- should be called from addclient |
| 132 local callback = function( event ) | 132 local callback = function( event ) |
| 133 if EV_TIMEOUT == event then -- timeout during connection | 133 if EV_TIMEOUT == event then -- timeout during connection |
| 134 self.fatalerror = "connection timeout" | 134 self.fatalerror = "connection timeout" |
| 135 self:ontimeout() -- call timeout listener | 135 self:ontimeout() -- call timeout listener |
| 266 self.interface, self.readcallback = nil, nil | 266 self.interface, self.readcallback = nil, nil |
| 267 end | 267 end |
| 268 interfacelist( "delete", self ) | 268 interfacelist( "delete", self ) |
| 269 return true | 269 return true |
| 270 end | 270 end |
| 271 | 271 |
| 272 function interface_mt:_lock(nointerface, noreading, nowriting) -- lock or unlock this interface or events | 272 function interface_mt:_lock(nointerface, noreading, nowriting) -- lock or unlock this interface or events |
| 273 self.nointerface, self.noreading, self.nowriting = nointerface, noreading, nowriting | 273 self.nointerface, self.noreading, self.nowriting = nointerface, noreading, nowriting |
| 274 return nointerface, noreading, nowriting | 274 return nointerface, noreading, nowriting |
| 275 end | 275 end |
| 276 | 276 |
| 277 --TODO: Deprecate | 277 --TODO: Deprecate |
| 278 function interface_mt:lock_read(switch) | 278 function interface_mt:lock_read(switch) |
| 279 if switch then | 279 if switch then |
| 280 return self:pause(); | 280 return self:pause(); |
| 281 else | 281 else |
| 298 if c then | 298 if c then |
| 299 self._connections = self._connections + c | 299 self._connections = self._connections + c |
| 300 end | 300 end |
| 301 return self._connections | 301 return self._connections |
| 302 end | 302 end |
| 303 | 303 |
| 304 -- Public methods | 304 -- Public methods |
| 305 function interface_mt:write(data) | 305 function interface_mt:write(data) |
| 306 if self.nowriting then return nil, "locked" end | 306 if self.nowriting then return nil, "locked" end |
| 307 --vdebug( "try to send data to client, id/data:", self.id, data ) | 307 --vdebug( "try to send data to client, id/data:", self.id, data ) |
| 308 data = tostring( data ) | 308 data = tostring( data ) |
| 341 self:_lock( true ) | 341 self:_lock( true ) |
| 342 self:_close( 0 ) | 342 self:_close( 0 ) |
| 343 return true | 343 return true |
| 344 end | 344 end |
| 345 end | 345 end |
| 346 | 346 |
| 347 function interface_mt:socket() | 347 function interface_mt:socket() |
| 348 return self.conn | 348 return self.conn |
| 349 end | 349 end |
| 350 | 350 |
| 351 function interface_mt:server() | 351 function interface_mt:server() |
| 352 return self._server or self; | 352 return self._server or self; |
| 353 end | 353 end |
| 354 | 354 |
| 355 function interface_mt:port() | 355 function interface_mt:port() |
| 356 return self._port | 356 return self._port |
| 357 end | 357 end |
| 358 | 358 |
| 359 function interface_mt:serverport() | 359 function interface_mt:serverport() |
| 360 return self._serverport | 360 return self._serverport |
| 361 end | 361 end |
| 362 | 362 |
| 363 function interface_mt:ip() | 363 function interface_mt:ip() |
| 364 return self._ip | 364 return self._ip |
| 365 end | 365 end |
| 366 | 366 |
| 367 function interface_mt:ssl() | 367 function interface_mt:ssl() |
| 368 return self._usingssl | 368 return self._usingssl |
| 369 end | 369 end |
| 370 | 370 |
| 371 function interface_mt:type() | 371 function interface_mt:type() |
| 372 return self._type or "client" | 372 return self._type or "client" |
| 373 end | 373 end |
| 374 | 374 |
| 375 function interface_mt:connections() | 375 function interface_mt:connections() |
| 376 return self._connections | 376 return self._connections |
| 377 end | 377 end |
| 378 | 378 |
| 379 function interface_mt:address() | 379 function interface_mt:address() |
| 380 return self.addr | 380 return self.addr |
| 381 end | 381 end |
| 382 | 382 |
| 383 function interface_mt:set_sslctx(sslctx) | 383 function interface_mt:set_sslctx(sslctx) |
| 384 self._sslctx = sslctx; | 384 self._sslctx = sslctx; |
| 385 if sslctx then | 385 if sslctx then |
| 386 self.starttls = nil; -- use starttls() of interface_mt | 386 self.starttls = nil; -- use starttls() of interface_mt |
| 387 else | 387 else |
| 393 if pattern then | 393 if pattern then |
| 394 self._pattern = pattern; | 394 self._pattern = pattern; |
| 395 end | 395 end |
| 396 return self._pattern; | 396 return self._pattern; |
| 397 end | 397 end |
| 398 | 398 |
| 399 function interface_mt:set_send(new_send) | 399 function interface_mt:set_send(new_send) |
| 400 -- No-op, we always use the underlying connection's send | 400 -- No-op, we always use the underlying connection's send |
| 401 end | 401 end |
| 402 | 402 |
| 403 function interface_mt:starttls(sslctx, call_onconnect) | 403 function interface_mt:starttls(sslctx, call_onconnect) |
| 404 debug( "try to start ssl at client id:", self.id ) | 404 debug( "try to start ssl at client id:", self.id ) |
| 405 local err | 405 local err |
| 406 self._sslctx = sslctx; | 406 self._sslctx = sslctx; |
| 407 if self._usingssl then -- startssl was already called | 407 if self._usingssl then -- startssl was already called |
| 426 debug "ssl session delayed until writebuffer is empty..." | 426 debug "ssl session delayed until writebuffer is empty..." |
| 427 end | 427 end |
| 428 self.starttls = false; | 428 self.starttls = false; |
| 429 return true | 429 return true |
| 430 end | 430 end |
| 431 | 431 |
| 432 function interface_mt:setoption(option, value) | 432 function interface_mt:setoption(option, value) |
| 433 if self.conn.setoption then | 433 if self.conn.setoption then |
| 434 return self.conn:setoption(option, value); | 434 return self.conn:setoption(option, value); |
| 435 end | 435 end |
| 436 return false, "setoption not implemented"; | 436 return false, "setoption not implemented"; |
| 437 end | 437 end |
| 438 | 438 |
| 439 function interface_mt:setlistener(listener) | 439 function interface_mt:setlistener(listener) |
| 440 self.onconnect, self.ondisconnect, self.onincoming, self.ontimeout, self.onreadtimeout, self.onstatus | 440 self.onconnect, self.ondisconnect, self.onincoming, self.ontimeout, self.onreadtimeout, self.onstatus |
| 441 = listener.onconnect, listener.ondisconnect, listener.onincoming, | 441 = listener.onconnect, listener.ondisconnect, listener.onincoming, |
| 442 listener.ontimeout, listener.onreadtimeout, listener.onstatus; | 442 listener.ontimeout, listener.onreadtimeout, listener.onstatus; |
| 443 end | 443 end |
| 497 readcallback = false; -- will be called on read events | 497 readcallback = false; -- will be called on read events |
| 498 nointerface = true; -- lock/unlock parameter of this interface | 498 nointerface = true; -- lock/unlock parameter of this interface |
| 499 noreading = false, nowriting = false; -- locks of the read/writecallback | 499 noreading = false, nowriting = false; -- locks of the read/writecallback |
| 500 startsslcallback = false; -- starting handshake callback | 500 startsslcallback = false; -- starting handshake callback |
| 501 position = false; -- position of client in interfacelist | 501 position = false; -- position of client in interfacelist |
| 502 | 502 |
| 503 -- Properties | 503 -- Properties |
| 504 _ip = ip, _port = port, _server = server, _pattern = pattern, | 504 _ip = ip, _port = port, _server = server, _pattern = pattern, |
| 505 _serverport = (server and server:port() or nil), | 505 _serverport = (server and server:port() or nil), |
| 506 _sslctx = sslctx; -- parameters | 506 _sslctx = sslctx; -- parameters |
| 507 _usingssl = false; -- client is using ssl; | 507 _usingssl = false; -- client is using ssl; |
| 573 interface.eventwrite = nil | 573 interface.eventwrite = nil |
| 574 return -1 | 574 return -1 |
| 575 end | 575 end |
| 576 end | 576 end |
| 577 end | 577 end |
| 578 | 578 |
| 579 interface.readcallback = function( event ) -- called on read events | 579 interface.readcallback = function( event ) -- called on read events |
| 580 --vdebug( "new client read event, id/ip/port:", tostring(interface.id), tostring(ip), tostring(port) ) | 580 --vdebug( "new client read event, id/ip/port:", tostring(interface.id), tostring(ip), tostring(port) ) |
| 581 if interface.noreading or interface.fatalerror then -- leave this event | 581 if interface.noreading or interface.fatalerror then -- leave this event |
| 582 --vdebug( "leaving this event because:", tostring(interface.noreading or interface.fatalerror) ) | 582 --vdebug( "leaving this event because:", tostring(interface.noreading or interface.fatalerror) ) |
| 583 interface.eventread = nil | 583 interface.eventread = nil |
| 646 do | 646 do |
| 647 function handleserver( server, addr, port, pattern, listener, sslctx ) -- creates an server interface | 647 function handleserver( server, addr, port, pattern, listener, sslctx ) -- creates an server interface |
| 648 debug "creating server interface..." | 648 debug "creating server interface..." |
| 649 local interface = { | 649 local interface = { |
| 650 _connections = 0; | 650 _connections = 0; |
| 651 | 651 |
| 652 conn = server; | 652 conn = server; |
| 653 onconnect = listener.onconnect; -- will be called when new client connected | 653 onconnect = listener.onconnect; -- will be called when new client connected |
| 654 eventread = false; -- read event handler | 654 eventread = false; -- read event handler |
| 655 eventclose = false; -- close event handler | 655 eventclose = false; -- close event handler |
| 656 readcallback = false; -- read event callback | 656 readcallback = false; -- read event callback |
| 657 fatalerror = false; -- error message | 657 fatalerror = false; -- error message |
| 658 nointerface = true; -- lock/unlock parameter | 658 nointerface = true; -- lock/unlock parameter |
| 659 | 659 |
| 660 _ip = addr, _port = port, _pattern = pattern, | 660 _ip = addr, _port = port, _pattern = pattern, |
| 661 _sslctx = sslctx; | 661 _sslctx = sslctx; |
| 662 } | 662 } |
| 663 interface.id = tostring(interface):match("%x+$"); | 663 interface.id = tostring(interface):match("%x+$"); |
| 664 interface.readcallback = function( event ) -- server handler, called on incoming connections | 664 interface.readcallback = function( event ) -- server handler, called on incoming connections |
| 693 clientinterface:starttls(sslctx, true) | 693 clientinterface:starttls(sslctx, true) |
| 694 else | 694 else |
| 695 clientinterface:_start_session( true ) | 695 clientinterface:_start_session( true ) |
| 696 end | 696 end |
| 697 debug( "accepted incoming client connection from:", client_ip or "<unknown IP>", client_port or "<unknown port>", "to", port or "<unknown port>"); | 697 debug( "accepted incoming client connection from:", client_ip or "<unknown IP>", client_port or "<unknown port>", "to", port or "<unknown port>"); |
| 698 | 698 |
| 699 client, err = server:accept() -- try to accept again | 699 client, err = server:accept() -- try to accept again |
| 700 end | 700 end |
| 701 return EV_READ | 701 return EV_READ |
| 702 end | 702 end |
| 703 | 703 |
| 704 server:settimeout( 0 ) | 704 server:settimeout( 0 ) |
| 705 setmetatable(interface, interface_mt) | 705 setmetatable(interface, interface_mt) |
| 706 interfacelist( "add", interface ) | 706 interfacelist( "add", interface ) |
| 707 interface:_start_session() | 707 interface:_start_session() |
| 708 return interface | 708 return interface |
| 741 local interface = handleclient( client, ip, port, nil, pattern, listeners, sslctx ) | 741 local interface = handleclient( client, ip, port, nil, pattern, listeners, sslctx ) |
| 742 interface:_start_connection(sslctx) | 742 interface:_start_connection(sslctx) |
| 743 return interface, client | 743 return interface, client |
| 744 --function handleclient( client, ip, port, server, pattern, listener, _, sslctx ) -- creates an client interface | 744 --function handleclient( client, ip, port, server, pattern, listener, _, sslctx ) -- creates an client interface |
| 745 end | 745 end |
| 746 | 746 |
| 747 function addclient( addr, serverport, listener, pattern, localaddr, localport, sslcfg, startssl ) | 747 function addclient( addr, serverport, listener, pattern, localaddr, localport, sslcfg, startssl ) |
| 748 local client, err = socket.tcp() -- creating new socket | 748 local client, err = socket.tcp() -- creating new socket |
| 749 if not client then | 749 if not client then |
| 750 debug( "cannot create socket:", err ) | 750 debug( "cannot create socket:", err ) |
| 751 return nil, err | 751 return nil, err |
| 832 return signal_events[signal_num]; | 832 return signal_events[signal_num]; |
| 833 end | 833 end |
| 834 | 834 |
| 835 local function link(sender, receiver, buffersize) | 835 local function link(sender, receiver, buffersize) |
| 836 local sender_locked; | 836 local sender_locked; |
| 837 | 837 |
| 838 function receiver:ondrain() | 838 function receiver:ondrain() |
| 839 if sender_locked then | 839 if sender_locked then |
| 840 sender:resume(); | 840 sender:resume(); |
| 841 sender_locked = nil; | 841 sender_locked = nil; |
| 842 end | 842 end |
| 843 end | 843 end |
| 844 | 844 |
| 845 function sender:onincoming(data) | 845 function sender:onincoming(data) |
| 846 receiver:write(data); | 846 receiver:write(data); |
| 847 if receiver.writebufferlen >= buffersize then | 847 if receiver.writebufferlen >= buffersize then |
| 848 sender_locked = true; | 848 sender_locked = true; |
| 849 sender:pause(); | 849 sender:pause(); |
