pub fn listen(&self, players_arc: PlayerList, core_tx: Sender<Box<dyn NetworkPacket + Send>>) {
        for stream in self.listener.incoming() {
            match stream {
                Ok(stream) => {
                    let try_clone_stream = || -> Result<TcpStream, io::Error> {
                        let timeout_duration = Some(Duration::from_secs(TIMEOUT_TIME));

                        stream.set_write_timeout(timeout_duration)?;
                        stream.set_read_timeout(timeout_duration)?;

                        // This program needs sockets to be non-blocking in order for ThreadPool to work properly.
                        stream.set_nonblocking(true)?; // Panic in case we cannot set our stream into non-blocking mode.

                        let cloned = stream.try_clone()?; // Clone the stream to simultaneously send to and receive from players.

                        Ok(cloned)
                    };

                    match try_clone_stream() {
                        Ok(mut receiver) => {
                            let players = players_arc.clone();

                            let mut found_id = 0;
                            'search: for _ in 1..(std::i8::MAX) as usize - 1 {
                                found_id += 1;

                                if players.contains_key(&(found_id as usize)) {
                                    continue;
                                }

                                break 'search;
                            }

                            let player_uid = found_id;
                            let uid_copy = player_uid.clone();

                            let tx = core_tx.clone();

                            self.net_workers.execute(move || {
                                let mut buffer = vec![0xff; 256];

                                loop {
                                    match receiver.read(&mut buffer) {
                                        Ok(size) => {
                                            if size <= 0 {
                                                // Connection has been closed.
                                                // Inform the core of player's disconnecction so the proper action can be taken.
                                                // TODO: Send a disconnect packet to core, with sender ID 0 (console). (Dispose NEEDED!)
                                                Core::static_log(&format!(
                                                    "Player with uid \"{}\" disconnected.",
                                                    uid_copy
                                                ));

                                                receiver.shutdown(Shutdown::Both).ok();

                                                tx.send(Box::new(DisconnectPlayer::new(
                                                    uid_copy,
                                                    String::from("Server Disconnect"),
                                                )))
                                                .unwrap();

                                                break; // TX clone should automatically be dropped by Rust's Ownership.
                                            }

                                            let mut buffer_reader = BufferReader::new(&buffer);

                                            while buffer_reader.get_index() < size {
                                                // Read the packet's op_code.
                                                // TODO: Check the buffer for corrupt data.
                                                let op_code = buffer_reader.read_byte();

                                                // Handle receiving packets here, and send objects to core.
                                                // TODO: match should return a packet, send must be done outside the match.
                                                match op_code {
                                                    PlayerIdentification::ID => {
                                                        let identify_packet =
                                                            PlayerIdentification::new(
                                                                &mut buffer_reader,
                                                                player_uid,
                                                            );

                                                        tx.send(Box::new(identify_packet)).unwrap();
                                                    }
                                                    PlayerSetBlock::ID => {
                                                        let setblock_packet = PlayerSetBlock::new(
                                                            &mut buffer_reader,
                                                            player_uid,
                                                        );

                                                        tx.send(Box::new(setblock_packet)).unwrap();
                                                    }
                                                    PlayerPositionAndOrientation::ID => {
                                                        let packet =
                                                            PlayerPositionAndOrientation::new(
                                                                &mut buffer_reader,
                                                                player_uid,
                                                            );
                                                        tx.send(Box::new(packet)).unwrap();
                                                    }
                                                    PlayerMessage::ID => {
                                                        let packet = PlayerMessage::new(
                                                            &mut buffer_reader,
                                                            player_uid,
                                                        );
                                                        tx.send(Box::new(packet)).unwrap();
                                                    }
                                                    _ => {}
                                                }
                                                //buffer.clear(); // Clean up the buffer after receiving a packet.
                                            }
                                        }
                                        Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {}
                                        Err(e) => {
                                            Core::static_log(&format!(
                                                "IO error on player received: {}",
                                                e
                                            ));
                                        }
                                    }
                                }
                            });
                            // TODO: Let the core edit players. Insertion should be move into core, not network.
                            let spawned_player = NetworkPlayer::new(uid_copy, stream);
                            players.insert(uid_copy, Box::new(spawned_player));
                        }
                        Err(e) => {
                            Core::static_log(&format!(
                                "Error parameterizing network stream: {}",
                                e
                            ));
                        }
                    }
                }
                Err(err) => {
                    Core::static_log(&format!("Error receiving network stream: {}", err));
                }
            }
        }
    }
