Module: SteamSocket

Included in:
GoldSrcSocket, MasterServerSocket, RCONSocket, SourceSocket
Defined in:
lib/steam/sockets/steam_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 SteamCondenser::TimeoutError.

Parameters:

  • timeout (Fixnum)

    The amount of milliseconds before a request times out



28
29
30
# File 'lib/steam/sockets/steam_socket.rb', line 28

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

Instance Method Details

#closeObject

Closes the underlying socket



44
45
46
# File 'lib/steam/sockets/steam_socket.rb', line 44

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

Parameters:

  • ip_address (String)

    Either the IP address or the DNS name of the server

  • port (Fixnum) (defaults to: 27015)

    The port the server is listening on



38
39
40
41
# File 'lib/steam/sockets/steam_socket.rb', line 38

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

Parameters:

  • buffer_length (Fixnum) (defaults to: 0)

    The data length to read from the socket

Returns:

  • (Fixnum)

    The number of bytes that have been read from the socket

Raises:

See Also:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/steam/sockets/steam_socket.rb', line 54

def receive_packet(buffer_length = 0)
  if select([@socket], nil, nil, @@timeout / 1000.0).nil?
    raise SteamCondenser::TimeoutError
  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.

Parameters:

  • data_packet (SteamPacket)

    The packet to send to the server

See Also:



85
86
87
88
89
# File 'lib/steam/sockets/steam_socket.rb', line 85

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