Remove commented code
This commit is contained in:
parent
3aa5856753
commit
e109b67cfb
196
src/lib.rs
196
src/lib.rs
|
@ -24,199 +24,3 @@ pub trait Packet: DeserializeOwned + Serialize + Send + Sync {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: DeserializeOwned + Serialize + Send + Sync> Packet for T {}
|
impl<T: DeserializeOwned + Serialize + Send + Sync> Packet for T {}
|
||||||
|
|
||||||
/* use bevy::{prelude::*, utils::HashMap};
|
|
||||||
use std::{
|
|
||||||
io::{self, ErrorKind},
|
|
||||||
net::{TcpStream, ToSocketAddrs},
|
|
||||||
sync::{mpsc::TryRecvError, Arc},
|
|
||||||
};
|
|
||||||
use tcp::{Connection, Listener, Packet, RawPacket};
|
|
||||||
|
|
||||||
mod tcp;
|
|
||||||
|
|
||||||
/// A Bevy resource that store the packets handlers for the client.
|
|
||||||
#[derive(Resource)]
|
|
||||||
pub struct ClientPacketHandler(Arc<HashMap<u64, Box<dyn Fn(RawPacket, &mut World) + Send + Sync>>>);
|
|
||||||
|
|
||||||
/// A connection to a remote server.
|
|
||||||
#[derive(Resource)]
|
|
||||||
pub struct ServerConnection(Connection);
|
|
||||||
|
|
||||||
impl ServerConnection {
|
|
||||||
/// Connects to a remote server.
|
|
||||||
pub fn connect<A: ToSocketAddrs>(addr: A) -> io::Result<Self> {
|
|
||||||
Ok(Self(Connection::new(TcpStream::connect(addr)?)?))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Send a [Packet] to the remote server.
|
|
||||||
pub fn send<P: Packet>(&self, packet: P) {
|
|
||||||
self.0.send(packet).ok();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A Bevy resource that store the packets handlers for the server.
|
|
||||||
#[derive(Resource)]
|
|
||||||
struct ServerPacketHandler(
|
|
||||||
Arc<HashMap<u64, Box<dyn Fn(Entity, ClientConnection, RawPacket, &mut World) + Send + Sync>>>,
|
|
||||||
);
|
|
||||||
|
|
||||||
/// A [ClientConnection] listener.
|
|
||||||
#[derive(Resource)]
|
|
||||||
pub struct ClientListener(Listener);
|
|
||||||
|
|
||||||
impl ClientListener {
|
|
||||||
/// Creates a new TCP listener on the given address.
|
|
||||||
pub fn bind<A: ToSocketAddrs>(addr: A) -> io::Result<Self> {
|
|
||||||
Ok(Self(Listener::bind(addr)?))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A connection to a remote client.
|
|
||||||
#[derive(Component)]
|
|
||||||
pub struct ClientConnection(Arc<Connection>);
|
|
||||||
|
|
||||||
impl ClientConnection {
|
|
||||||
/// Sends a [Packet] to the remote client.
|
|
||||||
pub fn send<P: Packet>(&self, packet: P) {
|
|
||||||
self.0.send(packet).ok();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A Bevy system to handle incoming [ClientConnection]s and remove the
|
|
||||||
/// [ClientListener] resource if it is no longer listening.
|
|
||||||
fn accept_connections(mut commands: Commands, listener: Option<Res<ClientListener>>) {
|
|
||||||
if let Some(listener) = listener {
|
|
||||||
match listener.0.accept() {
|
|
||||||
Ok(connection) => {
|
|
||||||
commands.spawn(ClientConnection(Arc::new(connection)));
|
|
||||||
}
|
|
||||||
Err(error) => match error.kind() {
|
|
||||||
ErrorKind::WouldBlock => {}
|
|
||||||
_ => commands.remove_resource::<ClientListener>(),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A Bevy system to handle incoming [Packet]s from
|
|
||||||
/// the [ClientConnection]s and the [ServerConnection].
|
|
||||||
/// It removes them if they are no longer connected.
|
|
||||||
fn receive_packets(world: &mut World) {
|
|
||||||
// Handle client packets
|
|
||||||
let mut packets = Vec::new();
|
|
||||||
let mut to_remove = false;
|
|
||||||
if let Some(connection) = world.get_resource::<ServerConnection>() {
|
|
||||||
if let Some(receiver) = connection.0.recv() {
|
|
||||||
loop {
|
|
||||||
match receiver.try_recv() {
|
|
||||||
Ok(packet) => packets.push(packet),
|
|
||||||
Err(TryRecvError::Empty) => break,
|
|
||||||
Err(TryRecvError::Disconnected) => {
|
|
||||||
to_remove = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if let Some(handlers) = world
|
|
||||||
.get_resource_mut::<ClientPacketHandler>()
|
|
||||||
.map(|handlers| Arc::clone(&handlers.0))
|
|
||||||
{
|
|
||||||
for packet in packets.into_iter() {
|
|
||||||
if let Some(handler) = handlers.get(&packet.packet_id()) {
|
|
||||||
(handler)(packet, world);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if to_remove {
|
|
||||||
world.remove_resource::<ServerConnection>();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle server packets
|
|
||||||
let mut packets = Vec::new();
|
|
||||||
let mut to_remove = Vec::new();
|
|
||||||
for (entity, connection) in world.query::<(Entity, &ClientConnection)>().iter(world) {
|
|
||||||
if let Some(receiver) = connection.0.recv() {
|
|
||||||
loop {
|
|
||||||
match receiver.try_recv() {
|
|
||||||
Ok(packet) => {
|
|
||||||
println!("YESSSS");
|
|
||||||
packets.push((entity, ClientConnection(Arc::clone(&connection.0)), packet));
|
|
||||||
}
|
|
||||||
Err(TryRecvError::Empty) => break,
|
|
||||||
Err(TryRecvError::Disconnected) => {
|
|
||||||
to_remove.push(entity);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if let Some(handlers) = world
|
|
||||||
.get_resource_mut::<ServerPacketHandler>()
|
|
||||||
.map(|handlers| Arc::clone(&handlers.0))
|
|
||||||
{
|
|
||||||
for (entity, connection, packet) in packets.into_iter() {
|
|
||||||
if let Some(handler) = handlers.get(&packet.packet_id()) {
|
|
||||||
(handler)(entity, connection, packet, world);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for entity in to_remove {
|
|
||||||
world.despawn(entity);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A network plugin.
|
|
||||||
pub struct NetworkPlugin;
|
|
||||||
|
|
||||||
impl Plugin for NetworkPlugin {
|
|
||||||
fn build(&self, app: &mut App) {
|
|
||||||
app.insert_resource(ServerPacketHandler(Arc::new(HashMap::new())));
|
|
||||||
app.insert_resource(ClientPacketHandler(Arc::new(HashMap::new())));
|
|
||||||
app.add_system(accept_connections);
|
|
||||||
app.add_system(receive_packets);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A extension trait to add packet handler.
|
|
||||||
pub trait NetworkAppExt {
|
|
||||||
/// Add a client packet handler.
|
|
||||||
fn add_client_packet_handler<P, H>(&mut self, handler: H) -> &mut Self
|
|
||||||
where
|
|
||||||
P: Packet,
|
|
||||||
H: Fn(RawPacket, &mut World) + Send + Sync + 'static;
|
|
||||||
|
|
||||||
/// Add a server packet handler.
|
|
||||||
fn add_server_packet_handler<P, H>(&mut self, handler: H) -> &mut Self
|
|
||||||
where
|
|
||||||
P: Packet,
|
|
||||||
H: Fn(Entity, ClientConnection, RawPacket, &mut World) + Send + Sync + 'static;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl NetworkAppExt for App {
|
|
||||||
fn add_client_packet_handler<P, H>(&mut self, handler: H) -> &mut Self
|
|
||||||
where
|
|
||||||
P: Packet,
|
|
||||||
H: Fn(RawPacket, &mut World) + Send + Sync + 'static,
|
|
||||||
{
|
|
||||||
Arc::get_mut(&mut self.world.resource_mut::<ClientPacketHandler>().0)
|
|
||||||
.unwrap()
|
|
||||||
.insert(P::packet_id(), Box::new(handler));
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
fn add_server_packet_handler<P, H>(&mut self, handler: H) -> &mut Self
|
|
||||||
where
|
|
||||||
P: Packet,
|
|
||||||
H: Fn(Entity, ClientConnection, RawPacket, &mut World) + Send + Sync + 'static,
|
|
||||||
{
|
|
||||||
Arc::get_mut(&mut self.world.resource_mut::<ServerPacketHandler>().0)
|
|
||||||
.unwrap()
|
|
||||||
.insert(P::packet_id(), Box::new(handler));
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
Loading…
Reference in a new issue