Module: EventMachine::Protocols::ObjectProtocol
Relationships & Source Files | |
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(said' => obj
)
end
end
Instance Method Summary
- #receive_data(data) Internal use only Internal use only
-
#receive_object(obj)
Invoked with ruby objects received over the network.
-
#send_object(obj)
Sends a ruby object over the network.
-
#serializer
By default returns Marshal, override to return JSON or YAML, or any other serializer/deserializer responding to #dump and #load.
Instance Method Details
#receive_data(data)
This method is for internal use only.
[ GitHub ]
# File 'lib/em/protocols/object_protocol.rb', line 23
def receive_data data (@buf ||= ''.dup) << data while @buf.size >= 4 if @buf.size >= 4+(size=@buf.unpack('N').first) @buf.slice!(0,4) receive_object serializer.load(@buf.slice!(0,size)) else break end end end
#receive_object(obj)
Invoked with ruby objects received over the network
# File 'lib/em/protocols/object_protocol.rb', line 37
def receive_object obj # stub end
#send_object(obj)
Sends a ruby object over the network
# File 'lib/em/protocols/object_protocol.rb', line 42
def send_object obj data = serializer.dump(obj) send_data [data.respond_to?(:bytesize) ? data.bytesize : data.size, data].pack('Na*') end
#serializer
By default returns Marshal, override to return JSON or YAML, or any other serializer/deserializer responding to #dump and #load.
# File 'lib/em/protocols/object_protocol.rb', line 18
def serializer Marshal end