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
use std::io;
use std;
use std::time::Duration;
use resend_predicates;
pub trait UdpLike: Sized {
fn send(&mut self, buf: &[u8]) -> io::Result<usize>;
fn recv(&mut self, buf: &mut [u8]) -> io::Result<usize>;
}
impl UdpLike for std::net::UdpSocket {
fn send(&mut self, buf: &[u8]) -> io::Result<usize> {
std::net::UdpSocket::send(self, buf)
}
fn recv(&mut self, buf: &mut [u8]) -> io::Result<usize> {
std::net::UdpSocket::recv(self, buf)
}
}
#[derive(Derivative)]
#[derivative(Debug)]
pub struct EndpointConfig {
pub max_msg_size: usize,
pub buffer_grow_space: usize,
pub window_size: u32,
pub new_set_unsent_action: NewSetUnsent,
#[derivative(Debug="ignore")]
pub resend_predicate: Box<FnMut(u32, Duration) -> bool>,
pub min_heartbeat_period: Duration,
}
impl EndpointConfig {
pub fn default() -> Self {
EndpointConfig {
max_msg_size: 2048,
buffer_grow_space: 2048,
window_size: 64,
new_set_unsent_action: NewSetUnsent::Panic,
resend_predicate: Box::new(resend_predicates::medium_combination),
min_heartbeat_period: Duration::from_millis(180),
}
}
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum NewSetUnsent {
Panic,
Clear,
IntoSet,
}
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum Guarantee {
None,
Order,
Delivery,
}
pub trait Sender: io::Write {
fn send_written(&mut self, Guarantee) -> io::Result<usize>;
fn clear_written(&mut self);
fn write_send(&mut self, data: &[u8], g: Guarantee) -> io::Result<(usize, usize)> {
let a = self.write(data)?;
Ok((a, self.send_written(g)?))
}
fn send_payload(&mut self, data: &[u8], g: Guarantee) -> io::Result<usize> {
self.clear_written();
self.write(data)?;
self.send_written(g)
}
}