Class: EventMachine::FileStreamer

Inherits:
Object
  • Object
show all
Includes:
Deferrable
Defined in:
lib/em/streamer.rb

Constant Summary collapse

MappingThreshold =

Use mapped streamer for files bigger than 16k

16384
BackpressureLevel =

Wait until next tick to send more data when 50k is still in the outgoing buffer

50000
ChunkSize =

Send 16k chunks at a time

16384

Instance Method Summary collapse

Methods included from Deferrable

#callback, #cancel_timeout, #errback, #fail, future, #set_deferred_status, #succeed, #timeout

Constructor Details

#initialize(connection, filename, args = {}) ⇒ FileStreamer

Stream a file over a given connection. An optional :http_chunks => true argument will use HTTP 1.1 style chunked-encoding semantics.

module FileSender
  def post_init
    streamer = EventMachine::FileStreamer.new(self, '/tmp/bigfile.tar')
    streamer.callback{
      # file was sent successfully
      close_connection_after_writing
    }
  end
end


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/em/streamer.rb', line 51

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

Instance Method Details

#stream_one_chunkObject

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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/em/streamer.rb', line 89

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