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
use ClientState;
use clientbound::ClientboundPacket;
use connection::Connection;
use errors::Result;
use serverbound::ServerboundPacket;
use std::net::TcpStream;
use std::borrow::Borrow;
/// Represents a single client connection, from the point of view of a server
pub struct Server {
conn: Connection<ServerboundPacket, ClientboundPacket>,
}
impl Server {
/// Create a new connection from an existing TcpStream
pub fn from_tcpstream(stream: TcpStream) -> Result<Self> {
Ok(Server {
conn: Connection::from_tcpstream(stream)?,
})
}
/// Try to read some packets from the client.
///
/// This function is nonblocking.
pub fn read(&mut self) -> Result<Vec<ServerboundPacket>> {
self.update_inbuf()?;
let mut ret = Vec::new();
loop {
if let Some(packet) = self.read_packet()? {
ret.push(packet);
} else {
break;
}
}
Ok(ret)
}
/// Send the given packet
///
/// This adds the packet to the outgoing buffer, and sends as much as is
/// possible. Returns the length of the outgoing buffer. If this is greater
/// than 0, you will need to call write() to send the remaining data.
pub fn send<T: Borrow<ClientboundPacket>>(&mut self, packet: T) -> Result<usize> {
self.conn.send(packet.borrow())
}
/// Write from the outgoing buffer to the TcpStream
///
/// Returns the amount of bytes written.
pub fn write(&mut self) -> Result<usize> {
self.conn.write()
}
/// Attempt to close this connection, disconnecting the client
///
/// All future sends and reads to this connection will fail.
pub fn close(&mut self) -> Result<()> {
self.conn.close()
}
/// Change the client state of this connection
pub fn set_clientstate(&mut self, new_state: ClientState) {
self.conn.set_clientstate(new_state)
}
/// Enable encryption with the given key.
///
/// It is an error to enable encryption if encryption has already been
/// enabled.
pub fn enable_encryption(&mut self, key: &[u8; 16]) {
self.conn.enable_encryption(key)
}
/// Enable compression.
///
/// It is generally an error to enable compression if compression has
/// already been enabled.
pub fn enable_compression(&mut self, threshold: usize) {
self.conn.enable_compression(threshold)
}
/// Read from the TcpStream and update the incoming buffer.
///
/// This is the only way to actually read from the TcpStream. Unless you
/// know for sure you need to call this, then you do not need to call this.
/// I.e. if you're just using server.read(), then you do not need to call
/// this function.
///
/// This function is nonblocking.
pub fn update_inbuf(&mut self) -> Result<()> {
self.conn.update_inbuf()
}
/// Read a single packet from the internal buffer.
///
/// This is only really useful if you want finegrained control over the
/// processing of packets, e.g. if you want to authenticate clients. In
/// most cases you'll just
/// want to use server.read()
///
/// You MUST be sure that server.update_inbuf() has been called before this,
/// this function will not attempt to read from the TcpStream, only from the
/// internal buffer.
pub fn read_packet(&mut self) -> Result<Option<ServerboundPacket>> {
self.conn.read_packet()
}
}