Skip to content

Commit

Permalink
remove write_lp and read_lp from iroh-bytes (#1594)
Browse files Browse the repository at this point in the history
the last place these were used in was CustomGet, which is gone now

## Description

Just some dead code elimination after removing CustomGet

## Notes & open questions

## Change checklist

- [ ] Self-review.
- [ ] Documentation updates if relevant.
- [ ] Tests if relevant.
  • Loading branch information
rklaehn authored Oct 7, 2023
1 parent 31f08bb commit 898b0f7
Showing 1 changed file with 2 additions and 62 deletions.
64 changes: 2 additions & 62 deletions iroh-bytes/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,17 +351,15 @@
//! In case nodes are permanently exchanging data, it is probably valuable to
//! keep a connection open and reuse it for multiple requests.
use std::fmt::{self, Display};
use std::io;
use std::str::FromStr;

use anyhow::{bail, ensure, Context, Result};
use anyhow::{ensure, Result};
use bao_tree::ChunkNum;
use bytes::{Bytes, BytesMut};
use bytes::Bytes;
use derive_more::From;
use quinn::VarInt;
use range_collections::RangeSet2;
use serde::{Deserialize, Serialize};
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
mod range_spec;
pub use range_spec::{NonEmptyRequestRangeSpecIter, RangeSpec, RangeSpecSeq};

Expand Down Expand Up @@ -528,64 +526,6 @@ impl GetRequest {
}
}

/// Write the given data to the provider sink, with a unsigned varint length prefix.
pub async fn write_lp<W: AsyncWrite + Unpin>(writer: &mut W, data: &[u8]) -> Result<()> {
ensure!(
data.len() < MAX_MESSAGE_SIZE,
"sending message is too large"
);

// send length prefix
let data_len = data.len() as u64;
writer.write_u64_le(data_len).await?;

// write message
writer.write_all(data).await?;
Ok(())
}

/// Reads a length prefixed message.
///
/// # Returns
///
/// The message as raw bytes. If the end of the stream is reached and there is no partial
/// message, returns `None`.
pub async fn read_lp(
mut reader: impl AsyncRead + Unpin,
buffer: &mut BytesMut,
) -> Result<Option<Bytes>> {
let size = match reader.read_u64_le().await {
Ok(size) => size,
Err(err) if err.kind() == io::ErrorKind::UnexpectedEof => return Ok(None),
Err(err) => return Err(err.into()),
};

let reader = reader.take(size);
read_fixed_size(reader, buffer, size).await
}

pub(crate) async fn read_fixed_size(
reader: impl AsyncRead + Unpin,
buffer: &mut BytesMut,
size: u64,
) -> Result<Option<Bytes>> {
if size > MAX_MESSAGE_SIZE as u64 {
bail!("Incoming message exceeds MAX_MESSAGE_SIZE");
}

let mut reader = reader.take(size);
let size = usize::try_from(size).context("frame larger than usize")?;

buffer.reserve(size);
loop {
let r = reader.read_buf(buffer).await?;
if r == 0 {
break;
}
}
Ok(Some(buffer.split_to(size).freeze()))
}

/// Reasons to close connections or stop streams.
///
/// A QUIC **connection** can be *closed* and a **stream** can request the other side to
Expand Down

0 comments on commit 898b0f7

Please sign in to comment.