Sha256: cdd16ea4b99949268bb9a977cf83ca317b580110032d82db13f9d35bb4e38996

Contents?: true

Size: 984 Bytes

Versions: 4

Compression:

Stored size: 984 Bytes

Contents

module EventMachine
  module Protocols
    # 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
    #
    module ObjectProtocol
      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

      # Invoked with ruby objects received over the network
      def receive_object obj
        # stub
      end

      # Sends a ruby object over the network
      def send_object obj
        data = Marshal.dump(obj)
        send_data [data.respond_to?(:bytesize) ? data.bytesize : data.size, data].pack('Na*')
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
eventmachine-eventmachine-0.12.8 lib/em/protocols/object_protocol.rb
eventmachine-eventmachine-0.12.9 lib/em/protocols/object_protocol.rb
eventmachine-0.12.8-java lib/em/protocols/object_protocol.rb
eventmachine-0.12.8 lib/em/protocols/object_protocol.rb