pub async fn server(config: Config) -> Result<SocketAddr, tokio::io::Error> {
    let listener = TcpListener::bind(&config.tcp_address).await?;

    info!("TCP Server: ONLINE ON [{}], waiting for a client", config.tcp_address);

    loop {
        let (stream, address) = listener.accept().await?;
        info!("TCP Server: client [{}] connected, waiting for flag", address);

        let (stream_read, stream_write) = stream.into_split();
        let mut reader = BufReader::new(stream_read);
        let mut line = String::new();

        reader.read_line(&mut line).await?;

        line.pop();
        if line != config.init_flag.to_string() {
            warn!("TCP Server: invalid flag {:?}", line);
            kick(stream_write, &address).await;
            continue;
        }
        line.clear();

        info!("TCP Server: waiting for UDP address");
        reader.read_line(&mut line).await?;
        line.pop();

        let udp_address = match SocketAddr::from_str(&line) {
            Ok(udp_address) => udp_address,
            Err(error) => {
                warn!("TCP Server: invalid UDP address. {}", error);
                kick(stream_write, &address).await;
                continue;
            }
        };

        info!("TCP Server: initialization complete, shutting down");
        return Ok(udp_address);
    }
}
/// Creates a TCP server simulating SKJ's TCP server behaviour.
///
/// Returns UDP socket address received from the client.
/// Returns an Error on any communication issue.
///
/// If wrong flag or UDP address was provided, it just loops back and waits for another client.
/// This way you don't have to restart the server if something went wrong.
///
/// # Arguments
/// * `config` - A Config structure
///
Creates a TCP server simulating SKJ's TCP server behaviour.
Returns UDP socket address received from the client.
Returns an Error on any communication issue.

If wrong flag or UDP address was provided, it just loops back and waits for another client.
This way you don't have to restart the server if something went wrong.
Creates a TCP server simulating SKJ ' s TCP server behaviour . Returns UDP socket address received from the client . Returns an Error on any communication issue . If wrong flag or UDP address was provided it just loops back and waits for another client . This way you don ' t have to restart the server if something went wrong .
pub async fn server(config: Config) -> Result<SocketAddr, tokio::io::Error> {
    let listener = TcpListener::bind(&config.tcp_address).await?;
    info!("TCP Server: ONLINE ON [{}], waiting for a client", config.tcp_address);
    loop {
        let (stream, address) = listener.accept().await?;
        info!("TCP Server: client [{}] connected, waiting for flag", address);
        let (stream_read, stream_write) = stream.into_split();
        let mut reader = BufReader::new(stream_read);
        let mut line = String::new();
        reader.read_line(&mut line).await?;
        line.pop();
        if line != config.init_flag.to_string() {
            warn!("TCP Server: invalid flag {:?}", line);
            kick(stream_write, &address).await;
            continue;
        }
        line.clear();
        info!("TCP Server: waiting for UDP address");
        reader.read_line(&mut line).await?;
        line.pop();
        let udp_address = match SocketAddr::from_str(&line) {
            Ok(udp_address) => udp_address,
            Err(error) => {
                warn!("TCP Server: invalid UDP address. {}", error);
                kick(stream_write, &address).await;
                continue;
            }
        };
        info!("TCP Server: initialization complete, shutting down");
        return Ok(udp_address);
    }
}
pub async fn server ( config : Config ) -> Result < SocketAddr , tokio :: io :: Error > { let listener = TcpListener :: bind ( & config . tcp_address ) . await ? ; info ! ( " " config tcp_address ) ; loop { let ( stream , address ) = listener . accept ( ) . await ? ; info ! ( " " address ) ; let ( stream_read , stream_write ) = stream . into_split ( ) ; let mut reader = BufReader :: new ( stream_read ) ; let mut line = String :: new ( ) ; reader . read_line ( & mut line ) . await ? ; line . pop ( ) ; if line != config . init_flag . to_string ( ) { warn ! ( " " line ) ; kick ( stream_write , & address ) . await ; continue ; } line . clear ( ) ; info ! ( " " ) ; reader . read_line ( & mut line ) . await ? ; line . pop ( ) ; let udp_address = match SocketAddr :: from_str ( & line ) { Ok ( udp_address ) => udp_address , Err ( error ) => { warn ! ( " " error ) ; kick ( stream_write , & address ) . await ; continue ; } } ; info ! ( " " ) ; return Ok ( udp_address ) ; } }
Creates a TCP server simulating SKJ's TCP server behaviour.
Creates a TCP server simulating SKJ ' s TCP server behaviour .
config
Config