Class: FlashConnection

Inherits:
EventMachine::Connection
  • Object
show all
Defined in:
lib/protolink/flash_connection.rb

Direct Known Subclasses

ProtoSocket

Instance Method Summary collapse

Constructor Details

#initializeFlashConnection

Returns a new instance of FlashConnection.



4
5
6
7
8
# File 'lib/protolink/flash_connection.rb', line 4

def initialize
  @buffer = ''
  
  set_comm_inactivity_timeout 60
end

Instance Method Details

#log(text) ⇒ Object

TODO: redundant code



40
41
42
# File 'lib/protolink/flash_connection.rb', line 40

def log text
  puts "#{self}: #{text}" if Rails.env != "production" || $DEBUG
end

#receive_data(data) ⇒ Object

Raw bytes



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/protolink/flash_connection.rb', line 26

def receive_data data
  @buffer += data
  
  while @buffer.include? "\0"
    packet = @buffer[0, @buffer.index("\0")]
    @buffer = @buffer[(@buffer.index("\0")+1)..-1]
    
    receive_line packet
  end
rescue => ex
  p ex, ex.backtrace
end

#receive_line(line) ⇒ Object



19
20
21
22
23
# File 'lib/protolink/flash_connection.rb', line 19

def receive_line line
  receive_json JSON.parse(line)
rescue JSON::ParserError
  log "JSON parsing error"
end

#send_json(json) ⇒ Object

JSON packets



10
11
12
# File 'lib/protolink/flash_connection.rb', line 10

def send_json json
  send_data json.to_json + "\0"
end

#send_line(line) ⇒ Object

Null-terminated lines



15
16
17
# File 'lib/protolink/flash_connection.rb', line 15

def send_line line
  send_data line + "\0"
end

#to_sObject



44
45
46
# File 'lib/protolink/flash_connection.rb', line 44

def to_s
  "connection #{inspect}"
end