lib/zmachine/channel.rb in zmachine-0.3.2 vs lib/zmachine/channel.rb in zmachine-0.4.0

- old
+ new

@@ -6,11 +6,10 @@ attr_accessor :socket attr_accessor :raw def initialize - @inbound_buffer = ByteBuffer.allocate(1024 * 1024) @outbound_queue = ConcurrentLinkedQueue.new @raw = false end # methods that need to be implemented in sub classes: @@ -28,11 +27,11 @@ # closed? # peer # write_outbound_data def can_send? - !@outbound_queue.empty? + connected? && !@outbound_queue.empty? end def send_data(data) ZMachine.logger.debug("zmachine:channel:#{__method__}", channel: self) if ZMachine.debug raise RuntimeError.new("send_data called after close") if @closed_callback @@ -53,28 +52,16 @@ # pop it off and keep looping. If no, the outbound network # buffers are full, so break out of here. break if buffer.has_remaining @outbound_queue.poll end - maybe_close_with_callback end - def close(after_writing = false, &block) + def close return true if closed? - ZMachine.logger.debug("zmachine:channel:#{__method__}", channel: self, after_writing: after_writing, caller: caller[0].inspect) if ZMachine.debug - @close_scheduled = true - @closed_callback = block if block - @outbound_queue.clear unless after_writing - maybe_close_with_callback - end - - def maybe_close_with_callback - ZMachine.logger.debug("zmachine:channel:#{__method__}", channel: self, can_send: can_send?) if ZMachine.debug - return false if can_send? - return true unless @close_scheduled + ZMachine.logger.debug("zmachine:channel:#{__method__}", channel: self, caller: caller[0].inspect) if ZMachine.debug + @outbound_queue.clear close! - @closed_callback.call if @closed_callback - return true end end end