lib/zmachine/connection.rb in zmachine-0.3.2 vs lib/zmachine/connection.rb in zmachine-0.4.0
- old
+ new
@@ -8,10 +8,11 @@
class Connection
extend Forwardable
attr_accessor :channel
+ attr_reader :timer
def self.new(*args)
allocate.instance_eval do
initialize(*args)
@args = args
@@ -59,28 +60,32 @@
end
# EventMachine Connection API
def_delegator :@channel, :bound?
+ def_delegator :@channel, :can_send?
def_delegator :@channel, :closed?
def_delegator :@channel, :connected?
def_delegator :@channel, :connection_pending?
def close_connection(after_writing = false)
- @channel.close(after_writing) do
- ZMachine.close_connection(self)
- end
+ ZMachine.close_connection(self, after_writing)
end
alias :close :close_connection
def close_connection_after_writing
close_connection(true)
end
alias :close_after_writing close_connection_after_writing
+ def close!
+ @timer.cancel if @timer
+ @channel.close!
+ end
+
def comm_inactivity_timeout
@inactivity_timeout
end
def comm_inactivity_timeout=(value)
@@ -211,24 +216,23 @@
else
writable! if @channel_key.writable?
readable! if @channel_key.readable?
end
rescue Java::JavaNioChannels::CancelledKeyException
- # channel may have been closed by write handler. ignore exception and
- # wait for cleanup
+ ZMachine.close_connection(self)
end
def mark_active!
@last_activity = System.nano_time
renew_timer if @inactivity_timeout
end
def renew_timer
@timer.cancel if @timer
if connection_pending? && @connect_timeout
- @timer = ZMachine.add_timer(@connect_timeout) { ZMachine.close_connection(self, Errno::ETIMEDOUT) }
+ @timer = ZMachine.add_timer(@connect_timeout) { ZMachine.close_connection(self, true, Errno::ETIMEDOUT) }
elsif @inactivity_timeout
- @timer = ZMachine.add_timer(@inactivity_timeout) { ZMachine.close_connection(self, Errno::ETIMEDOUT) }
+ @timer = ZMachine.add_timer(@inactivity_timeout) { ZMachine.close_connection(self, true, Errno::ETIMEDOUT) }
end
end
end
end