mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-26 16:21:39 +00:00
core/: Remove deprecated read/write functions (#2213)
Co-authored-by: Max Inden <mail@max-inden.de>
This commit is contained in:
@ -30,7 +30,11 @@
|
|||||||
- Report abortion of pending connection through `DialError`,
|
- Report abortion of pending connection through `DialError`,
|
||||||
`UnknownPeerDialError` or `IncomingConnectionError` (see [PR 2191]).
|
`UnknownPeerDialError` or `IncomingConnectionError` (see [PR 2191]).
|
||||||
|
|
||||||
|
- Remove deprecated functions `upgrade::write_one`, `upgrade::write_with_len_prefix`
|
||||||
|
and `upgrade::read_one` (see [PR 2213]).
|
||||||
|
|
||||||
[PR 2145]: https://github.com/libp2p/rust-libp2p/pull/2145
|
[PR 2145]: https://github.com/libp2p/rust-libp2p/pull/2145
|
||||||
|
[PR 2213]: https://github.com/libp2p/rust-libp2p/pull/2213
|
||||||
[PR 2142]: https://github.com/libp2p/rust-libp2p/pull/2142
|
[PR 2142]: https://github.com/libp2p/rust-libp2p/pull/2142
|
||||||
[PR 2137]: https://github.com/libp2p/rust-libp2p/pull/2137
|
[PR 2137]: https://github.com/libp2p/rust-libp2p/pull/2137
|
||||||
[PR 2183]: https://github.com/libp2p/rust-libp2p/pull/2183
|
[PR 2183]: https://github.com/libp2p/rust-libp2p/pull/2183
|
||||||
|
@ -69,8 +69,6 @@ mod transfer;
|
|||||||
|
|
||||||
use futures::future::Future;
|
use futures::future::Future;
|
||||||
|
|
||||||
#[allow(deprecated)]
|
|
||||||
pub use self::transfer::ReadOneError;
|
|
||||||
pub use self::{
|
pub use self::{
|
||||||
apply::{apply, apply_inbound, apply_outbound, InboundUpgradeApply, OutboundUpgradeApply},
|
apply::{apply, apply_inbound, apply_outbound, InboundUpgradeApply, OutboundUpgradeApply},
|
||||||
denied::DeniedUpgrade,
|
denied::DeniedUpgrade,
|
||||||
|
@ -21,7 +21,7 @@
|
|||||||
//! Contains some helper futures for creating upgrades.
|
//! Contains some helper futures for creating upgrades.
|
||||||
|
|
||||||
use futures::prelude::*;
|
use futures::prelude::*;
|
||||||
use std::{error, fmt, io};
|
use std::io;
|
||||||
|
|
||||||
// TODO: these methods could be on an Ext trait to AsyncWrite
|
// TODO: these methods could be on an Ext trait to AsyncWrite
|
||||||
|
|
||||||
@ -40,42 +40,6 @@ pub async fn write_length_prefixed(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Send a message to the given socket, then shuts down the writing side.
|
|
||||||
///
|
|
||||||
/// > **Note**: Prepends a variable-length prefix indicate the length of the message. This is
|
|
||||||
/// > compatible with what `read_one` expects.
|
|
||||||
///
|
|
||||||
#[deprecated(
|
|
||||||
since = "0.29.0",
|
|
||||||
note = "Use `write_length_prefixed` instead. You will need to manually close the stream using `socket.close().await`."
|
|
||||||
)]
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub async fn write_one(
|
|
||||||
socket: &mut (impl AsyncWrite + Unpin),
|
|
||||||
data: impl AsRef<[u8]>,
|
|
||||||
) -> Result<(), io::Error> {
|
|
||||||
write_varint(socket, data.as_ref().len()).await?;
|
|
||||||
socket.write_all(data.as_ref()).await?;
|
|
||||||
socket.close().await?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Send a message to the given socket with a length prefix appended to it. Also flushes the socket.
|
|
||||||
///
|
|
||||||
/// > **Note**: Prepends a variable-length prefix indicate the length of the message. This is
|
|
||||||
/// > compatible with what `read_one` expects.
|
|
||||||
#[deprecated(since = "0.29.0", note = "Use `write_length_prefixed` instead.")]
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub async fn write_with_len_prefix(
|
|
||||||
socket: &mut (impl AsyncWrite + Unpin),
|
|
||||||
data: impl AsRef<[u8]>,
|
|
||||||
) -> Result<(), io::Error> {
|
|
||||||
write_varint(socket, data.as_ref().len()).await?;
|
|
||||||
socket.write_all(data.as_ref()).await?;
|
|
||||||
socket.flush().await?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Writes a variable-length integer to the `socket`.
|
/// Writes a variable-length integer to the `socket`.
|
||||||
///
|
///
|
||||||
/// > **Note**: Does **NOT** flush the socket.
|
/// > **Note**: Does **NOT** flush the socket.
|
||||||
@ -162,78 +126,6 @@ pub async fn read_length_prefixed(
|
|||||||
Ok(buf)
|
Ok(buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reads a length-prefixed message from the given socket.
|
|
||||||
///
|
|
||||||
/// The `max_size` parameter is the maximum size in bytes of the message that we accept. This is
|
|
||||||
/// necessary in order to avoid DoS attacks where the remote sends us a message of several
|
|
||||||
/// gigabytes.
|
|
||||||
///
|
|
||||||
/// > **Note**: Assumes that a variable-length prefix indicates the length of the message. This is
|
|
||||||
/// > compatible with what `write_one` does.
|
|
||||||
#[deprecated(since = "0.29.0", note = "Use `read_length_prefixed` instead.")]
|
|
||||||
#[allow(dead_code, deprecated)]
|
|
||||||
pub async fn read_one(
|
|
||||||
socket: &mut (impl AsyncRead + Unpin),
|
|
||||||
max_size: usize,
|
|
||||||
) -> Result<Vec<u8>, ReadOneError> {
|
|
||||||
let len = read_varint(socket).await?;
|
|
||||||
if len > max_size {
|
|
||||||
return Err(ReadOneError::TooLarge {
|
|
||||||
requested: len,
|
|
||||||
max: max_size,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut buf = vec![0; len];
|
|
||||||
socket.read_exact(&mut buf).await?;
|
|
||||||
Ok(buf)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Error while reading one message.
|
|
||||||
#[derive(Debug)]
|
|
||||||
#[deprecated(
|
|
||||||
since = "0.29.0",
|
|
||||||
note = "Use `read_length_prefixed` instead of `read_one` to avoid depending on this type."
|
|
||||||
)]
|
|
||||||
pub enum ReadOneError {
|
|
||||||
/// Error on the socket.
|
|
||||||
Io(std::io::Error),
|
|
||||||
/// Requested data is over the maximum allowed size.
|
|
||||||
TooLarge {
|
|
||||||
/// Size requested by the remote.
|
|
||||||
requested: usize,
|
|
||||||
/// Maximum allowed.
|
|
||||||
max: usize,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(deprecated)]
|
|
||||||
impl From<std::io::Error> for ReadOneError {
|
|
||||||
fn from(err: std::io::Error) -> ReadOneError {
|
|
||||||
ReadOneError::Io(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(deprecated)]
|
|
||||||
impl fmt::Display for ReadOneError {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
match *self {
|
|
||||||
ReadOneError::Io(ref err) => write!(f, "{}", err),
|
|
||||||
ReadOneError::TooLarge { .. } => write!(f, "Received data size over maximum"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(deprecated)]
|
|
||||||
impl error::Error for ReadOneError {
|
|
||||||
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
|
|
||||||
match *self {
|
|
||||||
ReadOneError::Io(ref err) => Some(err),
|
|
||||||
ReadOneError::TooLarge { .. } => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
Reference in New Issue
Block a user