Module: Server

Included in:
GameServer, MasterServer
Defined in:
lib/steam/servers/server.rb

Overview

This module is included by all classes implementing server functionality

It provides basic name resolution features.

Instance Method Summary collapse

Instance Method Details

#initialize(address, port = nil) ⇒ Object

Creates a new server instance with the given address and port



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/steam/servers/server.rb', line 14

def initialize(address, port = nil)
  address = address.to_s
  address, port = address.split(':', 2) if address.include? ':'

  @host_names   = []
  @ip_addresses = []
  @ip_index     = 0
  @port         = port.to_i

  Socket.getaddrinfo(address, port, Socket::AF_INET, Socket::SOCK_DGRAM).
         each do |address|
    @host_names << address[2]
    @ip_addresses << address[3]
  end

  @ip_address = @ip_addresses.first

  init_socket
end

#rotate_ipObject

Rotate this server’s IP address to the next one in the IP list

This method will return true, if the IP list reached its end. If the list contains only one IP address, this method will instantly return true.



38
39
40
41
42
43
44
45
46
47
# File 'lib/steam/servers/server.rb', line 38

def rotate_ip
  return true if @ip_addresses.size == 1

  @ip_index = (@ip_index + 1) % @ip_addresses.size
  @ip_address = @ip_addresses[@ip_index]

  init_socket

  @ip_index == 0
end