123456789_123456789_123456789_123456789_123456789_

Class: ActionCable::Connection::Stream

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: actioncable/lib/action_cable/connection/stream.rb

Class Method Summary

Instance Method Summary

Constructor Details

.new(event_loop, socket) ⇒ Stream

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/stream.rb', line 12

def initialize(event_loop, socket)
  @event_loop    = event_loop
  @socket_object = socket
  @stream_send   = socket.env["stream.send"]

  @rack_hijack_io = nil
  @write_lock = Mutex.new

  @write_head = nil
  @write_buffer = Queue.new
end

Instance Method Details

#clean_rack_hijack (private)

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/stream.rb', line 110

def clean_rack_hijack
  return unless @rack_hijack_io
  @event_loop.detach(@rack_hijack_io, self)
  @rack_hijack_io = nil
end

#close

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/stream.rb', line 28

def close
  shutdown
  @socket_object.client_gone
end

#each(&callback)

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/stream.rb', line 24

def each(&callback)
  @stream_send ||= callback
end

#flush_write_buffer

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/stream.rb', line 72

def flush_write_buffer
  @write_lock.synchronize do
    loop do
      if @write_head.nil?
        return true if @write_buffer.empty?
        @write_head = @write_buffer.pop
      end

      written = @rack_hijack_io.write_nonblock(@write_head, exception: false)
      case written
      when :wait_writable
        return false
      when @write_head.bytesize
        @write_head = nil
      else
        @write_head = @write_head.byteslice(written, @write_head.bytesize)
        return false
      end
    end
  end
end

#hijack_rack_socket

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/stream.rb', line 98

def hijack_rack_socket
  return unless @socket_object.env["rack.hijack"]

  # This should return the underlying io according to the SPEC:
  @rack_hijack_io = @socket_object.env["rack.hijack"].call
  # Retain existing behavior if required:
  @rack_hijack_io ||= @socket_object.env["rack.hijack_io"]

  @event_loop.attach(@rack_hijack_io, self)
end

#receive(data)

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/stream.rb', line 94

def receive(data)
  @socket_object.parse(data)
end

#shutdown

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/stream.rb', line 33

def shutdown
  clean_rack_hijack
end

#write(data)

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/stream.rb', line 37

def write(data)
  if @stream_send
    return @stream_send.call(data)
  end

  if @write_lock.try_lock
    begin
      if @write_head.nil? && @write_buffer.empty?
        written = @rack_hijack_io.write_nonblock(data, exception: false)

        case written
        when :wait_writable
          # proceed below
        when data.bytesize
          return data.bytesize
        else
          @write_head = data.byteslice(written, data.bytesize)
          @event_loop.writes_pending @rack_hijack_io

          return data.bytesize
        end
      end
    ensure
      @write_lock.unlock
    end
  end

  @write_buffer << data
  @event_loop.writes_pending @rack_hijack_io

  data.bytesize
rescue EOFError, Errno::ECONNRESET
  @socket_object.client_gone
end