Class: MasterServer

Inherits:
Object
  • Object
show all
Includes:
Server
Defined in:
lib/steam/servers/master_server.rb

Overview

This class represents a Steam master server and can be used to get game servers which are publicly available

An intance of this class can be used much like Steam’s server browser to get a list of available game servers, including filters to narrow down the search results.

Author:

  • Sebastian Staudt

Constant Summary collapse

GOLDSRC_MASTER_SERVER =

The master server address to query for GoldSrc game servers

'hl1master.steampowered.com', 27010
SOURCE_MASTER_SERVER =

The master server address to query for GoldSrc game servers

'hl2master.steampowered.com', 27011
REGION_US_EAST_COAST =

The region code for the US east coast

0x00
REGION_US_WEST_COAST =

The region code for the US west coast

0x01
REGION_SOUTH_AMERICA =

The region code for South America

0x02
REGION_EUROPE =

The region code for Europe

0x03
REGION_ASIA =

The region code for Asia

0x04
REGION_AUSTRALIA =

The region code for Australia

0x05
REGION_MIDDLE_EAST =

The region code for the Middle East

0x06
REGION_AFRICA =

The region code for Africa

0x07
REGION_ALL =

The region code for the whole world

0xFF

Instance Attribute Summary

Attributes included from Server

#host_names, #ip_addresses

Instance Method Summary collapse

Methods included from Server

#initialize, #rotate_ip

Instance Method Details

#challengeObject

Note:

Please note that this is not needed for finding servers using #servers.

Request a challenge number from the master server.

This is used for further communication with the master server.

Returns:

  • The challenge number from the master server

See Also:



65
66
67
68
69
70
# File 'lib/steam/servers/master_server.rb', line 65

def challenge
  failsafe do
    @socket.send C2M_CHECKMD5_Packet.new
    @socket.reply.challenge
  end
end

#init_socketObject

Initializes the socket to communicate with the master server

See Also:



75
76
77
# File 'lib/steam/servers/master_server.rb', line 75

def init_socket
  @socket = MasterServerSocket.new @ip_address, @port
end

#send_heartbeat(data) ⇒ Array<SteamPacket>

Sends a constructed heartbeat to the master server

This can be used to check server versions externally.

Parameters:

  • The (Hash<Symbol, Object>)

    data to send with the heartbeat request

Returns:

  • (Array<SteamPacket>)

    Zero or more reply packets from the server. Zero means either the heartbeat was accepted by the master or there was a timeout. So usually it’s best to repeat a heartbeat a few times when not receiving any packets.

Raises:

See Also:



146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/steam/servers/master_server.rb', line 146

def send_heartbeat(data)
  failsafe do
    @socket.send S2M_HEARTBEAT2_Packet.new(data)

    reply_packets = []
    begin
      loop { reply_packets << @socket.reply }
    rescue TimeoutException
    end
  end

  reply_packets
end

#servers(region_code = MasterServer::REGION_ALL, filters = '') ⇒ Array<Array<String>>

Note:

Receiving all servers from the master server is taking quite some time.

Returns a list of game server matching the given region and filters

Filtering: Instead of filtering the results sent by the master server locally, you should at least use the following filters to narrow down the results sent by the master server.

Available filters:

  • ‘typed`: Request only dedicated servers

  • ‘secure1`: Request only secure servers

  • gamedir`: Request only servers of a specific mod

  • map`: Request only servers running a specific map

  • ‘linux1`: Request only linux servers

  • ‘emtpy1`: Request only non-empty servers

  • ‘full`: Request only servers not full

  • ‘proxy1`: Request only spectator proxy servers

Parameters:

  • region_code (Fixnum) (defaults to: MasterServer::REGION_ALL)

    The region code to specify a location of the game servers

  • filters (String) (defaults to: '')

    The filters that game servers should match

Returns:

  • (Array<Array<String>>)

    A list of game servers matching the given region and filters

See Also:



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/steam/servers/master_server.rb', line 105

def servers(region_code = MasterServer::REGION_ALL, filters = '')
  fail_count     = 0
  finished       = false
  current_server = '0.0.0.0:0'
  server_array   = []

  failsafe do
    begin
      @socket.send A2M_GET_SERVERS_BATCH2_Packet.new(region_code, current_server, filters)
      begin
        servers = @socket.reply.servers
        servers.each do |server|
          if server == '0.0.0.0:0'
            finished = true
          else
            current_server = server
            server_array << server.split(':')
          end
        end
        fail_count = 0
      rescue TimeoutException
        raise $! if (fail_count += 1) == 3
      end
    end while !finished
  end

  server_array
end