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
|
use crate::connection::CraftConnection;
use crate::reader::CraftReader;
use crate::writer::CraftWriter;
use mcproto_rs::protocol::{PacketDirection, State};
use std::convert::TryFrom;
use std::io::BufReader as StdBufReader;
use std::net::TcpStream;
#[cfg(feature = "async")]
use futures::io::{AsyncRead, AsyncWrite, BufReader as AsyncBufReader};
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(feature = "async")]
impl<R, W> CraftConnection<AsyncBufReader<R>, W>
where
R: AsyncRead + Send + Sync + Unpin,
W: AsyncWrite + Send + Sync + Unpin,
{
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;
let reader = AsyncBufReader::with_capacity(BUF_SIZE, reader);
Self {
reader: CraftReader::wrap_with_state(reader, read_direction, state),
writer: CraftWriter::wrap_with_state(writer, read_direction.opposite(), state),
}
}
}
|