1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
mod locator;
pub use locator::*;
mod manager;
pub use manager::*;
#[cfg(feature = "transport_tcp")]
mod tcp;
#[cfg(feature = "transport_udp")]
mod udp;
#[cfg(all(feature = "transport_unixsock-stream", target_family = "unix"))]
mod unixsock_stream;
use crate::io::{RBuf, WBuf};
use crate::proto::SessionMessage;
use async_std::sync::Arc;
use async_trait::async_trait;
use std::cmp::PartialEq;
use std::collections::HashMap;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::ops::Deref;
use zenoh_util::core::{ZError, ZErrorKind, ZResult};
#[async_trait]
pub trait LinkTrait {
fn get_mtu(&self) -> usize;
fn get_src(&self) -> Locator;
fn get_dst(&self) -> Locator;
fn is_reliable(&self) -> bool;
fn is_streamed(&self) -> bool;
async fn write(&self, buffer: &[u8]) -> ZResult<usize>;
async fn write_all(&self, buffer: &[u8]) -> ZResult<()>;
async fn read(&self, buffer: &mut [u8]) -> ZResult<usize>;
async fn read_exact(&self, buffer: &mut [u8]) -> ZResult<()>;
async fn close(&self) -> ZResult<()>;
}
const WBUF_SIZE: usize = 64;
#[derive(Clone)]
pub struct Link(Arc<dyn LinkTrait + Send + Sync>);
impl Link {
fn new(link: Arc<dyn LinkTrait + Send + Sync>) -> Link {
Self(link)
}
pub async fn write_session_message(&self, msg: SessionMessage) -> ZResult<()> {
let mut wbuf = WBuf::new(WBUF_SIZE, false);
if self.is_streamed() {
wbuf.write_bytes(&[0u8, 0u8]);
}
wbuf.write_session_message(&msg);
if self.is_streamed() {
let length: u16 = wbuf.len() as u16 - 2;
let bits = wbuf.get_first_slice_mut(..2);
bits.copy_from_slice(&length.to_le_bytes());
}
let mut buffer = vec![0u8; wbuf.len()];
wbuf.copy_into_slice(&mut buffer[..]);
self.write_all(&buffer).await
}
pub async fn read_session_message(&self) -> ZResult<Vec<SessionMessage>> {
let buffer = if self.is_streamed() {
let mut length_bytes = [0u8; 2];
let _ = self.read_exact(&mut length_bytes).await?;
let to_read = u16::from_le_bytes(length_bytes) as usize;
let mut buffer = vec![0u8; to_read];
let _ = self.read_exact(&mut buffer).await?;
buffer
} else {
let mut buffer = vec![0u8; self.get_mtu()];
let n = self.read(&mut buffer).await?;
buffer.truncate(n);
buffer
};
let mut rbuf = RBuf::from(buffer);
let mut messages: Vec<SessionMessage> = Vec::with_capacity(1);
while rbuf.can_read() {
match rbuf.read_session_message() {
Some(msg) => messages.push(msg),
None => {
let e = format!("Decoding error on link: {}", self);
return zerror!(ZErrorKind::InvalidMessage { descr: e });
}
}
}
Ok(messages)
}
}
impl Deref for Link {
type Target = Arc<dyn LinkTrait + Send + Sync>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Eq for Link {}
impl PartialEq for Link {
fn eq(&self, other: &Self) -> bool {
(self.0.get_src() == other.0.get_src()) && (self.0.get_dst() == other.0.get_dst())
}
}
impl Hash for Link {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.get_src().hash(state);
self.0.get_dst().hash(state);
}
}
impl fmt::Display for Link {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} => {}", self.0.get_src(), self.0.get_dst())
}
}
impl fmt::Debug for Link {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Link")
.field("src", &self.0.get_src())
.field("dst", &self.0.get_dst())
.field("mtu", &self.0.get_mtu())
.field("is_reliable", &self.0.is_reliable())
.field("is_streamed", &self.0.is_streamed())
.finish()
}
}
pub type LinkProperties = HashMap<usize, String>;
#[async_trait]
pub trait LinkManagerTrait {
async fn new_link(&self, dst: &Locator) -> ZResult<Link>;
async fn new_listener(&self, locator: &Locator) -> ZResult<Locator>;
async fn del_listener(&self, locator: &Locator) -> ZResult<()>;
async fn get_listeners(&self) -> Vec<Locator>;
async fn get_locators(&self) -> Vec<Locator>;
}
pub type LinkManager = Arc<dyn LinkManagerTrait + Send + Sync>;