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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use crate::{
    GenericServerRef, Listener, Request, Response, Server, ServerConnection, ServerCtx, ServerRef,
    ServerReply, ServerState, TypedAsyncRead, TypedAsyncWrite,
};
use log::*;
use serde::{de::DeserializeOwned, Serialize};
use std::{io, sync::Arc};
use tokio::sync::mpsc;

mod tcp;
pub use tcp::*;

#[cfg(unix)]
mod unix;

#[cfg(unix)]
pub use unix::*;

#[cfg(windows)]
mod windows;

#[cfg(windows)]
pub use windows::*;

/// Extension trait to provide a reference implementation of starting a server
/// that will listen for new connections (exposed as [`TypedAsyncWrite`] and [`TypedAsyncRead`])
/// and process them using the [`Server`] implementation
pub trait ServerExt {
    type Request;
    type Response;

    /// Start a new server using the provided listener
    fn start<L, R, W>(self, listener: L) -> io::Result<Box<dyn ServerRef>>
    where
        L: Listener<Output = (W, R)> + 'static,
        R: TypedAsyncRead<Request<Self::Request>> + Send + 'static,
        W: TypedAsyncWrite<Response<Self::Response>> + Send + 'static;
}

impl<S, Req, Res, Data> ServerExt for S
where
    S: Server<Request = Req, Response = Res, LocalData = Data> + Sync + 'static,
    Req: DeserializeOwned + Send + Sync + 'static,
    Res: Serialize + Send + 'static,
    Data: Default + Send + Sync + 'static,
{
    type Request = Req;
    type Response = Res;

    fn start<L, R, W>(self, listener: L) -> io::Result<Box<dyn ServerRef>>
    where
        L: Listener<Output = (W, R)> + 'static,
        R: TypedAsyncRead<Request<Self::Request>> + Send + 'static,
        W: TypedAsyncWrite<Response<Self::Response>> + Send + 'static,
    {
        let server = Arc::new(self);
        let state = Arc::new(ServerState::new());

        let task = tokio::spawn(task(server, Arc::clone(&state), listener));

        Ok(Box::new(GenericServerRef { state, task }))
    }
}

async fn task<S, Req, Res, Data, L, R, W>(server: Arc<S>, state: Arc<ServerState>, mut listener: L)
where
    S: Server<Request = Req, Response = Res, LocalData = Data> + Sync + 'static,
    Req: DeserializeOwned + Send + Sync + 'static,
    Res: Serialize + Send + 'static,
    Data: Default + Send + Sync + 'static,
    L: Listener<Output = (W, R)> + 'static,
    R: TypedAsyncRead<Request<Req>> + Send + 'static,
    W: TypedAsyncWrite<Response<Res>> + Send + 'static,
{
    loop {
        let server = Arc::clone(&server);
        match listener.accept().await {
            Ok((mut writer, mut reader)) => {
                let mut connection = ServerConnection::new();
                let connection_id = connection.id;
                let state = Arc::clone(&state);

                // Create some default data for the new connection and pass it
                // to the callback prior to processing new requests
                let local_data = {
                    let mut data = Data::default();
                    server.on_accept(&mut data).await;
                    Arc::new(data)
                };

                // Start a writer task that reads from a channel and forwards all
                // data through the writer
                let (tx, mut rx) = mpsc::channel::<Response<Res>>(1);
                connection.writer_task = Some(tokio::spawn(async move {
                    while let Some(data) = rx.recv().await {
                        // trace!("[Conn {}] Sending {:?}", connection_id, data.payload);
                        if let Err(x) = writer.write(data).await {
                            error!("[Conn {}] Failed to send {:?}", connection_id, x);
                            break;
                        }
                    }
                }));

                // Start a reader task that reads requests and processes them
                // using the provided handler
                connection.reader_task = Some(tokio::spawn(async move {
                    loop {
                        match reader.read().await {
                            Ok(Some(request)) => {
                                let reply = ServerReply {
                                    origin_id: request.id.clone(),
                                    tx: tx.clone(),
                                };

                                let ctx = ServerCtx {
                                    connection_id,
                                    request,
                                    reply: reply.clone(),
                                    local_data: Arc::clone(&local_data),
                                };

                                server.on_request(ctx).await;
                            }
                            Ok(None) => {
                                debug!("[Conn {}] Connection closed", connection_id);
                                break;
                            }
                            Err(x) => {
                                // NOTE: We do NOT break out of the loop, as this could happen
                                //       if someone sends bad data at any point, but does not
                                //       mean that the reader itself has failed. This can
                                //       happen from getting non-compliant typed data
                                error!("[Conn {}] {}", connection_id, x);
                            }
                        }
                    }
                }));

                state
                    .connections
                    .write()
                    .await
                    .insert(connection_id, connection);
            }
            Err(x) => {
                error!("Server no longer accepting connections: {}", x);
                break;
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{IntoSplit, MpscListener, MpscTransport};
    use async_trait::async_trait;

    pub struct TestServer;

    #[async_trait]
    impl Server for TestServer {
        type Request = u16;
        type Response = String;
        type LocalData = ();

        async fn on_request(&self, ctx: ServerCtx<Self::Request, Self::Response, Self::LocalData>) {
            // Always send back "hello"
            ctx.reply.send("hello".to_string()).await.unwrap();
        }
    }

    #[tokio::test]
    async fn should_invoke_handler_upon_receiving_a_request() {
        // Create a test listener where we will forward a connection
        let (tx, listener) = MpscListener::channel(100);

        // Make bounded transport pair and send off one of them to act as our connection
        let (mut transport, connection) =
            MpscTransport::<Request<u16>, Response<String>>::pair(100);
        tx.send(connection.into_split())
            .await
            .expect("Failed to feed listener a connection");

        let _server = ServerExt::start(TestServer, listener).expect("Failed to start server");

        transport
            .write(Request::new(123))
            .await
            .expect("Failed to send request");

        let response: Response<String> = transport.read().await.unwrap().unwrap();
        assert_eq!(response.payload, "hello");
    }
}