Module: EventMachine::Protocols::BerpProtocol

Defined in:
lib/em-berp-protocol.rb,
lib/em-berp-protocol/version.rb

Constant Summary collapse

VERSION =
"1.0.1"

Instance Method Summary collapse

Instance Method Details

#receive_data(data) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/em-berp-protocol.rb', line 7

def receive_data(data)
  (@buf ||= '') << data

  while @buf.size >= 4
    if @buf.size >= 4+(size = @buf.unpack('N').first)
      @buf.slice!(0,4)
      begin
        slice = @buf.slice!(0,size)
        receive_object(BERT.decode(slice))
      rescue TypeError => e
        handle_bert_error("TypeError: #{e}")
      rescue EOFError => e
        handle_bert_error("EOFError: #{e} with data #{slice.inspect}")
      end
    else
      break
    end
  end
end

#receive_object(obj) ⇒ Object

Invoked with ruby objects received over the network



28
29
30
# File 'lib/em-berp-protocol.rb', line 28

def receive_object(obj)
  # stub
end

#send_object(obj) ⇒ Object

Sends a ruby object over the network



33
34
35
36
# File 'lib/em-berp-protocol.rb', line 33

def send_object(obj)
  data = BERT.encode(obj)
  send_data([data.respond_to?(:bytesize) ? data.bytesize : data.size, data].pack('Na*'))
end