Class: NIO::WebSocket::RawAdapter
- Inherits:
-
Object
- Object
- NIO::WebSocket::RawAdapter
show all
- Defined in:
- lib/nio/websocket/raw_adapter.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(io, options) ⇒ RawAdapter
Returns a new instance of RawAdapter.
4
5
6
7
8
9
|
# File 'lib/nio/websocket/raw_adapter.rb', line 4
def initialize(io, options)
@inner = io
@options = options
@buffer = ""
@mutex = Mutex.new
end
|
Instance Attribute Details
#closing ⇒ Object
Returns the value of attribute closing.
10
11
12
|
# File 'lib/nio/websocket/raw_adapter.rb', line 10
def closing
@closing
end
|
#inner ⇒ Object
Returns the value of attribute inner.
10
11
12
|
# File 'lib/nio/websocket/raw_adapter.rb', line 10
def inner
@inner
end
|
#monitor ⇒ Object
Returns the value of attribute monitor.
10
11
12
|
# File 'lib/nio/websocket/raw_adapter.rb', line 10
def monitor
@monitor
end
|
#options ⇒ Object
Returns the value of attribute options.
10
11
12
|
# File 'lib/nio/websocket/raw_adapter.rb', line 10
def options
@options
end
|
Instance Method Details
#add_to_reactor ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/nio/websocket/raw_adapter.rb', line 26
def add_to_reactor
@monitor = Reactor.selector.register(inner, :rw)
monitor.value = proc do
begin
read if monitor.readable?
pump_buffer if monitor.writable?
rescue Errno::ECONNRESET, EOFError, Errno::ECONNABORTED
teardown
WebSocket.logger.info "#{inner} socket closed"
rescue IO::WaitReadable
rescue IO::WaitWritable
monitor.interests = :rw
end
if @closing
if !monitor.readable? && @buffer.empty?
teardown
WebSocket.logger.info "#{inner} closed"
else
monitor.interests = :rw unless monitor.closed?
end
end
end
end
|
#close ⇒ Object
17
18
19
20
21
22
23
24
|
# File 'lib/nio/websocket/raw_adapter.rb', line 17
def close
return false if @closing
@closing = true
monitor.interests = :rw
Reactor.selector.wakeup
true
end
|
#pump_buffer ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/nio/websocket/raw_adapter.rb', line 69
def pump_buffer
@mutex.synchronize do
written = 0
begin
written = inner.write_nonblock @buffer unless @buffer.empty?
WebSocket.logger.debug { "Pumped #{written} bytes of data from buffer to #{inner}:\n#{@buffer}" } unless @buffer.empty? || !WebSocket.log_traffic?
@buffer = @buffer.byteslice(written..-1) if written > 0
WebSocket.logger.debug { "The buffer is now:\n#{@buffer}" } unless @buffer.empty? || !WebSocket.log_traffic?
rescue IO::WaitWritable, IO::WaitReadable
return written
ensure
monitor.interests = @buffer.empty? ? :r : :rw
end
written
end
end
|
#read ⇒ Object
51
52
53
54
55
56
57
58
|
# File 'lib/nio/websocket/raw_adapter.rb', line 51
def read
data = inner.read_nonblock(16384)
if data
WebSocket.logger.debug { "Incoming data on #{inner}:\n#{data}" } if WebSocket.log_traffic?
yield data if block_given?
end
data
end
|
#teardown ⇒ Object
12
13
14
15
|
# File 'lib/nio/websocket/raw_adapter.rb', line 12
def teardown
monitor.close
inner.close
end
|
#write(data) ⇒ Object
60
61
62
63
64
65
66
67
|
# File 'lib/nio/websocket/raw_adapter.rb', line 60
def write(data)
@mutex.synchronize do
@buffer << data
end
return unless monitor
pump_buffer
Reactor.selector.wakeup unless monitor.interests == :r
end
|