-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreplication.rs
More file actions
72 lines (63 loc) · 1.47 KB
/
Copy pathreplication.rs
File metadata and controls
72 lines (63 loc) · 1.47 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
//! Creates connected replication endpoints as a networking smoke test.
use std::time::Instant;
use byte_engine::{
core::factory::Factory,
network::{Replicable, channel::ChannelServer as Server},
space::Positionable,
};
use math::Point;
use serde::{Deserialize, Serialize};
fn main() {
let mut server = Server::new();
let mut client_a = server.client();
let mut client_b = server.client();
client_a.connect(Instant::now());
client_b.connect(Instant::now());
let mut update = || {
client_a.update().unwrap();
client_b.update().unwrap();
server.update(Instant::now()).unwrap();
};
let replicable_factory = Factory::new();
replicable_factory.create(Object {
position: Point::new(0.5, 0.5, 0.5),
payload: Commands::Spawn as u8,
});
update();
update();
update();
let mut data = [0_u8; 1024];
data[0] = Commands::Spawn as u8;
server.send(true, data);
client_a.update().unwrap();
client_b.update().unwrap();
server.update(Instant::now()).unwrap();
for packet in server.drain_received() {
if packet.data[0] == Commands::Spawn as u8 {
println!("Requested spawn");
}
}
}
#[repr(u8)]
#[derive(Debug, Clone, Serialize, Deserialize)]
enum Commands {
Spawn,
}
#[derive(Clone)]
struct Object {
position: Point,
payload: u8,
}
impl Positionable for Object {
fn position(&self) -> Point {
self.position
}
fn set_position(&mut self, position: Point) {
self.position = position;
}
}
impl Replicable for Object {
fn payload(&self) -> &u8 {
&self.payload
}
}