blob: f773814973409ef3dabde103decc3ab7cc263c16 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
require 'nio'
module ActionCable
module Connection
class StreamEventLoop
def initialize
@nio = NIO::Selector.new
@map = {}
@stopping = false
@todo = Queue.new
Thread.new do
Thread.current.abort_on_exception = true
run
end
end
def attach(io, stream)
@todo << lambda do
@map[io] = stream
@nio.register(io, :r)
end
@nio.wakeup
end
def detach(io, stream)
@todo << lambda do
@nio.deregister(io)
@map.delete io
end
@nio.wakeup
end
def stop
@stopping = true
@nio.wakeup
end
def run
loop do
if @stopping
@nio.close
break
end
until @todo.empty?
@todo.pop(true).call
end
if monitors = @nio.select
monitors.each do |monitor|
io = monitor.io
stream = @map[io]
begin
stream.receive io.read_nonblock(4096)
rescue IO::WaitReadable
next
rescue EOFError
stream.close
end
end
end
end
end
end
end
end
|