pub fn listen(opts: &utils::Opts) -> Result<()> {
    match opts.transport {
        utils::Protocol::Tcp => {
            let listener = TcpListener::bind(format!("{}:{}", opts.host, opts.port))?;
            print_started_listen(opts);

            let (mut stream, _) = listener.accept()?;
            match &opts.mode {
                Mode::History => {
                    #[cfg(unix)]
                    {
                        termios_handler::setup_fd()?;
                        listen_tcp_normal(stream, opts)?;
                    }
                    print_error("Normal history is not supported on your platform");
                }
                Mode::LocalHistory => {
                    let t = pipe_thread(stream.try_clone()?, stdout());
                    print_connection_recieved();
                    readline_decorator(|command| {
                        stream
                            .write_all((command + "\n").as_bytes())
                            .expect("Failed to send TCP.");
                    })?;
                    t.join().unwrap();
                }
                Mode::Normal => {
                    listen_tcp_normal(stream, opts)?;
                }
            }
        }

        utils::Protocol::Udp => {
            // Can be made better probably...
            // Rustline is needed here because otherwise you can't delete characters
            let socket = UdpSocket::bind(format!("{}:{}", opts.host, opts.port))?;
            let socket_clone = socket.try_clone()?;

            print_started_listen(opts);

            use std::sync::{Arc, Mutex};
            let addr: Arc<Mutex<Option<SocketAddr>>> = Arc::from(Mutex::new(None));
            let addr_clone = addr.clone();

            std::thread::spawn(move || loop {
                let mut buffer = [0; 1024];
                if let Ok((len, src_addr)) = socket_clone.recv_from(&mut buffer) {
                    *addr_clone.lock().unwrap() = Some(src_addr);
                    stdout().write_all(&buffer[..len]).unwrap();
                    stdout().flush().unwrap();
                }
            });

            loop {
                readline_decorator(|command| {
                    if let Some(addr) = *addr.lock().unwrap() {
                        socket.send_to((command + "\n").as_bytes(), addr).unwrap();
                    }
                })?;
            }
        }
    }
    Ok(())
}
/* Listen on given host and port */
Listen on given host and port
Listen on given host and port
pub fn listen(opts: &utils::Opts) -> Result<()> {
    match opts.transport {
        utils::Protocol::Tcp => {
            let listener = TcpListener::bind(format!("{}:{}", opts.host, opts.port))?;
            print_started_listen(opts);
            let (mut stream, _) = listener.accept()?;
            match &opts.mode {
                Mode::History => {
                    #[cfg(unix)]
                    {
                        termios_handler::setup_fd()?;
                        listen_tcp_normal(stream, opts)?;
                    }
                    print_error("Normal history is not supported on your platform");
                }
                Mode::LocalHistory => {
                    let t = pipe_thread(stream.try_clone()?, stdout());
                    print_connection_recieved();
                    readline_decorator(|command| {
                        stream
                            .write_all((command + "\n").as_bytes())
                            .expect("Failed to send TCP.");
                    })?;
                    t.join().unwrap();
                }
                Mode::Normal => {
                    listen_tcp_normal(stream, opts)?;
                }
            }
        }
        utils::Protocol::Udp => {
            // Can be made better probably...
            // Rustline is needed here because otherwise you can't delete characters
            let socket = UdpSocket::bind(format!("{}:{}", opts.host, opts.port))?;
            let socket_clone = socket.try_clone()?;
            print_started_listen(opts);
            use std::sync::{Arc, Mutex};
            let addr: Arc<Mutex<Option<SocketAddr>>> = Arc::from(Mutex::new(None));
            let addr_clone = addr.clone();
            std::thread::spawn(move || loop {
                let mut buffer = [0; 1024];
                if let Ok((len, src_addr)) = socket_clone.recv_from(&mut buffer) {
                    *addr_clone.lock().unwrap() = Some(src_addr);
                    stdout().write_all(&buffer[..len]).unwrap();
                    stdout().flush().unwrap();
                }
            });
            loop {
                readline_decorator(|command| {
                    if let Some(addr) = *addr.lock().unwrap() {
                        socket.send_to((command + "\n").as_bytes(), addr).unwrap();
                    }
                })?;
            }
        }
    }
    Ok(())
}
pub fn listen ( opts : & utils :: Opts ) -> Result < ( ) > { match opts . transport { utils :: Protocol :: Tcp => { let listener = TcpListener :: bind ( format ! ( " " opts host opts port ) ) ? ; print_started_listen ( opts ) ; let ( mut stream , _ ) = listener . accept ( ) ? ; match & opts . mode { Mode :: History => { # [ cfg ( unix ) ] { termios_handler :: setup_fd ( ) ? ; listen_tcp_normal ( stream , opts ) ? ; } print_error ( " " ) ; } Mode :: LocalHistory => { let t = pipe_thread ( stream . try_clone ( ) ? , stdout ( ) ) ; print_connection_recieved ( ) ; readline_decorator ( | command | { stream . write_all ( ( command + " \n " ) . as_bytes ( ) ) . expect ( " " ) ; } ) ? ; t . join ( ) . unwrap ( ) ; } Mode :: Normal => { listen_tcp_normal ( stream , opts ) ? ; } } } utils :: Protocol :: Udp => { // Can be made better probably... // Rustline is needed here because otherwise you can't delete characters let socket = UdpSocket :: bind ( format ! ( " " opts host opts port ) ) ? ; let socket_clone = socket . try_clone ( ) ? ; print_started_listen ( opts ) ; use std :: sync :: { Arc , Mutex } ; let addr : Arc < Mutex < Option < SocketAddr > > > = Arc :: from ( Mutex :: new ( None ) ) ; let addr_clone = addr . clone ( ) ; std :: thread :: spawn ( move | | loop { let mut buffer = [ 0 ; 1024 ] ; if let Ok ( ( len , src_addr ) ) = socket_clone . recv_from ( & mut buffer ) { * addr_clone . lock ( ) . unwrap ( ) = Some ( src_addr ) ; stdout ( ) . write_all ( & buffer [ .. len ] ) . unwrap ( ) ; stdout ( ) . flush ( ) . unwrap ( ) ; } } ) ; loop { readline_decorator ( | command | { if let Some ( addr ) = * addr . lock ( ) . unwrap ( ) { socket . send_to ( ( command + " \n " ) . as_bytes ( ) , addr ) . unwrap ( ) ; } } ) ? ; } } } Ok ( ( ) ) }
Listen on given host and port
Listen on given host and port
opts
&utils::Opts