Class: EventMachine::RTMP::Connection

Inherits:
Connection
  • Object
show all
Includes:
IOHelpers
Defined in:
lib/em-rtmp/connection.rb

Direct Known Subclasses

SecureConnection

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#initializeConnection

Initialize the connection and setup our handshake

Returns nothing



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/em-rtmp/connection.rb', line 13

def initialize
  super
  @buffer = Buffer.new
  @chunk_size = 128
  @response_router = ResponseRouter.new(self)
  @handshake = Handshake.new(self)
  @callbacks = { :handshake_complete => [], :ready => [], :disconnected => [] }
  @channels = []
  @pending_requests = {}
  @state = :connecting
end

Instance Attribute Details

#channelsObject

Returns the value of attribute channels.



8
9
10
# File 'lib/em-rtmp/connection.rb', line 8

def channels
  @channels
end

#chunk_sizeObject

Returns the value of attribute chunk_size.



8
9
10
# File 'lib/em-rtmp/connection.rb', line 8

def chunk_size
  @chunk_size
end

#pending_requestsObject

Returns the value of attribute pending_requests.



8
9
10
# File 'lib/em-rtmp/connection.rb', line 8

def pending_requests
  @pending_requests
end

#stateObject

Returns the value of attribute state.



8
9
10
# File 'lib/em-rtmp/connection.rb', line 8

def state
  @state
end

Instance Method Details

#begin_rtmp_handshakeObject

Start the RTMP handshake process

Returns nothing



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

def begin_rtmp_handshake
  change_state :handshake
  @handshake.issue_challenge
end

#buffer_changedObject

Called when the buffer is changed, indicating that we may want to change state or delegate to another object

Returns nothing



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/em-rtmp/connection.rb', line 146

def buffer_changed

  loop do
    break if bytes_waiting < 1

    case state
    when :handshake
      if @handshake.buffer_changed == :handshake_complete
        @handshake = nil
        change_state :handshake_complete
      end
      break
    when :handshake_complete, :ready
      @response_router.buffer_changed
      next
    end
  end

end

#bytes_waitingObject

Obtain the number of bytes waiting to be read in the buffer

Returns an Integer



107
108
109
# File 'lib/em-rtmp/connection.rb', line 107

def bytes_waiting
  @buffer.remaining
end

#change_state(state) ⇒ Object

Used to track changes in state

state - Symbol, new state to enter

Returns nothing



30
31
32
33
34
35
# File 'lib/em-rtmp/connection.rb', line 30

def change_state(state)
  return if @state == state
  Logger.info "state changed from #{@state} to #{state}", caller: caller
  @state = state
  run_callbacks state
end

#connection_completedObject

Perform the next step after the connection has been established Called by the Event Machine

Returns nothing



115
116
117
118
# File 'lib/em-rtmp/connection.rb', line 115

def connection_completed
  Logger.info "connection completed, issuing rtmp handshake"
  begin_rtmp_handshake
end

#on_disconnected(&blk) ⇒ Object

Called to add a callback for when the TCP connection has been disconnected.

blk - block to execute

Returns nothing



71
72
73
# File 'lib/em-rtmp/connection.rb', line 71

def on_disconnected(&blk)
  @callbacks[:disconnected] << blk
end

#on_handshake_complete(&blk) ⇒ Object

Called to add a callback for when the RTMP handshake is completed. Most useful for issuing an RTMP connect request.

blk - block to execute

Returns nothing



51
52
53
# File 'lib/em-rtmp/connection.rb', line 51

def on_handshake_complete(&blk)
  @callbacks[:handshake_complete] << blk
end

#on_ready(&blk) ⇒ Object

Called to add a callback for when the RTMP connection has been established and is ready for work.

blk - block to execute

Returns nothing



61
62
63
# File 'lib/em-rtmp/connection.rb', line 61

def on_ready(&blk)
  @callbacks[:ready] << blk
end

#read(length) ⇒ Object

Reads from the buffer, to facilitate IO operations

Returns the result of the read



91
92
93
94
# File 'lib/em-rtmp/connection.rb', line 91

def read(length)
  Logger.debug "reading #{length} bytes from buffer"
  @buffer.read length
end

#receive_data(data) ⇒ Object

Receives data and offers it to the appropriate delegate object. Fires a method call to buffer_changed to take action. Called by the Event machine

data - data received

Returns nothing



136
137
138
139
140
# File 'lib/em-rtmp/connection.rb', line 136

def receive_data(data)
  Logger.debug "received #{data.length} bytes"
  @buffer.append data
  buffer_changed
end

#run_callbacks(event) ⇒ Object

Called to run the callbacks for a specific event

event - symbol representing the event to run callbacks for

Returns nothing



80
81
82
83
84
85
86
# File 'lib/em-rtmp/connection.rb', line 80

def run_callbacks(event)
  if @callbacks.keys.include? event
    @callbacks[event].each do |blk|
      blk.call
    end
  end
end

#unbindObject

Change our state to disconnected if we lose the connection. Called by the Event Machine

Returns nothing



124
125
126
127
# File 'lib/em-rtmp/connection.rb', line 124

def unbind
  Logger.info "disconnected from peer"
  change_state :disconnected
end

#write(data) ⇒ Object

Writes data to the EventMachine connection

Returns nothing



99
100
101
102
# File 'lib/em-rtmp/connection.rb', line 99

def write(data)
  Logger.debug "sending #{data.length} bytes to stream"
  send_data data
end