Skip to content
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

Add Display to structs #3

Merged
merged 5 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
[package]
name = "rcon"
version = "0.1.0"
version = "0.1.1"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rand = "*"
bytes = "*"
bytes = "1.1.*"
rand = "0.8.*"
serde = { version = "1.0.*", features = ["derive"] }
thiserror = "1.0.*"
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ To work with TCP, the `TcpStream` structure built into the `std::net` module is
- [Description of RCON at developer.valvesoftware.com](https://developer.valvesoftware.com/wiki/Source_RCON_Protocol)

## Dependencies
- [rand](https://crates.io/crates/rand) for generate a random request ID
- [bytes](https://crates.io/crates/bytes) for converting types to bytes, for subsequent transmission via tcp
- [rand](https://crates.io/crates/rand) for generate a random request ID
- [serde](https://crates.io/crates/serde) for serializing errors
- [thiserror](https://crates.io/crates/thiserror) for serializing errors too

## Games that support this protocol
- Minecraft
Expand All @@ -26,16 +28,19 @@ To work with TCP, the `TcpStream` structure built into the `std::net` module is
use rcon::{AuthRequest, RCONClient, RCONError, RCONRequest};

fn main() -> Result<(), RCONError> {
let server_url = String::from("donkey-engine.host");
let server_url = "donkey-engine.host".to_string();

// Create new RCON client
let mut client = rcon::RCONClient::new(server_url)?;
let auth_result = client.auth(AuthRequest::new(String::from("RCON_SECRET")))?;

// Auth request to RCON server (SERVERDATA_AUTH)
let auth_result = client.auth(AuthRequest::new("RCON_SECRET".to_string()))?;
assert!(auth_result.is_success());
let version = client.execute(RCONRequest {
id: 228,
request_type: 2,
body: String::from("VERSION"),
})?;

// Execute command request to RCON server (SERVERDATA_EXECCOMMAND)
let version = client.execute(RCONRequest::new("VERSION".to_string()))?;
assert_eq!(version.body, "1.0.0");

Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::io::{Read, Write};
use std::net::TcpStream;

/// Simple RCON client
#[derive(Debug)]
pub struct RCONClient {
pub url: String,
pub(self) socket: TcpStream,
Expand Down
6 changes: 6 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
use serde::Serialize;
use thiserror::Error;

/// Errors enum for project
#[derive(Error, Debug, Serialize)]
pub enum RCONError {
/// Error with TCP connection
#[error("TCP connection error: {0}")]
TcpConnectionError(String),
/// Error with types converting
#[error("Error with types conversion: {0}")]
TypeError(String),
}
20 changes: 16 additions & 4 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use rand::Rng;

/// Common type for TCP response
#[derive(Debug)]
pub(crate) struct ExecuteResponse {
pub(crate) response_id: i32,
pub(crate) response_type: i32,
pub(crate) response_body: String,
}

/// Request for auth in RCON
#[derive(Debug)]
pub struct AuthRequest {
pub id: usize,
pub request_type: u8,
Expand All @@ -26,6 +28,7 @@ impl AuthRequest {
}

/// Response from auth request
#[derive(Debug)]
pub struct AuthResponse {
pub id: isize,
pub response_type: u8,
Expand All @@ -34,21 +37,30 @@ pub struct AuthResponse {
impl AuthResponse {
/// Is auth success
pub fn is_success(&self) -> bool {
if self.id == -1 {
return false;
}
true
self.id != -1
}
}

/// Request for RCON command
#[derive(Debug)]
pub struct RCONRequest {
pub id: usize,
pub request_type: u8,
pub body: String,
}

impl RCONRequest {
pub fn new(body: String) -> Self {
Self {
id: rand::thread_rng().gen::<usize>(),
request_type: 2,
body,
}
}
}

/// Response for RCON command
#[derive(Debug)]
pub struct RCONResponse {
pub id: isize,
pub response_type: u8,
Expand Down