Skip to content

Commit

Permalink
fix: implement framing in recv_task
Browse files Browse the repository at this point in the history
  • Loading branch information
nothendev committed Oct 8, 2023
1 parent c11f434 commit 0eb2355
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
51 changes: 28 additions & 23 deletions protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,33 +218,38 @@ impl PlayerNet {
async {
let mut buf = BytesMut::new();

let mut first = true;
while read.read_buf(&mut buf).await? != 0 {
let compres = compressing__.get_copy().await;
loop {
let bufslice = &buf[..];
let mut input = Input::new(&bufslice);
let spack = if let Some(cmp) = compression.filter(|_| compres) {
trace!("[recv]compressing");
let sp: SerializedPacket = SerializedPacketCompressed::deserialize
.parse_with(&mut input)?
.into();
if sp.length < cmp {
warn!(?sp, ?cmp, "packet length was less than threshold");
}
sp
if let Some(packet) = match (if compressing__.get_copy().await {
SerializedPacketCompressed::deserialize
.parse_with(&mut input)
.map(SerializedPacket::from)
} else {
trace!("[recv]not compressing");
SerializedPacket::deserialize.parse_with(&mut input)?
};
let read_bytes = buf.len();
let unread_bytes = read_bytes - input.offset;
trace!(buf=%format!("{:#x?}", &buf[unread_bytes..]), ?input.offset, ?read_bytes, ?unread_bytes, ?spack, "recving packet");
let offset = input.offset;
drop((bufslice, input));
s_recv.send_async(spack).await?;
buf = buf.split_off(offset);
SerializedPacket::deserialize.parse_with(&mut input)
}) {
Ok(packet) => Some(packet),
Err(crate::error::Error::Ser(
crate::ser::SerializationError::UnexpectedEof { .. },
)) => None,
Err(other) => return Err(other),
} {
let offset = input.offset;
drop((bufslice, input));
buf = buf.split_off(offset);
s_recv.send_async(packet).await?;
} else {
if read.read_buf(&mut buf).await? == 0 {
if buf.is_empty() {
break;
} else {
return Err(crate::error::Error::ConnectionEnded);
}
}
}
}
Ok::<(), crate::error::Error>(())

Ok(())
}
.await?;

Expand Down
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
edition = "2021"

0 comments on commit 0eb2355

Please sign in to comment.