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
+ }
0 commit comments