Class: ZMachine::TCPChannel
Instance Attribute Summary
Attributes inherited from Channel
#raw, #socket
Instance Method Summary
collapse
Methods inherited from Channel
#can_send?, #close, #send_data, #write_outbound_data
Constructor Details
Returns a new instance of TCPChannel.
11
12
13
14
|
# File 'lib/zmachine/tcp_channel.rb', line 11
def initialize
super
@buffer = Thread.current[:tcp_channel_buffer] ||= ByteBuffer.allocate(1024 * 1024)
end
|
Instance Method Details
#accept ⇒ Object
32
33
34
35
36
37
38
39
40
|
# File 'lib/zmachine/tcp_channel.rb', line 32
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
20
21
22
23
24
25
26
|
# File 'lib/zmachine/tcp_channel.rb', line 20
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
28
29
30
|
# File 'lib/zmachine/tcp_channel.rb', line 28
def bound?
@socket.is_a?(ServerSocketChannel) && @socket.socket.bound?
end
|
#close! ⇒ Object
83
84
85
86
|
# File 'lib/zmachine/tcp_channel.rb', line 83
def close!
ZMachine.logger.debug("zmachine:tcp_channel:#{__method__}", channel: self) if ZMachine.debug
@socket.close
end
|
#closed? ⇒ Boolean
88
89
90
|
# File 'lib/zmachine/tcp_channel.rb', line 88
def closed?
@socket.socket.closed?
end
|
#connect(address, port) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/zmachine/tcp_channel.rb', line 42
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
67
68
69
70
|
# File 'lib/zmachine/tcp_channel.rb', line 67
def connected?
return false if @socket.is_a?(ServerSocketChannel)
@socket.connected?
end
|
#connection_pending? ⇒ Boolean
57
58
59
|
# File 'lib/zmachine/tcp_channel.rb', line 57
def connection_pending?
@socket.connection_pending?
end
|
#finish_connecting ⇒ Object
61
62
63
64
65
|
# File 'lib/zmachine/tcp_channel.rb', line 61
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
92
93
94
|
# File 'lib/zmachine/tcp_channel.rb', line 92
def peer
[@socket.socket.port, @socket.socket.inet_address.host_address]
end
|
#read_inbound_data ⇒ Object
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/zmachine/tcp_channel.rb', line 72
def read_inbound_data
ZMachine.logger.debug("zmachine:tcp_channel:#{__method__}", channel: self) if ZMachine.debug
@buffer.clear
raise IOException.new("EOF") if @socket.read(@buffer) == -1
@buffer.flip
return if @buffer.limit == 0
data = java.util.Arrays.copyOfRange(@buffer.array, @buffer.position, @buffer.limit)
data = String.from_java_bytes(data) unless @raw
data
end
|
#selectable_fd ⇒ Object
16
17
18
|
# File 'lib/zmachine/tcp_channel.rb', line 16
def selectable_fd
@socket
end
|