Trait rust_mc_proto::DataBufferReader

source ·
pub trait DataBufferReader {
Show 36 methods // Required method fn read_bytes(&mut self, size: usize) -> Result<Vec<u8>, ProtocolError>; // Provided methods fn read_byte(&mut self) -> Result<u8, ProtocolError> { ... } fn read_buffer(&mut self, size: usize) -> Result<ByteBuffer, ProtocolError> { ... } fn read_string(&mut self) -> Result<String, ProtocolError> { ... } fn read_unsigned_short(&mut self) -> Result<u16, ProtocolError> { ... } fn read_boolean(&mut self) -> Result<bool, ProtocolError> { ... } fn read_short(&mut self) -> Result<i16, ProtocolError> { ... } fn read_long(&mut self) -> Result<i64, ProtocolError> { ... } fn read_float(&mut self) -> Result<f32, ProtocolError> { ... } fn read_double(&mut self) -> Result<f64, ProtocolError> { ... } fn read_int(&mut self) -> Result<i32, ProtocolError> { ... } fn read_uuid(&mut self) -> Result<Uuid, ProtocolError> { ... } fn read_usize_varint_size( &mut self, ) -> Result<(usize, usize), ProtocolError> { ... } fn read_u8_varint_size(&mut self) -> Result<(u8, u8), ProtocolError> { ... } fn read_u16_varint_size(&mut self) -> Result<(u16, u16), ProtocolError> { ... } fn read_u32_varint_size(&mut self) -> Result<(u32, u32), ProtocolError> { ... } fn read_u64_varint_size(&mut self) -> Result<(u64, u64), ProtocolError> { ... } fn read_u128_varint_size(&mut self) -> Result<(u128, u128), ProtocolError> { ... } fn read_isize_varint_size( &mut self, ) -> Result<(isize, isize), ProtocolError> { ... } fn read_i8_varint_size(&mut self) -> Result<(i8, i8), ProtocolError> { ... } fn read_i16_varint_size(&mut self) -> Result<(i16, i16), ProtocolError> { ... } fn read_i32_varint_size(&mut self) -> Result<(i32, i32), ProtocolError> { ... } fn read_i64_varint_size(&mut self) -> Result<(i64, i64), ProtocolError> { ... } fn read_i128_varint_size(&mut self) -> Result<(i128, i128), ProtocolError> { ... } fn read_usize_varint(&mut self) -> Result<usize, ProtocolError> { ... } fn read_u8_varint(&mut self) -> Result<u8, ProtocolError> { ... } fn read_u16_varint(&mut self) -> Result<u16, ProtocolError> { ... } fn read_u32_varint(&mut self) -> Result<u32, ProtocolError> { ... } fn read_u64_varint(&mut self) -> Result<u64, ProtocolError> { ... } fn read_u128_varint(&mut self) -> Result<u128, ProtocolError> { ... } fn read_isize_varint(&mut self) -> Result<isize, ProtocolError> { ... } fn read_i8_varint(&mut self) -> Result<i8, ProtocolError> { ... } fn read_i16_varint(&mut self) -> Result<i16, ProtocolError> { ... } fn read_i32_varint(&mut self) -> Result<i32, ProtocolError> { ... } fn read_i64_varint(&mut self) -> Result<i64, ProtocolError> { ... } fn read_i128_varint(&mut self) -> Result<i128, ProtocolError> { ... }
}
Expand description

Packet data reader trait

Required Methods§

source

fn read_bytes(&mut self, size: usize) -> Result<Vec<u8>, ProtocolError>

Read bytes

Provided Methods§

source

fn read_byte(&mut self) -> Result<u8, ProtocolError>

Read byte

source

fn read_buffer(&mut self, size: usize) -> Result<ByteBuffer, ProtocolError>

source

fn read_string(&mut self) -> Result<String, ProtocolError>

Read String

