Documentation
¶
Overview ¶
Package bercon provides a BattlEye RCON connection handling (send commands, receive responses, keep alive).
Usage:
package main
import (
"fmt"
"time"
"github.com/djedu/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
- Variables
- type Connection
- func (c *Connection) Close() error
- func (c *Connection) IsAlive() bool
- func (c *Connection) Send(command string) ([]byte, error)
- func (c *Connection) SetBufferSize(size uint16)
- func (c *Connection) SetDeadlineTimeout(seconds int)
- func (c *Connection) SetKeepaliveTimeout(seconds int)
- func (c *Connection) SetMicroSleepTimeout(milliseconds int)
- func (c *Connection) StartKeepAlive()
- type PacketEvent
- type Response
- type Timeouts
Constants ¶
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 ¶
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 ¶
PacketEvent is a struct for broadcasting incoming packets (like login or messages) so that client code can handle them (log them, etc.).