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
use async_trait::async_trait;
use serde::{de::DeserializeOwned, Serialize};

mod config;
pub use config::*;

mod connection;
pub use connection::*;

mod context;
pub use context::*;

mod ext;
pub use ext::*;

mod r#ref;
pub use r#ref::*;

mod reply;
pub use reply::*;

mod state;
pub use state::*;

/// Interface for a general-purpose server that receives requests to handle
#[async_trait]
pub trait Server: Send {
    /// Type of data received by the server
    type Request: DeserializeOwned + Send + Sync;

    /// Type of data sent back by the server
    type Response: Serialize + Send;

    /// Type of data to store locally tied to the specific connection
    type LocalData: Send + Sync;

    /// Returns configuration tied to server instance
    fn config(&self) -> ServerConfig {
        ServerConfig::default()
    }

    /// Invoked immediately on server start, being provided the raw listener to use (untyped
    /// transport), and returning the listener when ready to start (enabling servers that need to
    /// tweak a listener to do so)
    /* async fn on_start(&mut self, listener: L) -> Box<dyn Listener<Output = >> {
    } */

    /// Invoked upon a new connection becoming established, which provides a mutable reference to
    /// the data created for the connection. This can be useful in performing some additional
    /// initialization on the data prior to it being used anywhere else.
    #[allow(unused_variables)]
    async fn on_accept(&self, local_data: &mut Self::LocalData) {}

    /// Invoked upon receiving a request from a client. The server should process this
    /// request, which can be found in `ctx`, and send one or more replies in response.
    async fn on_request(&self, ctx: ServerCtx<Self::Request, Self::Response, Self::LocalData>);
}