Module: EventMachine::Protocols::FrameObjectProtocol

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

Overview

FrameObjectProtocol is a reimplementation of ObjectProtocol suitable for UDP

module RubyServer
  include EM::P::FrameObjectProtocol

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

Instance Method Summary collapse

Instance Method Details

#handle_exception(datagram, e) ⇒ Object

Invoked whenever FrameObjectProtocol fails to deserialize an object



73
74
75
# File 'lib/em/protocols/frame_object_protocol.rb', line 73

def handle_exception datagram, e
  # stub
end

#receive_data(data) ⇒ Object



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/em/protocols/frame_object_protocol.rb', line 36

def receive_data data
  p data

  begin
    if data.size < 4
      raise "Received invalid datagram, datagram way too small"
    end

    size=data.unpack('N').first

    if data.size != 4+size
      actual_size = data.size-4
      raise "Received invalid datagram: expected size=#{size}, actual size=#{actual_size}"
    end

    obj = serializer.load data[4..-1]
  rescue => e
    handle_exception(data, e)
    return
  end

  p obj
  receive_object obj
end

#receive_object(obj) ⇒ Object

Invoked with ruby objects received over the network



62
63
64
# File 'lib/em/protocols/frame_object_protocol.rb', line 62

def receive_object obj
  # stub
end

#send_object(obj) ⇒ Object

Sends a ruby object over the network



67
68
69
70
# File 'lib/em/protocols/frame_object_protocol.rb', line 67

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

#serializerObject

By default returns Marshal, override to return JSON or YAML, or any other serializer/deserializer responding to #dump and #load.



31
32
33
# File 'lib/em/protocols/frame_object_protocol.rb', line 31

def serializer
  Marshal
end