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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
use crate::{
    utils::Timer, GenericServerRef, Listener, Request, Response, Server, ServerConnection,
    ServerCtx, ServerRef, ServerReply, ServerState, Shutdown, TypedAsyncRead, TypedAsyncWrite,
};
use log::*;
use serde::{de::DeserializeOwned, Serialize};
use std::{
    io,
    sync::{Arc, Weak},
};
use tokio::sync::{mpsc, Mutex};

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,
{
    // Grab a copy of our server's configuration so we can leverage it below
    let config = server.config();

    // Create the timer that will be used shutdown the server after duration elapsed
    let (shutdown_tx, mut shutdown_rx) = mpsc::channel(1);

    // NOTE: We do a manual map such that the shutdown sender is not captured and dropped when
    //       there is no shutdown after configured. This is because we need the future for the
    //       shutdown receiver to last forever in the event that there is no shutdown configured,
    //       not return immediately, which is what would happen if the sender was dropped.
    #[allow(clippy::manual_map)]
    let mut shutdown_timer = match config.shutdown {
        // Create a timer, start it, and drop it so it will always happen
        Shutdown::After(duration) => {
            Timer::new(duration, async move {
                let _ = shutdown_tx.send(()).await;
            })
            .start();
            None
        }
        Shutdown::Lonely(duration) => Some(Timer::new(duration, async move {
            let _ = shutdown_tx.send(()).await;
        })),
        Shutdown::Never => None,
    };

    if let Some(timer) = shutdown_timer.as_mut() {
        info!(
            "Server shutdown timer configured: {}s",
            timer.duration().as_secs_f32()
        );
        timer.start();
    }

    let mut shutdown_timer = shutdown_timer.map(|timer| Arc::new(Mutex::new(timer)));

    loop {
        let server = Arc::clone(&server);

        // Receive a new connection, exiting if no longer accepting connections or if the shutdown
        // signal has been received
        let (mut writer, mut reader) = tokio::select! {
            result = listener.accept() => {
                match result {
                    Ok(x) => x,
                    Err(x) => {
                        error!("Server no longer accepting connections: {}", x);
                        if let Some(timer) = shutdown_timer.take() {
                            timer.lock().await.abort();
                        }
                        break;
                    }
                }
            }
            _ = shutdown_rx.recv() => {
                info!(
                    "Server shutdown triggered after {}s",
                    config.shutdown.duration().unwrap_or_default().as_secs_f32(),
                );
                break;
            }
        };

        let mut connection = ServerConnection::new();
        let connection_id = connection.id;
        let state = Arc::clone(&state);

        // Ensure that the shutdown timer is cancelled now that we have a connection
        if let Some(timer) = shutdown_timer.as_ref() {
            timer.lock().await.stop();
        }

        // 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
        let weak_state = Arc::downgrade(&state);
        let weak_shutdown_timer = shutdown_timer
            .as_ref()
            .map(Arc::downgrade)
            .unwrap_or_default();
        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);

                        // Remove the connection from our state if it has closed
                        if let Some(state) = Weak::upgrade(&weak_state) {
                            state.connections.write().await.remove(&connection_id);

                            // If we have no more connections, start the timer
                            if let Some(timer) = Weak::upgrade(&weak_shutdown_timer) {
                                if state.connections.read().await.is_empty() {
                                    timer.lock().await.start();
                                }
                            }
                        }
                        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);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        IntoSplit, MpscListener, MpscTransport, MpscTransportReadHalf, MpscTransportWriteHalf,
        ServerConfig,
    };
    use async_trait::async_trait;
    use std::time::Duration;

    pub struct TestServer(ServerConfig);

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

        fn config(&self) -> ServerConfig {
            self.0.clone()
        }

        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();
        }
    }

    #[allow(clippy::type_complexity)]
    fn make_listener(
        buffer: usize,
    ) -> (
        mpsc::Sender<(
            MpscTransportWriteHalf<Response<String>>,
            MpscTransportReadHalf<Request<u16>>,
        )>,
        MpscListener<(
            MpscTransportWriteHalf<Response<String>>,
            MpscTransportReadHalf<Request<u16>>,
        )>,
    ) {
        MpscListener::channel(buffer)
    }

    #[tokio::test]
    async fn should_invoke_handler_upon_receiving_a_request() {
        // Create a test listener where we will forward a connection
        let (tx, listener) = make_listener(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(ServerConfig::default()), 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");
    }

    #[tokio::test]
    async fn should_lonely_shutdown_if_no_connections_received_after_n_secs_when_config_set() {
        let (_tx, listener) = make_listener(100);

        let server = ServerExt::start(
            TestServer(ServerConfig {
                shutdown: Shutdown::Lonely(Duration::from_millis(100)),
            }),
            listener,
        )
        .expect("Failed to start server");

        // Wait for some time
        tokio::time::sleep(Duration::from_millis(300)).await;

        assert!(server.is_finished(), "Server shutdown not triggered!");
    }

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

        // Make bounded transport pair and send off one of them to act as our connection
        let (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(ServerConfig {
                shutdown: Shutdown::Lonely(Duration::from_millis(100)),
            }),
            listener,
        )
        .expect("Failed to start server");

        // Drop the connection by dropping the transport
        drop(transport);

        // Wait for some time
        tokio::time::sleep(Duration::from_millis(300)).await;

        assert!(server.is_finished(), "Server shutdown not triggered!");
    }

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

        // Make bounded transport pair and send off one of them to act as our connection
        let (_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(ServerConfig {
                shutdown: Shutdown::Lonely(Duration::from_millis(100)),
            }),
            listener,
        )
        .expect("Failed to start server");

        // Wait for some time
        tokio::time::sleep(Duration::from_millis(300)).await;

        assert!(!server.is_finished(), "Server shutdown when it should not!");
    }

    #[tokio::test]
    async fn should_shutdown_after_n_seconds_even_with_connections_if_config_set_to_after() {
        let (tx, listener) = make_listener(100);

        // Make bounded transport pair and send off one of them to act as our connection
        let (_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(ServerConfig {
                shutdown: Shutdown::After(Duration::from_millis(100)),
            }),
            listener,
        )
        .expect("Failed to start server");

        // Wait for some time
        tokio::time::sleep(Duration::from_millis(300)).await;

        assert!(server.is_finished(), "Server shutdown not triggered!");
    }

    #[tokio::test]
    async fn should_shutdown_after_n_seconds_if_config_set_to_after() {
        let (_tx, listener) = make_listener(100);

        let server = ServerExt::start(
            TestServer(ServerConfig {
                shutdown: Shutdown::After(Duration::from_millis(100)),
            }),
            listener,
        )
        .expect("Failed to start server");

        // Wait for some time
        tokio::time::sleep(Duration::from_millis(300)).await;

        assert!(server.is_finished(), "Server shutdown not triggered!");
    }

    #[tokio::test]
    async fn should_never_shutdown_if_config_set_to_never() {
        let (_tx, listener) = make_listener(100);

        let server = ServerExt::start(
            TestServer(ServerConfig {
                shutdown: Shutdown::Never,
            }),
            listener,
        )
        .expect("Failed to start server");

        // Wait for some time
        tokio::time::sleep(Duration::from_millis(300)).await;

        assert!(!server.is_finished(), "Server shutdown when it should not!");
    }
}