Make packets use TcpStream instead of generic

This commit is contained in:
Garen Tyler 2021-04-26 14:21:27 -06:00
parent 811985ad67
commit b48143fb67
3 changed files with 53 additions and 53 deletions

View File

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

View File

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

View File

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