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 and the ability to rotate between different IP addresses belonging to a single DNS name.

Author:

  • Sebastian Staudt

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#host_namesArray<String> (readonly)

Returns a list of host names associated with this server

Returns:

  • (Array<String>)

    The host names of this server



19
20
21
# File 'lib/steam/servers/server.rb', line 19

def host_names
  @host_names
end

#ip_addressesArray<String> (readonly)

Returns a list of IP addresses associated with this server

Returns:

  • (Array<String>)

    The IP addresses of this server



24
25
26
# File 'lib/steam/servers/server.rb', line 24

def ip_addresses
  @ip_addresses
end

Instance Method Details

#disconnectObject

Note:

In the base implementation this does nothing, only connection-based communication channels have to be disconnected.

Disconnect the connections to this server



59
60
# File 'lib/steam/servers/server.rb', line 59

def disconnect
end

#initialize(address, port = nil) ⇒ Object

Creates a new server instance with the given address and port

Parameters:

  • address (String)

    Either an IP address, a DNS name or one of them combined with the port number. If a port number is given, e.g. ‘server.example.com:27016’ it will override the second argument.

  • port (Fixnum) (defaults to: nil)

    The port the server is listening on

Raises:

See Also:

  • #init_socket


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/steam/servers/server.rb', line 34

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

  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_ipBoolean

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

If this method returns ‘true`, it indicates that all IP addresses have been used, hinting at the server(s) being unreachable. An appropriate action should be taken to inform the user.

Servers with only one IP address will always cause this method to return ‘true` and the sockets will not be reinitialized.

Returns:

  • (Boolean)

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

See Also:

  • #init_socket


75
76
77
78
79
80
81
82
83
84
# File 'lib/steam/servers/server.rb', line 75

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