rcon

package
v0.0.0-...-de0a7f0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 15, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MSGLEN = 8196
)

Variables

View Source
var (
	CommandFailed          = errors.New("FAIL")
	ReconnectTriesExceeded = errors.New("there are no reconnects left")
)
View Source
var (
	Timeout = errors.New("connection request timed out before a connection was available")
)

Functions

This section is empty.

Types

type Connection

type Connection struct {
	// contains filtered or unexported fields
}

Connection represents a persistent connection to a HLL server using RCon. It can be used to issue commands against the HLL server and query data. The connection can either be utilised using the higher-level API methods, or by sending raw commands using ListCommand or Command.

A Connection is not thread-safe by default. Do not attempt to run multiple commands (either of the higher-level or low-level API). Doing so may either run into non-expected indefinitely blocking execution (until the context.Context deadline exceeds) or to mixed up data (sending a command and getting back the response for another command). Instead, in goroutines use a ConnectionPool and request a new connection for each goroutine. The ConnectionPool will ensure that one Connection is only used once at the same time. It also speeds up processing by opening a number of Connections until the pool size is reached.

func (*Connection) AddToMapRotation

func (c *Connection) AddToMapRotation(mapName string, afterMap string) error

AddToMapRotation Adds a map to the map rotation after the mentioned map.

func (*Connection) Command

func (c *Connection) Command(cmd string) (string, error)

Command executes the raw command provided and returns the result as a plain string.

func (*Connection) Context

func (c *Connection) Context() context.Context

func (*Connection) GameState

func (c *Connection) GameState() (GameState, error)

GameState returns information about the currently played round on the server.

func (*Connection) ListCommand

func (c *Connection) ListCommand(cmd string) ([]string, error)

ListCommand executes the raw command provided and returns the result as a list of strings. A list with regard to the RCon protocol is delimited by tab characters. The result is split by tab characters to produce the resulting list response.

func (*Connection) MapRotation

func (c *Connection) MapRotation(filters ...MapFilter) ([]string, error)

MapRotation Returns a list of map names, which are currently in the map rotation. Maps can be duplicated in the list.

func (*Connection) Maps

func (c *Connection) Maps(filters ...MapFilter) ([]string, error)

Maps Returns the available maps on the server. These map names can be used in commands like SwitchMap and AddToMapRotation

func (*Connection) PlayerIds

func (c *Connection) PlayerIds() ([]PlayerId, error)

PlayerIds issues the `get playerids` command to the server and returns a list of parsed PlayerIds. The players returned are the ones currently connected to the server.

func (*Connection) PlayerInfo

func (c *Connection) PlayerInfo(name string) (PlayerInfo, error)

PlayerInfo returns more information about a specific player by using its name. The player needs to be connected to the server for this command to succeed.

func (*Connection) ServerName

func (c *Connection) ServerName() (string, error)

ServerName returns the currently set server name

func (*Connection) ShowLog

func (c *Connection) ShowLog(d time.Duration) ([]string, error)

ShowLog is a higher-level method to read logs using RCon using the `showlog` raw command. While it would be possible to execute `showlog` with Command, it is not recommended to do so. Showlog has a different response size depending on the duration from when logs should be returned. As RCon does not provide a way to communicate the length of the response data, this method will try to guess if the returned data is complete and reads from the underlying stream of data until it has all. This is not the case with Command.

func (*Connection) Slots

func (c *Connection) Slots() (int, int, error)

Slots returns the current number of players connected to the server as the first return value. The second return value is the total number of players allowed to be connected to the server at the same time.

func (*Connection) SwitchMap

func (c *Connection) SwitchMap(mapName string) error

SwitchMap Changes the map on the server. The map name must be one that is available on the server. You can get the available maps with the Maps function. If the map is not in the map rotation, yet, then it will be added to the Map Rotation.

func (*Connection) WithContext

func (c *Connection) WithContext(ctx context.Context) error

WithContext inherits applicable values from the given context.Context and applies them to the underlying RCon connection. There is generally no need to call this method explicitly, the ConnectionPool (where you usually get this Connection from) takes care of propagating the outer context.

However, n cases where you want to have a different context.Context for retrieving a Connection from the ConnectionPool and when executing commands, using this method can be useful. One use case might be to have a different timeout while waiting for a Connection from the ConnectionPool, as when executing a command on the Connection.

Returns an error if context.Context values could not be applied to the underlying Connection.

type ConnectionPool

type ConnectionPool struct {
	// contains filtered or unexported fields
}

func NewConnectionPool

func NewConnectionPool(logger lager.Logger, h string, p int, pw string) *ConnectionPool

func (*ConnectionPool) GetWithContext

func (p *ConnectionPool) GetWithContext(ctx context.Context) (*Connection, error)

GetWithContext returns a connection from the pool. This method does not guarantee you to get a new, fresh Connection. A Connection might either be opened newly for this request, re-used from the pool of idle connections or one that was returned to the pool just now.

If there are no idle connections and if the limit of open connections is already reached, the request to retrieve a Connection will be queued. The request might or might not be fulfilled later, once a Connection is returned to the pool.

It is recommended to provide a context.Context with a deadline. The deadline will be the maximum time the caller is ok with waiting for a connection before a Timeout error is returned. If no deadline is provided in the context.Context, GetWithContext might wait indefinitely.

func (*ConnectionPool) Return

func (p *ConnectionPool) Return(c *Connection)

Return returns a previously gathered Connection from GetWithContext back to the pool for later use. The Connection might either be closed, put into a pool of "hot", idle connections or directly returned to a queued GetWithContext request.

func (*ConnectionPool) SetMaxIdle

func (p *ConnectionPool) SetMaxIdle(mi int)

SetMaxIdle sets the maximum number of idle connections in the pool. Idle connections are established connections to the server (warm) but are not ye/anymore used. Warm connections are preferably used to fulfill connection requests (GetWithContext) to reduce the overhead of opening and closing a connection on every request. Consider a high max idle connection to benefit from re-using connections as much as possible.

func (*ConnectionPool) SetPoolSize

func (p *ConnectionPool) SetPoolSize(s int)

SetPoolSize sets the maximum number of open connections at any time. A request for a connection when the pool reached this size (and no idle connections are available) will be put into a queue and be served once a connection is returned to the pool. This queue is on a best effort basis and might fail based on the provided deadline to GetWithContext.

func (*ConnectionPool) Shutdown

func (p *ConnectionPool) Shutdown()

func (*ConnectionPool) WithConnection

func (p *ConnectionPool) WithConnection(ctx context.Context, f func(c *Connection)) error

type GameScore

type GameScore struct {
	Axis   int
	Allies int
}

type GameState

type GameState struct {
	Players       PlayerCount
	Score         GameScore
	RemainingTime time.Duration
	Map           string
	NextMap       string
}

type MapFilter

type MapFilter func(idx int, name string, result []string) bool

MapFilter A filter used in commands that return list of maps, e.g. Maps or MapRotation. The filter should return true, when the map should be included in the result set and false when the map should be skipped.

type PlayerCount

type PlayerCount struct {
	Axis   int
	Allies int
}

type PlayerId

type PlayerId struct {
	Name      string
	SteamId64 string
}

type PlayerInfo

type PlayerInfo struct {
	Name      string
	SteamId64 string
	Team      string
	Role      string
	Loadout   string
	Unit      Unit
	Kills     int
	Deaths    int
	Score     Score
	Level     int
}

type Score

type Score struct {
	CombatEffectiveness int
	Offensive           int
	Defensive           int
	Support             int
}

type Unit

type Unit struct {
	Id   int
	Name string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL