Class: SteamMist::Rcon::Pass

Inherits:
Object
  • Object
show all
Defined in:
lib/steam_mist/rcon/pass.rb

Overview

This basically handles sending and receiving packets.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ip, port = 27015) ⇒ Pass

Initialize.

Parameters:

  • ip (String)

    the IP address of the Rcon server. Passed to Listener.

  • port (Numeric) (defaults to: 27015)

    the port of the Rcon server. Passed to Listener.



23
24
25
26
27
# File 'lib/steam_mist/rcon/pass.rb', line 23

def initialize(ip, port = 27015)
  @packet_factory = PacketFactory.new
  @listener       = Listener.new ip, port
  @empty_packet   = Packet.from_hash :type => Packet::SERVERDATA_RESPONSE_VALUE
end

Instance Attribute Details

#listenerListener (readonly)

The listener for sending and receiving data from the server.

Returns:



16
17
18
# File 'lib/steam_mist/rcon/pass.rb', line 16

def listener
  @listener
end

#packet_factoryPacketFactory (readonly)

The packet factory that should be used when creating packets. This factory is used to increment the ID number on sequential packets.

Returns:



11
12
13
# File 'lib/steam_mist/rcon/pass.rb', line 11

def packet_factory
  @packet_factory
end

Instance Method Details

#auth(password) ⇒ Boolean

This authenticates the connection with the server. A password is required.

Parameters:

  • password (String)

    the password to authenticate with.

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/steam_mist/rcon/pass.rb', line 88

def auth(password)
  packet = packet_factory.create_packet
  packet.type = Packet::SERVERDATA_AUTH
  packet.body = password

  #response = send_packet packet
  write packet

  # discard
  on_packet

  response = on_packet

  response.id == packet.id
end

#closevoid

This method returns an undefined value.

This closes the connection by calling Listener#close.



107
108
109
# File 'lib/steam_mist/rcon/pass.rb', line 107

def close
  listener.close
end

#on_packet {|packet| ... } ⇒ Packet

Retrieves the next packet from the stream. If a block is given, it yields to that.

Yield Parameters:

Returns:



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/steam_mist/rcon/pass.rb', line 34

def on_packet(&block)
  ensure_connected.on_data do |con|
    packet = Packet.from_stream con
    
    if block
      block.call packet
    end

    packet
  end
end

#send_packet(packet) ⇒ Packet+

Sends a packet along with an empty packet. This allows the client to figure out the server sent multiple packets to the client for the same request packet. Returns an array of packets if the server sent multiple packets.

Parameters:

  • packet (Packet)

    the packet to send

Returns:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/steam_mist/rcon/pass.rb', line 61

def send_packet(packet)
  empty_packet = @empty_packet.dup
  empty_packet.id = packet.id
  write(packet) and write(empty_packet)
  response_packets = []

  begin
    while response_packets.empty? || !response_packets.last.weird? do
      response_packets << on_packet
    end

  rescue TimeoutError; end

  response_packets.pop(2)

  if response_packets.length == 1
    response_packets[0]
  else
    response_packets
  end
end

#write(packet) ⇒ void, Numeric

Sends the given packet to the server.

Parameters:

Returns:

  • (void, Numeric)


50
51
52
# File 'lib/steam_mist/rcon/pass.rb', line 50

def write(packet)
  ensure_connected.write packet.format
end