Skip to content

Commit 76b93ec

Browse files
committed
Cannot compile: &UtpStream is not Send
1 parent 1d074ee commit 76b93ec

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

examples/recv_file.rs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use rustorrent::utp::{listener, UtpError, stream::UtpStream, UtpState};
2+
use std::env;
3+
use std::sync::Arc;
4+
use shared_arena::SharedArena;
5+
6+
async fn handle_conn(conn: UtpStream, arena: Arc<SharedArena<[u8; 64]>>) -> Result<(), UtpError> {
7+
log::debug!("pending on read stream");
8+
let mut buf = arena.alloc_with(|b|{
9+
let buf_ref = unsafe { &*(b.as_mut_ptr() as *mut [u8; 64])};
10+
buf_ref
11+
});
12+
let _size = conn.read(buf.as_mut()).await?;
13+
log::debug!("received data: {}", String::from_utf8_lossy(buf.as_ref()));
14+
Ok(())
15+
}
16+
17+
async fn run() -> Result<(), UtpError> {
18+
let mut args = env::args();
19+
args.next();
20+
let listen_addr = args.next().expect("listen addr must input");
21+
let l = listener::UtpListener::bind(listen_addr).await;
22+
let arena: SharedArena<[u8; 64]> = SharedArena::new();
23+
let arena_ref = Arc::new(arena);
24+
loop {
25+
let (conn, peer) = l.accept().await;
26+
log::debug!("accepting connection from {:?}", peer);
27+
tokio::spawn(handle_conn(conn, arena_ref.clone()));
28+
}
29+
}
30+
31+
fn main() {
32+
env_logger::init();
33+
let rt = tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap();
34+
let fut = async {
35+
run().await.map_err(|e| log::debug!("run exit with error: {:?}", e))
36+
};
37+
let _ = rt.block_on(fut);
38+
}

examples/send_file.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use std::env;
2+
use rustorrent::utp::{listener, UtpError};
3+
use std::net::SocketAddr;
4+
use tokio::fs;
5+
use tokio::io::AsyncWriteExt;
6+
7+
async fn run() -> Result<(), UtpError> {
8+
let mut args = env::args();
9+
args.next();
10+
let listen_ip = args.next().expect("listen ip must input");
11+
let remote_addr_arg = args.next().expect("remote address must input");
12+
let remote_addr: SocketAddr = remote_addr_arg.parse().unwrap();
13+
let file_name = args.next().expect("send file name must input");
14+
log::debug!("before open");
15+
let mut f = fs::File::open(file_name).await?;
16+
log::debug!("before connect");
17+
let l = listener::UtpListener::bind(format!("{}:0", listen_ip)).await;
18+
let mut conn = l.connect(remote_addr).await?;
19+
conn.write_all("hello".as_bytes()).await?;
20+
Ok(())
21+
}
22+
fn main() {
23+
env_logger::init();
24+
let rt = tokio::runtime::Builder::new_multi_thread().enable_all().build().unwrap();
25+
let fut = async {
26+
run().await.map_err(|e| log::debug!("run exit with error: {:?}", e));
27+
};
28+
let _ = rt.block_on(fut);
29+
}

0 commit comments

Comments
 (0)