Class: Tamashii::Server::Connection::Stream
- Inherits:
-
Object
- Object
- Tamashii::Server::Connection::Stream
- Defined in:
- lib/tamashii/server/connection/stream.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#event_loop ⇒ Object
readonly
Returns the value of attribute event_loop.
Instance Method Summary collapse
- #close ⇒ Object
- #each(&callback) ⇒ Object
- #flush_write_buffer ⇒ Object
- #hijack_rack_socket ⇒ Object
-
#initialize(event_loop, socket) ⇒ Stream
constructor
A new instance of Stream.
- #receive(data) ⇒ Object
- #shutdown ⇒ Object
- #write(data) ⇒ Object
Constructor Details
#initialize(event_loop, socket) ⇒ Stream
Returns a new instance of Stream.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/tamashii/server/connection/stream.rb', line 10 def initialize(event_loop, socket) @event_loop = event_loop @socket = socket @stream_send = socket.env['stream.send'] @rack_hijack_io = nil @write_lock = Mutex.new @write_head = nil @write_buffer = Queue.new end |
Instance Attribute Details
#event_loop ⇒ Object (readonly)
Returns the value of attribute event_loop.
8 9 10 |
# File 'lib/tamashii/server/connection/stream.rb', line 8 def event_loop @event_loop end |
Instance Method Details
#close ⇒ Object
26 27 28 29 |
# File 'lib/tamashii/server/connection/stream.rb', line 26 def close shutdown @socket.client_gone end |
#each(&callback) ⇒ Object
22 23 24 |
# File 'lib/tamashii/server/connection/stream.rb', line 22 def each(&callback) @stream_send ||= callback end |
#flush_write_buffer ⇒ Object
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/tamashii/server/connection/stream.rb', line 45 def flush_write_buffer @write_lock.synchronize do loop do return true if @write_buffer.empty? && @write_head.nil? @write_head = @write_buffer.pop if @write_head.nil? return unless process_flush end end end |
#hijack_rack_socket ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/tamashii/server/connection/stream.rb', line 60 def hijack_rack_socket return unless @socket.env['rack.hijack'] @socket.env['rack.hijack'].call @rack_hijack_io = @socket.env['rack.hijack_io'] @event_loop.attach(@rack_hijack_io, self) end |
#receive(data) ⇒ Object
56 57 58 |
# File 'lib/tamashii/server/connection/stream.rb', line 56 def receive(data) @socket.parse(data) end |
#shutdown ⇒ Object
31 32 33 |
# File 'lib/tamashii/server/connection/stream.rb', line 31 def shutdown clean_rack_hijack end |
#write(data) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/tamashii/server/connection/stream.rb', line 35 def write(data) return @stream_send.call(data) if @stream_send return write_safe(data) if @write_lock.try_lock write_buffer(data) data.bytesize rescue EOFError, Errno::ECONNRESET @socket.client_gone end |