class EventMachine::FileStreamer

@author Francis Cianfrocca
end
end
}
close_connection_after_writing
# file was sent successfully
streamer.callback{
streamer = EventMachine::FileStreamer.new(self, ‘/tmp/bigfile.tar’)
def post_init
module FileSender
@example
if available (it is part of eventmachine gem itself).
Streaming uses buffering for files larger than 16K and uses so-called fast file reader (a C++ extension)
instantiated. Typically FileStreamer instances are not reused.
Streams a file over a given connection. Streaming begins once the object is

def ensure_mapping_extension_is_present

Other tags:
    Private: -
def ensure_mapping_extension_is_present
  @@fastfilereader ||= (require 'fastfilereaderext')
end

def initialize connection, filename, args = {}

Options Hash: (**args)
  • :http_chunks (Boolean) -- Use HTTP 1.1 style chunked-encoding semantics.

Parameters:
  • filename (String) -- File path
  • connection (EventMachine::Connection) --
def initialize connection, filename, args = {}
  @connection = connection
  @http_chunks = args[:http_chunks]
  if File.exist?(filename)
    @size = File.size(filename)
    if @size <= MappingThreshold
      stream_without_mapping filename
    else
      stream_with_mapping filename
    end
  else
    fail "file not found"
  end
end

def stream_one_chunk

Other tags:
    Private: -
def stream_one_chunk
  loop {
    if @position < @size
      if @connection.get_outbound_data_size > BackpressureLevel
        EventMachine::next_tick {stream_one_chunk}
        break
      else
        len = @size - @position
        len = ChunkSize if (len > ChunkSize)
        @connection.send_data( "#{len.to_s(16)}\r\n" ) if @http_chunks
        @connection.send_data( @mapping.get_chunk( @position, len ))
        @connection.send_data("\r\n") if @http_chunks
        @position += len
      end
    else
      @connection.send_data "0\r\n\r\n" if @http_chunks
      @mapping.close
      succeed
      break
    end
  }
end

def stream_with_mapping filename

Other tags:
    Private: -
def stream_with_mapping filename
  ensure_mapping_extension_is_present
  @position = 0
  @mapping = EventMachine::FastFileReader::Mapper.new filename
  stream_one_chunk
end

def stream_without_mapping filename

Other tags:
    Private: -
def stream_without_mapping filename
  if @http_chunks
    @connection.send_data "#{@size.to_s(16)}\r\n"
    @connection.send_file_data filename
    @connection.send_data "\r\n0\r\n\r\n"
  else
    @connection.send_file_data filename
  end
  succeed
end