Use mapped streamer for files bigger than 16k
Wait until next tick to send more data when 50k is still in the outgoing buffer
Send 16k chunks at a time
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
# File lib/em/streamer.rb, line 51
51: def initialize connection, filename, args = {}
52: @connection = connection
53: @http_chunks = args[:http_chunks]
54:
55: if File.exist?(filename)
56: @size = File.size?(filename)
57: if @size <= MappingThreshold
58: stream_without_mapping filename
59: else
60: stream_with_mapping filename
61: end
62: else
63: fail "file not found"
64: end
65: end
Used internally to stream one chunk at a time over multiple reactor ticks
# File lib/em/streamer.rb, line 89
89: def stream_one_chunk
90: loop {
91: if @position < @size
92: if @connection.get_outbound_data_size > BackpressureLevel
93: EventMachine::next_tick {stream_one_chunk}
94: break
95: else
96: len = @size - @position
97: len = ChunkSize if (len > ChunkSize)
98:
99: @connection.send_data( "#{len.to_s(16)}\r\n" ) if @http_chunks
100: @connection.send_data( @mapping.get_chunk( @position, len ))
101: @connection.send_data("\r\n") if @http_chunks
102:
103: @position += len
104: end
105: else
106: @connection.send_data "0\r\n\r\n" if @http_chunks
107: @mapping.close
108: succeed
109: break
110: end
111: }
112: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.