Skip to content

Commit

Permalink
even more optimization crimes
Browse files Browse the repository at this point in the history
  • Loading branch information
dankmeme01 committed Nov 21, 2023
1 parent 7f6d34c commit 850f609
Show file tree
Hide file tree
Showing 12 changed files with 177 additions and 322 deletions.
84 changes: 17 additions & 67 deletions server/game/src/bytebufferext.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::io::Read;

use crate::data::types::cocos;
use anyhow::{anyhow, Result};
use anyhow::Result;
use bytebuffer::{ByteBuffer, ByteReader};

type ByteVec = Vec<u8>;
Expand All @@ -10,28 +8,15 @@ pub trait Encodable {
fn encode(&self, buf: &mut ByteBuffer);
}

// empty is similar to default but default does silly things i think
pub trait Empty {
fn empty() -> Self
pub trait Decodable {
fn decode(buf: &mut ByteBuffer) -> Result<Self>
where
Self: Sized;
fn decode_from_reader(buf: &mut ByteReader) -> Result<Self>
where
Self: Sized;
}

pub trait Decodable: Empty {
fn decode(&mut self, buf: &mut ByteBuffer) -> Result<()>;
fn decode_from_reader(&mut self, buf: &mut ByteReader) -> Result<()>;
}

// FastDecodable is a more efficient Decodable that has no Empty requirement,
// so the struct is constructed with all the appropriate values immediately,
// instead of making an ::empty() variant and then filling in the values.
pub trait FastDecodable: Sized {
fn decode_fast(buf: &mut ByteBuffer) -> Result<Self>;
fn decode_fast_from_reader(buf: &mut ByteReader) -> Result<Self>;
}

pub trait Serializable: Encodable + Decodable {}

macro_rules! encode_impl {
($packet_type:ty, $buf:ident, $self:ident, $encode:expr) => {
impl crate::bytebufferext::Encodable for $packet_type {
Expand All @@ -43,32 +28,19 @@ macro_rules! encode_impl {
}

macro_rules! decode_impl {
($packet_type:ty, $buf:ident, $self:ident, $decode:expr) => {
($packet_type:ty, $buf:ident, $decode:expr) => {
impl crate::bytebufferext::Decodable for $packet_type {
fn decode(&mut $self, $buf: &mut bytebuffer::ByteBuffer) -> anyhow::Result<()> {
fn decode($buf: &mut bytebuffer::ByteBuffer) -> anyhow::Result<Self> {
$decode
}

fn decode_from_reader(&mut $self, $buf: &mut bytebuffer::ByteReader) -> anyhow::Result<()> {
fn decode_from_reader($buf: &mut bytebuffer::ByteReader) -> anyhow::Result<Self> {
$decode
}
}
};
}

macro_rules! empty_impl {
($typ:ty, $empty:expr) => {
impl crate::bytebufferext::Empty for $typ {
fn empty() -> Self
where
Self: Sized,
{
$empty
}
}
};
}

macro_rules! encode_unimpl {
($packet_type:ty) => {
impl crate::bytebufferext::Encodable for $packet_type {
Expand All @@ -85,14 +57,14 @@ macro_rules! encode_unimpl {
macro_rules! decode_unimpl {
($packet_type:ty) => {
impl crate::bytebufferext::Decodable for $packet_type {
fn decode(&mut self, _: &mut bytebuffer::ByteBuffer) -> anyhow::Result<()> {
fn decode(_: &mut bytebuffer::ByteBuffer) -> anyhow::Result<Self> {
Err(anyhow::anyhow!(
"decoding unimplemented for {}",
stringify!($packet_type)
))
}

fn decode_from_reader(&mut self, _: &mut bytebuffer::ByteReader) -> anyhow::Result<()> {
fn decode_from_reader(_: &mut bytebuffer::ByteReader) -> anyhow::Result<Self> {
Err(anyhow::anyhow!(
"decoding unimplemented for {}",
stringify!($packet_type)
Expand All @@ -101,9 +73,9 @@ macro_rules! decode_unimpl {
}
};
}

pub(crate) use decode_impl;
pub(crate) use decode_unimpl;
pub(crate) use empty_impl;
pub(crate) use encode_impl;
pub(crate) use encode_unimpl;

Expand All @@ -130,10 +102,8 @@ pub trait ByteBufferExtRead {
fn read_bool(&mut self) -> Result<bool>;
// read a byte vector, prefixed with 4 bytes indicating length
fn read_byte_array(&mut self) -> Result<ByteVec>;
fn read_bytes_into(&mut self, dest: &mut [u8]) -> Result<()>;

fn read_value<T: Decodable>(&mut self) -> Result<T>;
fn read_value_fast<T: FastDecodable>(&mut self) -> Result<T>;
fn read_optional_value<T: Decodable>(&mut self) -> Result<Option<T>>;

fn read_value_vec<T: Decodable>(&mut self) -> Result<Vec<T>>;
Expand Down Expand Up @@ -193,7 +163,7 @@ impl ByteBufferExtWrite for ByteBuffer {
}

macro_rules! impl_extread {
($decode_fn:ident, $fast_fn:ident) => {
($decode_fn:ident) => {
fn read_bool(&mut self) -> Result<bool> {
Ok(self.read_u8()? == 1u8)
}
Expand All @@ -203,26 +173,8 @@ macro_rules! impl_extread {
Ok(self.read_bytes(length)?)
}

fn read_bytes_into(&mut self, dest: &mut [u8]) -> Result<()> {
self.flush_bits();

if self.get_rpos() + dest.len() > self.len() {
return Err(anyhow!("could not read enough bytes from buffer"));
}

self.read_exact(dest)?;

Ok(())
}

fn read_value<T: Decodable>(&mut self) -> Result<T> {
let mut value = T::empty();
value.$decode_fn(self)?;
Ok(value)
}

fn read_value_fast<T: FastDecodable>(&mut self) -> Result<T> {
T::$fast_fn(self)
T::$decode_fn(self)
}

fn read_optional_value<T: Decodable>(&mut self) -> Result<Option<T>> {
Expand All @@ -236,9 +188,7 @@ macro_rules! impl_extread {
let mut out = Vec::new();
let length = self.read_u32()? as usize;
for _ in 0..length {
let mut value = T::empty();
value.$decode_fn(self)?;
out.push(value);
out.push(self.read_value()?);
}

Ok(out)
Expand All @@ -259,9 +209,9 @@ macro_rules! impl_extread {
}

impl ByteBufferExtRead for ByteBuffer {
impl_extread!(decode, decode_fast);
impl_extread!(decode);
}

impl<'a> ByteBufferExtRead for ByteReader<'a> {
impl_extread!(decode_from_reader, decode_fast_from_reader);
impl_extread!(decode_from_reader);
}
38 changes: 9 additions & 29 deletions server/game/src/data/packets/client/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,9 @@ packet!(PingPacket, 10000, false, {
id: u32,
});

empty_impl!(PingPacket, Self { id: 0 });

encode_unimpl!(PingPacket);

decode_impl!(PingPacket, buf, self, {
self.id = buf.read_u32()?;
Ok(())
});
decode_impl!(PingPacket, buf, Ok(Self { id: buf.read_u32()? }));

/* CryptoHandshakeStartPacket - 10001 */

Expand All @@ -26,17 +21,10 @@ packet!(CryptoHandshakeStartPacket, 10001, false, {

encode_unimpl!(CryptoHandshakeStartPacket);

empty_impl!(CryptoHandshakeStartPacket, {
Self {
protocol: 0,
key: CryptoPublicKey::empty(),
}
});

decode_impl!(CryptoHandshakeStartPacket, buf, self, {
self.protocol = buf.read_u16()?;
self.key = buf.read_value()?;
Ok(())
decode_impl!(CryptoHandshakeStartPacket, buf, {
let protocol = buf.read_u16()?;
let key = buf.read_value()?;
Ok(Self { protocol, key })
});

/* KeepalivePacket - 10002 */
Expand All @@ -52,18 +40,10 @@ packet!(LoginPacket, 10003, true, {

encode_unimpl!(LoginPacket);

empty_impl!(
LoginPacket,
Self {
account_id: 0,
token: "".to_string(),
}
);

decode_impl!(LoginPacket, buf, self, {
self.account_id = buf.read_i32()?;
self.token = buf.read_string()?;
Ok(())
decode_impl!(LoginPacket, buf, {
let account_id = buf.read_i32()?;
let token = buf.read_string()?;
Ok(Self { account_id, token })
});

/* DisconnectPacket - 10004 */
Expand Down
55 changes: 15 additions & 40 deletions server/game/src/data/packets/client/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,14 @@ packet!(SyncIconsPacket, 11000, false, {

encode_unimpl!(SyncIconsPacket);

empty_impl!(
decode_impl!(
SyncIconsPacket,
Self {
icons: PlayerIconData::default()
}
buf,
Ok(Self {
icons: buf.read_value()?
})
);

decode_impl!(SyncIconsPacket, buf, self, {
self.icons = buf.read_value()?;
Ok(())
});

/* RequestProfilesPacket - 11001 */

packet!(RequestProfilesPacket, 11001, false, {
Expand All @@ -32,15 +28,14 @@ packet!(RequestProfilesPacket, 11001, false, {

encode_unimpl!(RequestProfilesPacket);

empty_impl!(RequestProfilesPacket, Self { ids: Vec::new() });

decode_impl!(RequestProfilesPacket, buf, self, {
decode_impl!(RequestProfilesPacket, buf, {
let len = buf.read_u32()?;
let mut ids = Vec::new();
for _ in 0..len {
self.ids.push(buf.read_i32()?);
ids.push(buf.read_i32()?);
}

Ok(())
Ok(Self { ids })
});

/* LevelJoinPacket - 11002 */
Expand All @@ -51,11 +46,10 @@ packet!(LevelJoinPacket, 11002, false, {

encode_unimpl!(LevelJoinPacket);

empty_impl!(LevelJoinPacket, Self { level_id: 0 });

decode_impl!(LevelJoinPacket, buf, self, {
self.level_id = buf.read_i32()?;
Ok(())
decode_impl!(LevelJoinPacket, buf, {
Ok(Self {
level_id: buf.read_i32()?,
})
});

/* LevelLeavePacket - 11003 */
Expand All @@ -70,17 +64,7 @@ packet!(PlayerDataPacket, 11004, false, {

encode_unimpl!(PlayerDataPacket);

empty_impl!(
PlayerDataPacket,
Self {
data: PlayerData::default() // :(
}
);

decode_impl!(PlayerDataPacket, buf, self, {
self.data = buf.read_value_fast()?;
Ok(())
});
decode_impl!(PlayerDataPacket, buf, Ok(Self { data: buf.read_value()? }));

/* VoicePacket - 11010 */

Expand All @@ -90,13 +74,4 @@ packet!(VoicePacket, 11010, true, {

encode_unimpl!(VoicePacket);

empty_impl!(VoicePacket, {
Self {
data: EncodedAudioFrame::empty(),
}
});

decode_impl!(VoicePacket, buf, self, {
self.data = buf.read_value()?;
Ok(())
});
decode_impl!(VoicePacket, buf, Ok(Self { data: buf.read_value()? }));
Loading

0 comments on commit 850f609

Please sign in to comment.