Sha256: 68cc0b4ea2678a37f07297950b5f3c63be029ae16802ac314182762a20524d3d
Contents?: true
Size: 1.3 KB
Versions: 9
Compression:
Stored size: 1.3 KB
Contents
require 'json' module Listen module TCP class Message attr_reader :body, :object, :payload, :size HEADER_SIZE = 4 HEADER_FORMAT = 'N' PAYLOAD_FORMAT = "#{HEADER_FORMAT}a*" # Initializes a new message # # @param [Object] object to initialize message with # def initialize(object = nil) self.object = object if object end # Generates message size and payload for given object def object=(obj) @object = obj @body = JSON.generate(@object) @size = @body.bytesize @payload = [@size, @body].pack(PAYLOAD_FORMAT) end # Extracts message size and loads object from given payload def payload=(payload) @payload = payload @size, @body = @payload.unpack(PAYLOAD_FORMAT) @object = JSON.parse(@body) end # Extracts a message from given buffer def self.from_buffer(buffer) if buffer.bytesize > HEADER_SIZE size = buffer.unpack(HEADER_FORMAT).first payload_size = HEADER_SIZE + size if buffer.bytesize >= payload_size payload = buffer.slice!(0...payload_size) new.tap do |message| message.payload = payload end end end end end end end
Version data entries
9 entries across 9 versions & 1 rubygems