diff --git a/src/lib.rs b/src/lib.rs index bbfe983..817daed 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,199 +24,3 @@ pub trait Packet: DeserializeOwned + Serialize + Send + Sync { } impl 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>>); - -/// A connection to a remote server. -#[derive(Resource)] -pub struct ServerConnection(Connection); - -impl ServerConnection { - /// Connects to a remote server. - pub fn connect(addr: A) -> io::Result { - Ok(Self(Connection::new(TcpStream::connect(addr)?)?)) - } - - /// Send a [Packet] to the remote server. - pub fn send(&self, packet: P) { - self.0.send(packet).ok(); - } -} - -/// A Bevy resource that store the packets handlers for the server. -#[derive(Resource)] -struct ServerPacketHandler( - Arc>>, -); - -/// A [ClientConnection] listener. -#[derive(Resource)] -pub struct ClientListener(Listener); - -impl ClientListener { - /// Creates a new TCP listener on the given address. - pub fn bind(addr: A) -> io::Result { - Ok(Self(Listener::bind(addr)?)) - } -} - -/// A connection to a remote client. -#[derive(Component)] -pub struct ClientConnection(Arc); - -impl ClientConnection { - /// Sends a [Packet] to the remote client. - pub fn send(&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>) { - 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::(), - }, - } - } -} - -/// 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::() { - 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::() - .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::(); - } - - // 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::() - .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(&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(&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(&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::().0) - .unwrap() - .insert(P::packet_id(), Box::new(handler)); - self - } - - fn add_server_packet_handler(&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::().0) - .unwrap() - .insert(P::packet_id(), Box::new(handler)); - self - } -} - */