libp2p_request_response/
codec.rs

1// Copyright 2020 Parity Technologies (UK) Ltd.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the "Software"),
5// to deal in the Software without restriction, including without limitation
6// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7// and/or sell copies of the Software, and to permit persons to whom the
8// Software is furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19// DEALINGS IN THE SOFTWARE.
20
21use async_trait::async_trait;
22use futures::prelude::*;
23use std::io;
24
25/// A `Codec` defines the request and response types
26/// for a request-response [`Behaviour`](crate::Behaviour) protocol or
27/// protocol family and how they are encoded / decoded on an I/O stream.
28#[async_trait]
29pub trait Codec {
30    /// The type of protocol(s) or protocol versions being negotiated.
31    type Protocol: AsRef<str> + Send + Clone;
32    /// The type of inbound and outbound requests.
33    type Request: Send;
34    /// The type of inbound and outbound responses.
35    type Response: Send;
36
37    /// Reads a request from the given I/O stream according to the
38    /// negotiated protocol.
39    async fn read_request<T>(
40        &mut self,
41        protocol: &Self::Protocol,
42        io: &mut T,
43    ) -> io::Result<Self::Request>
44    where
45        T: AsyncRead + Unpin + Send;
46
47    /// Reads a response from the given I/O stream according to the
48    /// negotiated protocol.
49    async fn read_response<T>(
50        &mut self,
51        protocol: &Self::Protocol,
52        io: &mut T,
53    ) -> io::Result<Self::Response>
54    where
55        T: AsyncRead + Unpin + Send;
56
57    /// Writes a request to the given I/O stream according to the
58    /// negotiated protocol.
59    async fn write_request<T>(
60        &mut self,
61        protocol: &Self::Protocol,
62        io: &mut T,
63        req: Self::Request,
64    ) -> io::Result<()>
65    where
66        T: AsyncWrite + Unpin + Send;
67
68    /// Writes a response to the given I/O stream according to the
69    /// negotiated protocol.
70    async fn write_response<T>(
71        &mut self,
72        protocol: &Self::Protocol,
73        io: &mut T,
74        res: Self::Response,
75    ) -> io::Result<()>
76    where
77        T: AsyncWrite + Unpin + Send;
78}