Module: EventMachine::Protocols::ObjectProtocol

Defined in:
lib/em/protocols/object_protocol.rb

Overview

ObjectProtocol allows for easy communication using marshaled ruby objects

module RubyServer
  include EM::P::ObjectProtocol

  def receive_object obj
    send_object({'you said' => obj})
  end
end

Instance Method Summary collapse

Instance Method Details

#receive_data(data) ⇒ Object

:nodoc:



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/em/protocols/object_protocol.rb', line 14

def receive_data data # :nodoc:
  (@buf ||= '') << data

  while @buf.size >= 4
    if @buf.size >= 4+(size=@buf.unpack('N').first)
      @buf.slice!(0,4)
      receive_object Marshal.load(@buf.slice!(0,size))
    else
      break
    end
  end
end

#receive_object(obj) ⇒ Object

Invoked with ruby objects received over the network



28
29
30
# File 'lib/em/protocols/object_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/protocols/object_protocol.rb', line 33

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