1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use std::net::{TcpListener, TcpStream};
use std::io::Read;
use std::thread;


#[path = "client_mem.rs"]
mod client_mem;
#[path = "configuration.rs"]
mod configuration;


/// Handle funnction of the client connection.
fn handle_client(mut client: TcpStream){
    let mut data_msg = [0 as u8; 50];

    while match client.read(&mut data_msg){
        Ok(size) => {
            if size != 0 {
                let ip = client_mem::get_client_ip(&mut client);

                client_mem::send_message_to_clients(ip, &data_msg[0..size]);
                true
            }else{
                client_mem::shutdown_client(&mut client);
                false
            }
        },
            Err(_) => {
                println!("Error on client connection!");
                client_mem::shutdown_client(&mut client);
                false
            }
    }{}
}



pub async fn start_connection(){
    let listener = TcpListener::bind(format!("0.0.0.0:{}", configuration::get_port())).unwrap();
    for client in listener.incoming(){
        match client{
            Ok(mut client) => {
                let ip = client_mem::get_client_ip(&mut client);
                println!("New client is connected! (IP: {})", ip);

                thread::spawn(move|| {
                    if configuration::is_user_allowed(ip) == 1 {
                        client_mem::add_client(&mut client);
                        handle_client(client);
                    }else{
                        client_mem::shutdown_client(&mut client);
                    }
                });
            },
            Err(e) => {
                println!("Error on client: {}", e);
            }
        }
    }

    drop(listener);
}