libp2p_relay/protocol.rs
1// Copyright 2021 Protocol Labs.
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 std::time::Duration;
22
23use libp2p_swarm::StreamProtocol;
24
25use crate::proto;
26
27pub(crate) mod inbound_hop;
28pub(crate) mod inbound_stop;
29pub(crate) mod outbound_hop;
30pub(crate) mod outbound_stop;
31pub const HOP_PROTOCOL_NAME: StreamProtocol =
32 StreamProtocol::new("/libp2p/circuit/relay/0.2.0/hop");
33pub const STOP_PROTOCOL_NAME: StreamProtocol =
34 StreamProtocol::new("/libp2p/circuit/relay/0.2.0/stop");
35
36pub(crate) const MAX_MESSAGE_SIZE: usize = 4096;
37
38#[derive(Debug, Clone, Copy, PartialEq, Eq)]
39pub struct Limit {
40 duration: Option<Duration>,
41 data_in_bytes: Option<u64>,
42}
43
44impl Limit {
45 pub fn duration(&self) -> Option<Duration> {
46 self.duration
47 }
48
49 pub fn data_in_bytes(&self) -> Option<u64> {
50 self.data_in_bytes
51 }
52}
53
54impl From<proto::Limit> for Limit {
55 fn from(limit: proto::Limit) -> Self {
56 Limit {
57 duration: limit.duration.map(|d| Duration::from_secs(d.into())),
58 data_in_bytes: limit.data,
59 }
60 }
61}