Make MCTypes and packets generic for AsyncRead and AsyncWrite

This commit is contained in:
Garen Tyler 2021-03-20 14:30:41 -06:00
parent 8d5d0ac338
commit e3541ea38f
4 changed files with 80 additions and 75 deletions

View File

@ -4,8 +4,7 @@ pub use functions::*;
pub use numbers::*; pub use numbers::*;
pub use other::*; pub use other::*;
use serde_json::json; use serde_json::json;
use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
use tokio::net::TcpStream;
// /// Make sure all types can serialize and deserialize to/from `Vec<u8>`. // /// Make sure all types can serialize and deserialize to/from `Vec<u8>`.
// pub trait MCType: Into<Vec<u8>> + TryFrom<Vec<u8>> + Display { // pub trait MCType: Into<Vec<u8>> + TryFrom<Vec<u8>> + Display {
@ -17,13 +16,16 @@ pub mod functions {
use super::*; use super::*;
/// Read a single byte from the given `TcpStream`. /// Read a single byte from the given `TcpStream`.
pub async fn read_byte(t: &mut TcpStream) -> tokio::io::Result<u8> { pub async fn read_byte<T: AsyncRead + Unpin>(t: &mut T) -> tokio::io::Result<u8> {
let mut buffer = [0u8; 1]; let mut buffer = [0u8; 1];
t.read_exact(&mut buffer).await?; t.read_exact(&mut buffer).await?;
Ok(buffer[0]) Ok(buffer[0])
} }
/// Read `l` bytes from the given `TcpStream`. /// Read `l` bytes from the given `TcpStream`.
pub async fn read_bytes(t: &mut TcpStream, l: usize) -> tokio::io::Result<Vec<u8>> { pub async fn read_bytes<T: AsyncRead + Unpin>(
t: &mut T,
l: usize,
) -> tokio::io::Result<Vec<u8>> {
let mut buffer = vec![]; let mut buffer = vec![];
for _ in 0..l { for _ in 0..l {
buffer.push(read_byte(t).await?); buffer.push(read_byte(t).await?);
@ -31,12 +33,15 @@ pub mod functions {
Ok(buffer) Ok(buffer)
} }
/// Write a single byte to the given `TcpStream`. /// Write a single byte to the given `TcpStream`.
pub async fn write_byte(t: &mut TcpStream, value: u8) -> tokio::io::Result<()> { pub async fn write_byte<T: AsyncWrite + Unpin>(t: &mut T, value: u8) -> tokio::io::Result<()> {
t.write(&[value]).await?; t.write(&[value]).await?;
Ok(()) Ok(())
} }
/// Write multiple bytes to the given `TcpStream`. /// Write multiple bytes to the given `TcpStream`.
pub async fn write_bytes(t: &mut TcpStream, bytes: &[u8]) -> tokio::io::Result<()> { pub async fn write_bytes<T: AsyncWrite + Unpin>(
t: &mut T,
bytes: &[u8],
) -> tokio::io::Result<()> {
for b in bytes { for b in bytes {
write_byte(t, *b).await?; write_byte(t, *b).await?;
} }
@ -127,7 +132,7 @@ pub mod other {
} }
} }
impl MCBoolean { impl MCBoolean {
pub async fn read(t: &mut TcpStream) -> tokio::io::Result<MCBoolean> { pub async fn read<T: AsyncRead + Unpin>(t: &mut T) -> tokio::io::Result<MCBoolean> {
let b = read_byte(t).await?; let b = read_byte(t).await?;
Ok(MCBoolean::try_from(vec![b]).unwrap()) Ok(MCBoolean::try_from(vec![b]).unwrap())
} }
@ -212,10 +217,10 @@ pub mod other {
} }
} }
impl MCString { impl MCString {
pub async fn read(t: &mut TcpStream) -> tokio::io::Result<Self> { pub async fn read<T: AsyncRead + Unpin>(t: &mut T) -> tokio::io::Result<Self> {
let str_len = MCVarInt::read(t).await?; let str_len = MCVarInt::read(t).await?;
let mut str_bytes = vec![]; let mut str_bytes = vec![];
for _ in 0..str_len.into() { for _ in 0i32..str_len.into() {
str_bytes.push(read_byte(t).await?); str_bytes.push(read_byte(t).await?);
} }
Ok(MCString { Ok(MCString {
@ -296,7 +301,7 @@ pub mod other {
} }
} }
impl MCChat { impl MCChat {
pub async fn read(_t: &mut TcpStream) -> tokio::io::Result<Self> { pub async fn read<T: AsyncRead + Unpin>(_t: &mut T) -> tokio::io::Result<Self> {
Err(io_error("Cannot read MCChat from stream")) Err(io_error("Cannot read MCChat from stream"))
} }
} }
@ -347,7 +352,7 @@ pub mod other {
z: 0.into(), z: 0.into(),
} }
} }
pub async fn read(_t: &mut TcpStream) -> tokio::io::Result<Self> { pub async fn read<T: AsyncRead + Unpin>(_t: &mut T) -> tokio::io::Result<Self> {
Err(io_error("Cannot read MCPosition from stream")) Err(io_error("Cannot read MCPosition from stream"))
} }
} }
@ -374,7 +379,7 @@ pub mod numbers {
pub value: i8, // -128 to 127 pub value: i8, // -128 to 127
} }
impl MCByte { impl MCByte {
pub async fn read(t: &mut TcpStream) -> tokio::io::Result<MCByte> { pub async fn read<T: AsyncRead + Unpin>(t: &mut T) -> tokio::io::Result<MCByte> {
Ok(MCByte::from_bytes(vec![read_byte(t).await?])) Ok(MCByte::from_bytes(vec![read_byte(t).await?]))
} }
pub fn from_bytes(v: Vec<u8>) -> MCByte { pub fn from_bytes(v: Vec<u8>) -> MCByte {
@ -432,7 +437,7 @@ pub mod numbers {
pub value: u8, // 0 to 255 pub value: u8, // 0 to 255
} }
impl MCUnsignedByte { impl MCUnsignedByte {
pub async fn read(t: &mut TcpStream) -> tokio::io::Result<MCUnsignedByte> { pub async fn read<T: AsyncRead + Unpin>(t: &mut T) -> tokio::io::Result<MCUnsignedByte> {
Ok(MCUnsignedByte::from_bytes(vec![read_byte(t).await?])) Ok(MCUnsignedByte::from_bytes(vec![read_byte(t).await?]))
} }
pub fn from_bytes(v: Vec<u8>) -> MCUnsignedByte { pub fn from_bytes(v: Vec<u8>) -> MCUnsignedByte {
@ -490,7 +495,7 @@ pub mod numbers {
pub value: i16, // -32768 to 32767 pub value: i16, // -32768 to 32767
} }
impl MCShort { impl MCShort {
pub async fn read(t: &mut TcpStream) -> tokio::io::Result<MCShort> { pub async fn read<T: AsyncRead + Unpin>(t: &mut T) -> tokio::io::Result<MCShort> {
let mut bytes = Vec::new(); let mut bytes = Vec::new();
bytes.push(read_byte(t).await?); // MSD bytes.push(read_byte(t).await?); // MSD
bytes.push(read_byte(t).await?); // LSD bytes.push(read_byte(t).await?); // LSD
@ -553,7 +558,7 @@ pub mod numbers {
pub value: u16, // 0 to 65535 pub value: u16, // 0 to 65535
} }
impl MCUnsignedShort { impl MCUnsignedShort {
pub async fn read(t: &mut TcpStream) -> tokio::io::Result<MCUnsignedShort> { pub async fn read<T: AsyncRead + Unpin>(t: &mut T) -> tokio::io::Result<MCUnsignedShort> {
let mut bytes = Vec::new(); let mut bytes = Vec::new();
bytes.push(read_byte(t).await?); // MSD bytes.push(read_byte(t).await?); // MSD
bytes.push(read_byte(t).await?); // LSD bytes.push(read_byte(t).await?); // LSD
@ -616,7 +621,7 @@ pub mod numbers {
pub value: i32, // -2147483648 to 2147483647 pub value: i32, // -2147483648 to 2147483647
} }
impl MCInt { impl MCInt {
pub async fn read(t: &mut TcpStream) -> tokio::io::Result<MCInt> { pub async fn read<T: AsyncRead + Unpin>(t: &mut T) -> tokio::io::Result<MCInt> {
let mut bytes = Vec::new(); let mut bytes = Vec::new();
for _ in 0..4 { for _ in 0..4 {
bytes.push(read_byte(t).await?); bytes.push(read_byte(t).await?);
@ -680,7 +685,7 @@ pub mod numbers {
pub value: u32, // 0 to 4294967295 pub value: u32, // 0 to 4294967295
} }
impl MCUnsignedInt { impl MCUnsignedInt {
pub async fn read(t: &mut TcpStream) -> tokio::io::Result<MCUnsignedInt> { pub async fn read<T: AsyncRead + Unpin>(t: &mut T) -> tokio::io::Result<MCUnsignedInt> {
let mut bytes = Vec::new(); let mut bytes = Vec::new();
for _ in 0..4 { for _ in 0..4 {
bytes.push(read_byte(t).await?); bytes.push(read_byte(t).await?);
@ -744,7 +749,7 @@ pub mod numbers {
pub value: i64, // -9223372036854775808 to 9223372036854775807 pub value: i64, // -9223372036854775808 to 9223372036854775807
} }
impl MCLong { impl MCLong {
pub async fn read(t: &mut TcpStream) -> tokio::io::Result<MCLong> { pub async fn read<T: AsyncRead + Unpin>(t: &mut T) -> tokio::io::Result<MCLong> {
let mut bytes = Vec::new(); let mut bytes = Vec::new();
for _ in 0..8 { for _ in 0..8 {
bytes.push(read_byte(t).await?); bytes.push(read_byte(t).await?);
@ -808,7 +813,7 @@ pub mod numbers {
pub value: u64, // 0 to 18446744073709551615 pub value: u64, // 0 to 18446744073709551615
} }
impl MCUnsignedLong { impl MCUnsignedLong {
pub async fn read(t: &mut TcpStream) -> tokio::io::Result<MCUnsignedLong> { pub async fn read<T: AsyncRead + Unpin>(t: &mut T) -> tokio::io::Result<MCUnsignedLong> {
let mut bytes = Vec::new(); let mut bytes = Vec::new();
for _ in 0..8 { for _ in 0..8 {
bytes.push(read_byte(t).await?); bytes.push(read_byte(t).await?);
@ -872,7 +877,7 @@ pub mod numbers {
pub value: f32, // 32-bit floating point number pub value: f32, // 32-bit floating point number
} }
impl MCFloat { impl MCFloat {
pub async fn read(t: &mut TcpStream) -> tokio::io::Result<MCFloat> { pub async fn read<T: AsyncRead + Unpin>(t: &mut T) -> tokio::io::Result<MCFloat> {
let mut bytes = Vec::new(); let mut bytes = Vec::new();
for _ in 0..4 { for _ in 0..4 {
bytes.push(read_byte(t).await?); bytes.push(read_byte(t).await?);
@ -936,7 +941,7 @@ pub mod numbers {
pub value: f64, // 64-bit floating point number pub value: f64, // 64-bit floating point number
} }
impl MCDouble { impl MCDouble {
pub async fn read(t: &mut TcpStream) -> tokio::io::Result<MCDouble> { pub async fn read<T: AsyncRead + Unpin>(t: &mut T) -> tokio::io::Result<MCDouble> {
let mut bytes = Vec::new(); let mut bytes = Vec::new();
for _ in 0..8 { for _ in 0..8 {
bytes.push(read_byte(t).await?); bytes.push(read_byte(t).await?);
@ -1000,7 +1005,7 @@ pub mod numbers {
pub value: i32, // Variable length 32-bit integer pub value: i32, // Variable length 32-bit integer
} }
impl MCVarInt { impl MCVarInt {
pub async fn read(t: &mut TcpStream) -> tokio::io::Result<MCVarInt> { pub async fn read<T: AsyncRead + Unpin>(t: &mut T) -> tokio::io::Result<MCVarInt> {
let mut num_read = 0; let mut num_read = 0;
let mut result = 0i32; let mut result = 0i32;
let mut read = 0u8; let mut read = 0u8;

View File

@ -2,7 +2,7 @@ use super::PacketCommon;
use crate::mctypes::*; use crate::mctypes::*;
use crate::CONFIG; use crate::CONFIG;
use std::convert::{Into, TryFrom}; use std::convert::{Into, TryFrom};
use tokio::net::TcpStream; use tokio::io::{AsyncRead, AsyncWrite};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct StatusResponse { pub struct StatusResponse {
@ -34,12 +34,12 @@ impl PacketCommon for StatusResponse {
fn id() -> u8 { fn id() -> u8 {
0x00 0x00
} }
async fn read(t: &'_ mut TcpStream) -> tokio::io::Result<Self> { async fn read<T: AsyncRead + Unpin + Send>(t: &mut T) -> tokio::io::Result<Self> {
let mut statusresponse = StatusResponse::new(); let mut statusresponse = StatusResponse::new();
statusresponse.json_response = MCString::read(t).await?; statusresponse.json_response = MCString::read(t).await?;
Ok(statusresponse) Ok(statusresponse)
} }
async fn write(&self, t: &'_ mut TcpStream) -> tokio::io::Result<()> { async fn write<T: AsyncWrite + Unpin + Send>(&self, t: &mut T) -> tokio::io::Result<()> {
for b in Into::<Vec<u8>>::into(self.clone()) { for b in Into::<Vec<u8>>::into(self.clone()) {
write_byte(t, b).await?; write_byte(t, b).await?;
} }
@ -75,12 +75,12 @@ impl PacketCommon for StatusPong {
fn id() -> u8 { fn id() -> u8 {
0x01 0x01
} }
async fn read(t: &mut TcpStream) -> tokio::io::Result<Self> { async fn read<T: AsyncRead + Unpin + Send>(t: &mut T) -> tokio::io::Result<Self> {
let mut statuspong = StatusPong::new(); let mut statuspong = StatusPong::new();
statuspong.payload = MCLong::read(t).await?; statuspong.payload = MCLong::read(t).await?;
Ok(statuspong) Ok(statuspong)
} }
async fn write(&self, t: &mut TcpStream) -> tokio::io::Result<()> { async fn write<T: AsyncWrite + Unpin + Send>(&self, t: &mut T) -> tokio::io::Result<()> {
for b in Into::<Vec<u8>>::into(self.clone()) { for b in Into::<Vec<u8>>::into(self.clone()) {
write_byte(t, b).await?; write_byte(t, b).await?;
} }
@ -121,13 +121,13 @@ impl PacketCommon for LoginSuccess {
fn id() -> u8 { fn id() -> u8 {
0x02 0x02
} }
async fn read(t: &mut TcpStream) -> tokio::io::Result<Self> { async fn read<T: AsyncRead + Unpin + Send>(t: &mut T) -> tokio::io::Result<Self> {
let mut loginsuccess = LoginSuccess::new(); let mut loginsuccess = LoginSuccess::new();
loginsuccess.uuid = MCString::read(t).await?; loginsuccess.uuid = MCString::read(t).await?;
loginsuccess.username = MCString::read(t).await?; loginsuccess.username = MCString::read(t).await?;
Ok(loginsuccess) Ok(loginsuccess)
} }
async fn write(&self, t: &mut TcpStream) -> tokio::io::Result<()> { async fn write<T: AsyncWrite + Unpin + Send>(&self, t: &mut T) -> tokio::io::Result<()> {
for b in Into::<Vec<u8>>::into(self.clone()) { for b in Into::<Vec<u8>>::into(self.clone()) {
write_byte(t, b).await?; write_byte(t, b).await?;
} }
@ -167,14 +167,14 @@ impl PacketCommon for LoginDisconnect {
fn id() -> u8 { fn id() -> u8 {
0x00 0x00
} }
async fn read(t: &mut TcpStream) -> tokio::io::Result<Self> { async fn read<T: AsyncRead + Unpin + Send>(t: &mut T) -> tokio::io::Result<Self> {
let mut logindisconnect = LoginDisconnect::new(); let mut logindisconnect = LoginDisconnect::new();
logindisconnect.reason = MCChat { logindisconnect.reason = MCChat {
text: MCString::read(t).await?, text: MCString::read(t).await?,
}; };
Ok(logindisconnect) Ok(logindisconnect)
} }
async fn write(&self, t: &mut TcpStream) -> tokio::io::Result<()> { async fn write<T: AsyncWrite + Unpin + Send>(&self, t: &mut T) -> tokio::io::Result<()> {
for b in Into::<Vec<u8>>::into(self.clone()) { for b in Into::<Vec<u8>>::into(self.clone()) {
write_byte(t, b).await?; write_byte(t, b).await?;
} }
@ -230,7 +230,7 @@ impl PacketCommon for JoinGame {
fn id() -> u8 { fn id() -> u8 {
0x01 0x01
} }
async fn read(t: &mut TcpStream) -> tokio::io::Result<Self> { async fn read<T: AsyncRead + Unpin + Send>(t: &mut T) -> tokio::io::Result<Self> {
let mut joingame = JoinGame::new(); let mut joingame = JoinGame::new();
joingame.entity_id = MCInt::read(t).await?; joingame.entity_id = MCInt::read(t).await?;
joingame.gamemode = MCUnsignedByte::read(t).await?; joingame.gamemode = MCUnsignedByte::read(t).await?;
@ -241,7 +241,7 @@ impl PacketCommon for JoinGame {
joingame.reduced_debug_info = MCBoolean::read(t).await?; joingame.reduced_debug_info = MCBoolean::read(t).await?;
Ok(joingame) Ok(joingame)
} }
async fn write(&self, t: &mut TcpStream) -> tokio::io::Result<()> { async fn write<T: AsyncWrite + Unpin + Send>(&self, t: &mut T) -> tokio::io::Result<()> {
for b in Into::<Vec<u8>>::into(self.clone()) { for b in Into::<Vec<u8>>::into(self.clone()) {
write_byte(t, b).await?; write_byte(t, b).await?;
} }
@ -279,12 +279,12 @@ impl PacketCommon for HeldItemChange {
fn id() -> u8 { fn id() -> u8 {
0x09 0x09
} }
async fn read(t: &mut TcpStream) -> tokio::io::Result<Self> { async fn read<T: AsyncRead + Unpin + Send>(t: &mut T) -> tokio::io::Result<Self> {
let mut helditemchange = HeldItemChange::new(); let mut helditemchange = HeldItemChange::new();
helditemchange.selected_slot = MCByte::read(t).await?; helditemchange.selected_slot = MCByte::read(t).await?;
Ok(helditemchange) Ok(helditemchange)
} }
async fn write(&self, t: &mut TcpStream) -> tokio::io::Result<()> { async fn write<T: AsyncWrite + Unpin + Send>(&self, t: &mut T) -> tokio::io::Result<()> {
for b in Into::<Vec<u8>>::into(self.clone()) { for b in Into::<Vec<u8>>::into(self.clone()) {
write_byte(t, b).await?; write_byte(t, b).await?;
} }
@ -348,13 +348,13 @@ impl PacketCommon for EntityStatus {
fn id() -> u8 { fn id() -> u8 {
0x1a 0x1a
} }
async fn read(t: &mut TcpStream) -> tokio::io::Result<Self> { async fn read<T: AsyncRead + Unpin + Send>(t: &mut T) -> tokio::io::Result<Self> {
let mut entitystatus = EntityStatus::new(); let mut entitystatus = EntityStatus::new();
entitystatus.entity_id = MCInt::read(t).await?; entitystatus.entity_id = MCInt::read(t).await?;
entitystatus.entity_status = MCByte::read(t).await?; entitystatus.entity_status = MCByte::read(t).await?;
Ok(entitystatus) Ok(entitystatus)
} }
async fn write(&self, t: &mut TcpStream) -> tokio::io::Result<()> { async fn write<T: AsyncWrite + Unpin + Send>(&self, t: &mut T) -> tokio::io::Result<()> {
for b in Into::<Vec<u8>>::into(self.clone()) { for b in Into::<Vec<u8>>::into(self.clone()) {
write_byte(t, b).await?; write_byte(t, b).await?;
} }
@ -407,7 +407,7 @@ impl PacketCommon for ClientboundPlayerPositionAndLook {
fn id() -> u8 { fn id() -> u8 {
0x08 0x08
} }
async fn read(t: &mut TcpStream) -> tokio::io::Result<Self> { async fn read<T: AsyncRead + Unpin + Send>(t: &mut T) -> tokio::io::Result<Self> {
let mut playerpositionandlook = ClientboundPlayerPositionAndLook::new(); let mut playerpositionandlook = ClientboundPlayerPositionAndLook::new();
playerpositionandlook.x = MCDouble::read(t).await?; playerpositionandlook.x = MCDouble::read(t).await?;
playerpositionandlook.y = MCDouble::read(t).await?; playerpositionandlook.y = MCDouble::read(t).await?;
@ -417,7 +417,7 @@ impl PacketCommon for ClientboundPlayerPositionAndLook {
playerpositionandlook.flags = MCByte::read(t).await?; playerpositionandlook.flags = MCByte::read(t).await?;
Ok(playerpositionandlook) Ok(playerpositionandlook)
} }
async fn write(&self, t: &mut TcpStream) -> tokio::io::Result<()> { async fn write<T: AsyncWrite + Unpin + Send>(&self, t: &mut T) -> tokio::io::Result<()> {
for b in Into::<Vec<u8>>::into(self.clone()) { for b in Into::<Vec<u8>>::into(self.clone()) {
write_byte(t, b).await?; write_byte(t, b).await?;
} }
@ -457,12 +457,12 @@ impl PacketCommon for SpawnPosition {
fn id() -> u8 { fn id() -> u8 {
0x05 0x05
} }
async fn read(t: &mut TcpStream) -> tokio::io::Result<Self> { async fn read<T: AsyncRead + Unpin + Send>(t: &mut T) -> tokio::io::Result<Self> {
let mut spawnposition = SpawnPosition::new(); let mut spawnposition = SpawnPosition::new();
spawnposition.position = MCPosition::read(t).await?; spawnposition.position = MCPosition::read(t).await?;
Ok(spawnposition) Ok(spawnposition)
} }
async fn write(&self, t: &mut TcpStream) -> tokio::io::Result<()> { async fn write<T: AsyncWrite + Unpin + Send>(&self, t: &mut T) -> tokio::io::Result<()> {
for b in Into::<Vec<u8>>::into(self.clone()) { for b in Into::<Vec<u8>>::into(self.clone()) {
write_byte(t, b).await?; write_byte(t, b).await?;
} }
@ -498,12 +498,12 @@ impl PacketCommon for KeepAlivePing {
fn id() -> u8 { fn id() -> u8 {
0x00 0x00
} }
async fn read(t: &mut TcpStream) -> tokio::io::Result<Self> { async fn read<T: AsyncRead + Unpin + Send>(t: &mut T) -> tokio::io::Result<Self> {
let mut keepalive = KeepAlivePing::new(); let mut keepalive = KeepAlivePing::new();
keepalive.payload = MCVarInt::read(t).await?; keepalive.payload = MCVarInt::read(t).await?;
Ok(keepalive) Ok(keepalive)
} }
async fn write(&self, t: &mut TcpStream) -> tokio::io::Result<()> { async fn write<T: AsyncWrite + Unpin + Send>(&self, t: &mut T) -> tokio::io::Result<()> {
for b in Into::<Vec<u8>>::into(self.clone()) { for b in Into::<Vec<u8>>::into(self.clone()) {
write_byte(t, b).await?; write_byte(t, b).await?;
} }
@ -543,12 +543,12 @@ impl PacketCommon for Disconnect {
fn id() -> u8 { fn id() -> u8 {
0x40 0x40
} }
async fn read(t: &mut TcpStream) -> tokio::io::Result<Self> { async fn read<T: AsyncRead + Unpin + Send>(t: &mut T) -> tokio::io::Result<Self> {
let mut keepalive = Disconnect::new(); let mut keepalive = Disconnect::new();
keepalive.reason = MCChat::read(t).await?; keepalive.reason = MCChat::read(t).await?;
Ok(keepalive) Ok(keepalive)
} }
async fn write(&self, t: &mut TcpStream) -> tokio::io::Result<()> { async fn write<T: AsyncWrite + Unpin + Send>(&self, t: &mut T) -> tokio::io::Result<()> {
for b in Into::<Vec<u8>>::into(self.clone()) { for b in Into::<Vec<u8>>::into(self.clone()) {
write_byte(t, b).await?; write_byte(t, b).await?;
} }
@ -589,13 +589,13 @@ impl PacketCommon for ClientboundChatMessage {
fn id() -> u8 { fn id() -> u8 {
0x02 0x02
} }
async fn read(t: &mut TcpStream) -> tokio::io::Result<Self> { async fn read<T: AsyncRead + Unpin + Send>(t: &mut T) -> tokio::io::Result<Self> {
let mut clientboundchatmessage = ClientboundChatMessage::new(); let mut clientboundchatmessage = ClientboundChatMessage::new();
clientboundchatmessage.text = MCChat::read(t).await?; clientboundchatmessage.text = MCChat::read(t).await?;
clientboundchatmessage.position = MCByte::read(t).await?; clientboundchatmessage.position = MCByte::read(t).await?;
Ok(clientboundchatmessage) Ok(clientboundchatmessage)
} }
async fn write(&self, t: &mut TcpStream) -> tokio::io::Result<()> { async fn write<T: AsyncWrite + Unpin + Send>(&self, t: &mut T) -> tokio::io::Result<()> {
for b in Into::<Vec<u8>>::into(self.clone()) { for b in Into::<Vec<u8>>::into(self.clone()) {
write_byte(t, b).await?; write_byte(t, b).await?;
} }

View File

@ -7,10 +7,10 @@ use crate::mctypes::MCVarInt;
pub use clientbound::*; pub use clientbound::*;
use core::convert::TryFrom; use core::convert::TryFrom;
pub use serverbound::*; pub use serverbound::*;
use tokio::net::TcpStream; use tokio::io::{AsyncRead, AsyncWrite};
/// A helper function to read the packet header. /// A helper function to read the packet header.
pub async fn read_packet_header(t: &mut TcpStream) -> tokio::io::Result<(MCVarInt, MCVarInt)> { pub async fn read_packet_header<T: AsyncRead + Unpin>(t: &mut T) -> tokio::io::Result<(MCVarInt, MCVarInt)> {
let length = MCVarInt::read(t).await?; let length = MCVarInt::read(t).await?;
let id = MCVarInt::read(t).await?; let id = MCVarInt::read(t).await?;
Ok((length, id)) Ok((length, id))
@ -28,7 +28,7 @@ macro_rules! register_packets {
pub fn new() -> Packet { pub fn new() -> Packet {
Packet::Null Packet::Null
} }
pub async fn write(&self, t: &mut TcpStream) -> tokio::io::Result<()> { pub async fn write<T: AsyncWrite + Unpin + Send>(&self, t: &mut T) -> tokio::io::Result<()> {
match self { match self {
$( $(
Packet::$name(p) => p.write(t).await, Packet::$name(p) => p.write(t).await,
@ -102,6 +102,6 @@ where
{ {
fn new() -> Self; fn new() -> Self;
fn id() -> u8; fn id() -> u8;
async fn read(t: &'_ mut TcpStream) -> tokio::io::Result<Self>; async fn read<T: AsyncRead + Unpin + Send>(t: &'_ mut T) -> tokio::io::Result<Self>;
async fn write(&self, t: &'_ mut TcpStream) -> tokio::io::Result<()>; async fn write<T: AsyncWrite + Unpin + Send>(&self, t: &'_ mut T) -> tokio::io::Result<()>;
} }

View File

@ -1,7 +1,7 @@
use super::PacketCommon; use super::PacketCommon;
use crate::mctypes::*; use crate::mctypes::*;
use std::convert::{Into, TryFrom}; use std::convert::{Into, TryFrom};
use tokio::net::TcpStream; use tokio::io::{AsyncRead, AsyncWrite};
/// Needed for every interaction with the server. /// Needed for every interaction with the server.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -43,7 +43,7 @@ impl PacketCommon for Handshake {
fn id() -> u8 { fn id() -> u8 {
0x00 0x00
} }
async fn read(t: &mut TcpStream) -> tokio::io::Result<Self> { async fn read<T: AsyncRead + Unpin + Send>(t: &mut T) -> tokio::io::Result<Self> {
let mut handshake = Handshake::new(); let mut handshake = Handshake::new();
handshake.protocol_version = MCVarInt::read(t).await?; handshake.protocol_version = MCVarInt::read(t).await?;
handshake.server_address = MCString::read(t).await?; handshake.server_address = MCString::read(t).await?;
@ -51,7 +51,7 @@ impl PacketCommon for Handshake {
handshake.next_state = MCVarInt::read(t).await?; handshake.next_state = MCVarInt::read(t).await?;
Ok(handshake) Ok(handshake)
} }
async fn write(&self, t: &mut TcpStream) -> tokio::io::Result<()> { async fn write<T: AsyncWrite + Unpin + Send>(&self, t: &mut T) -> tokio::io::Result<()> {
for b in Into::<Vec<u8>>::into(self.clone()) { for b in Into::<Vec<u8>>::into(self.clone()) {
write_byte(t, b).await?; write_byte(t, b).await?;
} }
@ -84,11 +84,11 @@ impl PacketCommon for StatusRequest {
fn id() -> u8 { fn id() -> u8 {
0x00 0x00
} }
async fn read(_t: &mut TcpStream) -> tokio::io::Result<Self> { async fn read<T: AsyncRead + Unpin + Send>(_t: &mut T) -> tokio::io::Result<Self> {
let statusrequest = StatusRequest::new(); let statusrequest = StatusRequest::new();
Ok(statusrequest) Ok(statusrequest)
} }
async fn write(&self, t: &mut TcpStream) -> tokio::io::Result<()> { async fn write<T: AsyncWrite + Unpin + Send>(&self, t: &mut T) -> tokio::io::Result<()> {
for b in Into::<Vec<u8>>::into(self.clone()) { for b in Into::<Vec<u8>>::into(self.clone()) {
write_byte(t, b).await?; write_byte(t, b).await?;
} }
@ -124,12 +124,12 @@ impl PacketCommon for StatusPing {
fn id() -> u8 { fn id() -> u8 {
0x01 0x01
} }
async fn read(t: &mut TcpStream) -> tokio::io::Result<Self> { async fn read<T: AsyncRead + Unpin + Send>(t: &mut T) -> tokio::io::Result<Self> {
let mut statusping = StatusPing::new(); let mut statusping = StatusPing::new();
statusping.payload = MCLong::read(t).await?; statusping.payload = MCLong::read(t).await?;
Ok(statusping) Ok(statusping)
} }
async fn write(&self, t: &mut TcpStream) -> tokio::io::Result<()> { async fn write<T: AsyncWrite + Unpin + Send>(&self, t: &mut T) -> tokio::io::Result<()> {
for b in Into::<Vec<u8>>::into(self.clone()) { for b in Into::<Vec<u8>>::into(self.clone()) {
write_byte(t, b).await?; write_byte(t, b).await?;
} }
@ -167,12 +167,12 @@ impl PacketCommon for LoginStart {
fn id() -> u8 { fn id() -> u8 {
0x00 0x00
} }
async fn read(t: &mut TcpStream) -> tokio::io::Result<Self> { async fn read<T: AsyncRead + Unpin + Send>(t: &mut T) -> tokio::io::Result<Self> {
let mut loginstart = LoginStart::new(); let mut loginstart = LoginStart::new();
loginstart.player_name = MCString::read(t).await?; loginstart.player_name = MCString::read(t).await?;
Ok(loginstart) Ok(loginstart)
} }
async fn write(&self, t: &mut TcpStream) -> tokio::io::Result<()> { async fn write<T: AsyncWrite + Unpin + Send>(&self, t: &mut T) -> tokio::io::Result<()> {
for b in Into::<Vec<u8>>::into(self.clone()) { for b in Into::<Vec<u8>>::into(self.clone()) {
write_byte(t, b).await?; write_byte(t, b).await?;
} }
@ -230,7 +230,7 @@ impl PacketCommon for ClientSettings {
fn id() -> u8 { fn id() -> u8 {
0x15 0x15
} }
async fn read(t: &mut TcpStream) -> tokio::io::Result<Self> { async fn read<T: AsyncRead + Unpin + Send>(t: &mut T) -> tokio::io::Result<Self> {
let mut clientsettings = ClientSettings::new(); let mut clientsettings = ClientSettings::new();
clientsettings.locale = MCString::read(t).await?; clientsettings.locale = MCString::read(t).await?;
clientsettings.view_distance = MCByte::read(t).await?; clientsettings.view_distance = MCByte::read(t).await?;
@ -239,7 +239,7 @@ impl PacketCommon for ClientSettings {
clientsettings.displayed_skin_parts = MCUnsignedByte::read(t).await?; clientsettings.displayed_skin_parts = MCUnsignedByte::read(t).await?;
Ok(clientsettings) Ok(clientsettings)
} }
async fn write(&self, t: &mut TcpStream) -> tokio::io::Result<()> { async fn write<T: AsyncWrite + Unpin + Send>(&self, t: &mut T) -> tokio::io::Result<()> {
for b in Into::<Vec<u8>>::into(self.clone()) { for b in Into::<Vec<u8>>::into(self.clone()) {
write_byte(t, b).await?; write_byte(t, b).await?;
} }
@ -275,12 +275,12 @@ impl PacketCommon for KeepAlivePong {
fn id() -> u8 { fn id() -> u8 {
0x00 0x00
} }
async fn read(t: &mut TcpStream) -> tokio::io::Result<Self> { async fn read<T: AsyncRead + Unpin + Send>(t: &mut T) -> tokio::io::Result<Self> {
let mut keepalive = KeepAlivePong::new(); let mut keepalive = KeepAlivePong::new();
keepalive.payload = MCVarInt::read(t).await?; keepalive.payload = MCVarInt::read(t).await?;
Ok(keepalive) Ok(keepalive)
} }
async fn write(&self, t: &mut TcpStream) -> tokio::io::Result<()> { async fn write<T: AsyncWrite + Unpin + Send>(&self, t: &mut T) -> tokio::io::Result<()> {
for b in Into::<Vec<u8>>::into(self.clone()) { for b in Into::<Vec<u8>>::into(self.clone()) {
write_byte(t, b).await?; write_byte(t, b).await?;
} }
@ -316,12 +316,12 @@ impl PacketCommon for ServerboundChatMessage {
fn id() -> u8 { fn id() -> u8 {
0x01 0x01
} }
async fn read(t: &mut TcpStream) -> tokio::io::Result<Self> { async fn read<T: AsyncRead + Unpin + Send>(t: &mut T) -> tokio::io::Result<Self> {
let mut serverboundchatmessage = ServerboundChatMessage::new(); let mut serverboundchatmessage = ServerboundChatMessage::new();
serverboundchatmessage.text = MCString::read(t).await?; serverboundchatmessage.text = MCString::read(t).await?;
Ok(serverboundchatmessage) Ok(serverboundchatmessage)
} }
async fn write(&self, t: &mut TcpStream) -> tokio::io::Result<()> { async fn write<T: AsyncWrite + Unpin + Send>(&self, t: &mut T) -> tokio::io::Result<()> {
for b in Into::<Vec<u8>>::into(self.clone()) { for b in Into::<Vec<u8>>::into(self.clone()) {
write_byte(t, b).await?; write_byte(t, b).await?;
} }
@ -359,12 +359,12 @@ impl PacketCommon for Player {
fn id() -> u8 { fn id() -> u8 {
0x03 0x03
} }
async fn read(t: &mut TcpStream) -> tokio::io::Result<Self> { async fn read<T: AsyncRead + Unpin + Send>(t: &mut T) -> tokio::io::Result<Self> {
let mut player = Player::new(); let mut player = Player::new();
player.on_ground = MCBoolean::read(t).await?; player.on_ground = MCBoolean::read(t).await?;
Ok(player) Ok(player)
} }
async fn write(&self, t: &mut TcpStream) -> tokio::io::Result<()> { async fn write<T: AsyncWrite + Unpin + Send>(&self, t: &mut T) -> tokio::io::Result<()> {
for b in Into::<Vec<u8>>::into(self.clone()) { for b in Into::<Vec<u8>>::into(self.clone()) {
write_byte(t, b).await?; write_byte(t, b).await?;
} }
@ -411,7 +411,7 @@ impl PacketCommon for PlayerPosition {
fn id() -> u8 { fn id() -> u8 {
0x04 0x04
} }
async fn read(t: &mut TcpStream) -> tokio::io::Result<Self> { async fn read<T: AsyncRead + Unpin + Send>(t: &mut T) -> tokio::io::Result<Self> {
let mut playerposition = PlayerPosition::new(); let mut playerposition = PlayerPosition::new();
playerposition.x = MCDouble::read(t).await?; playerposition.x = MCDouble::read(t).await?;
playerposition.y = MCDouble::read(t).await?; playerposition.y = MCDouble::read(t).await?;
@ -419,7 +419,7 @@ impl PacketCommon for PlayerPosition {
playerposition.on_ground = MCBoolean::read(t).await?; playerposition.on_ground = MCBoolean::read(t).await?;
Ok(playerposition) Ok(playerposition)
} }
async fn write(&self, t: &mut TcpStream) -> tokio::io::Result<()> { async fn write<T: AsyncWrite + Unpin + Send>(&self, t: &mut T) -> tokio::io::Result<()> {
for b in Into::<Vec<u8>>::into(self.clone()) { for b in Into::<Vec<u8>>::into(self.clone()) {
write_byte(t, b).await?; write_byte(t, b).await?;
} }
@ -463,14 +463,14 @@ impl PacketCommon for PlayerLook {
fn id() -> u8 { fn id() -> u8 {
0x05 0x05
} }
async fn read(t: &mut TcpStream) -> tokio::io::Result<Self> { async fn read<T: AsyncRead + Unpin + Send>(t: &mut T) -> tokio::io::Result<Self> {
let mut playerlook = PlayerLook::new(); let mut playerlook = PlayerLook::new();
playerlook.yaw = MCFloat::read(t).await?; playerlook.yaw = MCFloat::read(t).await?;
playerlook.pitch = MCFloat::read(t).await?; playerlook.pitch = MCFloat::read(t).await?;
playerlook.on_ground = MCBoolean::read(t).await?; playerlook.on_ground = MCBoolean::read(t).await?;
Ok(playerlook) Ok(playerlook)
} }
async fn write(&self, t: &mut TcpStream) -> tokio::io::Result<()> { async fn write<T: AsyncWrite + Unpin + Send>(&self, t: &mut T) -> tokio::io::Result<()> {
for b in Into::<Vec<u8>>::into(self.clone()) { for b in Into::<Vec<u8>>::into(self.clone()) {
write_byte(t, b).await?; write_byte(t, b).await?;
} }
@ -523,7 +523,7 @@ impl PacketCommon for ServerboundPlayerPositionAndLook {
fn id() -> u8 { fn id() -> u8 {
0x06 0x06
} }
async fn read(t: &mut TcpStream) -> tokio::io::Result<Self> { async fn read<T: AsyncRead + Unpin + Send>(t: &mut T) -> tokio::io::Result<Self> {
let mut playerpositionandlook = ServerboundPlayerPositionAndLook::new(); let mut playerpositionandlook = ServerboundPlayerPositionAndLook::new();
playerpositionandlook.x = MCDouble::read(t).await?; playerpositionandlook.x = MCDouble::read(t).await?;
playerpositionandlook.y = MCDouble::read(t).await?; playerpositionandlook.y = MCDouble::read(t).await?;
@ -533,7 +533,7 @@ impl PacketCommon for ServerboundPlayerPositionAndLook {
playerpositionandlook.on_ground = MCBoolean::read(t).await?; playerpositionandlook.on_ground = MCBoolean::read(t).await?;
Ok(playerpositionandlook) Ok(playerpositionandlook)
} }
async fn write(&self, t: &mut TcpStream) -> tokio::io::Result<()> { async fn write<T: AsyncWrite + Unpin + Send>(&self, t: &mut T) -> tokio::io::Result<()> {
for b in Into::<Vec<u8>>::into(self.clone()) { for b in Into::<Vec<u8>>::into(self.clone()) {
write_byte(t, b).await?; write_byte(t, b).await?;
} }