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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
|
use std::{
collections::HashMap,
error::Error,
net::{AddrParseError, IpAddr, SocketAddr},
sync::Arc,
};
use bevy::prelude::{error, info};
use bytes::Bytes;
use quinn::{ClientConfig, Endpoint};
use quinn_proto::ConnectionStats;
use serde::Deserialize;
use tokio::sync::{
broadcast,
mpsc::{
self,
error::{TryRecvError, TrySendError},
},
};
use crate::shared::{
channel::{
channels_task, get_channel_id_from_type, reliable_receiver_task, unreliable_receiver_task,
Channel, ChannelAsyncMessage, ChannelId, ChannelSyncMessage, ChannelType, MultiChannelId,
},
InternalConnectionRef, QuinnetError, DEFAULT_KILL_MESSAGE_QUEUE_SIZE,
DEFAULT_MESSAGE_QUEUE_SIZE,
};
use super::{
certificate::{
load_known_hosts_store_from_config, CertificateVerificationMode, SkipServerVerification,
TofuServerVerification,
},
ClientAsyncMessage,
};
pub type ConnectionId = u64;
/// Connection event raised when the client just connected to the server. Raised in the CoreStage::PreUpdate stage.
pub struct ConnectionEvent {
pub id: ConnectionId,
}
/// ConnectionLost event raised when the client is considered disconnected from the server. Raised in the CoreStage::PreUpdate stage.
pub struct ConnectionLostEvent {
pub id: ConnectionId,
}
/// Configuration of a client connection, used when connecting to a server
#[derive(Debug, Deserialize, Clone)]
pub struct ConnectionConfiguration {
server_addr: SocketAddr,
server_hostname: String,
local_bind_addr: SocketAddr,
}
impl ConnectionConfiguration {
/// Creates a new ConnectionConfiguration
///
/// # Arguments
///
/// * `server_addr_str` - IP address and port of the server
/// * `local_bind_addr_str` - Local address and port to bind to separated by `:`. The address should usually be a wildcard like `0.0.0.0` (for an IPv4) or `[::]` (for an IPv6), which allow communication with any reachable IPv4 or IPv6 address. See [`std::net::SocketAddrV4`] and [`std::net::SocketAddrV6`] or [`quinn::endpoint::Endpoint`] for more precision. For the local port to bind to, use 0 to get an OS-assigned port.
///
/// # Examples
///
/// Connect to an IPv4 server hosted on localhost (127.0.0.1), which is listening on port 6000. Use 0 as a local bind port to let the OS assign a port.
/// ```
/// use bevy_quinnet::client::connection::ConnectionConfiguration;
/// let config = ConnectionConfiguration::from_strings(
/// "127.0.0.1:6000",
/// "0.0.0.0:0"
/// );
/// ```
/// Connect to an IPv6 server hosted on localhost (::1), which is listening on port 6000. Use 0 as a local bind port to let the OS assign a port.
/// ```
/// use bevy_quinnet::client::connection::ConnectionConfiguration;
/// let config = ConnectionConfiguration::from_strings(
/// "[::1]:6000",
/// "[::]:0"
/// );
/// ```
pub fn from_strings(
server_addr_str: &str,
local_bind_addr_str: &str,
) -> Result<Self, AddrParseError> {
let server_addr = server_addr_str.parse()?;
let local_bind_addr = local_bind_addr_str.parse()?;
Ok(Self::from_addrs(server_addr, local_bind_addr))
}
/// Same as [`ConnectionConfiguration::from_strings`], but with an additional `server_hostname` for certificate verification if it is not just the server IP.
pub fn from_strings_with_name(
server_addr_str: &str,
server_hostname: String,
local_bind_addr_str: &str,
) -> Result<Self, AddrParseError> {
Ok(Self::from_addrs_with_name(
server_addr_str.parse()?,
server_hostname,
local_bind_addr_str.parse()?,
))
}
/// Creates a new ConnectionConfiguration
///
/// # Arguments
///
/// * `server_ip` - IP address of the server
/// * `server_port` - Port of the server
/// * `local_bind_ip` - Local IP address to bind to. The address should usually be a wildcard like `0.0.0.0` (for an IPv4) or `0:0:0:0:0:0:0:0` (for an IPv6), which allow communication with any reachable IPv4 or IPv6 address. See [`std::net::Ipv4Addr`] and [`std::net::Ipv6Addr`] for more precision.
/// * `local_bind_port` - Local port to bind to. Use 0 to get an OS-assigned port.
///
/// # Examples
///
/// Connect to an IPv4 server hosted on localhost (127.0.0.1), which is listening on port 6000. Use 0 as a local bind port to let the OS assign a port.
/// ```
/// use bevy_quinnet::client::connection::ConnectionConfiguration;
/// let config = ConnectionConfiguration::from_ips(
/// IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
/// 6000,
/// IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)),
/// 0
/// );
/// ```
pub fn from_ips(
server_ip: IpAddr,
server_port: u16,
local_bind_ip: IpAddr,
local_bind_port: u16,
) -> Self {
Self::from_addrs(
SocketAddr::new(server_ip, server_port),
SocketAddr::new(local_bind_ip, local_bind_port),
)
}
/// Same as [`ConnectionConfiguration::from_ips`], but with an additional `server_hostname` for certificate verification if it is not just the server IP.
pub fn from_ips_with_name(
server_ip: IpAddr,
server_port: u16,
server_hostname: String,
local_bind_ip: IpAddr,
local_bind_port: u16,
) -> Self {
Self::from_addrs_with_name(
SocketAddr::new(server_ip, server_port),
server_hostname,
SocketAddr::new(local_bind_ip, local_bind_port),
)
}
/// Creates a new ConnectionConfiguration
///
/// # Arguments
///
/// * `server_addr` - IP address and port of the server
/// * `local_bind_addr` - Local address and port to bind to. For the local port to bind to, use 0 to get an OS-assigned port.
///
/// # Examples
///
/// Connect to an IPv4 server hosted on localhost (127.0.0.1), which is listening on port 6000. Use 0 as a local bind port to let the OS assign a port.
/// ```
/// use bevy_quinnet::client::connection::ConnectionConfiguration;
/// use std::{net::{IpAddr, Ipv4Addr, SocketAddr}};
/// let config = ConnectionConfiguration::from_addrs(
/// SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 6000),
/// SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0),
/// );
/// ```
pub fn from_addrs(server_addr: SocketAddr, local_bind_addr: SocketAddr) -> Self {
Self {
server_addr,
server_hostname: server_addr.ip().to_string(),
local_bind_addr,
}
}
/// Same as [`ConnectionConfiguration::from_addrs`], but with an additional `server_hostname` for certificate verification if it is not just the server IP.
pub fn from_addrs_with_name(
server_addr: SocketAddr,
server_hostname: String,
local_bind_addr: SocketAddr,
) -> Self {
Self {
server_addr,
server_hostname,
local_bind_addr,
}
}
}
/// Current state of a client connection
#[derive(Debug)]
pub(crate) enum ConnectionState {
Connecting,
Connected(InternalConnectionRef),
Disconnected,
}
#[derive(Debug)]
pub struct Connection {
pub(crate) state: ConnectionState,
channels: HashMap<ChannelId, Channel>,
default_channel: Option<ChannelId>,
last_gen_id: MultiChannelId,
bytes_from_server_recv: mpsc::Receiver<Bytes>,
close_sender: broadcast::Sender<()>,
pub(crate) from_async_client_recv: mpsc::Receiver<ClientAsyncMessage>,
pub(crate) to_channels_send: mpsc::Sender<ChannelSyncMessage>,
pub(crate) from_channels_recv: mpsc::Receiver<ChannelAsyncMessage>,
}
impl Connection {
pub(crate) fn new(
bytes_from_server_recv: mpsc::Receiver<Bytes>,
close_sender: broadcast::Sender<()>,
from_async_client_recv: mpsc::Receiver<ClientAsyncMessage>,
to_channels_send: mpsc::Sender<ChannelSyncMessage>,
from_channels_recv: mpsc::Receiver<ChannelAsyncMessage>,
) -> Self {
Self {
state: ConnectionState::Connecting,
channels: HashMap::new(),
last_gen_id: 0,
default_channel: None,
bytes_from_server_recv,
close_sender,
from_async_client_recv,
to_channels_send,
from_channels_recv,
}
}
pub fn receive_message<T: serde::de::DeserializeOwned>(
&mut self,
) -> Result<Option<T>, QuinnetError> {
match self.receive_payload()? {
Some(payload) => match bincode::deserialize(&payload) {
Ok(msg) => Ok(Some(msg)),
Err(_) => Err(QuinnetError::Deserialization),
},
None => Ok(None),
}
}
/// Same as [Connection::receive_message] but will log the error instead of returning it
pub fn try_receive_message<T: serde::de::DeserializeOwned>(&mut self) -> Option<T> {
match self.receive_message() {
Ok(message) => message,
Err(err) => {
error!("try_receive_message: {}", err);
None
}
}
}
pub fn send_message<T: serde::Serialize>(&self, message: T) -> Result<(), QuinnetError> {
match self.default_channel {
Some(channel) => self.send_message_on(channel, message),
None => Err(QuinnetError::NoDefaultChannel),
}
}
pub fn send_message_on<T: serde::Serialize>(
&self,
channel_id: ChannelId,
message: T,
) -> Result<(), QuinnetError> {
match &self.state {
ConnectionState::Disconnected => Err(QuinnetError::ConnectionClosed),
_ => match self.channels.get(&channel_id) {
Some(channel) => match bincode::serialize(&message) {
Ok(payload) => channel.send_payload(payload.into()),
Err(_) => Err(QuinnetError::Serialization),
},
None => Err(QuinnetError::UnknownChannel(channel_id)),
},
}
}
/// Same as [Connection::send_message] but will log the error instead of returning it
pub fn try_send_message<T: serde::Serialize>(&self, message: T) {
match self.send_message(message) {
Ok(_) => {}
Err(err) => error!("try_send_message: {}", err),
}
}
/// Same as [Connection::send_message_on] but will log the error instead of returning it
pub fn try_send_message_on<T: serde::Serialize>(&self, channel_id: ChannelId, message: T) {
match self.send_message_on(channel_id, message) {
Ok(_) => {}
Err(err) => error!("try_send_message_on: {}", err),
}
}
pub fn send_payload<T: Into<Bytes>>(&self, payload: T) -> Result<(), QuinnetError> {
match self.default_channel {
Some(channel) => self.send_payload_on(channel, payload),
None => Err(QuinnetError::NoDefaultChannel),
}
}
pub fn send_payload_on<T: Into<Bytes>>(
&self,
channel_id: ChannelId,
payload: T,
) -> Result<(), QuinnetError> {
match &self.state {
ConnectionState::Disconnected => Err(QuinnetError::ConnectionClosed),
_ => match self.channels.get(&channel_id) {
Some(channel) => channel.send_payload(payload.into()),
None => Err(QuinnetError::UnknownChannel(channel_id)),
},
}
}
/// Same as [Connection::send_payload] but will log the error instead of returning it
pub fn try_send_payload<T: Into<Bytes>>(&self, payload: T) {
match self.send_payload(payload) {
Ok(_) => {}
Err(err) => error!("try_send_payload: {}", err),
}
}
/// Same as [Connection::send_payload_on] but will log the error instead of returning it
pub fn try_send_payload_on<T: Into<Bytes>>(&self, channel_id: ChannelId, payload: T) {
match self.send_payload_on(channel_id, payload) {
Ok(_) => {}
Err(err) => error!("try_send_payload_on: {}", err),
}
}
pub fn receive_payload(&mut self) -> Result<Option<Bytes>, QuinnetError> {
match &self.state {
ConnectionState::Disconnected => Err(QuinnetError::ConnectionClosed),
_ => match self.bytes_from_server_recv.try_recv() {
Ok(msg_payload) => Ok(Some(msg_payload)),
Err(err) => match err {
TryRecvError::Empty => Ok(None),
TryRecvError::Disconnected => Err(QuinnetError::InternalChannelClosed),
},
},
}
}
/// Same as [Connection::receive_payload] but will log the error instead of returning it
pub fn try_receive_payload(&mut self) -> Option<Bytes> {
match self.receive_payload() {
Ok(payload) => payload,
Err(err) => {
error!("try_receive_payload: {}", err);
None
}
}
}
/// Immediately prevents new messages from being sent on the connection and signal the connection to closes all its background tasks. Before trully closing, the connection will wait for all buffered messages in all its opened channels to be properly sent according to their respective channel type.
pub(crate) fn disconnect(&mut self) -> Result<(), QuinnetError> {
match &self.state {
ConnectionState::Disconnected => Ok(()),
_ => {
self.state = ConnectionState::Disconnected;
match self.close_sender.send(()) {
Ok(_) => Ok(()),
Err(_) => {
// The only possible error for a send is that there is no active receivers, meaning that the tasks are already terminated.
Err(QuinnetError::ConnectionAlreadyClosed)
}
}
}
}
}
pub(crate) fn try_disconnect(&mut self) {
match &self.disconnect() {
Ok(_) => (),
Err(err) => error!("Failed to properly close clonnection: {}", err),
}
}
pub fn is_connected(&self) -> bool {
match self.state {
ConnectionState::Connected(_) => true,
_ => false,
}
}
/// Returns statistics about the current connection if connected.
pub fn stats(&self) -> Option<ConnectionStats> {
match &self.state {
ConnectionState::Connected(connection) => Some(connection.stats()),
_ => None,
}
}
/// Opens a channel of the requested [ChannelType] and returns its [ChannelId].
///
/// By default, when starting a [Connection]], Quinnet creates 1 channel instance of each [ChannelType], each with their own [ChannelId]. Among those, there is a `default` channel which will be used when you don't specify the channel. At startup, this default channel is a [ChannelType::OrderedReliable] channel.
///
/// If no channels were previously opened, the opened channel will be the new default channel.
///
/// Can fail if the Connection is closed.
pub fn open_channel(&mut self, channel_type: ChannelType) -> Result<ChannelId, QuinnetError> {
let channel_id = get_channel_id_from_type(channel_type, || {
self.last_gen_id += 1;
self.last_gen_id
});
match self.channels.contains_key(&channel_id) {
true => Ok(channel_id),
false => self.create_channel(channel_id),
}
}
/// Closes the channel with the corresponding [ChannelId].
///
/// No new messages will be able to be sent on this channel, however, the channel will properly try to send all the messages that were previously pushed to it, according to its [ChannelType], before fully closing.
///
/// If the closed channel is the current default channel, the default channel gets set to `None`.
///
/// Can fail if the [ChannelId] is unknown, or if the channel is already closed.
pub fn close_channel(&mut self, channel_id: ChannelId) -> Result<(), QuinnetError> {
match self.channels.remove(&channel_id) {
Some(channel) => {
if Some(channel_id) == self.default_channel {
self.default_channel = None;
}
channel.close()
}
None => Err(QuinnetError::UnknownChannel(channel_id)),
}
}
/// Set the default channel
pub fn set_default_channel(&mut self, channel_id: ChannelId) {
self.default_channel = Some(channel_id);
}
/// Get the default Channel Id
pub fn get_default_channel(&self) -> Option<ChannelId> {
self.default_channel
}
fn create_channel(&mut self, channel_id: ChannelId) -> Result<ChannelId, QuinnetError> {
let (bytes_to_channel_send, bytes_to_channel_recv) =
mpsc::channel::<Bytes>(DEFAULT_MESSAGE_QUEUE_SIZE);
let (channel_close_send, channel_close_recv) =
mpsc::channel(DEFAULT_KILL_MESSAGE_QUEUE_SIZE);
match self
.to_channels_send
.try_send(ChannelSyncMessage::CreateChannel {
channel_id,
bytes_to_channel_recv,
channel_close_recv,
}) {
Ok(_) => {
let channel = Channel::new(bytes_to_channel_send, channel_close_send);
self.channels.insert(channel_id, channel);
if self.default_channel.is_none() {
self.default_channel = Some(channel_id);
}
Ok(channel_id)
}
Err(err) => match err {
TrySendError::Full(_) => Err(QuinnetError::FullQueue),
TrySendError::Closed(_) => Err(QuinnetError::InternalChannelClosed),
},
}
}
}
pub(crate) async fn connection_task(
connection_id: ConnectionId,
config: ConnectionConfiguration,
cert_mode: CertificateVerificationMode,
to_sync_client_send: mpsc::Sender<ClientAsyncMessage>,
to_channels_recv: mpsc::Receiver<ChannelSyncMessage>,
from_channels_send: mpsc::Sender<ChannelAsyncMessage>,
close_recv: broadcast::Receiver<()>,
bytes_from_server_send: mpsc::Sender<Bytes>,
) {
info!(
"Connection {} trying to connect to server on: {} ...",
connection_id, config.server_addr
);
let client_cfg = configure_client(cert_mode, to_sync_client_send.clone())
.expect("Failed to configure client");
let mut endpoint =
Endpoint::client(config.local_bind_addr).expect("Failed to create client endpoint");
endpoint.set_default_client_config(client_cfg);
let connection = endpoint
.connect(config.server_addr, &config.server_hostname)
.expect("Failed to connect: configuration error")
.await;
match connection {
Err(e) => error!(
"Connection {}, error while connecting: {}",
connection_id, e
),
Ok(connection) => {
info!(
"Connection {} connected to {}",
connection_id,
connection.remote_address()
);
to_sync_client_send
.send(ClientAsyncMessage::Connected(connection.clone()))
.await
.expect("Failed to signal connection to sync client");
// Spawn a task to listen for the underlying connection being closed
{
let conn = connection.clone();
let to_sync_client = to_sync_client_send.clone();
tokio::spawn(async move {
let conn_err = conn.closed().await;
info!("Connection {} disconnected: {}", connection_id, conn_err);
// If we requested the connection to close, channel may have been closed already.
if !to_sync_client.is_closed() {
to_sync_client
.send(ClientAsyncMessage::ConnectionClosed(conn_err))
.await
.expect("Failed to signal connection lost to sync client");
}
})
};
// Spawn a task to listen for streams opened by the server
{
let close_recv = close_recv.resubscribe();
let connection_handle = connection.clone();
let bytes_incoming_send = bytes_from_server_send.clone();
tokio::spawn(async move {
reliable_receiver_task(
connection_id,
connection_handle,
close_recv,
bytes_incoming_send,
)
.await
});
}
// Spawn a task to listen for datagrams sent by the server
{
let close_recv = close_recv.resubscribe();
let connection_handle = connection.clone();
let bytes_incoming_send = bytes_from_server_send.clone();
tokio::spawn(async move {
unreliable_receiver_task(
connection_id,
connection_handle,
close_recv,
bytes_incoming_send,
)
.await
});
}
// Spawn a task to handle send channels for this connection
tokio::spawn(async move {
channels_task(connection, close_recv, to_channels_recv, from_channels_send).await
});
}
}
}
fn configure_client(
cert_mode: CertificateVerificationMode,
to_sync_client: mpsc::Sender<ClientAsyncMessage>,
) -> Result<ClientConfig, Box<dyn Error>> {
match cert_mode {
CertificateVerificationMode::SkipVerification => {
let crypto = rustls::ClientConfig::builder()
.with_safe_defaults()
.with_custom_certificate_verifier(SkipServerVerification::new())
.with_no_client_auth();
Ok(ClientConfig::new(Arc::new(crypto)))
}
CertificateVerificationMode::SignedByCertificateAuthority => {
Ok(ClientConfig::with_native_roots())
}
CertificateVerificationMode::TrustOnFirstUse(config) => {
let (store, store_file) = load_known_hosts_store_from_config(config.known_hosts)?;
let crypto = rustls::ClientConfig::builder()
.with_safe_defaults()
.with_custom_certificate_verifier(TofuServerVerification::new(
store,
config.verifier_behaviour,
to_sync_client,
store_file,
))
.with_no_client_auth();
Ok(ClientConfig::new(Arc::new(crypto)))
}
}
}
|