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;
pub struct ServerConnection {
pub id: ConnectionId,
pub(crate) reader_task: Option<JoinHandle<()>>,
pub(crate) writer_task: Option<JoinHandle<()>>,
}
impl Default for ServerConnection {
fn default() -> Self {
Self::new()
}
}
impl ServerConnection {
pub fn new() -> Self {
Self {
id: rand::random(),
reader_task: None,
writer_task: None,
}
}
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
}
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();
}
}
}