Module: SteamPacketFactory

Defined in:
lib/steam/packets/steam_packet_factory.rb

Overview

This module provides functionality to handle raw packet data, including data split into several UDP / TCP packets and BZIP2 compressed data. It’s the main utility to transform data bytes into packet objects.

See Also:

Author:

  • Sebastian Staudt

Class Method Summary collapse

Class Method Details

.packet_from_data(raw_data) ⇒ SteamPacket

Creates a new packet object based on the header byte of the given raw data

Parameters:

  • raw_data (String)

    The raw data of the packet

Returns:

  • (SteamPacket)

    The packet object generated from the packet data

Raises:



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
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/steam/packets/steam_packet_factory.rb', line 38

def self.packet_from_data(raw_data)
  header = raw_data[0].ord
  data = raw_data[1..-1]

  case header
    when SteamPacket::S2A_INFO_DETAILED_HEADER
      return S2A_INFO_DETAILED_Packet.new(data)
    when SteamPacket::A2S_INFO_HEADER
      return A2S_INFO_Packet.new
    when SteamPacket::S2A_INFO2_HEADER
      return S2A_INFO2_Packet.new(data)
    when SteamPacket::A2S_PLAYER_HEADER
      return A2S_PLAYER_Packet.new
    when SteamPacket::S2A_PLAYER_HEADER
      return S2A_PLAYER_Packet.new(data)
    when SteamPacket::A2S_RULES_HEADER
      return A2S_RULES_Packet
    when SteamPacket::S2A_RULES_HEADER
      return S2A_RULES_Packet.new(data)
    when SteamPacket::A2S_SERVERQUERY_GETCHALLENGE_HEADER
      return A2S_SERVERQUERY_GETCHALLENGE_Packet.new
    when SteamPacket::S2C_CHALLENGE_HEADER
      return S2C_CHALLENGE_Packet.new(data)
    when SteamPacket::A2M_GET_SERVERS_BATCH2_HEADER
      return A2M_GET_SERVERS_BATCH2_Packet.new(data)
    when SteamPacket::M2A_SERVER_BATCH_HEADER
      return M2A_SERVER_BATCH_Packet.new(data)
    when SteamPacket::M2C_ISVALIDMD5_HEADER
      return M2C_ISVALIDMD5_Packet.new(data)
    when SteamPacket::M2S_REQUESTRESTART_HEADER
      return M2S_REQUESTRESTART_Packet.new(data)
    when SteamPacket::RCON_GOLDSRC_CHALLENGE_HEADER,
         SteamPacket::RCON_GOLDSRC_NO_CHALLENGE_HEADER,
         SteamPacket::RCON_GOLDSRC_RESPONSE_HEADER
      return RCONGoldSrcResponse.new(data)
    when SteamPacket::S2A_LOGSTRING_HEADER
      return S2A_LOGSTRING_Packet.new(data)
    else
      raise PacketFormatError, "Unknown packet with header 0x#{header.to_s(16)} received."
  end
end

.reassemble_packet(split_packets, is_compressed = false, packet_checksum = 0) ⇒ SteamPacket

Reassembles the data of a split and/or compressed packet into a single packet object

Parameters:

  • split_packets (Array<String>)

    An array of packet data

  • is_compressed (Boolean) (defaults to: false)

    whether the data of this packet is compressed

  • packet_checksum (Fixnum) (defaults to: 0)

    The CRC32 checksum of the decompressed packet data

Returns:

Raises:

See Also:



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/steam/packets/steam_packet_factory.rb', line 93

def self.reassemble_packet(split_packets, is_compressed = false, packet_checksum = 0)
  packet_data = split_packets.join ''

  if is_compressed
    begin
      require 'bzip2-ruby'
    rescue LoadError
      raise SteamCondenserError, 'The "bzip2-ruby" gem is not installed. Please install it, if you want to query Source servers sending compressed packets.'
    end

    packet_data = Bzip2.decompress packet_data

    unless Zlib.crc32(packet_data) == packet_checksum
      raise PacketFormatError, 'CRC32 checksum mismatch of uncompressed packet data.'
    end
  end

  packet_from_data packet_data[4..-1]
end