Class: Steam::Networking::Packet

Inherits:
Object
  • Object
show all
Defined in:
lib/steam/networking/packet.rb

Overview

An object representing a packet from the connection.

The packets come in the form

LENGTH MAGIC BODY

To create a packet only the body needs to be supplied. The body should never contain the MAGIC or LENGTH

Constant Summary collapse

TCP_MAGIC =

Valve's TCP Packet identifier

'VT01'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body) ⇒ Packet

Instantiates a Packet object

Parameters:



26
27
28
29
30
31
32
33
34
# File 'lib/steam/networking/packet.rb', line 26

def initialize(body)
  raise 'packet must have raw tcp body' if body.nil? || body.empty?

  @body     = body
  @io       = ByteReader.new(StringIO.new(body))
  @iden     = ByteReader.new(StringIO.new(body)).unsigned_int32
  @msg_type = @iden & ~Message::PROTO_MASK
  self
end

Instance Attribute Details

#bodyObject (readonly)

The raw bytes of the TCP packet



17
18
19
# File 'lib/steam/networking/packet.rb', line 17

def body
  @body
end

#msg_typeObject (readonly) Also known as: emsg

The EMsg type derived from the Packet body



20
21
22
# File 'lib/steam/networking/packet.rb', line 20

def msg_type
  @msg_type
end

Instance Method Details

#as_message(msg) ⇒ Message

Converts the Packet into a Message object.

Parameters:

  • msg (Message)

    The message to decode from the packet

Returns:

  • (Message)

    the resulting message



65
66
67
68
69
70
71
72
73
74
# File 'lib/steam/networking/packet.rb', line 65

def as_message(msg)
  cm = if proto?
         ProtobufMessage.new(MsgHdrProtoBuf.new, msg, @msg_type)
       else
         ClientMessage.new(MsgHdr.new, msg, @msg_type)
       end

  cm.decode(@io)
  cm
end

#encodeString

Encode a Packet to a byte string

Returns:

  • (String)

    byte string representation of the Packet



39
40
41
42
43
44
45
# File 'lib/steam/networking/packet.rb', line 39

def encode
  stream = ByteWriter.new
  stream.write_unsigned_int32(body.length)
  stream.write(TCP_MAGIC)
  stream.write(body)
  stream.string
end

#multi?Bool

Returns true if the Packet contains other packets

Returns:

  • (Bool)


50
51
52
# File 'lib/steam/networking/packet.rb', line 50

def multi?
  @msg_type == EMsg::MULTI
end

#proto?Bool

Returns true if the Packet is Protobuf backed

Returns:

  • (Bool)


57
58
59
# File 'lib/steam/networking/packet.rb', line 57

def proto?
  (@iden & Message::PROTO_MASK) == Message::PROTO_MASK
end