|
| 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 | + |
| 21 | +pub use libp2p_core::ProtocolName; |
| 22 | + |
| 23 | +use async_trait::async_trait; |
| 24 | +use futures::prelude::*; |
| 25 | +use std::io; |
| 26 | + |
| 27 | +/// A `Codec` defines the request and response types |
| 28 | +/// for a request-response [`Behaviour`](crate::Behaviour) protocol or |
| 29 | +/// protocol family and how they are encoded / decoded on an I/O stream. |
| 30 | +#[async_trait] |
| 31 | +pub trait Codec { |
| 32 | + /// The type of protocol(s) or protocol versions being negotiated. |
| 33 | + type Protocol: ProtocolName + Send + Clone; |
| 34 | + /// The type of inbound and outbound requests. |
| 35 | + type Request: Send; |
| 36 | + /// The type of inbound and outbound responses. |
| 37 | + type Response: Send; |
| 38 | + |
| 39 | + /// Reads a request from the given I/O stream according to the |
| 40 | + /// negotiated protocol. |
| 41 | + async fn read_request<T>( |
| 42 | + &mut self, |
| 43 | + protocol: &Self::Protocol, |
| 44 | + io: &mut T, |
| 45 | + ) -> io::Result<Self::Request> |
| 46 | + where |
| 47 | + T: AsyncRead + Unpin + Send; |
| 48 | + |
| 49 | + /// Reads a response from the given I/O stream according to the |
| 50 | + /// negotiated protocol. |
| 51 | + async fn read_response<T>( |
| 52 | + &mut self, |
| 53 | + protocol: &Self::Protocol, |
| 54 | + io: &mut T, |
| 55 | + ) -> io::Result<Self::Response> |
| 56 | + where |
| 57 | + T: AsyncRead + Unpin + Send; |
| 58 | + |
| 59 | + /// Writes a request to the given I/O stream according to the |
| 60 | + /// negotiated protocol. |
| 61 | + async fn write_request<T>( |
| 62 | + &mut self, |
| 63 | + protocol: &Self::Protocol, |
| 64 | + io: &mut T, |
| 65 | + req: Self::Request, |
| 66 | + ) -> io::Result<()> |
| 67 | + where |
| 68 | + T: AsyncWrite + Unpin + Send; |
| 69 | + |
| 70 | + /// Writes a response to the given I/O stream according to the |
| 71 | + /// negotiated protocol. |
| 72 | + async fn write_response<T>( |
| 73 | + &mut self, |
| 74 | + protocol: &Self::Protocol, |
| 75 | + io: &mut T, |
| 76 | + res: Self::Response, |
| 77 | + ) -> io::Result<()> |
| 78 | + where |
| 79 | + T: AsyncWrite + Unpin + Send; |
| 80 | +} |
0 commit comments