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
use std::net::SocketAddr;
use shrev::{EventChannel, EventIterator, ReaderId};
use uuid::Uuid;
use amethyst_core::specs::{Component, VecStorage};
use super::NetEvent;
#[derive(Serialize)]
#[serde(bound = "")]
pub struct NetConnection<E: 'static> {
pub target: SocketAddr,
pub state: ConnectionState,
#[serde(skip)]
pub send_buffer: EventChannel<NetEvent<E>>,
#[serde(skip)]
pub receive_buffer: EventChannel<NetEvent<E>>,
#[serde(skip)]
send_reader: ReaderId<NetEvent<E>>,
}
impl<E: Send + Sync + 'static> NetConnection<E> {
pub fn new(target: SocketAddr) -> Self {
let mut send_buffer = EventChannel::new();
let send_reader = send_buffer.register_reader();
NetConnection {
target,
state: ConnectionState::Connecting,
send_buffer,
receive_buffer: EventChannel::<NetEvent<E>>::new(),
send_reader,
}
}
pub fn send_buffer_early_read(&mut self) -> EventIterator<'_, NetEvent<E>> {
self.send_buffer.read(&mut self.send_reader)
}
}
impl<E> PartialEq for NetConnection<E> {
fn eq(&self, other: &Self) -> bool {
self.target == other.target && self.state == other.state
}
}
impl<E: PartialEq> Eq for NetConnection<E> {}
impl<E: Send + Sync + 'static> Component for NetConnection<E> {
type Storage = VecStorage<Self>;
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ConnectionState {
Connected,
Connecting,
Disconnected,
}
pub struct NetIdentity {
pub uuid: Uuid,
}
impl Default for NetIdentity {
fn default() -> Self {
NetIdentity {
uuid: Uuid::new_v4(),
}
}
}
impl Component for NetIdentity {
type Storage = VecStorage<NetIdentity>;
}