Examples found in repository?
examples/recv_motd.rs (line 37)
34
35
36
37
38
fn read_status_response(conn: &mut MCConnTcp) -> Result<String, ProtocolError> {
    let mut packet = conn.read_packet()?;

    packet.read_string()
}
More examples
Hide additional examples
examples/test_compression.rs (line 34)
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
fn main() {
    let (tx, rx) = channel::<()>();

    let server_tx = tx.clone();
    thread::spawn(move || {
        let listener = TcpListener::bind("localhost:44447").unwrap();

        server_tx.send(()).unwrap();

        for stream in listener.incoming() {
            let mut stream = MCConnTcp::new(stream.unwrap());
            stream.set_compression(Some(2));

            let packet = stream.read_packet().unwrap();
            stream.write_packet(&packet).unwrap();
        }
    });

    rx.recv().unwrap();

    let mut conn = MCConnTcp::connect("localhost:44447").unwrap();
    conn.set_compression(Some(2));
    
    let mut packet = Packet::empty(0x12);
    packet.write_string(LONG_TEXT).unwrap();
    conn.write_packet(&packet).unwrap();

    let mut packet = conn.read_packet().unwrap();
    if packet.id == 0x12 && packet.read_string().unwrap() == LONG_TEXT {
        println!("success");
    }
}
examples/status_server.rs (line 63)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
fn accept_client(mut conn: MCConnTcp, server: Arc<Mutex<MinecraftServer>>) -> Result<(), ProtocolError> {
    let mut handshake = false;
    
    loop {
        let mut packet = match conn.read_packet() {
            Ok(i) => i,
            Err(_) => { 
                break; 
            },
        };

        if handshake {
            if packet.id == 0x00 {
                let mut status = Packet::empty(0x00);

                let serv = server.lock().unwrap();

                let motd = serv.motd.clone();
                let motd = motd.replace(
                    "PROTOCOL_VERSION", 
                    &serv.protocol_version.to_string());

                status.write_string(&motd)?;
                conn.write_packet(&status)?;
            } else if packet.id == 0x01 {
                let mut status = Packet::empty(0x01);
                status.write_long(packet.read_long()?)?;
                conn.write_packet(&status)?;
            }
        } else if packet.id == 0x00 {
            let protocol_version = packet.read_i32_varint()?;
            let server_address = packet.read_string()?;
            let server_port = packet.read_unsigned_short()?;
            let next_state = packet.read_u8_varint()?;

            if next_state != 1 { break; }

            println!("Client handshake info:");
            println!("  IP: {}", conn.stream.peer_addr().unwrap());
            println!("  Protocol version: {}", protocol_version);
            println!("  Server address: {}", server_address);
            println!("  Server port: {}", server_port);

            handshake = true;
        } else {
            break;
        }
    }

    conn.close();

    Ok(())
}
source

fn read_unsigned_short(&mut self) -> Result<u16, ProtocolError>

Read Unsigned Short as u16

Examples found in repository?
examples/status_server.rs (line 64)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
fn accept_client(mut conn: MCConnTcp, server: Arc<Mutex<MinecraftServer>>) -> Result<(), ProtocolError> {
    let mut handshake = false;
    
    loop {
        let mut packet = match conn.read_packet() {
            Ok(i) => i,
            Err(_) => { 
                break; 
            },
        };

        if handshake {
            if packet.id == 0x00 {
                let mut status = Packet::empty(0x00);

                let serv = server.lock().unwrap();

                let motd = serv.motd.clone();
                let motd = motd.replace(
                    "PROTOCOL_VERSION", 
                    &serv.protocol_version.to_string());

                status.write_string(&motd)?;
                conn.write_packet(&status)?;
            } else if packet.id == 0x01 {
                let mut status = Packet::empty(0x01);
                status.write_long(packet.read_long()?)?;
                conn.write_packet(&status)?;
            }
        } else if packet.id == 0x00 {
            let protocol_version = packet.read_i32_varint()?;
            let server_address = packet.read_string()?;
            let server_port = packet.read_unsigned_short()?;
            let next_state = packet.read_u8_varint()?;

            if next_state != 1 { break; }

            println!("Client handshake info:");
            println!("  IP: {}", conn.stream.peer_addr().unwrap());
            println!("  Protocol version: {}", protocol_version);
            println!("  Server address: {}", server_address);
            println!("  Server port: {}", server_port);

            handshake = true;
        } else {
            break;
        }
    }

    conn.close();

    Ok(())
}
source

fn read_boolean(&mut self) -> Result<bool, ProtocolError>

