Module: SteamCondenser::Servers::Packets::SteamPacketFactory

Defined in:
lib/steam-condenser/servers/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) ⇒ BasePacket

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:

  • (BasePacket)

    The packet object generated from the packet data

Raises:


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
# File 'lib/steam-condenser/servers/packets/steam_packet_factory.rb', line 39

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

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

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

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:


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/steam-condenser/servers/packets/steam_packet_factory.rb', line 87

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 SteamCondenser::Error, '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 SteamCondenser::Error::PacketFormat, 'CRC32 checksum mismatch of uncompressed packet data.'
    end
  end

  packet_from_data packet_data[4..-1]
end