mirror of
https://github.com/zhboner/realm.git
synced 2026-11-17 16:17:22 +00:00
51 lines
1.5 KiB
Rust
51 lines
1.5 KiB
Rust
use std::net::SocketAddr;
|
|
|
|
use tokio::net::{TcpStream, TcpListener};
|
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
|
|
|
use realm::relay::run_tcp;
|
|
use realm::utils::{Endpoint, ConnectOpts};
|
|
use realm::utils::timeoutfut;
|
|
|
|
#[tokio::test]
|
|
async fn zero_copy() {
|
|
env_logger::init();
|
|
let endpoint = Endpoint {
|
|
listen: "127.0.0.1:10000".parse().unwrap(),
|
|
remote: "127.0.0.1:20000".parse::<SocketAddr>().unwrap().into(),
|
|
opts: ConnectOpts {
|
|
zero_copy: true,
|
|
..Default::default()
|
|
},
|
|
};
|
|
|
|
tokio::spawn(async {
|
|
let mut stream = TcpStream::connect("127.0.0.1:10000").await.unwrap();
|
|
|
|
let mut buf = vec![0; 32];
|
|
|
|
for _ in 0..20 {
|
|
stream.write(b"Ping Ping Ping").await.unwrap();
|
|
let n = stream.read(&mut buf).await.unwrap();
|
|
log::debug!("a got: {:?}", std::str::from_utf8(&buf[..n]).unwrap());
|
|
assert_eq!(b"Pong Pong Pong", &buf[..n]);
|
|
}
|
|
});
|
|
|
|
tokio::spawn(async {
|
|
let lis = TcpListener::bind("127.0.0.1:20000").await.unwrap();
|
|
let (mut stream, _) = lis.accept().await.unwrap();
|
|
|
|
let mut buf = vec![0; 32];
|
|
|
|
for _ in 0..20 {
|
|
let n = stream.read(&mut buf).await.unwrap();
|
|
log::debug!("b got: {:?}", std::str::from_utf8(&buf[..n]).unwrap());
|
|
assert_eq!(b"Ping Ping Ping", &buf[..n]);
|
|
stream.write(b"Pong Pong Pong").await.unwrap();
|
|
}
|
|
});
|
|
|
|
let _ = timeoutfut(run_tcp((&endpoint).into()), 3).await;
|
|
}
|