Skip to content

Commit

Permalink
deps(dev): update libp2p to 0.54
Browse files Browse the repository at this point in the history
Signed-off-by: ljedrz <[email protected]>
  • Loading branch information
ljedrz committed Aug 27, 2024
1 parent 276e783 commit f8f2a6b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ tracing = { version = "0.1", default-features = false }
[dev-dependencies]
bincode = "1"
deadline = "0.2"
libp2p = { version = "0.53", default-features = false, features = ["macros", "noise", "ping", "tcp", "tokio", "yamux"] }
libp2p = { version = "0.54", default-features = false, features = ["macros", "noise", "ping", "tcp", "tokio", "yamux"] }
native-tls = "0.2"
once_cell = { version = "1", features = ["parking_lot"] }
peak_alloc = "0.2"
Expand Down
38 changes: 16 additions & 22 deletions examples/common/yamux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{fmt, io};

use bytes::{Buf, BufMut, Bytes, BytesMut};
use pea2pea::ConnectionSide;
use tokio_util::codec::{Decoder, Encoder, LengthDelimitedCodec};
use tokio_util::codec::{BytesCodec, Decoder, Encoder};
use tracing::*;

// the version used in Yamux message headers
Expand Down Expand Up @@ -75,7 +75,7 @@ impl Frame {

// a codec used to (de/en)code Yamux frames
pub struct Codec {
codec: LengthDelimitedCodec,
codec: BytesCodec,
// client or server
#[allow(dead_code)]
mode: Side,
Expand All @@ -92,12 +92,7 @@ impl Codec {
};

Self {
codec: LengthDelimitedCodec::builder()
.length_field_offset(8)
.length_field_length(4)
.length_adjustment(12)
.num_skip(0)
.new_codec(),
codec: BytesCodec::new(),
mode,
span,
}
Expand Down Expand Up @@ -237,20 +232,19 @@ impl Decoder for Codec {
type Error = io::Error;

fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
// decode frames based on the length in the Yamux header
let mut bytes = if let Some(bytes) = self.codec.decode(src)? {
bytes
} else {
return Ok(None);
};

// parse the full Yamux message
let version = bytes.get_u8();
let ty = Ty::try_from(bytes.get_u8())?;
let flags = decode_flags(bytes.get_u16())?;
let stream_id = bytes.get_u32();
let length = bytes.get_u32();
let payload = bytes.split_to(length as usize).freeze();
// parse the Yamux header
let version = src.get_u8();
let ty = Ty::try_from(src.get_u8())?;
let flags = decode_flags(src.get_u16())?;
let stream_id = src.get_u32();
let length = src.get_u32();

let payload = match ty {
Ty::Data => src.clone(),
Ty::Ping => length.to_be_bytes().as_slice().into(),
_ => unimplemented!(),
}
.freeze();

Ok(Some(Frame {
header: Header {
Expand Down

0 comments on commit f8f2a6b

Please sign in to comment.