zenoh_link_unixsock_stream/
lib.rs

1//
2// Copyright (c) 2023 ZettaScale Technology
3//
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7// which is available at https://www.apache.org/licenses/LICENSE-2.0.
8//
9// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10//
11// Contributors:
12//   ZettaScale Zenoh Team, <zenoh@zettascale.tech>
13//
14
15//! ⚠️ WARNING ⚠️
16//!
17//! This crate is intended for Zenoh's internal use.
18//!
19//! [Click here for Zenoh's documentation](https://docs.rs/zenoh/latest/zenoh)
20use std::str::FromStr;
21
22use async_trait::async_trait;
23use zenoh_core::zconfigurable;
24use zenoh_link_commons::LocatorInspector;
25use zenoh_protocol::core::{endpoint::Address, Locator, Metadata, Reliability};
26#[cfg(target_family = "unix")]
27use zenoh_protocol::transport::BatchSize;
28use zenoh_result::ZResult;
29
30#[cfg(target_family = "unix")]
31mod unicast;
32#[cfg(target_family = "unix")]
33pub use unicast::*;
34
35// Default MTU (UnixSocketStream PDU) in bytes.
36// NOTE: Since UnixSocketStream is a byte-stream oriented transport, theoretically it has
37//       no limit regarding the MTU. However, given the batching strategy
38//       adopted in Zenoh and the usage of 16 bits in Zenoh to encode the
39//       payload length in byte-streamed, the UNIXSOCKSTREAM MTU is constrained to
40//       2^16 - 1 bytes (i.e., 65535).
41#[cfg(target_family = "unix")]
42const UNIXSOCKSTREAM_MAX_MTU: BatchSize = BatchSize::MAX;
43
44pub const UNIXSOCKSTREAM_LOCATOR_PREFIX: &str = "unixsock-stream";
45
46const IS_RELIABLE: bool = true;
47
48zconfigurable! {
49    // Default MTU (UNIXSOCKSTREAM PDU) in bytes.
50    #[cfg(target_family = "unix")]
51    static ref UNIXSOCKSTREAM_DEFAULT_MTU: BatchSize = UNIXSOCKSTREAM_MAX_MTU;
52    // Amount of time in microseconds to throttle the accept loop upon an error.
53    // Default set to 100 ms.
54    #[cfg(target_family = "unix")]
55    static ref UNIXSOCKSTREAM_ACCEPT_THROTTLE_TIME: u64 = 100_000;
56}
57
58#[derive(Default, Clone, Copy)]
59pub struct UnixSockStreamLocatorInspector;
60#[async_trait]
61impl LocatorInspector for UnixSockStreamLocatorInspector {
62    fn protocol(&self) -> &str {
63        UNIXSOCKSTREAM_LOCATOR_PREFIX
64    }
65
66    async fn is_multicast(&self, _locator: &Locator) -> ZResult<bool> {
67        Ok(false)
68    }
69
70    fn is_reliable(&self, locator: &Locator) -> ZResult<bool> {
71        if let Some(reliability) = locator
72            .metadata()
73            .get(Metadata::RELIABILITY)
74            .map(Reliability::from_str)
75            .transpose()?
76        {
77            Ok(reliability == Reliability::Reliable)
78        } else {
79            Ok(IS_RELIABLE)
80        }
81    }
82}
83
84pub fn get_unix_path_as_string(address: Address<'_>) -> String {
85    address.to_string()
86}