Class: Bones::RPC::Protocol::ExtMessage

Inherits:
Object
  • Object
show all
Includes:
BinaryHelper
Defined in:
lib/bones/rpc/protocol/ext_message.rb

Direct Known Subclasses

Acknowledge, Synchronize

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BinaryHelper

included, #inspect, #receive_replies, #serialize

Class Method Details

.deserialize(buffer, adapter = nil) ⇒ Object


74
75
76
77
78
79
80
81
# File 'lib/bones/rpc/protocol/ext_message.rb', line 74

def self.deserialize(buffer, adapter = nil)
  message = allocate
  message.deserialize_ext_code(buffer)
  message.deserialize_ext_length(buffer)
  message.deserialize_ext_type(buffer)
  message.deserialize_ext_head(buffer)
  message
end

Instance Method Details

#dataObject


64
65
66
67
68
# File 'lib/bones/rpc/protocol/ext_message.rb', line 64

def data
  (self.class.fields - ExtMessage.fields).inject("".force_encoding('BINARY')) do |buffer, field|
    send("serialize_#{field}", buffer)
  end
end

#datasizeObject


70
71
72
# File 'lib/bones/rpc/protocol/ext_message.rb', line 70

def datasize
  data.bytesize
end

#deserialize_ext_length(buffer) ⇒ Object


41
42
43
44
45
46
47
48
49
50
# File 'lib/bones/rpc/protocol/ext_message.rb', line 41

def deserialize_ext_length(buffer)
  self.ext_length, = case ext_code
  when 0xC7
    buffer.read(1).unpack('C')
  when 0xC8
    buffer.read(2).unpack('n')
  when 0xC9
    buffer.read(4).unpack('N')
  end
end

#ext_codeObject


18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bones/rpc/protocol/ext_message.rb', line 18

def ext_code
  @ext_code ||= begin
    len = datasize
    if len <= 0xFF
      0xC7
    elsif len <= 0xFFFF
      0xC8
    elsif len <= 0xFFFFFFFF
      0xC9
    else
      raise ArgumentError, "datasize too large: #{len} (max #{0xFFFFFFFF} bytes)"
    end
  end
end

#ext_lengthObject


33
34
35
# File 'lib/bones/rpc/protocol/ext_message.rb', line 33

def ext_length
  @ext_length ||= datasize + 1
end

#ext_typeObject


37
38
39
# File 'lib/bones/rpc/protocol/ext_message.rb', line 37

def ext_type
  @ext_type ||= 0x0D
end

#serialize_ext_length(buffer) ⇒ Object


52
53
54
55
56
57
58
59
60
61
62
# File 'lib/bones/rpc/protocol/ext_message.rb', line 52

def serialize_ext_length(buffer)
  packer = case ext_code
  when 0xC7
    'C'
  when 0xC8
    'n'
  when 0xC9
    'N'
  end
  buffer << [ext_length].pack(packer)
end