Last active
April 16, 2019 08:35
-
-
Save CosineP/9ae05316d6ef104a94bf56bbe5cb8d83 to your computer and use it in GitHub Desktop.
amethyst_network reliable packet loss
This file contains hidden or 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
extern crate amethyst; | |
use amethyst::{ | |
prelude::*, | |
ecs::prelude::*, | |
network::*, | |
}; | |
fn main() -> amethyst::Result<()> { | |
let server = gen_game(true); | |
let client = gen_game(false); | |
server.join().unwrap(); | |
client.join().unwrap(); | |
Ok(()) | |
} | |
fn gen_game(is_server: bool) -> std::thread::JoinHandle<()> { | |
std::thread::spawn(move || { | |
if !is_server { | |
std::thread::sleep(std::time::Duration::from_secs(1)); | |
} | |
let game_data = GameDataBuilder::default(); | |
let game_data = if is_server { | |
game_data.with_bundle(NetworkBundle::<usize>::new( | |
"0.0.0.0:3456".parse().unwrap(), | |
vec![], | |
)).unwrap() | |
} else { | |
game_data.with_bundle(NetworkBundle::<usize>::new( | |
"0.0.0.0:3457".parse().unwrap(), | |
vec![], | |
)).unwrap() | |
}; | |
let mut game = Application::build("./", TestState::default()).unwrap() | |
.with_resource(NetParams { | |
is_server: is_server, | |
}) | |
.build(game_data).unwrap() | |
; | |
game.run(); | |
}) | |
} | |
struct NetParams { | |
is_server: bool, | |
} | |
fn init_net(world: &mut World) { | |
let is_server = world.read_resource::<NetParams>().is_server; | |
if !is_server { | |
world | |
.create_entity() | |
.with(NetConnection::<usize>::new("127.0.0.1:3456".parse().unwrap())) | |
.build(); | |
} else { | |
world | |
.create_entity() | |
.with(NetConnection::<usize>::new("127.0.0.1:3457".parse().unwrap())) | |
.build(); | |
} | |
} | |
struct TestState { | |
pub reader: Option<ReaderId<NetEvent<usize>>>, | |
count: usize, | |
} | |
impl Default for TestState { | |
fn default() -> Self { | |
Self { | |
reader: None, | |
count: 1000, | |
} | |
} | |
} | |
impl SimpleState for TestState { | |
fn on_start(&mut self, data: StateData<GameData>) { | |
init_net(data.world); | |
} | |
fn fixed_update(&mut self, data: StateData<GameData>) -> SimpleTrans { | |
let params = data.world.read_resource::<NetParams>(); | |
let mut chans = data.world.write_storage::<NetConnection<usize>>(); | |
if !params.is_server { | |
let test_struct = NetEvent::Reliable(self.count); | |
for chan in (&mut chans).join() { | |
chan.send_buffer.single_write(test_struct.clone()); | |
self.count -= 1; | |
} | |
} else { | |
for chan in (&mut chans).join() { | |
if self.reader.is_none() { | |
self.reader = Some(chan.receive_buffer.register_reader()); | |
} | |
for _e in chan.receive_buffer.read(self.reader.as_mut().unwrap()) { | |
self.count -= 1; | |
println!("expecting {} more packets", self.count); | |
} | |
} | |
} | |
if self.count <= 0 { | |
println!("sent all packets. you'll have to exit manually when done waiting."); | |
Trans::Quit | |
} else { | |
Trans::None | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment