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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};

use tokio::{
    net::{TcpListener, TcpStream},
    task::JoinHandle,
};

use crate::{global::Global, models::ServerConfig};

pub mod boblight;
pub mod flat;
pub mod json;
pub mod proto;

pub struct ServerHandle {
    join_handle: JoinHandle<()>,
}

pub async fn bind<T, E, F, H>(
    name: &'static str,
    options: T,
    global: Global,
    handle_client: H,
) -> std::io::Result<ServerHandle>
where
    T: ServerConfig + Send + 'static,
    F: futures::Future<Output = Result<(), E>> + Send + 'static,
    E: From<std::io::Error> + std::fmt::Display + Send + 'static,
    H: Fn((TcpStream, SocketAddr), Global) -> F + Send + 'static,
{
    // Compute binding address
    let address = SocketAddrV4::new(Ipv4Addr::new(0, 0, 0, 0), options.port());

    // Setup listener
    let listener = TcpListener::bind(&address).await?;

    // Notify we are listening
    info!(address = %address, "{} server listening", name);

    // Spawn accepting loop
    let join_handle = tokio::spawn(async move {
        let result: Result<(), _> = loop {
            match listener.accept().await {
                Ok(incoming) => {
                    tokio::spawn({
                        let peer_addr = incoming.1;
                        let ft = handle_client(incoming, global.clone());

                        async move {
                            let result = ft.await;

                            match result {
                                Ok(_) => {
                                    info!(peer_addr = %peer_addr, "client disconnected");
                                }
                                Err(error) => {
                                    error!(peer_addr = %peer_addr, error = %error, "client error");
                                }
                            }
                        }
                    });
                }
                Err(error) => break Err(error),
            }
        };

        if let Err(error) = result {
            error!(error = %error, "{} server terminated", name);
        }
    });

    Ok(ServerHandle { join_handle })
}

impl Drop for ServerHandle {
    fn drop(&mut self) {
        self.join_handle.abort();
    }
}