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
use serde::{Deserialize, Serialize};
use shrev::{EventChannel, EventIterator, ReaderId};
use std::{
fmt::{Debug, Formatter},
net::SocketAddr,
};
use uuid::Uuid;
use amethyst_core::ecs::{Component, VecStorage};
use crate::NetEvent;
#[derive(Serialize)]
#[serde(bound = "")]
#[allow(missing_debug_implementations)]
pub struct NetConnection<E: 'static> {
pub target_addr: SocketAddr,
pub state: ConnectionState,
#[serde(skip)]
send_buffer: EventChannel<NetEvent<E>>,
#[serde(skip)]
pub(crate) receive_buffer: EventChannel<NetEvent<E>>,
#[serde(skip)]
send_reader: ReaderId<NetEvent<E>>,
}
impl<E: Send + Sync + 'static> NetConnection<E> {
pub fn new(target_addr: SocketAddr) -> Self {
let mut send_buffer = EventChannel::new();
let send_reader = send_buffer.register_reader();
NetConnection {
target_addr,
state: ConnectionState::Connecting,
send_buffer,
receive_buffer: EventChannel::<NetEvent<E>>::new(),
send_reader,
}
}
pub(crate) fn send_buffer_early_read(&mut self) -> EventIterator<'_, NetEvent<E>> {
self.send_buffer.read(&mut self.send_reader)
}
pub fn queue_iter<I>(&mut self, iter: I)
where
I: IntoIterator<Item = NetEvent<E>>,
I::IntoIter: ExactSizeIterator,
{
self.send_buffer.iter_write(iter);
}
pub fn queue_vec(&mut self, events: &mut Vec<NetEvent<E>>) {
self.send_buffer.drain_vec_write(events);
}
pub fn queue(&mut self, event: NetEvent<E>) {
self.send_buffer.single_write(event);
}
pub fn received_events(
&self,
reader_id: &mut ReaderId<NetEvent<E>>,
) -> EventIterator<'_, NetEvent<E>> {
self.receive_buffer.read(reader_id)
}
pub fn register_reader(&mut self) -> ReaderId<NetEvent<E>> {
self.receive_buffer.register_reader()
}
}
impl<E> PartialEq for NetConnection<E> {
fn eq(&self, other: &Self) -> bool {
self.target_addr == other.target_addr
}
}
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,
}
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct NetIdentity {
pub uuid: Uuid,
}
impl Debug for NetIdentity {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let uuid_str = self.uuid.to_hyphenated().to_string();
f.debug_struct("NetIdentity")
.field("uuid", &uuid_str)
.finish()
}
}
impl Default for NetIdentity {
fn default() -> Self {
NetIdentity {
uuid: Uuid::new_v4(),
}
}
}
impl Component for NetIdentity {
type Storage = VecStorage<NetIdentity>;
}
#[cfg(test)]
mod tests {
use crate::{connection::NetConnection, net_event::NetEvent};
#[test]
fn can_read_received_events() {
let mut connection = test_connection();
let mut reader_id = connection.register_reader();
connection
.receive_buffer
.single_write(NetEvent::Connected("127.0.0.1:0".parse().unwrap()));
assert_eq!(connection.received_events(&mut reader_id).len(), 1);
}
#[test]
fn can_queue_packet_for_send() {
let mut connection = test_connection();
connection.queue(NetEvent::Connected("127.0.0.1:0".parse().unwrap()));
assert_eq!(connection.send_buffer_early_read().len(), 1);
}
#[test]
fn can_queue_vec_for_send() {
let mut connection = test_connection();
let mut to_send_data = Vec::new();
to_send_data.push(NetEvent::Connected("127.0.0.1:0".parse().unwrap()));
to_send_data.push(NetEvent::Connected("127.0.0.1:0".parse().unwrap()));
to_send_data.push(NetEvent::Connected("127.0.0.1:0".parse().unwrap()));
connection.queue_vec(&mut to_send_data);
assert_eq!(
connection
.send_buffer
.read(&mut connection.send_reader)
.len(),
3
);
}
fn test_connection() -> NetConnection<String> {
NetConnection::new("127.0.0.1:0".parse().unwrap())
}
}