mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-12 09:31:20 +00:00
feat(request-response): add modules for json
and cbor
messages
This patch adds two modules to `libp2p::request_response`: - `cbor` - `json` Both define a `Behaviour` type-alias that comes with a `Codec` implementation which uses the respective `serde` crate to serialize and deserialize the messages. Fixes #3905. Pull-Request: #3952.
This commit is contained in:
214
protocols/request-response/src/cbor.rs
Normal file
214
protocols/request-response/src/cbor.rs
Normal file
@ -0,0 +1,214 @@
|
||||
// Copyright 2023 Protocol Labs
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the "Software"),
|
||||
// to deal in the Software without restriction, including without limitation
|
||||
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
// and/or sell copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
/// A request-response behaviour using [`serde_cbor`] for serializing and deserializing the messages.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// # use libp2p_request_response::{cbor, ProtocolSupport, self as request_response};
|
||||
/// # use libp2p_swarm::{StreamProtocol, SwarmBuilder};
|
||||
/// #[derive(Debug, serde::Serialize, serde::Deserialize)]
|
||||
/// struct GreetRequest {
|
||||
/// name: String,
|
||||
/// }
|
||||
///
|
||||
/// #[derive(Debug, serde::Serialize, serde::Deserialize)]
|
||||
/// struct GreetResponse {
|
||||
/// message: String,
|
||||
/// }
|
||||
///
|
||||
/// let behaviour = cbor::Behaviour::<GreetRequest, GreetResponse>::new(
|
||||
/// [(StreamProtocol::new("/my-cbor-protocol"), ProtocolSupport::Full)],
|
||||
/// request_response::Config::default()
|
||||
/// );
|
||||
/// ```
|
||||
pub type Behaviour<Req, Resp> = crate::Behaviour<codec::Codec<Req, Resp>>;
|
||||
|
||||
mod codec {
|
||||
use async_trait::async_trait;
|
||||
use futures::prelude::*;
|
||||
use futures::{AsyncRead, AsyncWrite};
|
||||
use libp2p_swarm::StreamProtocol;
|
||||
use serde::{de::DeserializeOwned, Serialize};
|
||||
use std::{io, marker::PhantomData};
|
||||
|
||||
/// Max request size in bytes
|
||||
const REQUEST_SIZE_MAXIMUM: u64 = 1024 * 1024;
|
||||
/// Max response size in bytes
|
||||
const RESPONSE_SIZE_MAXIMUM: u64 = 10 * 1024 * 1024;
|
||||
|
||||
pub struct Codec<Req, Resp> {
|
||||
phantom: PhantomData<(Req, Resp)>,
|
||||
}
|
||||
|
||||
impl<Req, Resp> Default for Codec<Req, Resp> {
|
||||
fn default() -> Self {
|
||||
Codec {
|
||||
phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Req, Resp> Clone for Codec<Req, Resp> {
|
||||
fn clone(&self) -> Self {
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<Req, Resp> crate::Codec for Codec<Req, Resp>
|
||||
where
|
||||
Req: Send + Serialize + DeserializeOwned,
|
||||
Resp: Send + Serialize + DeserializeOwned,
|
||||
{
|
||||
type Protocol = StreamProtocol;
|
||||
type Request = Req;
|
||||
type Response = Resp;
|
||||
|
||||
async fn read_request<T>(&mut self, _: &Self::Protocol, io: &mut T) -> io::Result<Req>
|
||||
where
|
||||
T: AsyncRead + Unpin + Send,
|
||||
{
|
||||
let mut vec = Vec::new();
|
||||
|
||||
io.take(REQUEST_SIZE_MAXIMUM).read_to_end(&mut vec).await?;
|
||||
|
||||
serde_cbor::from_slice(vec.as_slice()).map_err(into_io_error)
|
||||
}
|
||||
|
||||
async fn read_response<T>(&mut self, _: &Self::Protocol, io: &mut T) -> io::Result<Resp>
|
||||
where
|
||||
T: AsyncRead + Unpin + Send,
|
||||
{
|
||||
let mut vec = Vec::new();
|
||||
|
||||
io.take(RESPONSE_SIZE_MAXIMUM).read_to_end(&mut vec).await?;
|
||||
|
||||
serde_cbor::from_slice(vec.as_slice()).map_err(into_io_error)
|
||||
}
|
||||
|
||||
async fn write_request<T>(
|
||||
&mut self,
|
||||
_: &Self::Protocol,
|
||||
io: &mut T,
|
||||
req: Self::Request,
|
||||
) -> io::Result<()>
|
||||
where
|
||||
T: AsyncWrite + Unpin + Send,
|
||||
{
|
||||
let data: Vec<u8> = serde_cbor::to_vec(&req).map_err(into_io_error)?;
|
||||
|
||||
io.write_all(data.as_ref()).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn write_response<T>(
|
||||
&mut self,
|
||||
_: &Self::Protocol,
|
||||
io: &mut T,
|
||||
resp: Self::Response,
|
||||
) -> io::Result<()>
|
||||
where
|
||||
T: AsyncWrite + Unpin + Send,
|
||||
{
|
||||
let data: Vec<u8> = serde_cbor::to_vec(&resp).map_err(into_io_error).unwrap();
|
||||
|
||||
io.write_all(data.as_ref()).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn into_io_error(err: serde_cbor::Error) -> io::Error {
|
||||
if err.is_syntax() || err.is_data() {
|
||||
return io::Error::new(io::ErrorKind::InvalidData, err);
|
||||
}
|
||||
|
||||
if err.is_eof() {
|
||||
return io::Error::new(io::ErrorKind::UnexpectedEof, err);
|
||||
}
|
||||
|
||||
io::Error::new(io::ErrorKind::Other, err)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::cbor::codec::Codec;
|
||||
use crate::Codec as _;
|
||||
use futures::AsyncWriteExt;
|
||||
use futures_ringbuf::Endpoint;
|
||||
use libp2p_swarm::StreamProtocol;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_codec() {
|
||||
let expected_request = TestRequest {
|
||||
payload: "test_payload".to_string(),
|
||||
};
|
||||
let expected_response = TestResponse {
|
||||
payload: "test_payload".to_string(),
|
||||
};
|
||||
let protocol = StreamProtocol::new("/test_cbor/1");
|
||||
let mut codec = Codec::default();
|
||||
|
||||
let (mut a, mut b) = Endpoint::pair(124, 124);
|
||||
codec
|
||||
.write_request(&protocol, &mut a, expected_request.clone())
|
||||
.await
|
||||
.expect("Should write request");
|
||||
a.close().await.unwrap();
|
||||
|
||||
let actual_request = codec
|
||||
.read_request(&protocol, &mut b)
|
||||
.await
|
||||
.expect("Should read request");
|
||||
b.close().await.unwrap();
|
||||
|
||||
assert_eq!(actual_request, expected_request);
|
||||
|
||||
let (mut a, mut b) = Endpoint::pair(124, 124);
|
||||
codec
|
||||
.write_response(&protocol, &mut a, expected_response.clone())
|
||||
.await
|
||||
.expect("Should write response");
|
||||
a.close().await.unwrap();
|
||||
|
||||
let actual_response = codec
|
||||
.read_response(&protocol, &mut b)
|
||||
.await
|
||||
.expect("Should read response");
|
||||
b.close().await.unwrap();
|
||||
|
||||
assert_eq!(actual_response, expected_response);
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
struct TestRequest {
|
||||
payload: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
struct TestResponse {
|
||||
payload: String,
|
||||
}
|
||||
}
|
202
protocols/request-response/src/json.rs
Normal file
202
protocols/request-response/src/json.rs
Normal file
@ -0,0 +1,202 @@
|
||||
// Copyright 2023 Protocol Labs
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the "Software"),
|
||||
// to deal in the Software without restriction, including without limitation
|
||||
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
// and/or sell copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
/// A request-response behaviour using [`serde_json`] for serializing and deserializing the messages.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// # use libp2p_request_response::{json, ProtocolSupport, self as request_response};
|
||||
/// # use libp2p_swarm::{StreamProtocol};
|
||||
/// #[derive(Debug, serde::Serialize, serde::Deserialize)]
|
||||
/// struct GreetRequest {
|
||||
/// name: String,
|
||||
/// }
|
||||
///
|
||||
/// #[derive(Debug, serde::Serialize, serde::Deserialize)]
|
||||
/// struct GreetResponse {
|
||||
/// message: String,
|
||||
/// }
|
||||
///
|
||||
/// let behaviour = json::Behaviour::<GreetRequest, GreetResponse>::new(
|
||||
/// [(StreamProtocol::new("/my-json-protocol"), ProtocolSupport::Full)],
|
||||
/// request_response::Config::default()
|
||||
/// );
|
||||
/// ```
|
||||
pub type Behaviour<Req, Resp> = crate::Behaviour<codec::Codec<Req, Resp>>;
|
||||
|
||||
mod codec {
|
||||
use async_trait::async_trait;
|
||||
use futures::prelude::*;
|
||||
use futures::{AsyncRead, AsyncWrite};
|
||||
use libp2p_swarm::StreamProtocol;
|
||||
use serde::{de::DeserializeOwned, Serialize};
|
||||
use std::{io, marker::PhantomData};
|
||||
|
||||
/// Max request size in bytes
|
||||
const REQUEST_SIZE_MAXIMUM: u64 = 1024 * 1024;
|
||||
/// Max response size in bytes
|
||||
const RESPONSE_SIZE_MAXIMUM: u64 = 10 * 1024 * 1024;
|
||||
|
||||
pub struct Codec<Req, Resp> {
|
||||
phantom: PhantomData<(Req, Resp)>,
|
||||
}
|
||||
|
||||
impl<Req, Resp> Default for Codec<Req, Resp> {
|
||||
fn default() -> Self {
|
||||
Codec {
|
||||
phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Req, Resp> Clone for Codec<Req, Resp> {
|
||||
fn clone(&self) -> Self {
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<Req, Resp> crate::Codec for Codec<Req, Resp>
|
||||
where
|
||||
Req: Send + Serialize + DeserializeOwned,
|
||||
Resp: Send + Serialize + DeserializeOwned,
|
||||
{
|
||||
type Protocol = StreamProtocol;
|
||||
type Request = Req;
|
||||
type Response = Resp;
|
||||
|
||||
async fn read_request<T>(&mut self, _: &Self::Protocol, io: &mut T) -> io::Result<Req>
|
||||
where
|
||||
T: AsyncRead + Unpin + Send,
|
||||
{
|
||||
let mut vec = Vec::new();
|
||||
|
||||
io.take(REQUEST_SIZE_MAXIMUM).read_to_end(&mut vec).await?;
|
||||
|
||||
Ok(serde_json::from_slice(vec.as_slice())?)
|
||||
}
|
||||
|
||||
async fn read_response<T>(&mut self, _: &Self::Protocol, io: &mut T) -> io::Result<Resp>
|
||||
where
|
||||
T: AsyncRead + Unpin + Send,
|
||||
{
|
||||
let mut vec = Vec::new();
|
||||
|
||||
io.take(RESPONSE_SIZE_MAXIMUM).read_to_end(&mut vec).await?;
|
||||
|
||||
Ok(serde_json::from_slice(vec.as_slice())?)
|
||||
}
|
||||
|
||||
async fn write_request<T>(
|
||||
&mut self,
|
||||
_: &Self::Protocol,
|
||||
io: &mut T,
|
||||
req: Self::Request,
|
||||
) -> io::Result<()>
|
||||
where
|
||||
T: AsyncWrite + Unpin + Send,
|
||||
{
|
||||
let data = serde_json::to_vec(&req)?;
|
||||
|
||||
io.write_all(data.as_ref()).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn write_response<T>(
|
||||
&mut self,
|
||||
_: &Self::Protocol,
|
||||
io: &mut T,
|
||||
resp: Self::Response,
|
||||
) -> io::Result<()>
|
||||
where
|
||||
T: AsyncWrite + Unpin + Send,
|
||||
{
|
||||
let data = serde_json::to_vec(&resp)?;
|
||||
|
||||
io.write_all(data.as_ref()).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::Codec;
|
||||
use futures::AsyncWriteExt;
|
||||
use futures_ringbuf::Endpoint;
|
||||
use libp2p_swarm::StreamProtocol;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_codec() {
|
||||
let expected_request = TestRequest {
|
||||
payload: "test_payload".to_string(),
|
||||
};
|
||||
let expected_response = TestResponse {
|
||||
payload: "test_payload".to_string(),
|
||||
};
|
||||
let protocol = StreamProtocol::new("/test_json/1");
|
||||
let mut codec: super::codec::Codec<TestRequest, TestResponse> =
|
||||
super::codec::Codec::default();
|
||||
|
||||
let (mut a, mut b) = Endpoint::pair(124, 124);
|
||||
codec
|
||||
.write_request(&protocol, &mut a, expected_request.clone())
|
||||
.await
|
||||
.expect("Should write request");
|
||||
a.close().await.unwrap();
|
||||
|
||||
let actual_request = codec
|
||||
.read_request(&protocol, &mut b)
|
||||
.await
|
||||
.expect("Should read request");
|
||||
b.close().await.unwrap();
|
||||
|
||||
assert_eq!(actual_request, expected_request);
|
||||
|
||||
let (mut a, mut b) = Endpoint::pair(124, 124);
|
||||
codec
|
||||
.write_response(&protocol, &mut a, expected_response.clone())
|
||||
.await
|
||||
.expect("Should write response");
|
||||
a.close().await.unwrap();
|
||||
|
||||
let actual_response = codec
|
||||
.read_response(&protocol, &mut b)
|
||||
.await
|
||||
.expect("Should read response");
|
||||
b.close().await.unwrap();
|
||||
|
||||
assert_eq!(actual_response, expected_response);
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
struct TestRequest {
|
||||
payload: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
struct TestResponse {
|
||||
payload: String,
|
||||
}
|
||||
}
|
@ -28,7 +28,7 @@
|
||||
//! over the actual messages being sent, which are defined in terms of a
|
||||
//! [`Codec`]. Creating a request/response protocol thus amounts
|
||||
//! to providing an implementation of this trait which can then be
|
||||
//! given to [`Behaviour::new`]. Further configuration options are
|
||||
//! given to [`Behaviour::with_codec`]. Further configuration options are
|
||||
//! available via the [`Config`].
|
||||
//!
|
||||
//! Requests are sent using [`Behaviour::send_request`] and the
|
||||
@ -39,6 +39,14 @@
|
||||
//! receiving a [`Message::Request`] via
|
||||
//! [`Event::Message`].
|
||||
//!
|
||||
//! ## Predefined codecs
|
||||
//!
|
||||
//! In case your message types implement [`serde::Serialize`] and [`serde::Deserialize`],
|
||||
//! you can use two predefined behaviours:
|
||||
//!
|
||||
//! - [`cbor::Behaviour`] for CBOR-encoded messages
|
||||
//! - [`json::Behaviour`] for JSON-encoded messages
|
||||
//!
|
||||
//! ## Protocol Families
|
||||
//!
|
||||
//! A single [`Behaviour`] instance can be used with an entire
|
||||
@ -58,8 +66,12 @@
|
||||
|
||||
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
|
||||
|
||||
#[cfg(feature = "cbor")]
|
||||
pub mod cbor;
|
||||
mod codec;
|
||||
mod handler;
|
||||
#[cfg(feature = "json")]
|
||||
pub mod json;
|
||||
|
||||
pub use codec::Codec;
|
||||
pub use handler::ProtocolSupport;
|
||||
@ -328,13 +340,26 @@ where
|
||||
pending_outbound_requests: HashMap<PeerId, SmallVec<[RequestProtocol<TCodec>; 10]>>,
|
||||
}
|
||||
|
||||
impl<TCodec> Behaviour<TCodec>
|
||||
where
|
||||
TCodec: Codec + Default + Clone + Send + 'static,
|
||||
{
|
||||
/// Creates a new `Behaviour` for the given protocols and configuration, using [`Default`] to construct the codec.
|
||||
pub fn new<I>(protocols: I, cfg: Config) -> Self
|
||||
where
|
||||
I: IntoIterator<Item = (TCodec::Protocol, ProtocolSupport)>,
|
||||
{
|
||||
Self::with_codec(TCodec::default(), protocols, cfg)
|
||||
}
|
||||
}
|
||||
|
||||
impl<TCodec> Behaviour<TCodec>
|
||||
where
|
||||
TCodec: Codec + Clone + Send + 'static,
|
||||
{
|
||||
/// Creates a new `Behaviour` for the given
|
||||
/// protocols, codec and configuration.
|
||||
pub fn new<I>(codec: TCodec, protocols: I, cfg: Config) -> Self
|
||||
pub fn with_codec<I>(codec: TCodec, protocols: I, cfg: Config) -> Self
|
||||
where
|
||||
I: IntoIterator<Item = (TCodec::Protocol, ProtocolSupport)>,
|
||||
{
|
||||
|
Reference in New Issue
Block a user