hello_world_tcp_encrypted/
hello_world_tcp_encrypted.rs

1use std::sync::{Arc, Mutex};
2use std::time::Duration;
3
4use bevy::prelude::*;
5use bevy_slinet::serializer::SerializerAdapter;
6use bevy_slinet::serializers::custom_crypt::{
7    CustomCryptClientPacket, CustomCryptEngine, CustomCryptSerializer, CustomCryptServerPacket,
8    CustomSerializationError,
9};
10
11use bevy_slinet::client::ClientPlugin;
12use bevy_slinet::packet_length_serializer::LittleEndian;
13use bevy_slinet::protocols::tcp::TcpProtocol;
14
15use bevy_slinet::server::{NewConnectionEvent, ServerPlugin};
16use bevy_slinet::{client, server, ClientConfig, ServerConfig};
17
18struct Config;
19
20impl ServerConfig for Config {
21    type ClientPacket = CustomCryptClientPacket;
22    type ServerPacket = CustomCryptServerPacket;
23    type Protocol = TcpProtocol;
24    type EncodeError = CustomSerializationError;
25    type DecodeError = CustomSerializationError;
26    fn build_serializer() -> SerializerAdapter<
27        Self::ClientPacket,
28        Self::ServerPacket,
29        Self::EncodeError,
30        Self::DecodeError,
31    > {
32        SerializerAdapter::Mutable(Arc::new(Mutex::new(CustomCryptSerializer::<
33            CustomCryptEngine,
34            CustomCryptClientPacket,
35            CustomCryptServerPacket,
36        >::new(
37            CustomCryptEngine::default()
38        ))))
39    }
40    type LengthSerializer = LittleEndian<u32>;
41}
42
43impl ClientConfig for Config {
44    type ClientPacket = CustomCryptClientPacket;
45    type ServerPacket = CustomCryptServerPacket;
46    type Protocol = TcpProtocol;
47    type EncodeError = CustomSerializationError;
48    type DecodeError = CustomSerializationError;
49
50    type LengthSerializer = LittleEndian<u32>;
51    fn build_serializer() -> SerializerAdapter<
52        Self::ServerPacket,
53        Self::ClientPacket,
54        Self::EncodeError,
55        Self::DecodeError,
56    > {
57        SerializerAdapter::Mutable(Arc::new(Mutex::new(CustomCryptSerializer::<
58            CustomCryptEngine,
59            Self::ServerPacket,
60            Self::ClientPacket,
61        >::new(
62            CustomCryptEngine::default()
63        ))))
64    }
65}
66fn main() {
67    let server_addr = "127.0.0.1:3000";
68    let server = std::thread::spawn(move || {
69        App::new()
70            .add_plugins((MinimalPlugins, ServerPlugin::<Config>::bind(server_addr)))
71            .add_observer(server_new_connection_system)
72            .add_observer(server_packet_receive_system)
73            .run();
74    });
75    println!("Waiting 1000ms to make sure the server side has started");
76    std::thread::sleep(Duration::from_millis(1000));
77    let client = std::thread::spawn(move || {
78        App::new()
79            .add_plugins(MinimalPlugins)
80            .add_plugins(ClientPlugin::<Config>::connect(server_addr))
81            .add_observer(client_packet_receive_system)
82            .run();
83    });
84    let client2 = std::thread::spawn(move || {
85        App::new()
86            .add_plugins(MinimalPlugins)
87            .add_plugins(ClientPlugin::<Config>::connect(server_addr))
88            .add_observer(client2_packet_receive_system)
89            .run();
90    });
91    server.join().unwrap();
92    client.join().unwrap();
93    client2.join().unwrap();
94}
95
96fn server_new_connection_system(new_connection: On<NewConnectionEvent<Config>>) {
97    new_connection
98        .event()
99        .connection
100        .send(CustomCryptServerPacket::String("Hello, World!".to_string()))
101        .unwrap();
102    println!(
103        "New connection from: {:?}",
104        new_connection.event().connection.peer_addr()
105    );
106}
107
108fn client_packet_receive_system(new_packet: On<client::PacketReceiveEvent<Config>>) {
109    match &new_packet.event().packet {
110        CustomCryptServerPacket::String(s) => println!("Server -> Client: {s}"),
111    }
112    new_packet
113        .event()
114        .connection
115        .send(CustomCryptClientPacket::String(
116            "Hello, Server!".to_string(),
117        ))
118        .unwrap();
119}
120
121fn client2_packet_receive_system(new_packet: On<client::PacketReceiveEvent<Config>>) {
122    match &new_packet.event().packet {
123        CustomCryptServerPacket::String(s) => println!("Server -> Client2: {s}"),
124    }
125    new_packet
126        .event()
127        .connection
128        .send(CustomCryptClientPacket::String(
129            "Hello, Server!, I'm Client2".to_string(),
130        ))
131        .unwrap();
132}
133
134fn server_packet_receive_system(new_packet: On<server::PacketReceiveEvent<Config>>) {
135    match &new_packet.event().packet {
136        CustomCryptClientPacket::String(s) => println!("Server <- Client: {s}"),
137    }
138    new_packet
139        .event()
140        .connection
141        .send(CustomCryptServerPacket::String(
142            "Hello, Client!".to_string(),
143        ))
144        .unwrap();
145}