diff --git a/scripts/net/Client.cs b/scripts/net/Client.cs index 922c0af..c30307b 100644 --- a/scripts/net/Client.cs +++ b/scripts/net/Client.cs @@ -2,6 +2,7 @@ using Network.PacketHandling; using Network.PacketHandling.Packets; using System; +using Util; namespace Network { public class Client : Peer { @@ -24,6 +25,13 @@ namespace Network { StartListening(port, "*"); } + public override void Uninitialize() { + if (ConnectionList.Contains(ServerConn)) { + Disconnect(ServerConn, DisconnectReason.MANUAL_DISCONNECT); + } + StopListening(); + } + public override void Process(float delta) { Timer += delta; if (Timer > 10) { @@ -41,6 +49,7 @@ namespace Network { } public override void Disconnected(Connection conn) { + Stop(); // Stop the client if it disconnects. } } } \ No newline at end of file diff --git a/scripts/net/Net.cs b/scripts/net/Net.cs index 5fe9605..2f61daf 100644 --- a/scripts/net/Net.cs +++ b/scripts/net/Net.cs @@ -28,13 +28,13 @@ namespace Network { public void StartClient(string address, int port) { if (IsClient || IsServer) { return; } Client = new Client(PacketPeer); - Client.Initialize(address, port); + Client.Start(address, port); } public void StartServer(string address, int port) { if (IsClient || IsServer) { return; } Server = new Server(PacketPeer); - Server.Initialize(address, port); + Server.Start(address, port); } } } diff --git a/scripts/net/Peer.cs b/scripts/net/Peer.cs index 6d4ed24..7c0c55a 100644 --- a/scripts/net/Peer.cs +++ b/scripts/net/Peer.cs @@ -13,6 +13,7 @@ namespace Network { private static PacketPeerUDP PacketPeer; private static Peer Singleton; public readonly bool IsServer; + private bool Initialized = false; public Protocol Protocol; @@ -23,6 +24,8 @@ namespace Network { public ConnectionList ConnectionList; public PacketDistributor PacketDistributor; + private bool ShouldListen = false; + public Peer(PacketPeerUDP packetPeer, bool isServer) { PacketPeer = packetPeer; IsServer = isServer; @@ -36,7 +39,19 @@ namespace Network { Process(delta); } + public void Start(string address, int port) { + Singleton = this; + Initialize(address, port); + Initialized = true; + } + + public void Stop() { + Uninitialize(); + Initialized = false; + } + public abstract void Initialize(string address, int port); + public abstract void Uninitialize(); public abstract void Process(float delta); public abstract void Connected(Connection conn); public abstract void Disconnected(Connection conn); @@ -54,7 +69,7 @@ namespace Network { GD.printerr("The Peer is already listening or the thread is active!"); return false; } - Singleton = this; + ShouldListen = true; PacketPeer.Listen(port, address); ThreadStart ChildRef = new ThreadStart(ListenerThreadMethod); ListenerThread = new Thread(ChildRef); @@ -62,10 +77,18 @@ namespace Network { return true; } + public void StopListening() { + if (ShouldListen) { + ShouldListen = false; + PacketPeer.Close(); + } + + } + private static void ListenerThreadMethod() { GD.print("Started Listener Thread."); - while (true) { + while (Singleton.ShouldListen) { PacketPeer.Wait(); byte[] Buffer = PacketPeer.GetPacket(); @@ -104,6 +127,10 @@ namespace Network { GD.print("TIMEOUT"); break; } + case DisconnectReason.MANUAL_DISCONNECT: { + GD.print("MANUAL DISCONNECT"); + break; + } default: { GD.print("Unknown."); break; diff --git a/scripts/net/Server.cs b/scripts/net/Server.cs index fc15b9a..34288bd 100644 --- a/scripts/net/Server.cs +++ b/scripts/net/Server.cs @@ -1,6 +1,6 @@ using Godot; using Network.PacketHandling; -using System.Threading; +using Util; namespace Network { public class Server : Peer { @@ -25,6 +25,13 @@ namespace Network { GD.print("Started server on " + address + ":" + port); } + public override void Uninitialize() { + StopListening(); + for (int i = 0; i < ConnectionList.Length; i++) { + Disconnect(ConnectionList[i], DisconnectReason.MANUAL_DISCONNECT); + } + } + public override void Process(float delta) { } diff --git a/scripts/util/DisconnectReason.cs b/scripts/util/DisconnectReason.cs index a9b9504..dda0162 100644 --- a/scripts/util/DisconnectReason.cs +++ b/scripts/util/DisconnectReason.cs @@ -1,6 +1,6 @@  namespace Util { public enum DisconnectReason { - TIMEOUT + TIMEOUT, MANUAL_DISCONNECT } }