/// Locks the current thread, waiting to receive connections.
Locks the current thread, waiting to receive connections.
Locks the current thread waiting to receive connections .
pub fn listen(&self, players_arc: PlayerList, core_tx: Sender<Box<dyn NetworkPacket + Send>>) {
        for stream in self.listener.incoming() {
            match stream {
                Ok(stream) => {
                    let try_clone_stream = || -> Result<TcpStream, io::Error> {
                        let timeout_duration = Some(Duration::from_secs(TIMEOUT_TIME));
                        stream.set_write_timeout(timeout_duration)?;
                        stream.set_read_timeout(timeout_duration)?;
                        // This program needs sockets to be non-blocking in order for ThreadPool to work properly.
                        stream.set_nonblocking(true)?; // Panic in case we cannot set our stream into non-blocking mode.
                        let cloned = stream.try_clone()?; // Clone the stream to simultaneously send to and receive from players.
                        Ok(cloned)
                    };
                    match try_clone_stream() {
                        Ok(mut receiver) => {
                            let players = players_arc.clone();
                            let mut found_id = 0;
                            'search: for _ in 1..(std::i8::MAX) as usize - 1 {
                                found_id += 1;
                                if players.contains_key(&(found_id as usize)) {
                                    continue;
                                }
                                break 'search;
                            }
                            let player_uid = found_id;
                            let uid_copy = player_uid.clone();
                            let tx = core_tx.clone();
                            self.net_workers.execute(move || {
                                let mut buffer = vec![0xff; 256];
                                loop {
                                    match receiver.read(&mut buffer) {
                                        Ok(size) => {
                                            if size <= 0 {
                                                // Connection has been closed.
                                                // Inform the core of player's disconnecction so the proper action can be taken.
                                                // TODO: Send a disconnect packet to core, with sender ID 0 (console). (Dispose NEEDED!)
                                                Core::static_log(&format!(
                                                    "Player with uid \"{}\" disconnected.",
                                                    uid_copy
                                                ));
                                                receiver.shutdown(Shutdown::Both).ok();
                                                tx.send(Box::new(DisconnectPlayer::new(
                                                    uid_copy,
                                                    String::from("Server Disconnect"),
                                                )))
                                                .unwrap();
                                                break; // TX clone should automatically be dropped by Rust's Ownership.
                                            }
                                            let mut buffer_reader = BufferReader::new(&buffer);
                                            while buffer_reader.get_index() < size {
                                                // Read the packet's op_code.
                                                // TODO: Check the buffer for corrupt data.
                                                let op_code = buffer_reader.read_byte();
                                                // Handle receiving packets here, and send objects to core.
                                                // TODO: match should return a packet, send must be done outside the match.
                                                match op_code {
                                                    PlayerIdentification::ID => {
                                                        let identify_packet =
                                                            PlayerIdentification::new(
                                                                &mut buffer_reader,
                                                                player_uid,
                                                            );
                                                        tx.send(Box::new(identify_packet)).unwrap();
                                                    }
                                                    PlayerSetBlock::ID => {
                                                        let setblock_packet = PlayerSetBlock::new(
                                                            &mut buffer_reader,
                                                            player_uid,
                                                        );
                                                        tx.send(Box::new(setblock_packet)).unwrap();
                                                    }
                                                    PlayerPositionAndOrientation::ID => {
                                                        let packet =
                                                            PlayerPositionAndOrientation::new(
                                                                &mut buffer_reader,
                                                                player_uid,
                                                            );
                                                        tx.send(Box::new(packet)).unwrap();
                                                    }
                                                    PlayerMessage::ID => {
                                                        let packet = PlayerMessage::new(
                                                            &mut buffer_reader,
                                                            player_uid,
                                                        );
                                                        tx.send(Box::new(packet)).unwrap();
                                                    }
                                                    _ => {}
                                                }
                                                //buffer.clear(); // Clean up the buffer after receiving a packet.
                                            }
                                        }
                                        Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {}
                                        Err(e) => {
                                            Core::static_log(&format!(
                                                "IO error on player received: {}",
                                                e
                                            ));
                                        }
                                    }
                                }
                            });
                            // TODO: Let the core edit players. Insertion should be move into core, not network.
                            let spawned_player = NetworkPlayer::new(uid_copy, stream);
                            players.insert(uid_copy, Box::new(spawned_player));
                        }
                        Err(e) => {
                            Core::static_log(&format!(
                                "Error parameterizing network stream: {}",
                                e
                            ));
                        }
                    }
                }
                Err(err) => {
                    Core::static_log(&format!("Error receiving network stream: {}", err));
                }
            }
        }
    }