Read Boolean

source

fn read_short(&mut self) -> Result<i16, ProtocolError>

Read Short as i16

source

fn read_long(&mut self) -> Result<i64, ProtocolError>

Read Long as i64

Examples found in repository?
examples/status_server.rs (line 58)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
fn accept_client(mut conn: MCConnTcp, server: Arc<Mutex<MinecraftServer>>) -> Result<(), ProtocolError> {
    let mut handshake = false;
    
    loop {
        let mut packet = match conn.read_packet() {
            Ok(i) => i,
            Err(_) => { 
                break; 
            },
        };

        if handshake {
            if packet.id == 0x00 {
                let mut status = Packet::empty(0x00);

                let serv = server.lock().unwrap();

                let motd = serv.motd.clone();
                let motd = motd.replace(
                    "PROTOCOL_VERSION", 
                    &serv.protocol_version.to_string());

                status.write_string(&motd)?;
                conn.write_packet(&status)?;
            } else if packet.id == 0x01 {
                let mut status = Packet::empty(0x01);
                status.write_long(packet.read_long()?)?;
                conn.write_packet(&status)?;
            }
        } else if packet.id == 0x00 {
            let protocol_version = packet.read_i32_varint()?;
            let server_address = packet.read_string()?;
            let server_port = packet.read_unsigned_short()?;
            let next_state = packet.read_u8_varint()?;

            if next_state != 1 { break; }

            println!("Client handshake info:");
            println!("  IP: {}", conn.stream.peer_addr().unwrap());
            println!("  Protocol version: {}", protocol_version);
            println!("  Server address: {}", server_address);
            println!("  Server port: {}", server_port);

            handshake = true;
        } else {
            break;
        }
    }

    conn.close();

    Ok(())
}
source

fn read_float(&mut self) -> Result<f32, ProtocolError>

Read Float as f32

source

fn read_double(&mut self) -> Result<f64, ProtocolError>

Read Double as f64

source

fn read_int(&mut self) -> Result<i32, ProtocolError>

Read Int as i32

source

fn read_uuid(&mut self) -> Result<Uuid, ProtocolError>

Read UUID

source

fn read_usize_varint_size(&mut self) -> Result<(usize, usize), ProtocolError>

Read VarInt as usize with size in bytes (varint, size)

source

fn read_u8_varint_size(&mut self) -> Result<(u8, u8), ProtocolError>

Read VarInt as u8 with size in bytes (varint, size)

source

fn read_u16_varint_size(&mut self) -> Result<(u16, u16), ProtocolError>

Read VarInt as u16 with size in bytes (varint, size)

source

fn read_u32_varint_size(&mut self) -> Result<(u32, u32), ProtocolError>

Read VarInt as u32 with size in bytes (varint, size)

source

fn read_u64_varint_size(&mut self) -> Result<(u64, u64), ProtocolError>

Read VarInt as u64 with size in bytes (varint, size)

source

fn read_u128_varint_size(&mut self) -> Result<(u128, u128), ProtocolError>

Read VarInt as u128 with size in bytes (varint, size)

source

fn read_isize_varint_size(&mut self) -> Result<(isize, isize), ProtocolError>

Read VarInt as isize with size in bytes (varint, size)

source

fn read_i8_varint_size(&mut self) -> Result<(i8, i8), ProtocolError>

Read VarInt as i8 with size in bytes (varint, size)

source

fn read_i16_varint_size(&mut self) -> Result<(i16, i16), ProtocolError>

Read VarInt as i16 with size in bytes (varint, size)

source

fn read_i32_varint_size(&mut self) -> Result<(i32, i32), ProtocolError>

Read VarInt as i32 with size in bytes (varint, size)

source

fn read_i64_varint_size(&mut self) -> Result<(i64, i64), ProtocolError>

Read VarInt as i64 with size in bytes (varint, size)

source

fn read_i128_varint_size(&mut self) -> Result<(i128, i128), ProtocolError>

Read VarInt as i128 with size in bytes (varint, size)

source

fn read_usize_varint(&mut self) -> Result<usize, ProtocolError>

Read VarInt as usize

source

fn read_u8_varint(&mut self) -> Result<u8, ProtocolError>

