-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Impl FromPlayers & ToPlayers messages
- Loading branch information
Showing
15 changed files
with
457 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use std::{ | ||
net::SocketAddr, | ||
time::{Duration, Instant}, | ||
}; | ||
|
||
use bincode::error::EncodeError; | ||
use de_net::{OutPackage, PackageBuilder, PackageIterator, Peers, Reliability}; | ||
|
||
const UNRELIABLE_TIMEOUT: Duration = Duration::from_millis(10); | ||
const RELIABLE_TIMEOUT: Duration = Duration::from_millis(50); | ||
|
||
/// Buffers of player messages and package builder. | ||
pub(super) struct PlayerBuffer { | ||
unreliable: PackageBuilder, | ||
unordered: PackageBuilder, | ||
semi_ordered: PackageBuilder, | ||
} | ||
|
||
impl PlayerBuffer { | ||
pub(super) fn new(target: SocketAddr) -> Self { | ||
Self { | ||
unreliable: PackageBuilder::new(Reliability::Unreliable, Peers::Players, target), | ||
unordered: PackageBuilder::new(Reliability::Unordered, Peers::Players, target), | ||
semi_ordered: PackageBuilder::new(Reliability::SemiOrdered, Peers::Players, target), | ||
} | ||
} | ||
|
||
/// Pushes a single message to an appropriate buffer. | ||
pub(super) fn push<E>( | ||
&mut self, | ||
reliability: Reliability, | ||
message: &E, | ||
) -> Result<(), EncodeError> | ||
where | ||
E: bincode::Encode, | ||
{ | ||
self.builder_mut(reliability).push(message) | ||
} | ||
|
||
/// Builds packages from old enough messages and removes the packages from | ||
/// the buffer. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `time` - current time. | ||
pub(super) fn build(&mut self, time: Instant) -> PlayerPackageIterator<'_> { | ||
let unreliable_threshodl = time - UNRELIABLE_TIMEOUT; | ||
let reliable_threshodl = time - RELIABLE_TIMEOUT; | ||
|
||
PlayerPackageIterator { | ||
index: 0, | ||
iterators: [ | ||
self.unreliable.build_old(unreliable_threshodl), | ||
self.unordered.build_old(reliable_threshodl), | ||
self.semi_ordered.build_old(reliable_threshodl), | ||
], | ||
} | ||
} | ||
|
||
fn builder_mut(&mut self, reliability: Reliability) -> &mut PackageBuilder { | ||
match reliability { | ||
Reliability::Unreliable => &mut self.unreliable, | ||
Reliability::Unordered => &mut self.unordered, | ||
Reliability::SemiOrdered => &mut self.semi_ordered, | ||
} | ||
} | ||
} | ||
|
||
pub(super) struct PlayerPackageIterator<'a> { | ||
index: usize, | ||
iterators: [PackageIterator<'a>; 3], | ||
} | ||
|
||
impl<'a> Iterator for PlayerPackageIterator<'a> { | ||
type Item = OutPackage; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
while self.index < self.iterators.len() { | ||
let item = self.iterators[self.index].next(); | ||
if item.is_some() { | ||
return item; | ||
} | ||
self.index += 1; | ||
} | ||
|
||
None | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use std::net::SocketAddr; | ||
|
||
use de_net::Reliability; | ||
|
||
pub(super) struct InMessage<M> { | ||
meta: MessageMeta, | ||
message: M, | ||
} | ||
|
||
impl<M> InMessage<M> { | ||
pub(super) fn new(source: SocketAddr, reliability: Reliability, message: M) -> Self { | ||
Self { | ||
meta: MessageMeta { | ||
source, | ||
reliability, | ||
}, | ||
message, | ||
} | ||
} | ||
|
||
pub(super) fn meta(&self) -> MessageMeta { | ||
self.meta | ||
} | ||
|
||
pub(super) fn message(&self) -> &M { | ||
&self.message | ||
} | ||
} | ||
|
||
#[derive(Clone, Copy)] | ||
pub(super) struct MessageMeta { | ||
pub(super) source: SocketAddr, | ||
pub(super) reliability: Reliability, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.