Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref: Remove duplicate read_lp implementation #801

Merged
merged 1 commit into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::time::{Duration, Instant};

use crate::blobs::Collection;
use crate::protocol::{
read_bao_encoded, read_lp_data, write_lp, AuthToken, Handshake, Request, Res, Response,
read_bao_encoded, read_lp, write_lp, AuthToken, Handshake, Request, Res, Response,
};
use crate::tls::{self, Keypair, PeerId};
use abao::decode::AsyncSliceDecoder;
Expand Down Expand Up @@ -173,7 +173,7 @@ where
// track total amount of blob data transferred
let mut data_len = 0;
// read next message
match read_lp_data(&mut reader, &mut in_buffer).await? {
match read_lp(&mut reader, &mut in_buffer).await? {
Some(response_buffer) => {
let response: Response = postcard::from_bytes(&response_buffer)?;
match response.data {
Expand Down Expand Up @@ -252,7 +252,7 @@ async fn handle_blob_response(
mut reader: quinn::RecvStream,
buffer: &mut BytesMut,
) -> Result<DataStream> {
match read_lp_data(&mut reader, buffer).await? {
match read_lp(&mut reader, buffer).await? {
Some(response_buffer) => {
let response: Response = postcard::from_bytes(&response_buffer)?;
match response.data {
Expand Down
44 changes: 1 addition & 43 deletions src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use postcard::experimental::max_size::MaxSize;
use quinn::VarInt;
use serde::{Deserialize, Serialize};
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
use tracing::debug;

use crate::util::{self, Hash};

Expand Down Expand Up @@ -86,7 +85,7 @@ pub(crate) async fn read_lp(
mut reader: impl AsyncRead + Unpin,
buffer: &mut BytesMut,
) -> Result<Option<Bytes>> {
let size = match read_prefix(&mut reader).await {
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()),
Expand All @@ -102,28 +101,6 @@ pub(crate) async fn read_lp(
Ok(Some(buffer.split_to(size).freeze()))
}

/// Return a buffer for the data, based on a given size, from the given source.
/// The new buffer is split off from the buffer that is passed into the function.
pub(crate) async fn read_size_data<R: AsyncRead + Unpin>(
size: u64,
reader: R,
buffer: &mut BytesMut,
) -> Result<Bytes> {
debug!("reading {}", size);
let mut reader = reader.take(size);
let size = usize::try_from(size)?;
let mut read = 0;
while read != size {
let r = reader.read_buf(buffer).await?;
read += r;
if r == 0 {
break;
}
}
debug!("finished reading");
Ok(buffer.split_to(size).freeze())
}

/// Read and decode the given bao encoded data from the provided source.
///
/// After the data is read successfully, the reader will be at the end of the data.
Expand All @@ -139,25 +116,6 @@ pub(crate) async fn read_bao_encoded<R: AsyncRead + Unpin>(
Ok(decoded)
}

/// Return a buffer of the data, based on the length prefix, from the given source.
/// The new buffer is split off from the buffer that is passed in the function.
pub(crate) async fn read_lp_data<R: AsyncRead + Unpin>(
mut reader: R,
buffer: &mut BytesMut,
) -> Result<Option<Bytes>> {
// read length prefix
let size = read_prefix(&mut reader).await?;

let response = read_size_data(size, reader, buffer).await?;
Ok(Some(response))
}

async fn read_prefix<R: AsyncRead + Unpin>(mut reader: R) -> Result<u64, io::Error> {
// read length prefix
let size = reader.read_u64_le().await?;
Ok(size)
}

/// A token used to authenticate a handshake.
///
/// The token has a printable representation which can be serialised using [`Display`] and
Expand Down