Module: Rexpro::Message

Defined in:
lib/rexpro/message.rb

Defined Under Namespace

Modules: Base Classes: Error, ScriptRequest, ScriptResponse, SessionRequest, SessionResponse

Constant Summary collapse

PROTOCOL_VERSION =
1
SERIALIZER_MSGPACK =
0
SERIALIZER_JSON =
1
ZERO_UUID =
[0, 0, 0, 0].pack('NNNN')
TYPE_ERROR =
0
TYPE_SESSION_REQUEST =
1
TYPE_SESSION_RESPONSE =
2
TYPE_SCRIPT_REQUEST =
3
TYPE_SCRIPT_RESPONSE =
5

Class Method Summary collapse

Class Method Details

.generate_uuidObject



22
23
24
25
26
27
# File 'lib/rexpro/message.rb', line 22

def generate_uuid
  @uuid ||= UUID.new
  hex = @uuid.generate(:compact)
  ints = hex.each_char.each_slice(8).map { |h| Integer(h.join, 16) }
  ints.pack('NNNN')
end

.read_from(io) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rexpro/message.rb', line 33

def read_from(io)
  version = io.readbyte
  if version != PROTOCOL_VERSION
    raise RexproException, "Unknown protocol version #{version}"
  end

  header = io.read(10)
  if header.nil? || header.size < 10
    raise RexproException, "Unexpected EOF: #{header.inspect}"
  end

  serializer_type, reserved, type, size = header.unpack('CNCN')
  type_class = types[type]
  unless type_class
    raise RexproException, "Unknown message type #{type}"
  end
  fields = type_class.fields

  unpacker = MessagePack::Unpacker.new(io)
  array_size = unpacker.read_array_header
  if array_size != fields.length
    raise RexproException,
          "Expected #{fields.length} fields, got #{array_size}"
  end

  attrs = fields.inject({}) do |memo, field|
    memo[field] = unpacker.read
    memo
  end

  attrs[:serializer_type] = serializer_type

  type_class.new(attrs)
end

.typesObject



29
30
31
# File 'lib/rexpro/message.rb', line 29

def types
  @types ||= {}
end