Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework lib to use bevy triggers, update deps #10

Merged
merged 2 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
592 changes: 260 additions & 332 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bevy_slinet"
version = "0.11.0"
version = "0.12.0"
authors = [
"Sliman4 <[email protected]>",
"aggyomfg <[email protected]>"
Expand Down
64 changes: 34 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ impl ClientConfig for Config {
type LengthSerializer = LittleEndian<u32>;
fn build_serializer(
) -> SerializerAdapter<Self::ClientPacket, Self::ServerPacket, Self::SerializerError> {
SerializerAdapter::ReadOnly(Arc::new(BincodeSerializer::<DefaultOptions>::default()))
SerializerAdapter::ReadOnly(Arc::new(
BincodeSerializer::<DefaultOptions>::default(),
))
}
}

Expand All @@ -54,25 +56,24 @@ fn main() {
App::new()
.add_plugins(MinimalPlugins)
.add_plugins(ClientPlugin::<Config>::connect("127.0.0.1:3000"))
.add_systems(Update, (connection_establish_system, packet_receive_system))
.observe(connection_establish_system)
.observe(packet_receive_system)
.run()
}

fn connection_establish_system(mut events: EventReader<ConnectionEstablishEvent<Config>>) {
for _event in events.read() {
println!("Connected!");
}
fn connection_establish_system(event: Trigger<ConnectionEstablishEvent<Config>>) {
println!("Connected!");
}

fn packet_receive_system(mut events: EventReader<PacketReceiveEvent<Config>>) {
for event in events.read() {
match &event.packet {
ServerPacket::String(s) => println!("Got a message: {}", s),
}
event
.connection
.send(ClientPacket::String("Hello, Server!".to_string())).unwrap();
fn packet_receive_system(event: Trigger<PacketReceiveEvent<Config>>) {
match &event.event().packet {
ServerPacket::String(s) => println!("Got a message: {}", s),
}
event
.event()
.connection
.send(ClientPacket::String("Hello, Server!".to_string()))
.unwrap();
}
```

Expand All @@ -98,7 +99,9 @@ impl ServerConfig for Config {
type LengthSerializer = LittleEndian<u32>;
fn build_serializer(
) -> SerializerAdapter<Self::ClientPacket, Self::ServerPacket, Self::SerializerError> {
SerializerAdapter::ReadOnly(Arc::new(BincodeSerializer::<DefaultOptions>::default()))
SerializerAdapter::ReadOnly(Arc::new(
BincodeSerializer::<DefaultOptions>::default(),
))
}
}

Expand All @@ -116,27 +119,28 @@ fn main() {
App::new()
.add_plugins(MinimalPlugins)
.add_plugins(ServerPlugin::<Config>::bind("127.0.0.1:3000").unwrap())
.add_systems(Update, (new_connection_system, packet_receive_system))
.observe(new_connection_system)
.observe(packet_receive_system)
.run()
}

fn new_connection_system(mut events: EventReader<NewConnectionEvent<Config>>) {
for event in events.read() {
event
.connection
.send(ServerPacket::String("Hello, Client!".to_string())).unwrap();
}
fn new_connection_system(event: Trigger<NewConnectionEvent<Config>>) {
event
.event()
.connection
.send(ServerPacket::String("Hello, Client!".to_string()))
.unwrap();
}

fn packet_receive_system(mut events: EventReader<PacketReceiveEvent<Config>>) {
for event in events.read() {
match &event.packet {
ClientPacket::String(s) => println!("Got a message from a client: {}", s),
}
event
.connection
.send(ServerPacket::String("Hello, Client!".to_string())).unwrap();
fn packet_receive_system(event: Trigger<PacketReceiveEvent<Config>>) {
match &event.event().packet {
ClientPacket::String(s) => println!("Got a message from a client: {}", s),
}
event
.event()
.connection
.send(ServerPacket::String("Hello, Client!".to_string()))
.unwrap();
}
```

Expand Down
63 changes: 31 additions & 32 deletions examples/hello_world_tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,54 +54,53 @@ fn main() {
let server = std::thread::spawn(move || {
App::new()
.add_plugins((MinimalPlugins, ServerPlugin::<Config>::bind(server_addr)))
.add_systems(
Update,
(server_new_connection_system, server_packet_receive_system),
)
.observe(server_new_connection_system)
.observe(server_packet_receive_system)
.run();
});
println!("Waiting 5000ms to make sure the server side has started");
std::thread::sleep(Duration::from_millis(5000));
println!("Waiting 1000ms to make sure the server side has started");
std::thread::sleep(Duration::from_millis(1000));
let client = std::thread::spawn(move || {
App::new()
.add_plugins(MinimalPlugins)
.add_plugins(ClientPlugin::<Config>::connect(server_addr))
.add_systems(Update, client_packet_receive_system)
.observe(client_packet_receive_system)
.run();
});
server.join().unwrap();
client.join().unwrap();
}

fn server_new_connection_system(mut events: EventReader<NewConnectionEvent<Config>>) {
for event in events.read() {
event
.connection
.send(ServerPacket::String("Hello, World!".to_string()))
.unwrap();
}
fn server_new_connection_system(new_connection: Trigger<NewConnectionEvent<Config>>) {
new_connection
.event()
.connection
.send(ServerPacket::String("Hello, World!".to_string()))
.unwrap();
println!(
"New connection from {:?}",
new_connection.event().connection.peer_addr()
);
}

fn client_packet_receive_system(mut events: EventReader<client::PacketReceiveEvent<Config>>) {
for event in events.read() {
match &event.packet {
ServerPacket::String(s) => println!("Server -> Client: {s}"),
}
event
.connection
.send(ClientPacket::String("Hello, Server!".to_string()))
.unwrap();
fn client_packet_receive_system(new_packet: Trigger<client::PacketReceiveEvent<Config>>) {
match &new_packet.event().packet {
ServerPacket::String(s) => println!("Server -> Client: {s}"),
}
new_packet
.event()
.connection
.send(ClientPacket::String("Hello, Server!".to_string()))
.unwrap();
}

fn server_packet_receive_system(mut events: EventReader<server::PacketReceiveEvent<Config>>) {
for event in events.read() {
match &event.packet {
ClientPacket::String(s) => println!("Server <- Client: {s}"),
}
event
.connection
.send(ServerPacket::String("Hello, Client!".to_string()))
.unwrap();
fn server_packet_receive_system(new_packet: Trigger<server::PacketReceiveEvent<Config>>) {
match &new_packet.event().packet {
ClientPacket::String(s) => println!("Server <- Client: {s}"),
}
new_packet
.event()
.connection
.send(ServerPacket::String("Hello, Client!".to_string()))
.unwrap();
}
95 changes: 46 additions & 49 deletions examples/hello_world_tcp_encrypted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,86 +53,83 @@ impl ClientConfig for Config {
))))
}
}

fn main() {
let server_addr = "127.0.0.1:3000";
let server = std::thread::spawn(move || {
App::new()
.add_plugins((MinimalPlugins, ServerPlugin::<Config>::bind(server_addr)))
.add_systems(
Update,
(server_new_connection_system, server_packet_receive_system),
)
.observe(server_new_connection_system)
.observe(server_packet_receive_system)
.run();
});
println!("Waiting 5000ms to make sure the server side has started");
std::thread::sleep(Duration::from_millis(5000));
println!("Waiting 1000ms to make sure the server side has started");
std::thread::sleep(Duration::from_millis(1000));
let client = std::thread::spawn(move || {
App::new()
.add_plugins(MinimalPlugins)
.add_plugins(ClientPlugin::<Config>::connect(server_addr))
.add_systems(Update, client_packet_receive_system)
.observe(client_packet_receive_system)
.run();
});
let client2 = std::thread::spawn(move || {
App::new()
.add_plugins(MinimalPlugins)
.add_plugins(ClientPlugin::<Config>::connect(server_addr))
.add_systems(Update, client2_packet_receive_system)
.observe(client2_packet_receive_system)
.run();
});
server.join().unwrap();
client.join().unwrap();
client2.join().unwrap();
}

fn server_new_connection_system(mut events: EventReader<NewConnectionEvent<Config>>) {
for event in events.read() {
event
.connection
.send(CustomCryptServerPacket::String("Hello, World!".to_string()))
.unwrap();
}
fn server_new_connection_system(new_connection: Trigger<NewConnectionEvent<Config>>) {
new_connection
.event()
.connection
.send(CustomCryptServerPacket::String("Hello, World!".to_string()))
.unwrap();
println!(
"New connection from: {:?}",
new_connection.event().connection.peer_addr()
);
}

fn client_packet_receive_system(mut events: EventReader<client::PacketReceiveEvent<Config>>) {
for event in events.read() {
match &event.packet {
CustomCryptServerPacket::String(s) => println!("Server -> Client: {s}"),
}
event
.connection
.send(CustomCryptClientPacket::String(
"Hello, Server!".to_string(),
))
.unwrap();
fn client_packet_receive_system(new_packet: Trigger<client::PacketReceiveEvent<Config>>) {
match &new_packet.event().packet {
CustomCryptServerPacket::String(s) => println!("Server -> Client: {s}"),
}
new_packet
.event()
.connection
.send(CustomCryptClientPacket::String(
"Hello, Server!".to_string(),
))
.unwrap();
}

fn client2_packet_receive_system(mut events: EventReader<client::PacketReceiveEvent<Config>>) {
for event in events.read() {
match &event.packet {
CustomCryptServerPacket::String(s) => println!("Server -> Client2: {s}"),
}
event
.connection
.send(CustomCryptClientPacket::String(
"Hello, Server!, I'm Client2".to_string(),
))
.unwrap();
fn client2_packet_receive_system(new_packet: Trigger<client::PacketReceiveEvent<Config>>) {
match &new_packet.event().packet {
CustomCryptServerPacket::String(s) => println!("Server -> Client2: {s}"),
}
new_packet
.event()
.connection
.send(CustomCryptClientPacket::String(
"Hello, Server!, I'm Client2".to_string(),
))
.unwrap();
}

fn server_packet_receive_system(mut events: EventReader<server::PacketReceiveEvent<Config>>) {
for event in events.read() {
match &event.packet {
CustomCryptClientPacket::String(s) => println!("Server <- Client: {s}"),
}
event
.connection
.send(CustomCryptServerPacket::String(
"Hello, Client!".to_string(),
))
.unwrap();
fn server_packet_receive_system(new_packet: Trigger<server::PacketReceiveEvent<Config>>) {
match &new_packet.event().packet {
CustomCryptClientPacket::String(s) => println!("Server <- Client: {s}"),
}
new_packet
.event()
.connection
.send(CustomCryptServerPacket::String(
"Hello, Client!".to_string(),
))
.unwrap();
}
Loading
Loading