Class: EventMachine::RTMP::Request

Inherits:
ConnectionDelegate show all
Includes:
Deferrable
Defined in:
lib/em-rtmp/request.rb

Direct Known Subclasses

ConnectRequest

Instance Attribute Summary collapse

Attributes inherited from ConnectionDelegate

#state

Instance Method Summary collapse

Methods inherited from ConnectionDelegate

#bytes_waiting, #change_state, #read, #write

Methods included from IOHelpers

#read_bitfield, #read_double_be, #read_int29, #read_safe, #read_uint16_be, #read_uint24_be, #read_uint32_be, #read_uint32_le, #read_uint8, #write_bitfield, #write_double_be, #write_int29, #write_uint16_be, #write_uint24_be, #write_uint32_be, #write_uint32_le, #write_uint8

Constructor Details

#initialize(connection) ⇒ Request

Initialize, setting attributes

attrs - Hash of attributes to write

Returns nothing



23
24
25
26
27
28
# File 'lib/em-rtmp/request.rb', line 23

def initialize(connection)
  super connection
  self.header = Header.new
  self.message = Message.new
  self.body = ""
end

Instance Attribute Details

#bodyObject

The request implementation here references a Header object and a message body. The body can be any object that responds to to_s.



16
17
18
# File 'lib/em-rtmp/request.rb', line 16

def body
  @body
end

#headerObject

The request implementation here references a Header object and a message body. The body can be any object that responds to to_s.



16
17
18
# File 'lib/em-rtmp/request.rb', line 16

def header
  @header
end

#messageObject

The request implementation here references a Header object and a message body. The body can be any object that responds to to_s.



16
17
18
# File 'lib/em-rtmp/request.rb', line 16

def message
  @message
end

Instance Method Details

#chunk_countObject

Determines the number of chunks we will send

Returns the chunk count as an Integer



47
48
49
# File 'lib/em-rtmp/request.rb', line 47

def chunk_count
  (body.length / chunk_size.to_f).ceil
end

#chunk_sizeObject

Determines the proper chunk size for each packet we will send

Returns the chunk size as an Integer



40
41
42
# File 'lib/em-rtmp/request.rb', line 40

def chunk_size
  @connection.chunk_size
end

#chunksObject

Splits the body into chunks for sending

Returns an Array of Strings, each a chunk to send



54
55
56
57
58
59
60
# File 'lib/em-rtmp/request.rb', line 54

def chunks
  (0...chunk_count).map do |chunk|
    offset_start = chunk_size * chunk
    offset_end = [offset_start + chunk_size, body.length].min - 1
    body[offset_start..offset_end]
  end
end

#header_length_for_chunk(offset) ⇒ Object

Determine the proper header length for a given chunk

Returns an Integer



65
66
67
# File 'lib/em-rtmp/request.rb', line 65

def header_length_for_chunk(offset)
  offset == 0 ? 12 : 1
end

#sendObject

Update the header and send each chunk with an appropriate header.

Returns the number of bytes written



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/em-rtmp/request.rb', line 72

def send
  bytes_sent = 0
  update_header

  Logger.info "head: #{header.inspect}"
  Logger.info "body: #{message.inspect}"

  for i in 0..(chunk_count-1)
    self.header.header_length = header_length_for_chunk(i)
    bytes_sent += send_chunk chunks[i]
  end

  PendingRequest.create self, @connection

  bytes_sent
end

#send_chunk(chunk) ⇒ Object

Send a chunk to the stream

body - Body string to write

Returns the number of bytes written



94
95
96
97
# File 'lib/em-rtmp/request.rb', line 94

def send_chunk(chunk)
  Logger.debug "sending chunk (#{chunk.length})", indent: 1
  write(header.encode) + write(chunk)
end

#update_headerObject

Updates the header to reflect the actual length of the body

Returns nothing



33
34
35
# File 'lib/em-rtmp/request.rb', line 33

def update_header
  header.body_length = body.length
end