Class: Rcon::Response

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

Overview

wraps the response we receive from the server. It might not be obvious at first why have this additional datastructure. There are two main motivations.

First, to separate how we deal with an AuthResponse vs a CommandResponse.

Secondly, when we are dealing with segmented responses, instead of modifying the first packet in place to add subsequent parts of the body, we modify the Response object corresponding with the total response. i.e. a Response is a sum of Packets.

Direct Known Subclasses

AuthResponse, CommandResponse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, type:, body:) ⇒ Response

instantiate a new Rcon::Response

Parameters:

  • id (Integer)

    the id of the initial request packet that the response corresponds to

  • type (Symbol)

    the response type; see Packet::RESPONSE_PACKET_TYPE

  • body (String)

    the response body


40
41
42
43
44
# File 'lib/rcon/response.rb', line 40

def initialize(id:, type:, body:)
  @id = id
  @type = type
  @body = body
end

Instance Attribute Details

#bodyString

the response body, which may be the concatenation of the bodies of several packets.

Returns:

  • (String)

    the current value of body


17
18
19
# File 'lib/rcon/response.rb', line 17

def body
  @body
end

#idInteger (readonly)

the initial request packet id corresponding to the response (except maybe for AuthResponse, see AuthResponse#success?

Returns:

  • (Integer)

    the current value of id


17
18
19
# File 'lib/rcon/response.rb', line 17

def id
  @id
end

#typeSymbol (readonly)

the type of response; see Packet::RESPONSE_PACKET_TYPE

Returns:

  • (Symbol)

    the current value of type


17
18
19
# File 'lib/rcon/response.rb', line 17

def type
  @type
end

Class Method Details

.from_packet(packet) ⇒ AuthResponse, CommandResponse

instantiate an instance of a Rcon::Response subclass given a packet.

Parameters:

  • packet (Packet)

    the packet

Returns:


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rcon/response.rb', line 22

def self.from_packet(packet)
  params = { id: packet.id, type: packet.type, body: packet.body }
  case packet.type
  when :SERVERDATA_AUTH_RESPONSE
    AuthResponse.new(**params)
  when :SERVERDATA_RESPONSE_VALUE
    CommandResponse.new(**params)
  else
    raise Error::UnsupportedResponseTypeError.new(packet.type)
  end
end