Module: SteamSocket

Included in:
GoldSrcSocket, MasterServerSocket, RCONSocket, SourceSocket
Defined in:
lib/steam/sockets/steam_socket.rb

Overview

This module defines common methods for sockets used to connect to game and master servers.

See Also:

Author:

  • Sebastian Staudt

Since:

  • 0.5.0

Constant Summary collapse

@@timeout =

The default timeout

Since:

  • 0.11.0

1000

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.timeout=(timeout) ⇒ Object

Sets the timeout for socket operations. This usually only affects timeouts, i.e. when a server does not respond in time.

Due to the server-side implementation of the RCON protocol, each RCON request will also wait this amount of time after execution. So if you need RCON requests to execute fast, you should set this to a adequatly low value.

timeout The amount of milliseconds before a request times out

Since:

  • 0.5.0



36
37
38
# File 'lib/steam/sockets/steam_socket.rb', line 36

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

Instance Method Details

#closeObject

Closes the underlying socket

Since:

  • 0.5.0



46
47
48
# File 'lib/steam/sockets/steam_socket.rb', line 46

def close
  @socket.close
end

#initialize(ip_address, port = 27015) ⇒ Object

Since:

  • 0.5.0



40
41
42
43
# File 'lib/steam/sockets/steam_socket.rb', line 40

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

#receive_packet(buffer_length = 0) ⇒ Object

Since:

  • 0.5.0



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/steam/sockets/steam_socket.rb', line 50

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

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

  data = @socket.recv @buffer.remaining
  bytes_read = @buffer.write data
  @buffer.truncate bytes_read
  @buffer.rewind

  bytes_read
end

#send(data_packet) ⇒ Object

Since:

  • 0.5.0



69
70
71
72
73
# File 'lib/steam/sockets/steam_socket.rb', line 69

def send(data_packet)
  puts "Sending data packet of type \"#{data_packet.class.to_s}\"." if $DEBUG

  @socket.send data_packet.to_s, 0
end