Class: TorigoyaKit::Protocol::Packet

Inherits:
Object
  • Object
show all
Defined in:
lib/torigoya_kit/protocol.rb

Overview

MessageKind byte // Version [4]byte // uint32, little endian Length [4]byte // uint32, little endian Message []byte // data, msgpacked

Constant Summary collapse

Signature =
"TG"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind, version, data) ⇒ Packet

Returns a new instance of Packet.



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

def initialize(kind, version, data)
  if kind < MessageKindIndexBegin || kind > MessageKindIndexEnd
    raise "invalid header"
  end

  @kind = [kind].pack("C*")               # byte
  @version = [version].pack("V*")         # little endian, 32bit, unsigned int
  @message = data.to_msgpack              # msgpacked
  @length = [@message.size].pack("V*")    # little endian, 32bit, unsigned int
end

Instance Attribute Details

#kindObject (readonly)

Returns the value of attribute kind.



51
52
53
# File 'lib/torigoya_kit/protocol.rb', line 51

def kind
  @kind
end

#lengthObject (readonly)

Returns the value of attribute length.



51
52
53
# File 'lib/torigoya_kit/protocol.rb', line 51

def length
  @length
end

#messageObject (readonly)

Returns the value of attribute message.



51
52
53
# File 'lib/torigoya_kit/protocol.rb', line 51

def message
  @message
end

#versionObject (readonly)

Returns the value of attribute version.



51
52
53
# File 'lib/torigoya_kit/protocol.rb', line 51

def version
  @version
end

Instance Method Details

#to_binaryObject



54
55
56
# File 'lib/torigoya_kit/protocol.rb', line 54

def to_binary
  return Signature + @kind + @version + @length + @message
end