lib/zmachine/tcp_channel.rb in zmachine-0.1.1 vs lib/zmachine/tcp_channel.rb in zmachine-0.1.3
- old
+ new
@@ -9,28 +9,32 @@
def initialize(selector)
super(selector)
@close_scheduled = false
@connect_pending = false
+ @server_socket = false
end
def register
@channel_key ||= @socket.register(@selector, current_events, self)
end
def bind(address, port)
+ @server_socket = true
address = InetSocketAddress.new(address, port)
@socket = ServerSocketChannel.open
@socket.configure_blocking(false)
@socket.bind(address)
end
def accept
client_socket = socket.accept
return unless client_socket
client_socket.configure_blocking(false)
- TCPChannel.new(client_socket, @selector)
+ channel = TCPChannel.new(@selector)
+ channel.socket = client_socket
+ channel
end
def connect(address, port)
address = InetSocketAddress.new(address, port)
@socket = SocketChannel.open
@@ -52,10 +56,14 @@
# next pass through the loop, because writable will fire.
raise RuntimeError.new("immediate-connect unimplemented")
end
end
+ def close_connection(flush = true)
+ @reactor.unbind_channel(self) if schedule_close(flush)
+ end
+
def close
if @channel_key
@channel_key.cancel
@channel_key = nil
end
@@ -82,11 +90,10 @@
def write_outbound_data
until @outbound_queue.empty?
buffer = @outbound_queue.first
@socket.write(buffer) if buffer.remaining > 0
-
# Did we consume the whole outbound buffer? If yes,
# pop it off and keep looping. If no, the outbound network
# buffers are full, so break out of here.
if buffer.remaining == 0
@outbound_queue.shift
@@ -143,15 +150,20 @@
if @channel_key.interest_ops != events
@channel_key.interest_ops(events)
end
end
+ # these two are a bit misleading .. only used for the zmq channel
def has_more?
false
end
+ def can_send?
+ false
+ end
+
def current_events
- if @socket.respond_to?(:accept)
+ if @socket.is_a?(ServerSocketChannel)
return SelectionKey::OP_ACCEPT
end
events = 0