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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
use std::io::{Error, ErrorKind};
use std::net::{SocketAddr, ToSocketAddrs};
use std::sync::mpsc::TryRecvError;
use std::collections::VecDeque;
use shared::stats::{Stats, StatsCollector};
use shared::ticker::Ticker;
use super::{
Config,
Connection, ConnectionEvent,
RateLimiter, PacketModifier, Socket
};
#[derive(Debug, PartialEq)]
pub enum ClientEvent {
Connection,
ConnectionFailed,
ConnectionLost(bool),
ConnectionClosed(bool),
Message(Vec<u8>),
PacketLost(Vec<u8>),
ConnectionCongestionStateChanged(bool)
}
#[derive(Debug)]
pub struct Client<S: Socket, R: RateLimiter, M: PacketModifier> {
config: Config,
socket: Option<S>,
connection: Option<Connection<R, M>>,
ticker: Ticker,
peer_address: Option<SocketAddr>,
local_address: Option<SocketAddr>,
events: VecDeque<ClientEvent>,
should_receive: bool,
stats_collector: StatsCollector,
stats: Stats
}
impl<S: Socket, R: RateLimiter, M: PacketModifier> Client<S, R, M> {
pub fn new(config: Config) -> Client<S, R, M> {
Client {
config: config,
socket: None,
connection: None,
ticker: Ticker::new(config),
peer_address: None,
local_address: None,
events: VecDeque::new(),
should_receive: false,
stats_collector: StatsCollector::new(config),
stats: Stats {
bytes_sent: 0,
bytes_received: 0
}
}
}
pub fn bytes_sent(&self) -> u32 {
self.stats.bytes_sent
}
pub fn bytes_received(&self) -> u32 {
self.stats.bytes_received
}
pub fn peer_addr(&self) -> Result<SocketAddr, Error> {
self.peer_address.ok_or_else(|| Error::new(ErrorKind::AddrNotAvailable, ""))
}
pub fn local_addr(&self) -> Result<SocketAddr, Error> {
self.local_address.ok_or_else(|| Error::new(ErrorKind::AddrNotAvailable, ""))
}
pub fn connection(&mut self) -> Result<&mut Connection<R, M>, Error> {
if let Some(connection) = self.connection.as_mut() {
Ok(connection)
} else {
Err(Error::new(ErrorKind::NotConnected, ""))
}
}
pub fn socket(&mut self) -> Result<&mut S, Error> {
if let Some(socket) = self.socket.as_mut() {
Ok(socket)
} else {
Err(Error::new(ErrorKind::NotConnected, ""))
}
}
pub fn config(&self) -> Config {
self.config
}
pub fn set_config(&mut self, config: Config) {
self.config = config;
self.ticker.set_config(config);
self.stats_collector.set_config(config);
if let Some(connection) = self.connection.as_mut() {
connection.set_config(config);
}
}
pub fn connect<A: ToSocketAddrs>(&mut self, addr: A) -> Result<(), Error> {
if self.socket.is_none() {
let socket = try!(S::new(
"0.0.0.0:0",
self.config.packet_max_size
));
let peer_addr = try!(addr.to_socket_addrs()).nth(0).unwrap();
let local_addr = try!(socket.local_addr());
self.socket = Some(socket);
self.peer_address = Some(peer_addr);
self.local_address = Some(local_addr);
self.connection = Some(Connection::new(
self.config,
local_addr,
peer_addr,
R::new(self.config),
M::new(self.config)
));
self.should_receive = true;
Ok(())
} else {
Err(Error::new(ErrorKind::AlreadyExists, ""))
}
}
pub fn receive(&mut self) -> Result<ClientEvent, TryRecvError> {
if self.socket.is_none() {
Err(TryRecvError::Disconnected)
} else {
if self.should_receive {
self.ticker.begin_tick();
let peer_address = self.peer_address.unwrap();
let mut bytes_received = 0;
while let Ok((addr, packet)) = self.socket.as_mut().unwrap().try_recv() {
if addr == peer_address {
bytes_received += packet.len();
self.connection.as_mut().unwrap().receive_packet(packet);
}
}
self.stats_collector.set_bytes_received(bytes_received as u32);
for e in self.connection.as_mut().unwrap().events() {
self.events.push_back(match e {
ConnectionEvent::Connected => ClientEvent::Connection,
ConnectionEvent::FailedToConnect => ClientEvent::ConnectionFailed,
ConnectionEvent::Lost(by_remote) => ClientEvent::ConnectionLost(by_remote),
ConnectionEvent::Closed(by_remote) => ClientEvent::ConnectionClosed(by_remote),
ConnectionEvent::Message(payload) => ClientEvent::Message(payload),
ConnectionEvent::CongestionStateChanged(c) => ClientEvent::ConnectionCongestionStateChanged(c),
ConnectionEvent::PacketLost(payload) => ClientEvent::PacketLost(payload)
});
}
self.should_receive = false;
}
if let Some(event) = self.events.pop_front() {
Ok(event)
} else {
Err(TryRecvError::Empty)
}
}
}
pub fn send(&mut self, auto_tick: bool) -> Result<(), Error> {
if self.socket.is_some() {
let peer_address = self.peer_address.unwrap();
let bytes_sent = self.connection.as_mut().unwrap().send_packet(
self.socket.as_mut().unwrap(),
&peer_address
);
self.stats_collector.set_bytes_sent(bytes_sent);
self.stats_collector.tick();
self.stats = self.stats_collector.average();
self.should_receive = true;
if auto_tick {
self.ticker.end_tick();
}
Ok(())
} else {
Err(Error::new(ErrorKind::NotConnected, ""))
}
}
pub fn reset(&mut self) -> Result<(), Error> {
if self.socket.is_some() {
self.connection.as_mut().unwrap().reset();
self.stats_collector.reset();
self.stats.reset();
self.events.clear();
self.ticker.reset();
Ok(())
} else {
Err(Error::new(ErrorKind::NotConnected, ""))
}
}
pub fn disconnect(&mut self) -> Result<(), Error> {
if self.socket.is_some() {
self.reset().ok();
self.should_receive = false;
self.peer_address = None;
self.local_address = None;
self.connection = None;
self.socket = None;
Ok(())
} else {
Err(Error::new(ErrorKind::NotConnected, ""))
}
}
}