Class: ZMachine::TCPChannel
Instance Attribute Summary
Attributes inherited from Channel
#raw, #socket
Instance Method Summary
collapse
Methods inherited from Channel
#can_send?, #close, #initialize, #maybe_close_with_callback, #send_data, #write_outbound_data
Instance Method Details
#accept ⇒ Object
27
28
29
30
31
32
33
34
35
|
# File 'lib/zmachine/tcp_channel.rb', line 27
def accept
ZMachine.logger.debug("zmachine:tcp_channel:#{__method__}", channel: self) if ZMachine.debug
client_socket = @socket.accept
return unless client_socket
client_socket.configure_blocking(false)
channel = TCPChannel.new
channel.socket = client_socket
channel
end
|
#bind(address, port) ⇒ Object
15
16
17
18
19
20
21
|
# File 'lib/zmachine/tcp_channel.rb', line 15
def bind(address, port)
ZMachine.logger.debug("zmachine:tcp_channel:#{__method__}", channel: self) if ZMachine.debug
address = InetSocketAddress.new(address, port)
@socket = ServerSocketChannel.open
@socket.configure_blocking(false)
@socket.bind(address)
end
|
#bound? ⇒ Boolean
23
24
25
|
# File 'lib/zmachine/tcp_channel.rb', line 23
def bound?
@socket.is_a?(ServerSocketChannel) && @socket.socket.bound?
end
|
#close! ⇒ Object
78
79
80
81
|
# File 'lib/zmachine/tcp_channel.rb', line 78
def close!
ZMachine.logger.debug("zmachine:tcp_channel:#{__method__}", channel: self) if ZMachine.debug
@socket.close
end
|
#closed? ⇒ Boolean
83
84
85
|
# File 'lib/zmachine/tcp_channel.rb', line 83
def closed?
@socket.socket.closed?
end
|
#connect(address, port) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/zmachine/tcp_channel.rb', line 37
def connect(address, port)
ZMachine.logger.debug("zmachine:tcp_channel:#{__method__}", channel: self) if ZMachine.debug
address = InetSocketAddress.new(address, port)
@socket = SocketChannel.open
@socket.configure_blocking(false)
if socket.connect(address)
raise RuntimeError.new("immediate-connect unimplemented")
end
end
|
#connected? ⇒ Boolean
62
63
64
|
# File 'lib/zmachine/tcp_channel.rb', line 62
def connected?
@socket.connected?
end
|
#connection_pending? ⇒ Boolean
52
53
54
|
# File 'lib/zmachine/tcp_channel.rb', line 52
def connection_pending?
@socket.connection_pending?
end
|
#finish_connecting ⇒ Object
56
57
58
59
60
|
# File 'lib/zmachine/tcp_channel.rb', line 56
def finish_connecting
ZMachine.logger.debug("zmachine:tcp_channel:#{__method__}", channel: self) if ZMachine.debug
return unless connection_pending?
@socket.finish_connect
end
|
#peer ⇒ Object
87
88
89
|
# File 'lib/zmachine/tcp_channel.rb', line 87
def peer
[@socket.socket.port, @socket.socket.inet_address.host_address]
end
|
#read_inbound_data ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/zmachine/tcp_channel.rb', line 66
def read_inbound_data
ZMachine.logger.debug("zmachine:tcp_channel:#{__method__}", channel: self) if ZMachine.debug
buffer = @inbound_buffer
buffer.clear
raise IOException.new("EOF") if @socket.read(buffer) == -1
buffer.flip
return if buffer.limit == 0
data = buffer.array[buffer.position...buffer.limit]
data = String.from_java_bytes(data) unless @raw
data
end
|
#selectable_fd ⇒ Object
11
12
13
|
# File 'lib/zmachine/tcp_channel.rb', line 11
def selectable_fd
@socket
end
|