Read VarInt as u8

Examples found in repository?
examples/status_server.rs (line 65)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
fn accept_client(mut conn: MCConnTcp, server: Arc<Mutex<MinecraftServer>>) -> Result<(), ProtocolError> {
    let mut handshake = false;
    
    loop {
        let mut packet = match conn.read_packet() {
            Ok(i) => i,
            Err(_) => { 
                break; 
            },
        };

        if handshake {
            if packet.id == 0x00 {
                let mut status = Packet::empty(0x00);

                let serv = server.lock().unwrap();

                let motd = serv.motd.clone();
                let motd = motd.replace(
                    "PROTOCOL_VERSION", 
                    &serv.protocol_version.to_string());

                status.write_string(&motd)?;
                conn.write_packet(&status)?;
            } else if packet.id == 0x01 {
                let mut status = Packet::empty(0x01);
                status.write_long(packet.read_long()?)?;
                conn.write_packet(&status)?;
            }
        } else if packet.id == 0x00 {
            let protocol_version = packet.read_i32_varint()?;
            let server_address = packet.read_string()?;
            let server_port = packet.read_unsigned_short()?;
            let next_state = packet.read_u8_varint()?;

            if next_state != 1 { break; }

            println!("Client handshake info:");
            println!("  IP: {}", conn.stream.peer_addr().unwrap());
            println!("  Protocol version: {}", protocol_version);
            println!("  Server address: {}", server_address);
            println!("  Server port: {}", server_port);

            handshake = true;
        } else {
            break;
        }
    }

    conn.close();

    Ok(())
}
source

fn read_u16_varint(&mut self) -> Result<u16, ProtocolError>

Read VarInt as u16

source

fn read_u32_varint(&mut self) -> Result<u32, ProtocolError>

Read VarInt as u32

source

fn read_u64_varint(&mut self) -> Result<u64, ProtocolError>

Read VarInt as u64

source

fn read_u128_varint(&mut self) -> Result<u128, ProtocolError>

Read VarInt as u128

source

fn read_isize_varint(&mut self) -> Result<isize, ProtocolError>

Read VarInt as isize

source

fn read_i8_varint(&mut self) -> Result<i8, ProtocolError>

Read VarInt as i8

source

fn read_i16_varint(&mut self) -> Result<i16, ProtocolError>

Read VarInt as i16

source

fn read_i32_varint(&mut self) -> Result<i32, ProtocolError>

Read VarInt as i32

Examples found in repository?
examples/status_server.rs (line 62)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
fn accept_client(mut conn: MCConnTcp, server: Arc<Mutex<MinecraftServer>>) -> Result<(), ProtocolError> {
    let mut handshake = false;
    
    loop {
        let mut packet = match conn.read_packet() {
            Ok(i) => i,
            Err(_) => { 
                break; 
            },
        };

        if handshake {
            if packet.id == 0x00 {
                let mut status = Packet::empty(0x00);

                let serv = server.lock().unwrap();

                let motd = serv.motd.clone();
                let motd = motd.replace(
                    "PROTOCOL_VERSION", 
                    &serv.protocol_version.to_string());

                status.write_string(&motd)?;
                conn.write_packet(&status)?;
            } else if packet.id == 0x01 {
                let mut status = Packet::empty(0x01);
                status.write_long(packet.read_long()?)?;
                conn.write_packet(&status)?;
            }
        } else if packet.id == 0x00 {
            let protocol_version = packet.read_i32_varint()?;
            let server_address = packet.read_string()?;
            let server_port = packet.read_unsigned_short()?;
            let next_state = packet.read_u8_varint()?;

            if next_state != 1 { break; }

            println!("Client handshake info:");
            println!("  IP: {}", conn.stream.peer_addr().unwrap());
            println!("  Protocol version: {}", protocol_version);
            println!("  Server address: {}", server_address);
            println!("  Server port: {}", server_port);

            handshake = true;
        } else {
            break;
        }
    }

    conn.close();

    Ok(())
}
source

fn read_i64_varint(&mut self) -> Result<i64, ProtocolError>

Read VarInt as i64

source

fn read_i128_varint(&mut self) -> Result<i128, ProtocolError>

Read VarInt as i128

Implementors§