Class: Steam::Networking::Packet
- Inherits:
-
Object
- Object
- Steam::Networking::Packet
- 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
-
#body ⇒ Object
readonly
The raw bytes of the TCP packet.
-
#msg_type ⇒ Object
(also: #emsg)
readonly
The EMsg type derived from the Packet body.
Instance Method Summary collapse
-
#as_message(msg) ⇒ Message
Converts the Packet into a Message object.
-
#encode ⇒ String
Encode a Packet to a byte string.
-
#initialize(body) ⇒ Packet
constructor
Instantiates a Packet object.
-
#multi? ⇒ Bool
Returns true if the Packet contains other packets.
-
#proto? ⇒ Bool
Returns true if the Packet is Protobuf backed.
Constructor Details
#initialize(body) ⇒ Packet
Instantiates a Packet object
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
#body ⇒ Object (readonly)
The raw bytes of the TCP packet
17 18 19 |
# File 'lib/steam/networking/packet.rb', line 17 def body @body end |
#msg_type ⇒ Object (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.
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/steam/networking/packet.rb', line 65 def (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 |
#encode ⇒ String
Encode a Packet to a byte string
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
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
57 58 59 |
# File 'lib/steam/networking/packet.rb', line 57 def proto? (@iden & Message::PROTO_MASK) == Message::PROTO_MASK end |