uuid read&write + ver 1.0.6

This commit is contained in:
MeexReay 2024-05-23 00:28:51 +03:00
parent a74aa5856c
commit 75ad081cfe
5 changed files with 18 additions and 41 deletions

View File

@ -7,9 +7,10 @@ license-file = "LICENSE"
readme = "README.md" readme = "README.md"
keywords = ["minecraft", "protocol", "packets", "lightweight"] keywords = ["minecraft", "protocol", "packets", "lightweight"]
version = "0.1.5" version = "0.1.6"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
flate2 = "1.0.30" flate2 = "1.0.30"
bytebuffer = "2.2.0" bytebuffer = "2.2.0"
uuid = "1.8.0"

View File

@ -38,12 +38,12 @@ fn read_status_response(conn: &mut MCConnTcp) -> Result<String, ProtocolError> {
} }
fn main() { fn main() {
let mut conn = MCConnTcp::connect("localhost:25565").unwrap(); let mut conn = MCConnTcp::connect("sloganmc.ru:25565").unwrap();
send_handshake(&mut conn, 765, "localhost", 25565, 1).unwrap(); send_handshake(&mut conn, 765, "sloganmc.ru", 25565, 1).unwrap();
send_status_request(&mut conn).unwrap(); send_status_request(&mut conn).unwrap();
let motd = read_status_response(&mut conn).unwrap(); let motd = read_status_response(&mut conn).unwrap();
println!("Motd: {}", motd); dbg!(motd);
} }

File diff suppressed because one or more lines are too long

View File

@ -3,6 +3,7 @@ use std::net::{TcpStream, ToSocketAddrs};
use flate2::{Compress, Compression, Decompress, FlushCompress, Status, FlushDecompress}; use flate2::{Compress, Compression, Decompress, FlushCompress, Status, FlushDecompress};
use bytebuffer::ByteBuffer; use bytebuffer::ByteBuffer;
use uuid::Uuid;
pub trait Zigzag<T> { fn zigzag(&self) -> T; } pub trait Zigzag<T> { fn zigzag(&self) -> T; }
impl Zigzag<u8> for i8 { fn zigzag(&self) -> u8 { ((self << 1) ^ (self >> 7)) as u8 } } impl Zigzag<u8> for i8 { fn zigzag(&self) -> u8 { ((self << 1) ^ (self >> 7)) as u8 } }
@ -139,6 +140,12 @@ pub trait DataBufferReader {
Err(_) => Err(ProtocolError::ReadError), Err(_) => Err(ProtocolError::ReadError),
} }
} }
fn read_uuid(&mut self) -> Result<Uuid, ProtocolError> {
match self.read_bytes(16)?.try_into() {
Ok(i) => Ok(Uuid::from_u128(u128::from_be_bytes(i))),
Err(_) => Err(ProtocolError::ReadError),
}
}
fn read_usize_varint(&mut self) -> Result<usize, ProtocolError> { read_varint!(usize, self) } fn read_usize_varint(&mut self) -> Result<usize, ProtocolError> { read_varint!(usize, self) }
fn read_u8_varint(&mut self) -> Result<u8, ProtocolError> { read_varint!(u8, self) } fn read_u8_varint(&mut self) -> Result<u8, ProtocolError> { read_varint!(u8, self) }
@ -166,6 +173,9 @@ pub trait DataBufferWriter {
self.write_usize_varint(bytes.len())?; self.write_usize_varint(bytes.len())?;
self.write_bytes(bytes) self.write_bytes(bytes)
} }
fn write_uuid(&mut self, val: &Uuid) -> Result<(), ProtocolError> {
self.write_bytes(&val.as_u128().to_be_bytes())
}
fn write_unsigned_short(&mut self, val: u16) -> Result<(), ProtocolError> { fn write_unsigned_short(&mut self, val: u16) -> Result<(), ProtocolError> {
match self.write_bytes(&val.to_be_bytes()) { match self.write_bytes(&val.to_be_bytes()) {
Ok(_) => Ok(()), Ok(_) => Ok(()),

View File

@ -1,34 +0,0 @@
use std::{net::TcpListener, sync::Arc, thread};
use rust_mc_proto::{DataBufferReader, DataBufferWriter, MCConn, MCConnTcp, MinecraftConnection, Packet, ProtocolError};
/*
Example of simple server that sends motd
to client like an vanilla minecraft server
*/
fn accept_client(mut conn: MCConnTcp) {
loop {
let packet = match conn.read_packet() {
Ok(p) => p,
Err(_) => { break },
};
dbg!(packet);
}
}
fn main() {
let listener = TcpListener::bind("localhost:25565").unwrap();
for stream in listener.incoming() {
let stream = stream.unwrap();
thread::spawn(move || {
accept_client(MinecraftConnection::new(stream));
});
}
}