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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
pub mod connection;
pub mod error;
mod frame;
mod pause;
use async_trait::async_trait;
use futures::{AsyncRead, AsyncWrite, FutureExt};
use log::{info, trace};
use std::fmt;
use libp2prs_core::muxing::{IReadWrite, IStreamMuxer, ReadWriteEx, StreamInfo, StreamMuxer, StreamMuxerEx};
use libp2prs_core::transport::{ConnectionInfo, TransportError};
use libp2prs_core::upgrade::{UpgradeInfo, Upgrader};
use crate::connection::Connection;
use connection::{control::Control, stream::Stream, Id};
use error::ConnectionError;
use futures::future::BoxFuture;
use libp2prs_core::identity::Keypair;
use libp2prs_core::secure_io::SecureInfo;
use libp2prs_core::{Multiaddr, PeerId, PublicKey};
use parking_lot::Mutex;
use std::sync::Arc;
#[derive(Clone)]
pub struct Config {}
impl Config {
pub fn new() -> Self {
Config {}
}
}
impl Default for Config {
fn default() -> Self {
Self::new()
}
}
pub struct Mplex<C> {
conn: Arc<Mutex<Option<Connection<C>>>>,
ctrl: Control,
id: Id,
pub la: Multiaddr,
pub ra: Multiaddr,
pub local_priv_key: Keypair,
pub local_peer_id: PeerId,
pub remote_pub_key: PublicKey,
pub remote_peer_id: PeerId,
}
impl<C> Clone for Mplex<C> {
fn clone(&self) -> Self {
Mplex {
conn: self.conn.clone(),
ctrl: self.ctrl.clone(),
id: self.id,
la: self.la.clone(),
ra: self.ra.clone(),
local_priv_key: self.local_priv_key.clone(),
local_peer_id: self.local_peer_id,
remote_pub_key: self.remote_pub_key.clone(),
remote_peer_id: self.remote_peer_id,
}
}
}
impl<C> fmt::Debug for Mplex<C> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Mplex")
.field("Id", &self.id)
.field("Ra", &self.ra)
.field("Rid", &self.remote_peer_id)
.finish()
}
}
impl<C: ConnectionInfo + SecureInfo + AsyncRead + AsyncWrite + Send + Unpin + 'static> Mplex<C> {
pub fn new(io: C) -> Self {
let la = io.local_multiaddr();
let ra = io.remote_multiaddr();
let local_priv_key = io.local_priv_key();
let local_peer_id = io.local_peer();
let remote_pub_key = io.remote_pub_key();
let remote_peer_id = io.remote_peer();
let conn = Connection::new(io);
let id = conn.id();
let ctrl = conn.control();
Mplex {
conn: Arc::new(Mutex::new(Some(conn))),
ctrl,
id,
la,
ra,
local_priv_key,
local_peer_id,
remote_pub_key,
remote_peer_id,
}
}
}
impl<C: Send> ConnectionInfo for Mplex<C> {
fn local_multiaddr(&self) -> Multiaddr {
self.la.clone()
}
fn remote_multiaddr(&self) -> Multiaddr {
self.ra.clone()
}
}
impl<C> SecureInfo for Mplex<C> {
fn local_peer(&self) -> PeerId {
self.local_peer_id
}
fn remote_peer(&self) -> PeerId {
self.remote_peer_id
}
fn local_priv_key(&self) -> Keypair {
self.local_priv_key.clone()
}
fn remote_pub_key(&self) -> PublicKey {
self.remote_pub_key.clone()
}
}
impl StreamInfo for Stream {
fn id(&self) -> usize {
self.val() as usize
}
}
#[async_trait]
impl ReadWriteEx for Stream {
fn box_clone(&self) -> IReadWrite {
Box::new(self.clone())
}
}
impl<C: AsyncRead + AsyncWrite + Send + Unpin + 'static> StreamMuxerEx for Mplex<C> {}
#[async_trait]
impl<C: AsyncRead + AsyncWrite + Send + Unpin + 'static> StreamMuxer for Mplex<C> {
async fn open_stream(&mut self) -> Result<IReadWrite, TransportError> {
trace!("opening a new outbound substream for mplex...");
let s = self.ctrl.open_stream().await?;
Ok(Box::new(s))
}
async fn accept_stream(&mut self) -> Result<IReadWrite, TransportError> {
trace!("waiting for a new inbound substream for yamux...");
let s = self.ctrl.accept_stream().await?;
Ok(Box::new(s))
}
async fn close(&mut self) -> Result<(), TransportError> {
let _ = self.ctrl.close().await?;
Ok(())
}
fn task(&mut self) -> Option<BoxFuture<'static, ()>> {
if let Some(mut conn) = self.conn.lock().take() {
return Some(
async move {
while conn.next_stream().await.is_ok() {}
info!("connection is closed");
}
.boxed(),
);
}
None
}
fn box_clone(&self) -> IStreamMuxer {
Box::new(self.clone())
}
}
impl UpgradeInfo for Config {
type Info = &'static [u8];
fn protocol_info(&self) -> Vec<Self::Info> {
vec![b"/mplex/6.7.0"]
}
}
#[async_trait]
impl<T> Upgrader<T> for Config
where
T: ConnectionInfo + SecureInfo + AsyncRead + AsyncWrite + Send + Unpin + 'static,
{
type Output = Mplex<T>;
async fn upgrade_inbound(self, socket: T, _info: <Self as UpgradeInfo>::Info) -> Result<Self::Output, TransportError> {
trace!("upgrading mplex inbound");
Ok(Mplex::new(socket))
}
async fn upgrade_outbound(self, socket: T, _info: <Self as UpgradeInfo>::Info) -> Result<Self::Output, TransportError> {
trace!("upgrading mplex outbound");
Ok(Mplex::new(socket))
}
}
impl From<ConnectionError> for TransportError {
fn from(e: ConnectionError) -> Self {
TransportError::StreamMuxerError(Box::new(e))
}
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}