pub fn listen ( & self , players_arc : PlayerList , core_tx : Sender < Box < dyn NetworkPacket + Send > > ) { for stream in self . listener . incoming ( ) { match stream { Ok ( stream ) => { let try_clone_stream = | | -> Result < TcpStream , io :: Error > { let timeout_duration = Some ( Duration :: from_secs ( TIMEOUT_TIME ) ) ; stream . set_write_timeout ( timeout_duration ) ? ; stream . set_read_timeout ( timeout_duration ) ? ; // This program needs sockets to be non-blocking in order for ThreadPool to work properly. stream . set_nonblocking ( true ) ? ; // Panic in case we cannot set our stream into non-blocking mode. let cloned = stream . try_clone ( ) ? ; // Clone the stream to simultaneously send to and receive from players. Ok ( cloned ) } ; match try_clone_stream ( ) { Ok ( mut receiver ) => { let players = players_arc . clone ( ) ; let mut found_id = 0 ; ' search : for _ in 1 .. ( std :: i8 :: MAX ) as usize - 1 { found_id += 1 ; if players . contains_key ( & ( found_id as usize ) ) { continue ; } break ' search ; } let player_uid = found_id ; let uid_copy = player_uid . clone ( ) ; let tx = core_tx . clone ( ) ; self . net_workers . execute ( move | | { let mut buffer = vec ! [ 0xff 256 ] ; loop { match receiver . read ( & mut buffer ) { Ok ( size ) => { if size <= 0 { // Connection has been closed. // Inform the core of player's disconnecction so the proper action can be taken. // TODO: Send a disconnect packet to core, with sender ID 0 (console). (Dispose NEEDED!) Core :: static_log ( & format ! ( " \" \" " uid_copy ) ) ; receiver . shutdown ( Shutdown :: Both ) . ok ( ) ; tx . send ( Box :: new ( DisconnectPlayer :: new ( uid_copy , String :: from ( " " ) , ) ) ) . unwrap ( ) ; break ; // TX clone should automatically be dropped by Rust's Ownership. } let mut buffer_reader = BufferReader :: new ( & buffer ) ; while buffer_reader . get_index ( ) < size { // Read the packet's op_code. // TODO: Check the buffer for corrupt data. let op_code = buffer_reader . read_byte ( ) ; // Handle receiving packets here, and send objects to core. // TODO: match should return a packet, send must be done outside the match. match op_code { PlayerIdentification :: ID => { let identify_packet = PlayerIdentification :: new ( & mut buffer_reader , player_uid , ) ; tx . send ( Box :: new ( identify_packet ) ) . unwrap ( ) ; } PlayerSetBlock :: ID => { let setblock_packet = PlayerSetBlock :: new ( & mut buffer_reader , player_uid , ) ; tx . send ( Box :: new ( setblock_packet ) ) . unwrap ( ) ; } PlayerPositionAndOrientation :: ID => { let packet = PlayerPositionAndOrientation :: new ( & mut buffer_reader , player_uid , ) ; tx . send ( Box :: new ( packet ) ) . unwrap ( ) ; } PlayerMessage :: ID => { let packet = PlayerMessage :: new ( & mut buffer_reader , player_uid , ) ; tx . send ( Box :: new ( packet ) ) . unwrap ( ) ; } _ => { } } //buffer.clear(); // Clean up the buffer after receiving a packet. } } Err ( ref e ) if e . kind ( ) == io :: ErrorKind :: WouldBlock => { } Err ( e ) => { Core :: static_log ( & format ! ( " " e ) ) ; } } } } ) ; // TODO: Let the core edit players. Insertion should be move into core, not network. let spawned_player = NetworkPlayer :: new ( uid_copy , stream ) ; players . insert ( uid_copy , Box :: new ( spawned_player ) ) ; } Err ( e ) => { Core :: static_log ( & format ! ( " " e ) ) ; } } } Err ( err ) => { Core :: static_log ( & format ! ( " " err ) ) ; } } } }
Locks the current thread, waiting to receive connections.
Locks the current thread waiting to receive connections .
&self
players_arc
PlayerList
core_tx
Sender<Box<dyn NetworkPacket + Send>>
&self
players_arc
PlayerList
core_tx
Sender<Box<dyn NetworkPacket + Send>>