-
Notifications
You must be signed in to change notification settings - Fork 0
example-doc #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
example-doc #4
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -13,7 +13,7 @@ It empowers applications with easy integration capabilities with a Rust game ser | |||||
| translating internal server details into actionable events. | ||||||
|
|
||||||
| By interfacing with Rust's native RCON payloads, the library efficiently captures and translates | ||||||
| in-game occurrences — such as player connections, chats, combat and more — into events. | ||||||
| in-game occurrences — such as player connections, chats, combat, and more — into events. | ||||||
| This reactive design allows user code to be promptly informed about player-driven and server-driven actions. | ||||||
|
|
||||||
| Additionally, Rust RCON manages connection states intelligently, eliminating concerns about | ||||||
|
|
@@ -101,9 +101,291 @@ Rust RCON offers a plethora of events that give developers a detailed insight in | |||||
| - **WsErrorEvent**: Triggered when there's an error in the websocket communication. | ||||||
| - **WsClosedEvent**: Emitted when the websocket connection is closed, either due to errors or deliberate actions. | ||||||
|
|
||||||
| --- | ||||||
|
|
||||||
| ## Examples :alembic: | ||||||
|
|
||||||
| *Coming soon* | ||||||
| **Difference between classes:** | ||||||
|
|
||||||
| ```java | ||||||
| public class DefaultRustRconService { | ||||||
| } | ||||||
|
|
||||||
| public class DefaultRustRconRouter { | ||||||
| } | ||||||
|
|
||||||
| public class DefaultRustRconClient { | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| --- | ||||||
|
|
||||||
| <h3> DefaultRustRconService </h3> | ||||||
|
|
||||||
| ```java | ||||||
| public class DefaultRustRconService { | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| The `DefaultRustRconService` class is the **operational core** and the main implementation of the connection and management logic for a Rust game server using the RCON protocol. | ||||||
|
|
||||||
| Here are its main functions: | ||||||
|
|
||||||
| 1. **Component Orchestration**: Acts as a central "hub" that initializes and connects the WebSocket client (`RustWebSocketClient`), the event system (`EventBus`), the communication protocols | ||||||
| (`Codec`), and the data mappers (`RustDtoMappers`). It uses the `Lazy` pattern to initialize these components only when necessary. | ||||||
| 2. **Lifecycle Management**: Manages the starting (`start()`) and stopping (`stop()`) of the server connection and the message router. | ||||||
| 3. **Automation (Polling)**: In the `configure()` method, set up scheduled tasks (`ScheduledExecutorService`) that automatically poll the server at regular intervals to keep local data up to date. | ||||||
| Specifically: | ||||||
| * Server info (every 5 seconds). | ||||||
| * Player list (every 3 seconds). | ||||||
| * Team list (every 5 minutes). | ||||||
| 4. **State Management**: Maintains the current server state, such as the player list (`rustPlayers`), teams (`rustTeams`), and diagnostics, in memory (via `AtomicReference`), making them accessible to | ||||||
| the application without having to make a network request each time. | ||||||
| 5. **API Facade**: Provides high-level methods to access specific functionality, such as player management (`playerManagement()`) or Oxide framework management (`oxideManagement()`). | ||||||
|
|
||||||
| To use the `DefaultRustRconService` class, you must follow a standard lifecycle: configuration, startup, interaction, and shutdown. | ||||||
|
|
||||||
| Here are the main steps: | ||||||
|
|
||||||
| 1. Instantiation: Create an instance by passing a `RustRconConfiguration` object with connection details (IP, port, password). | ||||||
| 2. Event Registration (Optional): If you want to react to events (e.g., chat messages, server logs), register your listeners before or after startup. | ||||||
| 3. Startup: Call the `start()` method. This opens the WebSocket connection and starts automatic tasks (polling players, server info, etc.). | ||||||
| 4. Interaction: Use the exposed methods to send commands or read state (e.g., `playerManagement()`, `codec()`, `players()`). | ||||||
| 5. Stoppage: Call `stop()` when the application terminates to close connections and threads. | ||||||
|
|
||||||
| Here is a practical example of use: | ||||||
|
|
||||||
| ```java | ||||||
| import com.scalbox.rust.rcon.DefaultRustRconService; | ||||||
| import com.scalbox.rust.rcon.RustRconConfiguration; | ||||||
| import com.scalbox.rust.rcon.protocol.dto.RustPlayerDTO; | ||||||
|
|
||||||
| public class ExampleUsage { | ||||||
|
|
||||||
| public static void main(String[] args) { | ||||||
| // 1. Configuration | ||||||
| RustRconConfiguration config = new RustRconConfiguration( | ||||||
| "127.0.0.1", | ||||||
| 28016, | ||||||
| "your_password_rcon" | ||||||
| ); | ||||||
|
|
||||||
| // 2. Instantiation of the service | ||||||
| DefaultRustRconService rconService = new DefaultRustRconService(config); | ||||||
|
|
||||||
| // (Optional) Enable logging of all RCON traffic to the console | ||||||
| rconService.enableRconLogger(); | ||||||
|
|
||||||
| // 3. Start connection and background tasks | ||||||
| rconService.start(); | ||||||
|
|
||||||
| // 4. Interaction: Example of sending a command (banning a player) | ||||||
| // Note: playerManagement() offers high-level methods | ||||||
| // rconService.playerManagement().ban("steamId", "reason"); | ||||||
|
|
||||||
| // 4. Interaction: Read cached data (automatically updated every 3 seconds) | ||||||
| System.out.println("Online players: " + rconService.players().size()); | ||||||
|
|
||||||
| // 4. Interaction: Send raw command via Codec | ||||||
| rconService.codec().send("say Hello Server!"); | ||||||
|
|
||||||
| // 5. Stop (usually when the application shuts down) | ||||||
| rconService.stop(); | ||||||
| } | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| --- | ||||||
|
|
||||||
| <h3> DefaultRustRconRouter </h3> | ||||||
|
|
||||||
| ```java | ||||||
| public class DefaultRustRconRouter { | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| The `DefaultRustRconRouter` class acts as a **transport and correlation layer** between your application and the raw RCON client. While the `Service` (seen previously) handles the high-level logic | ||||||
| (players, teams), this `Router` handles the sending and receiving mechanics. | ||||||
|
|
||||||
| Here's what it does specifically: | ||||||
|
|
||||||
| 1. **Identifier (ID) Management**: The RCON protocol requires each packet sent to have a unique ID to associate the response with the correct request. This class generates these IDs automatically | ||||||
| (`identifierGenerator`) using an `AtomicInteger`. | ||||||
| 2. **Request/Response Correlation**: When you call `send()`, the class packages the message in a `RustRconRequest`, sends it, and returns a `CompletableFuture`. This allows the response to be handled | ||||||
| asynchronously as soon as it arrives. | ||||||
| 3. **Audit and Logging (Event Bus)**: Using the `handleRconProtocolExchange` method, every time a request is completed (successfully or unsuccessfully), an `RconProtocolExchangeEvent` is published to | ||||||
| the `EventBus`. This is essential for logging traffic or for plugins that need to analyze everything that passes "over the wire". | ||||||
| 4. **Client Abstraction**: Hides the connection details of the underlying `RustRconClient`, exposing only methods for sending messages and managing the lifecycle (`start`/`stop`). | ||||||
|
|
||||||
| ### How to use it | ||||||
|
|
||||||
| Typically, this class is instantiated and managed internally by the `DefaultRustRconService`. However, if you're building a custom implementation or need to send raw commands bypassing the high-level | ||||||
| logic, you can use it like this: | ||||||
|
|
||||||
| 1. Dependency Injection: Requires a RustRconClient (the physical connection) and an EventBus (for events). | ||||||
| 2. Lifecycle: You must call start() to connect and stop() to close. | ||||||
|
Comment on lines
+225
to
+226
|
||||||
| 3. Command Sending: You use the send() method, which accepts a message and (optionally) a mapping function to transform the response. | ||||||
|
|
||||||
| Here's an example of direct usage: | ||||||
|
|
||||||
| ```java | ||||||
| import com.google.common.eventbus.EventBus; | ||||||
| import com.scalbox.rust.rcon.DefaultRustRconRouter; | ||||||
| import com.scalbox.rust.rcon.RustRconClient; // Hypothetical implementation | ||||||
| import com.scalbox.rust.rcon.protocol.RustRconMessage; | ||||||
|
|
||||||
| public class RouterUsageExample { | ||||||
|
|
||||||
| public void example(RustRconClient rawClient, EventBus eventBus) { | ||||||
| // 1. Instantiation | ||||||
| DefaultRustRconRouter router = new DefaultRustRconRouter(rawClient, eventBus); | ||||||
|
|
||||||
| // 2. Starting the underlying connection | ||||||
| router.start(); | ||||||
|
|
||||||
| // 3. Creating a message (functional wrapper or object) | ||||||
| RustRconMessage message = () -> "status"; | ||||||
|
|
||||||
| // 4. Asynchronous sending and response handling | ||||||
| router.send(message).thenAccept(response -> { | ||||||
| System.out.println("Response from server: " + response.getMessage()); | ||||||
| }).exceptional(ex -> { | ||||||
|
||||||
| }).exceptional(ex -> { | |
| }).exceptionally(ex -> { |
Copilot
AI
Jan 5, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing word 'the' before 'team'. The phrase should read 'team list in the background' or 'teams in the background' for proper grammar.
| * Performs **automatic polling** (updates player list, server info, team in the background). | |
| * Performs **automatic polling** (updates player list, server info, team list in the background). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The list item format is inconsistent with the numbering in this section. Items 1-3 are numbered, but then there are three more interaction examples all labeled as '4.' followed by a '5.' Renumber the items sequentially (4, 5, 6, 7) to match the list structure.