rcon_client/
types.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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,
    pub password: String,
}

impl AuthRequest {
    /// Create new auth request data
    pub fn new(password: String) -> Self {
        Self {
            id: rand::thread_rng().gen::<usize>(),
            request_type: 3,
            password,
        }
    }
}

/// Response from auth request
#[derive(Debug)]
pub struct AuthResponse {
    pub id: isize,
    pub response_type: u8,
}

impl AuthResponse {
    /// Is auth success
    pub fn is_success(&self) -> bool {
        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,
    pub body: String,
}