123456789_123456789_123456789_123456789_123456789_

Class: EventMachine::IOStreamer

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
self, Deferrable
Inherits: Object
Defined in: lib/em/io_streamer.rb

Constant Summary

Deferrable - Included

Pool

Class Method Summary

Instance Method Summary

  • #stream_one_chunk private Internal use only Internal use only

    Used internally to stream one chunk at a time over multiple reactor ticks.

Deferrable - Included

#callback

Specify a block to be executed if and when the Deferrable object receives a status of :succeeded.

#cancel_callback

Cancels an outstanding callback to &block if any.

#cancel_errback

Cancels an outstanding errback to &block if any.

#cancel_timeout

Cancels an outstanding timeout if any.

#errback

Specify a block to be executed if and when the Deferrable object receives a status of :failed.

#fail

Sugar for set_deferred_status(:failed, ...).

#set_deferred_failure

Alias for Deferrable#fail.

#set_deferred_status

Sets the "disposition" (status) of the Deferrable object.

#set_deferred_success
#succeed

Sugar for set_deferred_status(:succeeded, ...).

#timeout

Setting a timeout on a Deferrable causes it to go into the failed state after the Timeout expires (passing no arguments to the object's errbacks).

Constructor Details

.new(connection, io, opts = {}) ⇒ IOStreamer

Parameters:

  • connection (EventMachine::Connection)
  • io (IO)

    Data source

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :http_chunks (Boolean) — default: false

    Use HTTP 1.1 style chunked-encoding semantics.

[ GitHub ]

  
# File 'lib/em/io_streamer.rb', line 33

def initialize(connection, io, opts = {})
  @connection = connection
  @io = io
  @http_chunks = opts[:http_chunks]

  @buff = String.new
  @io.binmode if @io.respond_to?(:binmode)
  stream_one_chunk
end

Instance Method Details

#stream_one_chunk (private)

This method is for internal use only.

Used internally to stream one chunk at a time over multiple reactor ticks

[ GitHub ]

  
# File 'lib/em/io_streamer.rb', line 47

def stream_one_chunk
  loop do
    if @io.eof?
      @connection.send_data "0\r\n\r\n" if @http_chunks
      succeed
      break
    end

    if @connection.respond_to?(:get_outbound_data_size) && (@connection.get_outbound_data_size > FileStreamer::BackpressureLevel)
      EventMachine::next_tick { stream_one_chunk }
      break
    end

    if @io.read(CHUNK_SIZE, @buff)
      @connection.send_data("#{@buff.length.to_s(16)}\r\n") if @http_chunks
      @connection.send_data(@buff)
      @connection.send_data("\r\n") if @http_chunks
    end
  end
end