aboutsummaryrefslogtreecommitdiff
path: root/src/message/handshake/protocol.rs
blob: 2a7d9aceff321c9445a21844ff95349e558a9c6b (plain)
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
use crate::serialize::{Deserialize, Serialize};

#[derive(Debug, Default)]
pub enum Protocol {
    Legacy = 0x00000001,
    #[default]
    Datastream = 0x00000002,
}

impl Protocol {
    pub fn new() -> Self {
        Protocol::default()
    }

    pub fn serialize(self) -> Vec<u8> {
        let proto: u32 = 0x80000002;

        proto.serialize().unwrap()
    }

    pub fn parse(buf: &[u8]) -> Self {
        let mut protolist: Vec<u32> = Vec::new();
        let mut pos = 0;
        loop {
            let (_, proto) = u32::parse(&buf[pos..(pos + 4)]).unwrap();
            if (proto & 0x80000000) >= 1 {
                protolist.push(proto - 0x80000000);
                break;
            } else {
                protolist.push(proto);
                pos += 4;
            }
        }

        Protocol::Datastream
    }
}