/// Starts the tcp listener for incoming connections
///
/// # Panics
///
/// This should really not panic
/// TODO
///
Starts the tcp listener for incoming connections
Starts the tcp listener for incoming connections
pub async fn listen_tcp(&self) {
        let listener = TcpListener::bind(&self.addr).await.unwrap();
        loop {
            let entries_copy = self.entries.clone();
            let (mut socket, _) = listener.accept().await.unwrap();
            tokio::spawn(async move {
                let mut cmd: [u8; 1] = [0; 1];
                loop {
                    let mut buf_socket = tokio::io::BufStream::new(socket);
                    match buf_socket.read(&mut cmd).await {
                        // socket closed
                        Ok(n) if n == 0 => return,
                        Ok(n) => n,
                        Err(e) => {
                            eprintln!("failed to read from socket; err = {:?}", e);
                            return;
                        }
                    };
                    let cmd = Command::from(cmd[0]);
                    if cmd == Command::Ping {
                        buf_socket.write_u8(Command::Pong.into()).await.unwrap();
                        buf_socket.flush().await.unwrap();
                        socket = buf_socket.into_inner();
                        continue;
                    }
                    let cache = buf_socket.read_u8().await.unwrap();
                    if let Some(e) = entries_copy.get(&cache) {
                        e.handle(cmd, &mut buf_socket).await;
                    } else {
                        log::error!("Could not find cache");
                    }
                    buf_socket.flush().await.unwrap();
                    // return the socket so that we don´t consume it
                    socket = buf_socket.into_inner();
                }
            });
        }
    }
pub async fn listen_tcp ( & self ) { let listener = TcpListener :: bind ( & self . addr ) . await . unwrap ( ) ; loop { let entries_copy = self . entries . clone ( ) ; let ( mut socket , _ ) = listener . accept ( ) . await . unwrap ( ) ; tokio :: spawn ( async move { let mut cmd : [ u8 ; 1 ] = [ 0 ; 1 ] ; loop { let mut buf_socket = tokio :: io :: BufStream :: new ( socket ) ; match buf_socket . read ( & mut cmd ) . await { // socket closed Ok ( n ) if n == 0 => return , Ok ( n ) => n , Err ( e ) => { eprintln ! ( " " e ) ; return ; } } ; let cmd = Command :: from ( cmd [ 0 ] ) ; if cmd == Command :: Ping { buf_socket . write_u8 ( Command :: Pong . into ( ) ) . await . unwrap ( ) ; buf_socket . flush ( ) . await . unwrap ( ) ; socket = buf_socket . into_inner ( ) ; continue ; } let cache = buf_socket . read_u8 ( ) . await . unwrap ( ) ; if let Some ( e ) = entries_copy . get ( & cache ) { e . handle ( cmd , & mut buf_socket ) . await ; } else { log :: error ! ( " " ) ; } buf_socket . flush ( ) . await . unwrap ( ) ; // return the socket so that we don´t consume it socket = buf_socket . into_inner ( ) ; } } ) ; } }
Starts the tcp listener for incoming connections
Starts the tcp listener for incoming connections
&self
panics
This should really not panic
TODO
This should really not panic TODO