Class: EventMachine::RTMP::Response

Inherits:
ConnectionDelegate show all
Defined in:
lib/em-rtmp/response.rb

Instance Attribute Summary collapse

Attributes inherited from ConnectionDelegate

#state

Class Method Summary collapse

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(channel_id, connection) ⇒ Response

Initialize as a logical stream on a given stream ID

Returns nothing.



9
10
11
12
13
14
15
16
# File 'lib/em-rtmp/response.rb', line 9

def initialize(channel_id, connection)
  super connection

  self.channel_id = channel_id
  self.header = Header.new
  self.body = ""
  self.waiting_on_bytes = 0
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



4
5
6
# File 'lib/em-rtmp/response.rb', line 4

def body
  @body
end

#channel_idObject

Returns the value of attribute channel_id.



4
5
6
# File 'lib/em-rtmp/response.rb', line 4

def channel_id
  @channel_id
end

#headerObject

Returns the value of attribute header.



4
5
6
# File 'lib/em-rtmp/response.rb', line 4

def header
  @header
end

#messageObject

Returns the value of attribute message.



4
5
6
# File 'lib/em-rtmp/response.rb', line 4

def message
  @message
end

#waiting_on_bytesObject

Returns the value of attribute waiting_on_bytes.



4
5
6
# File 'lib/em-rtmp/response.rb', line 4

def waiting_on_bytes
  @waiting_on_bytes
end

Class Method Details

.find_or_create(channel_id, connection) ⇒ Object

Find or create a channel by ID

channel_id - ID of channel to find or create connection - Connection to attach

Returns a Response instance



101
102
103
104
# File 'lib/em-rtmp/response.rb', line 101

def self.find_or_create(channel_id, connection)
  connection.channels[channel_id] ||= Response.new(channel_id, connection)
  connection.channels[channel_id]
end

Instance Method Details

#add_header(header) ⇒ Object

Inherit values from a given header

h - Header to add

Returns the instance header



30
31
32
# File 'lib/em-rtmp/response.rb', line 30

def add_header(header)
  self.header += header
end

#chunk_sizeObject

Determines the proper chunk size from the connection

Returns the chunk size as an Integer



37
38
39
# File 'lib/em-rtmp/response.rb', line 37

def chunk_size
  @connection.chunk_size
end

#complete?Boolean

Determine whether or not the stream is complete by checking the length of our body against that we expected from headers

Returns true or false

Returns:

  • (Boolean)


89
90
91
92
93
# File 'lib/em-rtmp/response.rb', line 89

def complete?
  complete = body.length >= header.body_length
  Logger.debug "response complete? #{complete} (#{body.length}/#{header.body_length})"
  complete
end

#read_next_chunkObject

Read the next data chunk from the stream

Returns the instance body



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/em-rtmp/response.rb', line 55

def read_next_chunk
  raise "No more data to read from stream" if header.body_length <= body.length

  Logger.debug "want #{read_size} (#{body.length}/#{header.body_length})"

  desired_size = read_size
  data = read(desired_size)
  data_length = data ? data.length : 0

  if data_length > 0
    self.body << data
  end

  if data_length != desired_size
    self.waiting_on_bytes = desired_size - data_length
  else
    self.waiting_on_bytes = 0
  end

  self.body
end

#read_sizeObject

Determines the proper amount of data to read this time around

Returns the chunk size as an Integer



44
45
46
47
48
49
50
# File 'lib/em-rtmp/response.rb', line 44

def read_size
  if waiting_on_bytes > 0
    waiting_on_bytes
  else
    [header.body_length - body.length, chunk_size].min
  end
end

#resetObject

Reset the body (leave the header) between successful responses

Returns nothing



21
22
23
# File 'lib/em-rtmp/response.rb', line 21

def reset
  self.body = ""
end

#waiting_in_chunk?Boolean

Determines whether or not we’re in the middle of a chunk waiting for more data, or it’s ok to go ahead and peek for a header.

Returns true or false

Returns:

  • (Boolean)


81
82
83
# File 'lib/em-rtmp/response.rb', line 81

def waiting_in_chunk?
  waiting_on_bytes > 0
end