forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquic_transport.rs
More file actions
239 lines (217 loc) · 7.32 KB
/
Copy pathquic_transport.rs
File metadata and controls
239 lines (217 loc) · 7.32 KB
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
use std::collections::HashMap;
use std::io;
use std::net::SocketAddr;
use std::sync::{mpsc, Arc};
use quinn::{Endpoint, ServerConfig};
use rustls::pki_types::PrivateKeyDer;
use tokio::runtime::Runtime;
use crate::runtime::cluster::NodeId;
use crate::runtime::network::{IncomingPacket, NetworkTransport, Packet};
struct QuicPeer {
send: quinn::SendStream,
addr: SocketAddr,
}
pub struct QuicTransport {
node_id: NodeId,
listen_addr: SocketAddr,
tokio_rt: Arc<Runtime>,
endpoint: Arc<Endpoint>,
incoming_rx: mpsc::Receiver<IncomingPacket>,
incoming_tx: mpsc::SyncSender<IncomingPacket>,
peers: Arc<std::sync::Mutex<HashMap<NodeId, QuicPeer>>>,
}
impl QuicTransport {
pub fn bind(addr: SocketAddr, node_id: NodeId) -> io::Result<Self> {
let tokio_rt = Arc::new(Runtime::new().map_err(|e| io::Error::other(e.to_string()))?);
let cert = rcgen::generate_simple_self_signed(vec!["localhost".into()])
.map_err(|e| io::Error::other(e.to_string()))?;
let cert_der = cert.cert.der().clone();
let priv_key = cert.signing_key.serialize_der();
let priv_key =
PrivateKeyDer::try_from(priv_key).map_err(|e| io::Error::other(e.to_string()))?;
let server_config = ServerConfig::with_single_cert(vec![cert_der], priv_key)
.map_err(|e| io::Error::other(e.to_string()))?;
let endpoint =
Arc::new(tokio_rt.block_on(async { Endpoint::server(server_config, addr) })?);
let listen_addr = endpoint.local_addr()?;
let (incoming_tx, incoming_rx) = mpsc::sync_channel(1024);
let peers = Arc::new(std::sync::Mutex::new(HashMap::new()));
// Background accept loop
let accept_ep = Arc::clone(&endpoint);
let accept_tx = incoming_tx.clone();
let accept_peers = Arc::clone(&peers);
let accept_rt = Arc::clone(&tokio_rt);
tokio_rt.spawn(async move {
loop {
match accept_ep.accept().await {
Some(incoming) => {
let tx = accept_tx.clone();
let p = Arc::clone(&accept_peers);
accept_rt.spawn(async move {
if let Err(e) = accept_one(incoming, tx, p).await {
tracing::warn!("nulang-quic accept: {}", e);
}
});
}
None => break,
}
}
});
Ok(QuicTransport {
node_id,
listen_addr,
tokio_rt,
endpoint,
incoming_rx,
incoming_tx,
peers,
})
}
}
async fn accept_one(
incoming: quinn::Incoming,
tx: mpsc::SyncSender<IncomingPacket>,
peers: Arc<std::sync::Mutex<HashMap<NodeId, QuicPeer>>>,
) -> io::Result<()> {
let conn = incoming
.await
.map_err(|e| io::Error::other(e.to_string()))?;
let remote = conn.remote_address();
let (mut send, mut recv) = conn
.accept_bi()
.await
.map_err(|e| io::Error::other(e.to_string()))?;
let mut buf = [0u8; 8];
recv.read_exact(&mut buf)
.await
.map_err(|e| io::Error::other(e.to_string()))?;
let peer_id = NodeId(u64::from_be_bytes(buf));
send.write_all(&0u64.to_be_bytes())
.await
.map_err(|e| io::Error::other(e.to_string()))?;
let (data_send, data_recv) = conn
.accept_bi()
.await
.map_err(|e| io::Error::other(e.to_string()))?;
peers.lock().unwrap().insert(
peer_id,
QuicPeer {
send: data_send,
addr: remote,
},
);
tokio::spawn(read_loop(peer_id, data_recv, tx));
Ok(())
}
async fn read_loop(
peer_id: NodeId,
mut recv: quinn::RecvStream,
tx: mpsc::SyncSender<IncomingPacket>,
) {
loop {
let mut lb = [0u8; 4];
if recv.read_exact(&mut lb).await.is_err() {
break;
}
let len = u32::from_be_bytes(lb) as usize;
if len == 0 || len > 16 * 1024 * 1024 {
break;
}
let mut payload = vec![0u8; len];
if recv.read_exact(&mut payload).await.is_err() {
break;
}
if let Some((seq, packet)) = Packet::from_bytes(&payload) {
let _ = tx.send(IncomingPacket {
from_node: peer_id,
seq,
packet,
});
}
}
}
impl NetworkTransport for QuicTransport {
fn connect(&mut self, nid: NodeId, addr: SocketAddr) -> io::Result<()> {
if self.peers.lock().unwrap().contains_key(&nid) {
return Ok(());
}
let conn = self.tokio_rt.block_on(async {
self.endpoint
.connect(addr, "localhost")
.map_err(|e| io::Error::other(e.to_string()))?
.await
.map_err(|e| io::Error::other(e.to_string()))
})?;
let (mut send, mut recv) = self.tokio_rt.block_on(async {
conn.open_bi()
.await
.map_err(|e| io::Error::other(e.to_string()))
})?;
self.tokio_rt.block_on(async {
send.write_all(&self.node_id.0.to_be_bytes())
.await
.map_err(|e| io::Error::other(e.to_string()))
})?;
let mut buf = [0u8; 8];
self.tokio_rt.block_on(async {
recv.read_exact(&mut buf)
.await
.map_err(|e| io::Error::other(e.to_string()))
})?;
let (data_send, data_recv) = self.tokio_rt.block_on(async {
conn.open_bi()
.await
.map_err(|e| io::Error::other(e.to_string()))
})?;
let tx = self.incoming_tx.clone();
tokio::spawn(read_loop(nid, data_recv, tx));
self.peers.lock().unwrap().insert(
nid,
QuicPeer {
send: data_send,
addr,
},
);
Ok(())
}
fn send(&mut self, to: NodeId, _addr: SocketAddr, packet: Packet) {
if let Some(peer) = self.peers.lock().unwrap().get_mut(&to) {
let payload = packet.to_bytes(0);
let len = (payload.len() as u32).to_be_bytes();
let mut framed = Vec::with_capacity(4 + payload.len());
framed.extend_from_slice(&len);
framed.extend_from_slice(&payload);
let _ = self
.tokio_rt
.block_on(async { peer.send.write_all(&framed).await });
}
}
fn receive(&self) -> Vec<IncomingPacket> {
let mut v = Vec::new();
while let Ok(p) = self.incoming_rx.try_recv() {
v.push(p);
}
v
}
fn node_id(&self) -> NodeId {
self.node_id
}
fn listen_addr(&self) -> SocketAddr {
self.listen_addr
}
fn disconnect(&mut self, nid: NodeId) {
self.peers.lock().unwrap().remove(&nid);
}
fn shutdown(&mut self) {
self.peers.lock().unwrap().clear();
let _ = self.tokio_rt.block_on(async {
self.endpoint.close(0u32.into(), b"shutdown");
});
}
fn connection_count(&self) -> usize {
self.peers.lock().unwrap().len()
}
fn connection_addr(&self, nid: NodeId) -> Option<SocketAddr> {
self.peers.lock().unwrap().get(&nid).map(|p| p.addr)
}
}