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
use crate::client::{ClientId, MessageId};
use core::id::ID;
use std::ops::Range;
pub type ServerId = ID<()>;
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ServerState {
Starting,
Open,
Closed,
}
impl Default for ServerState {
fn default() -> Self {
Self::Closed
}
}
pub trait Server: Send + Sync + Sized {
fn open(url: &str) -> Option<Self>;
fn close(self) -> Self;
fn id(&self) -> ServerId;
fn state(&self) -> ServerState;
fn clients(&self) -> &[ClientId];
fn disconnect(&mut self, id: ClientId);
fn disconnect_all(&mut self);
fn send(&mut self, id: ClientId, msg_id: MessageId, data: &[u8]) -> Option<Range<usize>>;
fn send_all(&mut self, id: MessageId, data: &[u8]);
fn read(&mut self) -> Option<(ClientId, MessageId, Vec<u8>)>;
fn read_all(&mut self) -> Vec<(ClientId, MessageId, Vec<u8>)> {
let mut result = vec![];
while let Some(msg) = self.read() {
result.push(msg);
}
result
}
fn process(&mut self) {}
}
impl Server for () {
fn open(_: &str) -> Option<Self> {
Some(())
}
fn close(self) -> Self {
self
}
fn id(&self) -> ServerId {
Default::default()
}
fn state(&self) -> ServerState {
ServerState::Closed
}
fn clients(&self) -> &[ClientId] {
&[]
}
fn disconnect(&mut self, _: ClientId) {}
fn disconnect_all(&mut self) {}
fn send(&mut self, _: ClientId, _: MessageId, _: &[u8]) -> Option<Range<usize>> {
None
}
fn send_all(&mut self, _: MessageId, _: &[u8]) {}
fn read(&mut self) -> Option<(ClientId, MessageId, Vec<u8>)> {
None
}
fn read_all(&mut self) -> Vec<(ClientId, MessageId, Vec<u8>)> {
vec![]
}
}