aboutsummaryrefslogtreecommitdiff
path: root/src/tcp.rs
blob: 37a94c5f14708f36da7b1eca16a7a06c08f5da56 (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
use crate::connection::CraftConnection;
use crate::reader::CraftReader;
use crate::writer::CraftWriter;
use mcproto_rs::protocol::{PacketDirection, State};
use std::io::BufReader as StdBufReader;
use std::net::TcpStream;

#[cfg(any(feature = "futures-io", feature = "tokio-io"))]
use crate::{CraftAsyncWriter, CraftAsyncReader, IntoBufferedAsyncRead};

pub const BUF_SIZE: usize = 8192;

pub type CraftTcpConnection = CraftConnection<StdBufReader<TcpStream>, TcpStream>;

impl CraftConnection<StdBufReader<TcpStream>, TcpStream> {
    pub fn connect_server_std(to: String) -> Result<Self, std::io::Error> {
        Self::from_std(TcpStream::connect(to)?, PacketDirection::ClientBound)
    }

    pub fn wrap_client_stream_std(stream: TcpStream) -> Result<Self, std::io::Error> {
        Self::from_std(stream, PacketDirection::ServerBound)
    }

    pub fn from_std(
        s1: TcpStream,
        read_direction: PacketDirection,
    ) -> Result<Self, std::io::Error> {
        Self::from_std_with_state(s1, read_direction, State::Handshaking)
    }

    pub fn from_std_with_state(
        s1: TcpStream,
        read_direction: PacketDirection,
        state: State,
    ) -> Result<Self, std::io::Error> {
        let write = s1.try_clone()?;
        let read = StdBufReader::with_capacity(BUF_SIZE, s1);

        Ok(Self {
            reader: CraftReader::wrap_with_state(read, read_direction, state),
            writer: CraftWriter::wrap_with_state(write, read_direction.opposite(), state),
        })
    }
}

#[cfg(any(feature = "futures-io", feature = "tokio-io"))]
impl<R, W> CraftConnection<R, W>
where
    CraftReader<R>: CraftAsyncReader,
    CraftWriter<W>: CraftAsyncWriter,
{
    pub fn from_unbuffered_async<U>(tuple: (U, W), read_direction: PacketDirection) -> Self
    where
        U: IntoBufferedAsyncRead<Target=R>,
    {
        Self::from_unbuffered_async_with_state(tuple, read_direction, State::Handshaking)
    }

    pub fn from_unbuffered_async_with_state<U>(
        tuple: (U, W),
        read_direction: PacketDirection,
        state: State,
    ) -> Self
    where
        U: IntoBufferedAsyncRead<Target=R>,
    {
        let (ru, writer) = tuple;
        let reader = ru.into_buffered(BUF_SIZE);
        Self::from_async_with_state((reader, writer), read_direction, state)
    }

    pub fn from_async(tuple: (R, W), read_direction: PacketDirection) -> Self {
        Self::from_async_with_state(tuple, read_direction, State::Handshaking)
    }

    pub fn from_async_with_state(
        tuple: (R, W),
        read_direction: PacketDirection,
        state: State,
    ) -> Self {
        let (reader, writer) = tuple;
        Self {
            reader: CraftReader::wrap_with_state(reader, read_direction, state),
            writer: CraftWriter::wrap_with_state(writer, read_direction.opposite(), state),
        }
    }
}