-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathpacket_buffer.rs
More file actions
382 lines (322 loc) · 11.7 KB
/
Copy pathpacket_buffer.rs
File metadata and controls
382 lines (322 loc) · 11.7 KB
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
//! Two implementations of a packet buffer.
//!
//! A packet buffer is used to hold the packets of a given type received
//! from players. When a packet is received, the IO threads
//! push the packet onto the buffer, and systems on the server
//! thread poll these packets out of the buffer.
//!
//! We provide two implementations, optimized for different cases:
//! * A buffer based on a large array, with two slots for each player.
//! This buffer works well for cases when packets of this type are received
//! very often, such as position updates.
//! * A buffer based on `crossbeam-channel`, best for cases where fewer
//! packets of this type are received.
//!
//! The former is not yet implemented, and we are currently using a `DashMap<Entity, SmallVec<[Box<dyn Packet>; 4]>>`.
use ahash::AHashMap;
use feather_core::{cast_packet, Packet, PacketType};
use fecs::Entity;
use num_traits::ToPrimitive;
use parking_lot::{Mutex, RwLock};
use smallvec::SmallVec;
use std::iter;
use strum::IntoEnumIterator;
/// The global packet store, storing packet buffers for packets of each type.
pub struct PacketBuffers {
/// Packet buffers, indexed by the `ToPrimitive` implementation
/// of `PacketType`.
buffers: Vec<PacketBuffer>,
}
lazy_static! {
/// The set of buffers which use a `MapBuffer` instead of an `ArrayBuffer`.
static ref USE_MAP_FOR: indexmap::IndexSet<PacketType> = indexmap::indexset![
PacketType::PlayerPosition,
PacketType::PlayerPositionAndLookServerbound,
PacketType::PlayerLook,
];
}
impl Default for PacketBuffers {
fn default() -> Self {
Self::new()
}
}
impl PacketBuffers {
/// Creates a new packet store with buffers allocated for all packet types.
pub fn new() -> Self {
Self {
buffers: PacketType::iter()
.map(|ty| {
if USE_MAP_FOR.contains(&ty) {
PacketBuffer::Map(MapBuffer::default())
} else {
PacketBuffer::Channel(ChannelBuffer::new())
}
})
.collect(),
}
}
/// Pushes a received packet onto the packet buffer for the packet's
/// type.
pub fn push(&self, entity: Entity, packet: Box<dyn Packet>) {
let index = packet.ty().to_usize().unwrap();
self.buffers[index].push(entity, packet);
}
/// Returns an iterator over packets received with type `T`.
///
/// # Panics
/// Panics if the underlying buffer is not a `ChannelBuffer`.
/// `received_for()` should be used instead if using an `ArrayBuffer`.
pub fn received<'a, T>(&'a self) -> impl Iterator<Item = (Entity, T)> + 'a
where
T: Packet,
{
let ty = T::ty_sized();
let index = ty.to_usize().unwrap();
self.buffers[index]
.poll()
.map(|(player, boxed)| (player, cast_packet(boxed)))
}
/// Returns an iterator over packets of type `T` received by the given player.
///
/// # Panics
/// Panics if the underlying buffer for this packet type is not a `MapBuffer` or an
/// `ArrayBuffer`. Use `received` instead.
pub fn received_for<T>(&self, player: Entity) -> impl Iterator<Item = T>
where
T: Packet,
{
let ty = T::ty_sized();
let index = ty.to_usize().unwrap();
self.buffers[index]
.received_for(player)
.map(|boxed| cast_packet(boxed))
}
}
/// One of two packet buffer implementations.
pub enum PacketBuffer {
Channel(ChannelBuffer),
Map(MapBuffer),
}
impl PacketBuffer {
/// Polls this buffer for newly received packets.
///
/// # Panics
/// Panics if the underlying buffer is not a `ChannelBuffer`.
/// `received_for()` should be used instead if using an `ArrayBuffer`.
pub fn poll<'a>(&'a self) -> impl Iterator<Item = (Entity, Box<dyn Packet>)> + 'a {
match self {
PacketBuffer::Channel(chan) => chan.poll(),
PacketBuffer::Map(_) => panic!("cannot poll a map-based packet buffer"),
}
}
/// Pushes a packet onto this buffer.
pub fn push(&self, player: Entity, packet: Box<dyn Packet>) {
match self {
PacketBuffer::Channel(chan) => chan.push(player, packet),
PacketBuffer::Map(map) => map.push(player, packet),
}
}
/// Drains packets received by the given player.
///
/// # Panics
/// Panics if the underlying buffer is not a `MapBuffer` or an `ArrayBuffer`.
pub fn received_for(&self, player: Entity) -> impl Iterator<Item = Box<dyn Packet>> {
match self {
PacketBuffer::Map(map) => map.received_for(player),
PacketBuffer::Channel(_) => {
panic!("cannot use received_for for a channel-based packet buffer")
}
}
}
}
/// A packet buffer based on an MPMC channel. Best for packet types
/// which are received less frequently.
pub struct ChannelBuffer {
sender: crossbeam::Sender<(Entity, Box<dyn Packet>)>,
receiver: crossbeam::Receiver<(Entity, Box<dyn Packet>)>,
}
impl ChannelBuffer {
fn new() -> Self {
let (sender, receiver) = crossbeam::unbounded();
Self { sender, receiver }
}
fn push(&self, player: Entity, packet: Box<dyn Packet>) {
let _ = self.sender.send((player, packet));
}
fn poll<'a>(&'a self) -> impl Iterator<Item = (Entity, Box<dyn Packet>)> + 'a {
self.receiver.try_iter()
}
}
enum Either<A, B> {
Left(A),
Right(B),
}
impl<A, B, I> Iterator for Either<A, B>
where
A: Iterator<Item = I>,
B: Iterator<Item = I>,
{
type Item = I;
fn next(&mut self) -> Option<Self::Item> {
match self {
Either::Left(a) => a.next(),
Either::Right(b) => b.next(),
}
}
}
type MapBufferVec = SmallVec<[Box<dyn Packet>; 2]>;
type MapBufferInner = AHashMap<Entity, Mutex<MapBufferVec>>;
#[derive(Default)]
pub struct MapBuffer(RwLock<MapBufferInner>);
impl MapBuffer {
fn push(&self, player: Entity, packet: Box<dyn Packet>) {
let guard = self.0.read();
if let Some(vec) = guard.get(&player) {
vec.lock().push(packet);
} else {
drop(guard);
self.0.write().insert(player, Mutex::new(smallvec![packet]));
}
}
fn received_for(&self, player: Entity) -> impl Iterator<Item = Box<dyn Packet>> {
let map_guard = self.0.read();
if let Some(vec) = map_guard.get(&player) {
let vec = vec
.lock()
.drain(..)
.collect::<SmallVec<[Box<dyn Packet>; 2]>>();
Either::Left(vec.into_iter())
} else {
Either::Right(iter::empty())
}
}
}
/* TODO: audit this implementation.
/// A packet buffer using an array of slots.
pub struct ArrayBuffer {
/// Internal array of length `2 * (num_players rounded up to the next power of two)`.
/// Packets received for a player with index `i` will
/// be located at `array[i]` and `array[n + i]`, where `n` is the number
/// of players rounded up to the next power of two.
///
/// Note that this array is type-erased; we do this as an optimization
/// to store the packets directly in the array instead of going through a
/// `Box`.
array: RwLock<NonNull<u8>>,
/// Memory layout of `array`.
array_layout: Mutex<Layout>,
/// Layout of a single packet.
single_packet: Layout,
/// Number of players for which this buffer has capacity.
max_players: AtomicUsize,
/// Pointer to the `None` value for the packet.
none_ptr: NonNull<u8>,
/// Length of the `None` value for the packet.
none_len: usize,
}
impl ArrayBuffer {
/// Creates a new, empty `ArrayBuffer` for packets of type `T`.
pub fn new<T>() -> Self {
let starting_n = 8;
let none = Box::new(Option::<T>::None);
let none_ptr = NonNull::new(Box::into_raw(none).cast()).expect("box has null pointer");
let (array, array_layout) =
unsafe { Self::allocate_for(Layout::new::<Option<T>>(), starting_n, none_ptr) };
Self {
array: RwLock::new(array),
array_layout: Mutex::new(array_layout),
single_packet: Layout::new::<Option<T>>(),
max_players: AtomicUsize::new(starting_n),
none_ptr,
none_len: std::mem::size_of::<Option<T>>(),
}
}
/// Returns the number of players supported by this buffer.
pub fn max_players(&self) -> usize {
self.max_players.load(Ordering::Acquire)
}
/// Extends this array buffer to support at least `n` __more__ players.
pub fn reserve(&self, extra: usize) {
let mut old_array = self.array.write();
let mut old_layout = self.array_layout.lock();
let current_max = self.max_players.load(Ordering::Acquire);
let new_max = (current_max + extra).next_power_of_two();
let (new_array, new_layout) =
unsafe { Self::allocate_for(self.single_packet, new_max, self.none_ptr) };
assert!(new_layout.size() > old_layout.size());
assert_eq!(new_layout.size() % 2, 0);
assert_eq!(old_layout.size() % 2, 0);
// copy existing packets to new array
unsafe {
std::ptr::copy_nonoverlapping(
old_array.as_ptr(),
new_array.as_ptr(),
old_layout.size() / 2,
);
std::ptr::copy_nonoverlapping(
old_array.as_ptr().offset((old_layout.size() / 2) as isize),
new_array.as_ptr().offset((new_layout.size() / 2) as isize),
old_layout.size() / 2,
);
}
*old_array = new_array;
*old_layout = new_layout;
self.max_players.store(new_max, Ordering::Release);
}
unsafe fn allocate_for(
single_packet: Layout,
max_players: usize,
none_ptr: NonNull<u8>,
) -> (NonNull<u8>, Layout) {
let new_layout = single_packet
.repeat(max_players * 2)
.map(|(layout, offset)| {
assert_eq!(offset, single_packet.size());
layout
})
.expect("invalid packet buffer layout");
let new_array =
NonNull::new(std::alloc::alloc(new_layout)).expect("allocator returned null pointer");
// fill array with `None` values
for i in 0..max_players * 2 {
let ptr = new_array
.as_ptr()
.offset((i * single_packet.size()) as isize);
std::ptr::copy_nonoverlapping(none_ptr.as_ptr(), ptr, single_packet.size());
}
(new_array, new_layout)
}
}
*/
#[cfg(test)]
mod tests {
use super::*;
use feather_core::network::packet::implementation::Request;
use fecs::{EntityBuilder, World};
#[test]
fn map_buffer() {
let buffer = MapBuffer::default();
let mut world = World::new();
let entity = EntityBuilder::new().build().spawn_in(&mut world);
dbg!();
buffer.push(entity, Box::new(Request {}));
dbg!();
let mut received = buffer.received_for(entity).collect::<Vec<_>>();
dbg!();
assert_eq!(received.len(), 1);
let first = received.remove(0);
let _ = cast_packet::<Request>(first);
}
#[test]
fn channel_buffer() {
let buffer = ChannelBuffer::new();
let mut world = World::new();
let entity = EntityBuilder::new().build().spawn_in(&mut world);
buffer.push(entity, Box::new(Request {}));
let mut received = buffer.poll().collect::<Vec<_>>();
assert_eq!(received.len(), 1);
let (rentity, first) = received.remove(0);
let _ = cast_packet::<Request>(first);
assert_eq!(entity, rentity);
}
}