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
use std::net::SocketAddr;
use naia_socket_shared::{PacketReader, Timer};
use crate::{message_manager::MessageManager, wrapping_number::wrapping_diff};
use super::{
ack_manager::AckManager, connection_config::ConnectionConfig, manifest::Manifest,
packet_notifiable::PacketNotifiable, packet_type::PacketType, protocol_type::ProtocolType,
replicate::ReplicateSafe, sequence_buffer::SequenceNumber, standard_header::StandardHeader,
};
pub struct BaseConnection<P: ProtocolType> {
address: SocketAddr,
heartbeat_timer: Timer,
timeout_timer: Timer,
ack_manager: AckManager,
message_manager: MessageManager<P>,
last_received_tick: u16,
}
impl<P: ProtocolType> BaseConnection<P> {
pub fn new(address: SocketAddr, config: &ConnectionConfig) -> Self {
return BaseConnection {
address,
heartbeat_timer: Timer::new(config.heartbeat_interval),
timeout_timer: Timer::new(config.disconnection_timeout_duration),
ack_manager: AckManager::new(),
message_manager: MessageManager::new(),
last_received_tick: 0,
};
}
pub fn mark_sent(&mut self) {
return self.heartbeat_timer.reset();
}
pub fn should_send_heartbeat(&self) -> bool {
return self.heartbeat_timer.ringing();
}
pub fn mark_heard(&mut self) {
return self.timeout_timer.reset();
}
pub fn should_drop(&self) -> bool {
return self.timeout_timer.ringing();
}
pub fn process_incoming_header(
&mut self,
header: &StandardHeader,
packet_notifiable: &mut Option<&mut dyn PacketNotifiable>,
) {
if wrapping_diff(self.last_received_tick, header.host_tick()) > 0 {
self.last_received_tick = header.host_tick();
}
self.ack_manager
.process_incoming(&header, &mut self.message_manager, packet_notifiable);
}
pub fn process_outgoing_header(
&mut self,
host_tick: Option<u16>,
last_received_tick: u16,
packet_type: PacketType,
payload: &[u8],
) -> Box<[u8]> {
let mut header_bytes = Vec::new();
let local_packet_index = self.ack_manager.get_local_packet_index();
let last_remote_packet_index = self.ack_manager.get_last_remote_packet_index();
let bit_field = self.ack_manager.get_ack_bitfield();
let header = StandardHeader::new(
packet_type,
local_packet_index,
last_remote_packet_index,
bit_field,
host_tick.unwrap_or(0),
last_received_tick,
);
header.write(&mut header_bytes);
self.ack_manager
.track_packet(packet_type, local_packet_index);
self.ack_manager.increment_local_packet_index();
[header_bytes.as_slice(), &payload]
.concat()
.into_boxed_slice()
}
pub fn get_next_packet_index(&self) -> SequenceNumber {
return self.ack_manager.get_local_packet_index();
}
pub fn send_message<R: ReplicateSafe<P>>(&mut self, message: &R, guaranteed_delivery: bool) {
return self
.message_manager
.queue_outgoing_message(message, guaranteed_delivery);
}
pub fn has_outgoing_messages(&self) -> bool {
return self.message_manager.has_outgoing_messages();
}
pub fn pop_outgoing_message(&mut self, next_packet_index: u16) -> Option<P> {
return self.message_manager.pop_outgoing_message(next_packet_index);
}
pub fn unpop_outgoing_message(&mut self, next_packet_index: u16, message: P) {
return self
.message_manager
.unpop_outgoing_message(next_packet_index, message);
}
pub fn process_message_data(
&mut self,
reader: &mut PacketReader,
manifest: &Manifest<P>,
packet_index: u16,
) {
return self
.message_manager
.process_data(reader, manifest, packet_index);
}
pub fn get_incoming_message(&mut self) -> Option<P> {
return self.message_manager.pop_incoming_message();
}
pub fn get_address(&self) -> SocketAddr {
return self.address;
}
pub fn get_last_received_tick(&self) -> u16 {
return self.last_received_tick;
}
}