Class: Srcon::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/srcon/connection.rb

Defined Under Namespace

Classes: AuthenticationFailure

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, password = nil) ⇒ Connection

Returns a new instance of Connection.

Parameters:

  • host (String)

    The host the server is listening on

  • port (Integer)

    The port the server is listening on

  • password (String) (defaults to: nil)

    The password for authentication



12
13
14
15
16
17
18
# File 'lib/srcon/connection.rb', line 12

def initialize(host, port, password = nil)
  @host = host
  @port = port
  @id   = rand(Process.pid)

  connect!(password)
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



7
8
9
# File 'lib/srcon/connection.rb', line 7

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



7
8
9
# File 'lib/srcon/connection.rb', line 7

def port
  @port
end

#socketObject (readonly)

Returns the value of attribute socket.



7
8
9
# File 'lib/srcon/connection.rb', line 7

def socket
  @socket
end

Instance Method Details

#receiveSrcon::Packet

Returns The deconstructed packet received from the socket.

Returns:

  • (Srcon::Packet)

    The deconstructed packet received from the socket



21
22
23
# File 'lib/srcon/connection.rb', line 21

def receive
  Srcon::Packet.from_message(*socket.recvmsg)
end

#send(message, type = Srcon::Packet::SERVERDATA_EXECCOMMAND) ⇒ Integer

Returns The number of bytes that were sent.

Parameters:

  • message (String)

    A message to send to the server

  • type (Integer) (defaults to: Srcon::Packet::SERVERDATA_EXECCOMMAND)

    The type of message

Returns:

  • (Integer)

    The number of bytes that were sent



29
30
31
32
33
# File 'lib/srcon/connection.rb', line 29

def send(message, type = Srcon::Packet::SERVERDATA_EXECCOMMAND)
  packet = Srcon::Packet.new(message, type, @id)
  @id += 1
  socket&.sendmsg(packet.to_b)
end