2020-03-10 00:07:36 +00:00
|
|
|
use lazy_static::*;
|
2020-03-22 13:47:21 +00:00
|
|
|
use std::{
|
2022-01-29 17:18:30 +00:00
|
|
|
net::{Ipv4Addr, SocketAddr},
|
2021-01-15 13:04:32 +00:00
|
|
|
sync::{
|
2021-02-10 10:37:42 +00:00
|
|
|
atomic::{AtomicU16, AtomicU64, Ordering},
|
2021-01-15 13:04:32 +00:00
|
|
|
Arc,
|
|
|
|
},
|
2020-03-22 13:47:21 +00:00
|
|
|
thread,
|
|
|
|
time::Duration,
|
|
|
|
};
|
2021-01-15 13:04:32 +00:00
|
|
|
use tokio::runtime::Runtime;
|
2020-03-10 00:07:36 +00:00
|
|
|
use tracing::*;
|
|
|
|
use tracing_subscriber::EnvFilter;
|
2021-04-15 08:16:42 +00:00
|
|
|
use veloren_network::{ConnectAddr, ListenAddr, Network, Participant, Pid, Promises, Stream};
|
2020-03-10 00:07:36 +00:00
|
|
|
|
2021-11-19 08:36:39 +00:00
|
|
|
// sleep time when only internal rust calculations are done
|
|
|
|
#[allow(dead_code)]
|
2022-07-15 12:08:04 +00:00
|
|
|
pub const SLEEP_INTERNAL: Duration = Duration::from_millis(3000);
|
2021-11-19 08:36:39 +00:00
|
|
|
// sleep time when we interact with the system, e.g. actually send TCP/UDP
|
|
|
|
// package
|
|
|
|
#[allow(dead_code)]
|
2022-07-15 12:08:04 +00:00
|
|
|
pub const SLEEP_EXTERNAL: Duration = Duration::from_millis(5000);
|
2021-11-19 08:36:39 +00:00
|
|
|
|
2020-05-27 15:58:57 +00:00
|
|
|
#[allow(dead_code)]
|
2020-08-21 14:21:00 +00:00
|
|
|
pub fn setup(tracing: bool, sleep: u64) -> (u64, u64) {
|
2020-03-10 00:07:36 +00:00
|
|
|
if sleep > 0 {
|
|
|
|
thread::sleep(Duration::from_millis(sleep));
|
|
|
|
}
|
|
|
|
|
|
|
|
let _subscriber = if tracing {
|
|
|
|
let filter = EnvFilter::from_default_env()
|
|
|
|
.add_directive("trace".parse().unwrap())
|
|
|
|
.add_directive("veloren_network::tests=trace".parse().unwrap())
|
|
|
|
.add_directive("veloren_network::controller=trace".parse().unwrap())
|
|
|
|
.add_directive("veloren_network::channel=trace".parse().unwrap())
|
|
|
|
.add_directive("veloren_network::message=trace".parse().unwrap())
|
|
|
|
.add_directive("veloren_network::metrics=trace".parse().unwrap())
|
2020-04-08 14:26:42 +00:00
|
|
|
.add_directive("veloren_network::types=trace".parse().unwrap());
|
2020-03-10 00:07:36 +00:00
|
|
|
|
|
|
|
Some(
|
|
|
|
tracing_subscriber::FmtSubscriber::builder()
|
|
|
|
// all spans/events with a level higher than TRACE (e.g, info, warn, etc.)
|
|
|
|
// will be written to stdout.
|
|
|
|
.with_max_level(Level::TRACE)
|
|
|
|
.with_env_filter(filter)
|
|
|
|
// sets this to be the default, global subscriber for this application.
|
|
|
|
.try_init(),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2020-03-22 13:47:21 +00:00
|
|
|
(0, 0)
|
|
|
|
}
|
|
|
|
|
2020-05-27 15:58:57 +00:00
|
|
|
#[allow(dead_code)]
|
2021-01-15 13:04:32 +00:00
|
|
|
pub fn network_participant_stream(
|
2021-04-15 08:16:42 +00:00
|
|
|
addr: (ListenAddr, ConnectAddr),
|
2021-01-15 13:04:32 +00:00
|
|
|
) -> (
|
|
|
|
Arc<Runtime>,
|
|
|
|
Network,
|
|
|
|
Participant,
|
|
|
|
Stream,
|
|
|
|
Network,
|
|
|
|
Participant,
|
|
|
|
Stream,
|
|
|
|
) {
|
|
|
|
let runtime = Arc::new(Runtime::new().unwrap());
|
|
|
|
let (n_a, p1_a, s1_a, n_b, p1_b, s1_b) = runtime.block_on(async {
|
2021-03-03 09:39:21 +00:00
|
|
|
let n_a = Network::new(Pid::fake(0), &runtime);
|
|
|
|
let n_b = Network::new(Pid::fake(1), &runtime);
|
2020-04-08 14:26:42 +00:00
|
|
|
|
2021-04-15 08:16:42 +00:00
|
|
|
n_a.listen(addr.0).await.unwrap();
|
|
|
|
let p1_b = n_b.connect(addr.1).await.unwrap();
|
2021-01-15 13:04:32 +00:00
|
|
|
let p1_a = n_a.connected().await.unwrap();
|
2020-04-08 14:26:42 +00:00
|
|
|
|
2021-04-15 08:16:42 +00:00
|
|
|
let s1_a = p1_a.open(4, Promises::ORDERED, 0).await.unwrap();
|
2021-01-15 13:04:32 +00:00
|
|
|
let s1_b = p1_b.opened().await.unwrap();
|
2020-04-08 14:26:42 +00:00
|
|
|
|
2021-01-15 13:04:32 +00:00
|
|
|
(n_a, p1_a, s1_a, n_b, p1_b, s1_b)
|
|
|
|
});
|
|
|
|
(runtime, n_a, p1_a, s1_a, n_b, p1_b, s1_b)
|
2020-04-08 14:26:42 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 15:58:57 +00:00
|
|
|
#[allow(dead_code)]
|
2021-04-15 08:16:42 +00:00
|
|
|
pub fn tcp() -> (ListenAddr, ConnectAddr) {
|
2020-03-22 13:47:21 +00:00
|
|
|
lazy_static! {
|
|
|
|
static ref PORTS: AtomicU16 = AtomicU16::new(5000);
|
|
|
|
}
|
|
|
|
let port = PORTS.fetch_add(1, Ordering::Relaxed);
|
2021-04-15 08:16:42 +00:00
|
|
|
(
|
2022-01-29 17:18:30 +00:00
|
|
|
ListenAddr::Tcp(SocketAddr::from((Ipv4Addr::LOCALHOST, port))),
|
|
|
|
ConnectAddr::Tcp(SocketAddr::from((Ipv4Addr::LOCALHOST, port))),
|
2021-04-15 08:16:42 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref UDP_PORTS: AtomicU16 = AtomicU16::new(5000);
|
2020-03-10 00:07:36 +00:00
|
|
|
}
|
2020-04-08 14:26:42 +00:00
|
|
|
|
2020-05-27 15:58:57 +00:00
|
|
|
#[allow(dead_code)]
|
2021-04-15 08:16:42 +00:00
|
|
|
pub fn quic() -> (ListenAddr, ConnectAddr) {
|
|
|
|
const LOCALHOST: &str = "localhost";
|
|
|
|
let port = UDP_PORTS.fetch_add(1, Ordering::Relaxed);
|
|
|
|
|
|
|
|
trace!("generating self-signed certificate");
|
|
|
|
let cert = rcgen::generate_simple_self_signed(vec![LOCALHOST.into()]).unwrap();
|
|
|
|
let key = cert.serialize_private_key_der();
|
|
|
|
let cert = cert.serialize_der().unwrap();
|
|
|
|
|
2021-11-19 14:40:59 +00:00
|
|
|
let key = rustls::PrivateKey(key);
|
|
|
|
let cert = rustls::Certificate(cert);
|
2021-04-15 08:16:42 +00:00
|
|
|
|
2021-11-19 14:40:59 +00:00
|
|
|
let mut root_store = rustls::RootCertStore::empty();
|
|
|
|
root_store.add(&cert).expect("cannot add cert to rootstore");
|
2021-04-15 08:16:42 +00:00
|
|
|
|
2021-11-19 14:40:59 +00:00
|
|
|
let server_config = quinn::ServerConfig::with_single_cert(vec![cert], key)
|
|
|
|
.expect("Server Config Cert/Key failed");
|
|
|
|
let client_config = quinn::ClientConfig::with_root_certificates(root_store);
|
2022-01-29 17:18:30 +00:00
|
|
|
use std::net::IpAddr;
|
2021-04-15 08:16:42 +00:00
|
|
|
(
|
2021-11-19 14:40:59 +00:00
|
|
|
ListenAddr::Quic(
|
|
|
|
SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), port),
|
|
|
|
server_config,
|
|
|
|
),
|
2021-04-15 08:16:42 +00:00
|
|
|
ConnectAddr::Quic(
|
2021-11-19 14:40:59 +00:00
|
|
|
SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), port),
|
2021-04-15 08:16:42 +00:00
|
|
|
client_config,
|
|
|
|
LOCALHOST.to_owned(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn udp() -> (ListenAddr, ConnectAddr) {
|
|
|
|
let port = UDP_PORTS.fetch_add(1, Ordering::Relaxed);
|
|
|
|
(
|
|
|
|
ListenAddr::Udp(SocketAddr::from(([127, 0, 0, 1], port))),
|
|
|
|
ConnectAddr::Udp(SocketAddr::from(([127, 0, 0, 1], port))),
|
|
|
|
)
|
2020-04-08 14:26:42 +00:00
|
|
|
}
|
2021-02-10 10:37:42 +00:00
|
|
|
|
|
|
|
#[allow(dead_code)]
|
2021-04-15 08:16:42 +00:00
|
|
|
pub fn mpsc() -> (ListenAddr, ConnectAddr) {
|
2021-02-10 10:37:42 +00:00
|
|
|
lazy_static! {
|
|
|
|
static ref PORTS: AtomicU64 = AtomicU64::new(5000);
|
|
|
|
}
|
|
|
|
let port = PORTS.fetch_add(1, Ordering::Relaxed);
|
2021-04-15 08:16:42 +00:00
|
|
|
(ListenAddr::Mpsc(port), ConnectAddr::Mpsc(port))
|
2021-02-10 10:37:42 +00:00
|
|
|
}
|