Comparison

net/server_select.lua @ 9581:1be99aedb0b3

net.server: Add an API for holding writes of outgoing data
author Kim Alvefur <zash@zash.se>
date Thu, 25 Oct 2018 15:12:59 +0200
parent 9580:5fe91bfb734c
child 9635:fc2266339cd8
comparison
equal deleted inserted replaced
9580:5fe91bfb734c 9581:1be99aedb0b3
483 end 483 end
484 handler.lock = function( self, switch ) 484 handler.lock = function( self, switch )
485 out_error( "server.lua, lock() is deprecated" ) 485 out_error( "server.lua, lock() is deprecated" )
486 handler.lock_read (self, switch) 486 handler.lock_read (self, switch)
487 if switch == true then 487 if switch == true then
488 local tmp = _sendlistlen 488 handler.pause_writes (self)
489 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen )
490 _writetimes[ handler ] = nil
491 if _sendlistlen ~= tmp then
492 nosend = true
493 end
494 elseif switch == false then 489 elseif switch == false then
495 if nosend then 490 handler.resume_writes (self)
496 nosend = false
497 write( "" )
498 end
499 end 491 end
500 return noread, nosend 492 return noread, nosend
501 end 493 end
494 handler.pause_writes = function (self)
495 local tmp = _sendlistlen
496 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen )
497 _writetimes[ handler ] = nil
498 if _sendlistlen ~= tmp then
499 nosend = true
500 end
501 end
502 handler.resume_writes = function (self)
503 if nosend then
504 nosend = false
505 write( "" )
506 end
507 end
508
502 local _readbuffer = function( ) -- this function reads data 509 local _readbuffer = function( ) -- this function reads data
503 local buffer, err, part = receive( socket, pattern ) -- receive buffer with "pattern" 510 local buffer, err, part = receive( socket, pattern ) -- receive buffer with "pattern"
504 if not err or (err == "wantread" or err == "timeout") then -- received something 511 if not err or (err == "wantread" or err == "timeout") then -- received something
505 local buffer = buffer or part or "" 512 local buffer = buffer or part or ""
506 local len = #buffer 513 local len = #buffer
714 local sender_locked; 721 local sender_locked;
715 local _sendbuffer = receiver.sendbuffer; 722 local _sendbuffer = receiver.sendbuffer;
716 function receiver.sendbuffer() 723 function receiver.sendbuffer()
717 _sendbuffer(); 724 _sendbuffer();
718 if sender_locked and receiver.bufferlen() < buffersize then 725 if sender_locked and receiver.bufferlen() < buffersize then
719 sender:resume(); -- Unlock now 726 sender:lock_read(false); -- Unlock now
720 sender_locked = nil; 727 sender_locked = nil;
721 end 728 end
722 end 729 end
723 730
724 local _readbuffer = sender.readbuffer; 731 local _readbuffer = sender.readbuffer;
725 function sender.readbuffer() 732 function sender.readbuffer()
726 _readbuffer(); 733 _readbuffer();
727 if not sender_locked and receiver.bufferlen() >= buffersize then 734 if not sender_locked and receiver.bufferlen() >= buffersize then
728 sender_locked = true; 735 sender_locked = true;
729 sender:pause(); 736 sender:lock_read(true);
730 end 737 end
731 end 738 end
732 sender:set_mode("*a"); 739 sender:set_mode("*a");
733 end 740 end
734 741