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
use crate::ConnectionId;
use tokio::task::JoinHandle;

/// Represents an individual connection on the server
pub struct ServerConnection {
    /// Unique identifier tied to the connection
    pub id: ConnectionId,

    /// Task that is processing incoming requests from the connection
    pub(crate) reader_task: Option<JoinHandle<()>>,

    /// Task that is processing outgoing responses to the connection
    pub(crate) writer_task: Option<JoinHandle<()>>,
}

impl Default for ServerConnection {
    fn default() -> Self {
        Self::new()
    }
}

impl ServerConnection {
    /// Creates a new connection, generating a unique id to represent the connection
    pub fn new() -> Self {
        Self {
            id: rand::random(),
            reader_task: None,
            writer_task: None,
        }
    }

    /// Returns true if connection is still processing incoming or outgoing messages
    pub fn is_active(&self) -> bool {
        let reader_active =
            self.reader_task.is_some() && !self.reader_task.as_ref().unwrap().is_finished();
        let writer_active =
            self.writer_task.is_some() && !self.writer_task.as_ref().unwrap().is_finished();
        reader_active || writer_active
    }

    /// Aborts the connection
    pub fn abort(&self) {
        if let Some(task) = self.reader_task.as_ref() {
            task.abort();
        }

        if let Some(task) = self.writer_task.as_ref() {
            task.abort();
        }
    }
}