aboutsummaryrefslogtreecommitdiff
path: root/minecraft-protocol/src/connect.rs
blob: f6dd9fe7336443161f562e1f423106fdc3a2e68b (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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//! parse sending and receiving packets with a server.

use crate::packets::game::GamePacket;
use crate::packets::handshake::HandshakePacket;
use crate::packets::login::LoginPacket;
use crate::packets::status::StatusPacket;
use crate::read::read_packet;
use crate::write::write_packet;
use crate::ServerIpAddress;
use tokio::net::TcpStream;

pub enum PacketFlow {
    ClientToServer,
    ServerToClient,
}

pub struct HandshakeConnection {
    pub flow: PacketFlow,
    /// The buffered writer
    pub stream: TcpStream,
}

pub struct GameConnection {
    pub flow: PacketFlow,
    /// The buffered writer
    pub stream: TcpStream,
}

pub struct StatusConnection {
    pub flow: PacketFlow,
    /// The buffered writer
    pub stream: TcpStream,
}

pub struct LoginConnection {
    pub flow: PacketFlow,
    /// The buffered writer
    pub stream: TcpStream,
}

impl HandshakeConnection {
    pub async fn new(address: &ServerIpAddress) -> Result<HandshakeConnection, String> {
        let ip = address.ip;
        let port = address.port;

        let stream = TcpStream::connect(format!("{}:{}", ip, port))
            .await
            .map_err(|_| "Failed to connect to server")?;

        // enable tcp_nodelay
        stream
            .set_nodelay(true)
            .expect("Error enabling tcp_nodelay");

        Ok(HandshakeConnection {
            flow: PacketFlow::ServerToClient,
            stream,
        })
    }

    pub fn login(self) -> LoginConnection {
        LoginConnection {
            flow: self.flow,
            stream: self.stream,
        }
    }

    pub fn status(self) -> StatusConnection {
        StatusConnection {
            flow: self.flow,
            stream: self.stream,
        }
    }

    pub async fn read(&mut self) -> Result<HandshakePacket, String> {
        read_packet::<HandshakePacket>(&self.flow, &mut self.stream).await
    }

    /// Write a packet to the server
    pub async fn write(&mut self, packet: HandshakePacket) {
        write_packet(packet, &mut self.stream).await;
    }
}

impl GameConnection {
    pub async fn read(&mut self) -> Result<GamePacket, String> {
        read_packet::<GamePacket>(&self.flow, &mut self.stream).await
    }

    /// Write a packet to the server
    pub async fn write(&mut self, packet: GamePacket) {
        write_packet(packet, &mut self.stream).await;
    }
}

impl StatusConnection {
    pub async fn read(&mut self) -> Result<StatusPacket, String> {
        read_packet::<StatusPacket>(&self.flow, &mut self.stream).await
    }

    /// Write a packet to the server
    pub async fn write(&mut self, packet: StatusPacket) {
        write_packet(packet, &mut self.stream).await;
    }
}

impl LoginConnection {
    pub async fn read(&mut self) -> Result<LoginPacket, String> {
        read_packet::<LoginPacket>(&self.flow, &mut self.stream).await
    }

    /// Write a packet to the server
    pub async fn write(&mut self, packet: LoginPacket) {
        write_packet(packet, &mut self.stream).await;
    }
}