bercon

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2025 License: AGPL-3.0 Imports: 8 Imported by: 1

Documentation

Overview

Package bercon provides a BattlEye RCON connection handling (send commands, receive responses, keep alive).

Usage:

package main

import (
	"fmt"
	"time"

	"github.com/woozymasta/bercon-cli/pkg/bercon"
)

func main() {
	// Open RCON connection
	conn, err := bercon.Open("127.0.0.1:2302", "MyRconPassword")
	if err != nil {
		fmt.Println("Failed to open connection:", err)
		return
	}
	defer conn.Close()

	// Start keepalive routine
	conn.StartKeepAlive()

	// Listen for loginPacket or messagePacket in a separate goroutine
	// (events will appear in conn.Messages channel)
	go func() {
		// This loop will receive packets such as messages from the server
		for event := range conn.Messages {
			// Handle PacketEvent
			// Example: log or print to console
			fmt.Printf("[EVENT] seq=%d time=%s data=%s\n",
				event.Seq, event.Time.Format(time.Stamp), string(event.Data))
		}
	}()

	// Now we can send a command and wait for a response
	resp, err := conn.Send("players")
	if err != nil {
		fmt.Println("Failed to send command:", err)
		return
	}
	// Print out the response from 'players' command
	fmt.Printf("Command 'players' response: %s\n", string(resp))

	// ... do more stuff, send more commands, etc.

	// Close connection when done
	fmt.Println("Closing connection...")
}

Index

Constants

View Source
const (
	DefaultKeepaliveTimeout  = 30   // Default keepalive in seconds, must not exceed 45
	DefaultDeadlineTimeout   = 5    // Default deadline timeout in seconds
	DefaultMicroSleepTimeout = 10   // Default micro-sleep timeout in milliseconds
	DefaultBufferSize        = 1024 // 1024 bytes max body data
	DefaultBufferHeaderSize  = 16   // 7 bytes header, 1 byte type, 1 byte seq, 1 byte terminator, 2 bytes pages, etc.
)

Default timeouts and buffer sizes.

Variables

View Source
var (
	ErrTimeout          = errors.New("deadline timeout reached")                      // deadline timeout reached
	ErrBufferFull       = errors.New("send command queue is full, try again later")   // send command queue is full, try again later
	ErrConnectionClosed = errors.New("connection closed unexpected")                  // connection closed unexpected
	ErrConnectionDown   = errors.New("connection to server is down, need reconnect")  // connection to server is down, need reconnect
	ErrReconnectFailed  = errors.New("failed to reconnect after several attempts")    // failed to reconnect after several attempts
	ErrPacketSize       = errors.New("packet size to small")                          // packet size to small
	ErrPacketHeader     = errors.New("packet header mismatched")                      // packet header mismatched
	ErrPacketCRC        = errors.New("CRC data not match")                            // CRC data not match
	ErrPacketUnknown    = errors.New("received unknown packet type")                  // received unknown packet type
	ErrNotResponse      = errors.New("server not response")                           // server not response
	ErrLoginFailed      = errors.New("login failed")                                  // login failed
	ErrNoLoginResponse  = errors.New("wait for login but get unexpected response")    // wait for login but get unexpected response
	ErrBadResponse      = errors.New("unexpected response data")                      // unexpected response data
	ErrBadSequence      = errors.New("returned not expected page number of sequence") // returned not expected page number of sequence
	ErrBadSize          = errors.New("size of buffer is greater than the allowed")    // size of buffer is greater than the allowed
	ErrBadPart          = errors.New("unexpected packet part returned")               // unexpected packet part returned
)

Functions

This section is empty.

Types

type Connection

type Connection struct {

	// Messages is a channel to which we will send PacketEvents for any non-command packets (e.g. loginPacket, messagePacket).
	// Client code can read from this channel to handle them externally (logging, saving to file, etc.).
	Messages chan PacketEvent
	// contains filtered or unexported fields
}

Connection represents a connection to the BattlEye server.

func Open

func Open(addr, pass string) (*Connection, error)

Open initializes and returns a new Connection to the specified BattlEye server using the provided address and password.

func (*Connection) Close

func (c *Connection) Close() error

Close gracefully closes the connection, releases resources, and ensures no further operations are performed.

func (*Connection) IsAlive

func (c *Connection) IsAlive() bool

IsAlive checks if the connection is active and not closed.

func (*Connection) Send

func (c *Connection) Send(command string) ([]byte, error)

Send dispatches a command to the BattlEye server and waits for a response.

func (*Connection) SetBufferSize

func (c *Connection) SetBufferSize(size uint16)

SetBufferSize updates the buffer size for receiving packets from the server.

func (*Connection) SetDeadlineTimeout

func (c *Connection) SetDeadlineTimeout(seconds int)

SetDeadlineTimeout sets the max time (in seconds) to wait for a server response.

func (*Connection) SetKeepaliveTimeout

func (c *Connection) SetKeepaliveTimeout(seconds int)

SetKeepaliveTimeout configures how often (in seconds) keepalive packets are sent to maintain the connection. If seconds >= 45, it resets to the default because the server typically disconnects if the interval is too long.

func (*Connection) SetMicroSleepTimeout

func (c *Connection) SetMicroSleepTimeout(milliseconds int)

SetMicroSleepTimeout adjusts the micro-sleep interval (in milliseconds) used in busy-wait loops.

func (*Connection) StartKeepAlive

func (c *Connection) StartKeepAlive()

StartKeepAlive begins a routine that sends periodic keepalive packets.

type PacketEvent added in v0.3.0

type PacketEvent struct {
	Time time.Time
	Data []byte
	Seq  byte
}

PacketEvent is a struct for broadcasting incoming packets (like login or messages) so that client code can handle them (log them, etc.).

type Response

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

Response represents a single response from the server, potentially part of a multipart response.

type Timeouts

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

Timeouts defines various timeout configurations for the connection.

Jump to

Keyboard shortcuts

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