~starkingdoms/starkingdoms

ref: 4613985b0d064b1a774fc56822c0dce41476b070 starkingdoms/protocol/src/lib.rs -rw-r--r-- 1.1 KiB
4613985b — core fixup! fixup 2 years ago
                                                                                
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
use serde::{Deserialize, Serialize};

pub const PROTOCOL_VERSION: u32 = 1;

#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub enum State {
    Handshake,
    Play
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum MessageC2S {
    Hello {
        version: u32,
        requested_username: String,
        next_state: State
    },

    Goodbye {
        reason: GoodbyeReason
    },

    Chat {
        message: String
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum MessageS2C {
    Hello {
        version: u32,
        given_username: String,
        next_state: State
    },

    Goodbye {
        reason: GoodbyeReason
    },

    Chat {
        from: String,
        message: String
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum GoodbyeReason {
    UnsupportedProtocol { supported: u32, got: u32 },
    UnexpectedPacket,
    UnexpectedNextState,
    UsernameTaken,
    Done
}

pub fn pc2s(pkt: &MessageC2S) -> Vec<u8> {
    rmp_serde::to_vec(pkt).unwrap()
}
pub fn ps2c(pkt: &MessageS2C) -> Vec<u8> {
    rmp_serde::to_vec(pkt).unwrap()
}