Module: SteamCondenser::Servers::Sockets::BaseSocket

Included in:
GoldSrcSocket, MasterServerSocket, RCONSocket, SourceSocket
Defined in:
lib/steam-condenser/servers/sockets/base_socket.rb

Overview

This module implements common functionality for sockets used to connect to game and master servers

Author:

  • Sebastian Staudt

Constant Summary collapse

@@timeout =

The default socket timeout

1000

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.timeout=(timeout) ⇒ Object

Sets the timeout for socket operations

Any request that takes longer than this time will cause a Error::Timeout.


32
33
34
# File 'lib/steam-condenser/servers/sockets/base_socket.rb', line 32

def self.timeout=(timeout)
  @@timeout = timeout
end

Instance Method Details

#closeObject

Closes the underlying socket


48
49
50
# File 'lib/steam-condenser/servers/sockets/base_socket.rb', line 48

def close
  @socket.close
end

#initialize(ip_address, port = 27015) ⇒ Object

Creates a new UDP socket to communicate with the server on the given IP address and port


42
43
44
45
# File 'lib/steam-condenser/servers/sockets/base_socket.rb', line 42

def initialize(ip_address, port = 27015)
  @socket = UDPSocket.new
  @socket.connect ip_address, port
end

#receive_packet(buffer_length = 0) ⇒ Fixnum

Reads the given amount of data from the socket and wraps it into the buffer

Raises:

See Also:


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/steam-condenser/servers/sockets/base_socket.rb', line 59

def receive_packet(buffer_length = 0)
  if select([@socket], nil, nil, @@timeout / 1000.0).nil?
    raise SteamCondenser::Error::Timeout
  end

  if buffer_length == 0
    @buffer.rewind
  else
    @buffer = StringIO.alloc buffer_length
  end

  begin
    data = @socket.recv @buffer.remaining
  rescue Errno::ECONNRESET
    @socket.close
    raise $!
  end
  bytes_read = @buffer.write data
  @buffer.truncate bytes_read
  @buffer.rewind

  bytes_read
end

#send(data_packet) ⇒ Object

Sends the given packet to the server

This converts the packet into a byte stream first before writing it to the socket.


91
92
93
94
95
96
97
98
# File 'lib/steam-condenser/servers/sockets/base_socket.rb', line 91

def send(data_packet)
  if log.debug?
    packet_class = data_packet.class.name[/[^:]*\z/]
    log.debug "Sending data packet of type \"#{packet_class}\"."
  end

  @socket.send data_packet.to_s, 0
end