Module: Revent::ASRCon

Defined in:
lib/revent/as_r.rb

Constant Summary collapse

POLICY_REQUEST =

Sent by flash player

"<policy-file-request/>\0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#sessionObject

The place to store something associated with this connection



18
19
20
# File 'lib/revent/as_r.rb', line 18

def session
  @session
end

Instance Method Details

#compress(value) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/revent/as_r.rb', line 98

def compress(value)
  @enc.reset
  @enc.write_amf3(value)
  s = @enc.stream
  @enc.reset

  Zlib::Deflate.deflate(s)
end

#on_message(message) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/revent/as_r.rb', line 81

def on_message(message)
  @user.on_message(self, message)
rescue
  @user.logger.error($!)
  @user.logger.error($!.backtrace.join("\n"))
  close_connection
end

#post_initObject



24
25
26
27
28
# File 'lib/revent/as_r.rb', line 24

def post_init
  @data = ''
  @dec = RubyAMF::IO::AMFDeserializer.new
  @enc = RubyAMF::IO::AMFSerializer.new
end

#receive_data(data) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/revent/as_r.rb', line 30

def receive_data(data)
  @data << data
  if @data == POLICY_REQUEST
    send_data(@user.policy + "\0")
    close_connection_after_writing
    return
  end

  if @data.size > @user.max_data_size
    close_connection
    return
  end

  while true
    @dec.reset
    @dec.stream = @data
    message = @dec.read_amf3
    break if message.nil?

    on_message(message)

    last_size = @data.size
    @data.slice!(0, @dec.stream_position)
    break if last_size == @dec.stream_position
  end
rescue
  @user.logger.error($!)
  @user.logger.error($!.backtrace.join("\n"))
  close_connection
end

#remote_ipObject



89
90
91
92
93
94
95
96
# File 'lib/revent/as_r.rb', line 89

def remote_ip
  peername = get_peername
  return @last_ip if peername.nil?

  a = get_peername[2, 6].unpack("nC4")
  a.delete_at(0)
  @last_ip = a.join('.')
end

#send_message(message, close_connection_after_writing = false) ⇒ Object




68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/revent/as_r.rb', line 68

def send_message(message, close_connection_after_writing = false)
  # Send AMF3 data
  @enc.reset
  @enc.write_amf3(message)
  send_data(@enc.stream)
  @enc.reset

  self.close_connection_after_writing if close_connection_after_writing
rescue
  @user.logger.error($!)
  @user.logger.error($!.backtrace.join("\n"))
end

#unbindObject



61
62
63
64
# File 'lib/revent/as_r.rb', line 61

def unbind
  @user.cons.synchronize { @user.cons.delete(self) }
  @user.on_unbind(self)
end

#user=(server) ⇒ Object



20
21
22
# File 'lib/revent/as_r.rb', line 20

def user=(server)
  @user = server
end