set nonblocking and better test
This commit is contained in:
parent
66c17dd6e8
commit
e346dcd89f
@ -11,10 +11,10 @@ version = "0.1.16"
|
|||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
flate2 = "1.0.32"
|
flate2 = "1.0.34"
|
||||||
bytebuffer = "2.3.0"
|
bytebuffer = "2.3.0"
|
||||||
uuid = "1.10.0"
|
uuid = "1.11.0"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = [ "atomic_compression" ]
|
default = []
|
||||||
atomic_compression = []
|
atomic_compression = []
|
@ -11,7 +11,7 @@ rust_mc_proto = { git = "https://github.com/MeexReay/rust_mc_proto" } # unstable
|
|||||||
```
|
```
|
||||||
|
|
||||||
Features:
|
Features:
|
||||||
- `atomic_compression` (default)
|
- `atomic_compression`
|
||||||
|
|
||||||
## How to use
|
## How to use
|
||||||
|
|
||||||
|
@ -85,6 +85,10 @@ impl MinecraftConnection<TcpStream> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn set_nonblocking(&mut self, state: bool) {
|
||||||
|
self.stream.set_nonblocking(state).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
/// Close TcpStream
|
/// Close TcpStream
|
||||||
pub fn close(&self) {
|
pub fn close(&self) {
|
||||||
let _ = self.stream.shutdown(std::net::Shutdown::Both);
|
let _ = self.stream.shutdown(std::net::Shutdown::Both);
|
||||||
|
138
src/tests.rs
138
src/tests.rs
@ -1,27 +1,10 @@
|
|||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use std::{net::TcpListener, thread};
|
use std::{net::TcpListener, thread};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_compression_server_client() -> Result<(), ProtocolError> {
|
fn test_data_transfer() -> Result<(), ProtocolError> {
|
||||||
fn test(first_text: &str) -> Result<bool, ProtocolError> {
|
|
||||||
let Ok(mut conn) = MCConnTcp::connect("localhost:44447") else {
|
|
||||||
return test(first_text);
|
|
||||||
};
|
|
||||||
conn.set_compression(Some(5));
|
|
||||||
|
|
||||||
let mut packet = Packet::empty(0x12);
|
|
||||||
packet.write_string(first_text)?;
|
|
||||||
conn.write_packet(&packet)?;
|
|
||||||
|
|
||||||
println!("[c -> s] sent packet with text \"{}\"", first_text);
|
|
||||||
|
|
||||||
let mut packet = conn.read_packet()?;
|
|
||||||
let text = packet.read_string()?;
|
|
||||||
|
|
||||||
println!("[c <- s] read packet with text \"{}\"", text);
|
|
||||||
|
|
||||||
Ok(packet.id() == 0x12 && text == first_text)
|
|
||||||
}
|
|
||||||
|
|
||||||
thread::spawn(move || -> Result<(), ProtocolError> {
|
thread::spawn(move || -> Result<(), ProtocolError> {
|
||||||
let listener =
|
let listener =
|
||||||
@ -29,25 +12,128 @@ fn test_compression_server_client() -> Result<(), ProtocolError> {
|
|||||||
|
|
||||||
for stream in listener.incoming() {
|
for stream in listener.incoming() {
|
||||||
let mut stream = MCConnTcp::new(stream.or(Err(ProtocolError::StreamConnectError))?);
|
let mut stream = MCConnTcp::new(stream.or(Err(ProtocolError::StreamConnectError))?);
|
||||||
|
|
||||||
stream.set_compression(Some(5));
|
stream.set_compression(Some(5));
|
||||||
|
|
||||||
let mut packet = stream.read_packet()?;
|
let mut packet = stream.read_packet()?;
|
||||||
let text = packet.read_string()?;
|
|
||||||
println!("[s <- c] read packet with text \"{}\"", text);
|
stream.write_packet(&Packet::build(packet.id(), |pack| {
|
||||||
stream.write_packet(&packet)?;
|
pack.write_boolean(packet.read_boolean()?)?;
|
||||||
println!("[s -> c] sent packet with text \"{}\"", text);
|
pack.write_byte(packet.read_byte()?)?;
|
||||||
|
pack.write_bytes(&packet.read_bytes(10)?)?;
|
||||||
|
pack.write_double(packet.read_double()?)?;
|
||||||
|
pack.write_float(packet.read_float()?)?;
|
||||||
|
pack.write_i128_varint(packet.read_i128_varint()?)?;
|
||||||
|
pack.write_u128_varint(packet.read_u128_varint()?)?;
|
||||||
|
pack.write_int(packet.read_int()?)?;
|
||||||
|
pack.write_long(packet.read_long()?)?;
|
||||||
|
pack.write_short(packet.read_short()?)?;
|
||||||
|
pack.write_uuid(&packet.read_uuid()?)?;
|
||||||
|
pack.write_string(&packet.read_string()?)?;
|
||||||
|
Ok(())
|
||||||
|
})?)?;
|
||||||
|
|
||||||
|
stream.set_compression(None);
|
||||||
|
|
||||||
|
let mut packet = stream.read_packet()?;
|
||||||
|
|
||||||
|
stream.write_packet(&Packet::build(packet.id(), |pack| {
|
||||||
|
pack.write_boolean(packet.read_boolean()?)?;
|
||||||
|
pack.write_byte(packet.read_byte()?)?;
|
||||||
|
pack.write_bytes(&packet.read_bytes(10)?)?;
|
||||||
|
pack.write_double(packet.read_double()?)?;
|
||||||
|
pack.write_float(packet.read_float()?)?;
|
||||||
|
pack.write_i128_varint(packet.read_i128_varint()?)?;
|
||||||
|
pack.write_u128_varint(packet.read_u128_varint()?)?;
|
||||||
|
pack.write_int(packet.read_int()?)?;
|
||||||
|
pack.write_long(packet.read_long()?)?;
|
||||||
|
pack.write_short(packet.read_short()?)?;
|
||||||
|
pack.write_uuid(&packet.read_uuid()?)?;
|
||||||
|
pack.write_string(&packet.read_string()?)?;
|
||||||
|
Ok(())
|
||||||
|
})?)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let conn = MCConnTcp::connect("localhost:44447");
|
||||||
|
|
||||||
assert!(test("12bcvf756iuyu,.,.")? && test("a")?);
|
while let Err(_) = conn {}
|
||||||
|
|
||||||
|
let mut conn = conn?;
|
||||||
|
|
||||||
|
conn.set_compression(Some(5));
|
||||||
|
|
||||||
|
conn.write_packet(&Packet::build(0xfe, |pack| {
|
||||||
|
pack.write_boolean(true)?;
|
||||||
|
pack.write_byte(0x12)?;
|
||||||
|
pack.write_bytes(&vec![0x01, 0x56, 0x47, 0x48, 0xf5, 0xc2, 0x45, 0x98, 0xde, 0x99])?;
|
||||||
|
pack.write_double(123456789.123456789f64)?;
|
||||||
|
pack.write_float(789456.44422f32)?;
|
||||||
|
pack.write_i128_varint(468927513325566)?;
|
||||||
|
pack.write_u128_varint(99859652365236523)?;
|
||||||
|
pack.write_int(77861346i32)?;
|
||||||
|
pack.write_long(789465123545678946i64)?;
|
||||||
|
pack.write_short(1233i16)?;
|
||||||
|
pack.write_uuid(&Uuid::try_parse("550e8400-e29b-41d4-a716-446655440000").map_err(|_| ProtocolError::CloneError)?)?;
|
||||||
|
pack.write_string("&packet.read_string()?")?;
|
||||||
|
Ok(())
|
||||||
|
})?)?;
|
||||||
|
|
||||||
|
let mut packet = conn.read_packet()?;
|
||||||
|
|
||||||
|
assert_eq!(packet.read_boolean()?, true);
|
||||||
|
assert_eq!(packet.read_byte()?, 0x12);
|
||||||
|
assert_eq!(packet.read_bytes(10)?, vec![0x01, 0x56, 0x47, 0x48, 0xf5, 0xc2, 0x45, 0x98, 0xde, 0x99]);
|
||||||
|
assert_eq!(packet.read_double()?, 123456789.123456789f64);
|
||||||
|
assert_eq!(packet.read_float()?, 789456.44422f32);
|
||||||
|
assert_eq!(packet.read_i128_varint()?, 468927513325566);
|
||||||
|
assert_eq!(packet.read_u128_varint()?, 99859652365236523);
|
||||||
|
assert_eq!(packet.read_int()?, 77861346i32);
|
||||||
|
assert_eq!(packet.read_long()?, 789465123545678946i64);
|
||||||
|
assert_eq!(packet.read_short()?, 1233i16);
|
||||||
|
assert_eq!(packet.read_uuid()?, Uuid::try_parse("550e8400-e29b-41d4-a716-446655440000").map_err(|_| ProtocolError::CloneError)?);
|
||||||
|
assert_eq!(packet.read_string()?, "&packet.read_string()?");
|
||||||
|
|
||||||
|
conn.set_compression(None);
|
||||||
|
|
||||||
|
conn.write_packet(&Packet::build(0xfe, |pack| {
|
||||||
|
pack.write_boolean(true)?;
|
||||||
|
pack.write_byte(0x12)?;
|
||||||
|
pack.write_bytes(&vec![0x01, 0x56, 0x47, 0x48, 0xf5, 0xc2, 0x45, 0x98, 0xde, 0x99])?;
|
||||||
|
pack.write_double(123456789.123456789f64)?;
|
||||||
|
pack.write_float(789456.44422f32)?;
|
||||||
|
pack.write_i128_varint(468927513325566)?;
|
||||||
|
pack.write_u128_varint(99859652365236523)?;
|
||||||
|
pack.write_int(77861346i32)?;
|
||||||
|
pack.write_long(789465123545678946i64)?;
|
||||||
|
pack.write_short(1233i16)?;
|
||||||
|
pack.write_uuid(&Uuid::try_parse("550e8400-e29b-41d4-a716-446655440000").map_err(|_| ProtocolError::CloneError)?)?;
|
||||||
|
pack.write_string("&packet.read_string()?")?;
|
||||||
|
Ok(())
|
||||||
|
})?)?;
|
||||||
|
|
||||||
|
let mut packet = conn.read_packet()?;
|
||||||
|
|
||||||
|
assert_eq!(packet.read_boolean()?, true);
|
||||||
|
assert_eq!(packet.read_byte()?, 0x12);
|
||||||
|
assert_eq!(packet.read_bytes(10)?, vec![0x01, 0x56, 0x47, 0x48, 0xf5, 0xc2, 0x45, 0x98, 0xde, 0x99]);
|
||||||
|
assert_eq!(packet.read_double()?, 123456789.123456789f64);
|
||||||
|
assert_eq!(packet.read_float()?, 789456.44422f32);
|
||||||
|
assert_eq!(packet.read_i128_varint()?, 468927513325566);
|
||||||
|
assert_eq!(packet.read_u128_varint()?, 99859652365236523);
|
||||||
|
assert_eq!(packet.read_int()?, 77861346i32);
|
||||||
|
assert_eq!(packet.read_long()?, 789465123545678946i64);
|
||||||
|
assert_eq!(packet.read_short()?, 1233i16);
|
||||||
|
assert_eq!(packet.read_uuid()?, Uuid::try_parse("550e8400-e29b-41d4-a716-446655440000").map_err(|_| ProtocolError::CloneError)?);
|
||||||
|
assert_eq!(packet.read_string()?, "&packet.read_string()?");
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_compression_atomic_bytebuffer() -> Result<(), ProtocolError> {
|
fn test_compression() -> Result<(), ProtocolError> {
|
||||||
let mut conn = MCConn::new(ByteBuffer::new());
|
let mut conn = MCConn::new(ByteBuffer::new());
|
||||||
conn.set_compression(Some(5));
|
conn.set_compression(Some(5));
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user