<commit_before>require "em-rtmp/request"

module EventMachine
  module RTMP
    class ConnectRequest < Request

      DEFAULT_PARAMETERS = {
        app: '',
        flashVer: 'WIN 10,1,85,3',
        swfUrl: '',
        tcUrl: '',
        fpad: false,
        capabilities: 239,
        audioCodecs: 3191,
        videoCodecs: 252,
        videoFunction: 1,
        pageUrl: nil,
        objectEncoding: 3
      }

      attr_accessor :app, :flashVer, :swfUrl, :tcUrl, :fpad, :capabilities,
                    :audioCodecs, :videoCodecs, :videoFunction, :pageUrl, :objectEncoding

      # Initialize the request and set sane defaults for an RTMP connect
      # request (transaction_id, command, header values)
      #
      # Returns nothing
      def initialize(connection)
        super connection

        self.header.channel_id = 3
        self.header.message_type = :amf0
        self.header

        self.message = Message.new version: 0
        self.message.command = "connect"
        self.message.transaction_id = 1

        callback do |res|
          Logger.debug "rtmp connect failed, becoming ready"
          @connection.change_state :ready
        end

        errback do |res|
          Logger.debug "rtmp connect failed, closing connection"
          @connection.close_connection
        end
      end

      # Construct a list of parameters given our class attributes, used
      # to form the connect message object.
      #
      # Returns a Hash of symbols => values
      def parameters
        instance_values = Hash[instance_variables.map {|k| [k.to_s[1..-1].to_sym, instance_variable_get(k)]}]
        DEFAULT_PARAMETERS.merge instance_values.select {|k,v| DEFAULT_PARAMETERS.key? k }
      end

      # Given the specific nature of a connect request, we can just set the message
      # values to our params then encode that as our body before sending.
      #
      # Returns the result of super
      def send
        self.message.values = [parameters]
        self.body = message.encode
        super
      end

    end
  end
end<commit_msg>Include a command message on connect to set up endpoints<commit_after>require "em-rtmp/request"

module EventMachine
  module RTMP
    class ConnectRequest < Request

      DEFAULT_PARAMETERS = {
        app: '',
        flashVer: 'WIN 10,1,85,3',
        swfUrl: '',
        tcUrl: '',
        fpad: false,
        capabilities: 239,
        audioCodecs: 3191,
        videoCodecs: 252,
        videoFunction: 1,
        pageUrl: nil,
        objectEncoding: 3
      }

      attr_accessor :app, :flashVer, :swfUrl, :tcUrl, :fpad, :capabilities,
                    :audioCodecs, :videoCodecs, :videoFunction, :pageUrl, :objectEncoding

      # Initialize the request and set sane defaults for an RTMP connect
      # request (transaction_id, command, header values)
      #
      # Returns nothing
      def initialize(connection)
        super connection

        self.header.channel_id = 3
        self.header.message_type = :amf0
        self.header

        self.message.version = 0
        self.message.transaction_id = 1
        self.message.command = "connect"

        callback do |res|
          Logger.debug "rtmp connect failed, becoming ready"
          @connection.change_state :ready
        end

        errback do |res|
          Logger.debug "rtmp connect failed, closing connection"
          @connection.close_connection
        end
      end

      # Construct a list of parameters given our class attributes, used
      # to form the connect message object.
      #
      # Returns a Hash of symbols => values
      def parameters
        instance_values = Hash[instance_variables.map {|k| [k.to_s[1..-1].to_sym, instance_variable_get(k)]}]
        DEFAULT_PARAMETERS.merge instance_values.select {|k,v| DEFAULT_PARAMETERS.key? k }
      end

      def command_message
        cm = RocketAMF::Values::CommandMessage.new
        cm.operation = 5
        cm.correlationId = ""
        cm.timestamp = 0
        cm.timeToLive = 0
        cm.messageId = UUID.random
        cm.body = {}
        cm.destination = ""
        cm.headers = { "DSMessagingVersion" => 1, "DSId" => "my-rtmps" }
        cm
      end

      # Given the specific nature of a connect request, we can just set the message
      # values to our params then encode that as our body before sending.
      #
      # Returns the result of super
      def send
        self.message.values = [parameters, false, "nil", "", command_message]
        self.body = message.encode
        super
      end

    end
  end
end
{'fraction_non_alphanumeric': 0.08178438661710037, 'fraction_numerical': 0.01137109118740433, 'mean_word_length': 2.0905405405405406, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4798684', 'n_tokens_mistral': 1315, 'n_tokens_neox': 1256, 'n_words': 387}