Add uninitializing and make client stop on disconnect

This commit is contained in:
Sofia 2017-11-25 16:43:23 +02:00
parent c44813d0d2
commit 545a3cc5c5
5 changed files with 49 additions and 6 deletions

View File

@ -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.
}
}
}

View File

@ -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);
}
}
}

View File

@ -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;

View File

@ -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) {
}

View File

@ -1,6 +1,6 @@

namespace Util {
public enum DisconnectReason {
TIMEOUT
TIMEOUT, MANUAL_DISCONNECT
}
}