Make disconnecting work good and add Peer.Running

This commit is contained in:
Sofia 2020-08-08 04:03:08 +03:00
parent d5edcab211
commit fc2e737c8a
5 changed files with 69 additions and 8 deletions

View File

@ -23,7 +23,7 @@ namespace NeonTea.Quakeball.Networking.Instances {
private byte LastPingIdent; private byte LastPingIdent;
public override void Start(string address, int port, PeerMessageListener listener) { public override void Start(string address, int port, PeerMessageListener listener) {
if (Peer != null) { if (Peer.Running) {
return; return;
} }
Peer = new Peer(Fingerprint); Peer = new Peer(Fingerprint);
@ -38,6 +38,19 @@ namespace NeonTea.Quakeball.Networking.Instances {
LocalPlayer.Controlled = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>(); LocalPlayer.Controlled = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
} }
public override void OnStop() {
Peer.Disconnect(Server.uid);
Server = null;
foreach (NetPlayer p in Players.Values) {
if (p.Id != LocalPlayer.Id) {
GameObject.Destroy(p.Controlled.gameObject);
}
}
Players.Clear();
Ping = 0;
}
public override void Connected(Connection conn) { public override void Connected(Connection conn) {
Terminal.Singleton.Println($"{conn.uid} Connected"); Terminal.Singleton.Println($"{conn.uid} Connected");
if (Server == null) { if (Server == null) {
@ -51,6 +64,14 @@ namespace NeonTea.Quakeball.Networking.Instances {
Terminal.Singleton.Println($"{conn.uid} Disconnected: {conn.ClosingReason}"); Terminal.Singleton.Println($"{conn.uid} Disconnected: {conn.ClosingReason}");
if (Server == conn) { if (Server == conn) {
Server = null; Server = null;
foreach (NetPlayer p in Players.Values) {
if (p.Id != LocalPlayer.Id) {
GameObject.Destroy(p.Controlled.gameObject);
}
}
Players.Clear();
Peer.Stop();
Ping = 0;
} }
} }

View File

@ -13,7 +13,12 @@ namespace NeonTea.Quakeball.Networking.Instances {
public Peer Peer; public Peer Peer;
public List<ulong> Connections = new List<ulong>(); public List<ulong> Connections = new List<ulong>();
public NetInstance() {
Peer = new Peer(Fingerprint);
}
public abstract void Start(string address, int port, PeerMessageListener listener); public abstract void Start(string address, int port, PeerMessageListener listener);
public abstract void OnStop();
public abstract void Connected(Connection conn); public abstract void Connected(Connection conn);
public abstract void Disconnected(Connection conn); public abstract void Disconnected(Connection conn);
public abstract void Handle(Connection conn, Packet packet); public abstract void Handle(Connection conn, Packet packet);
@ -25,10 +30,10 @@ namespace NeonTea.Quakeball.Networking.Instances {
public virtual void Update() { } public virtual void Update() { }
public void Stop() { public void Stop() {
if (Peer != null) { if (Peer.Running) {
OnStop();
Peer.Stop(); Peer.Stop();
Peer.MessageListener.Message("Stopping"); Peer.MessageListener.Message("Stopping");
Peer = null;
} }
} }

View File

@ -23,7 +23,7 @@ namespace NeonTea.Quakeball.Networking.Instances {
public static float PingInterval = 1; public static float PingInterval = 1;
public override void Start(string address, int port, PeerMessageListener listener) { public override void Start(string address, int port, PeerMessageListener listener) {
if (Peer != null) { if (Peer.Running) {
return; return;
} }
Peer = new Peer(Fingerprint); Peer = new Peer(Fingerprint);
@ -39,6 +39,17 @@ namespace NeonTea.Quakeball.Networking.Instances {
AddPlayer(LocalPlayer); AddPlayer(LocalPlayer);
} }
public override void OnStop() {
foreach (NetPlayer p in PlayerList) {
Peer.Disconnect(p.Id);
if (p.Controlled != null && p.Id != LocalPlayer.Id) {
GameObject.Destroy(p.Controlled.gameObject);
}
}
PlayerList.Clear();
Players.Clear();
}
public override void Connected(Connection conn) { public override void Connected(Connection conn) {
Terminal.Singleton.Println($"{conn.uid} Connected"); Terminal.Singleton.Println($"{conn.uid} Connected");
foreach (NetPlayer p in Players.Values) { foreach (NetPlayer p in Players.Values) {
@ -61,6 +72,7 @@ namespace NeonTea.Quakeball.Networking.Instances {
public override void Disconnected(Connection conn) { public override void Disconnected(Connection conn) {
Terminal.Singleton.Println($"{conn.uid} Disconnected: {conn.ClosingReason}"); Terminal.Singleton.Println($"{conn.uid} Disconnected: {conn.ClosingReason}");
if (Players.ContainsKey(conn.uid)) { if (Players.ContainsKey(conn.uid)) {
GameObject.Destroy(Players[conn.uid].Controlled.gameObject);
RemovePlayer(Players[conn.uid]); RemovePlayer(Players[conn.uid]);
} }
} }

View File

@ -26,6 +26,7 @@ namespace NeonTea.Quakeball.Networking {
Terminal.RegisterCommand("host", Host, "host [port] [address] - Hosts server at given address and port."); Terminal.RegisterCommand("host", Host, "host [port] [address] - Hosts server at given address and port.");
Terminal.RegisterCommand("join", Join, "join [address] [port] - Tries to join a server at given address and port."); Terminal.RegisterCommand("join", Join, "join [address] [port] - Tries to join a server at given address and port.");
Terminal.RegisterCommand("ping", Ping, "ping - return the current ping to any connection(s)."); Terminal.RegisterCommand("ping", Ping, "ping - return the current ping to any connection(s).");
Terminal.RegisterCommand("disconnect", Disconnect, "disconnect - Stop current server, or close current client abruptly.");
} }
} }
@ -101,6 +102,15 @@ namespace NeonTea.Quakeball.Networking {
return true; return true;
} }
private bool Disconnect(string[] args) {
if (Net.Singleton.Instance == null) {
Terminal.Println("No hosting or connecting has been done!");
return false;
}
Net.Singleton.Instance.Stop();
return true;
}
public void Message(string msg) { public void Message(string msg) {
MessageQueue.Enqueue(msg); MessageQueue.Enqueue(msg);
} }

View File

@ -31,6 +31,8 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
ConnectionManager.Interval = value; ConnectionManager.Interval = value;
} }
} }
/// <summary>Whether the Peer is currently doing anything or not.null</sumary>
public bool Running { get; private set; }
public ListenerThread ListenerThread; public ListenerThread ListenerThread;
public ConnectionManager ConnectionManager; public ConnectionManager ConnectionManager;
@ -48,8 +50,12 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
/// <summary>Starts the UdpClient, but does no networking as is.</summary> /// <summary>Starts the UdpClient, but does no networking as is.</summary>
public void Start(int sending_port) { public void Start(int sending_port) {
if (Running) {
return;
}
UdpClient = new UdpClient(sending_port); UdpClient = new UdpClient(sending_port);
MessageListener.Message("UdpClient Started"); MessageListener.Message("UdpClient Started");
Running = true;
} }
/// <summary>Abruptly stops the UdpClient and all relevant threads.</summary> /// <summary>Abruptly stops the UdpClient and all relevant threads.</summary>
@ -60,6 +66,7 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
} }
UdpClient.Dispose(); UdpClient.Dispose();
UdpClient.Close(); UdpClient.Close();
Running = false;
} }
/// <summary>Start listening to a given address and port. Usually 0.0.0.0, 0 for clients and 0.0.0.0, port for servers</summary> /// <summary>Start listening to a given address and port. Usually 0.0.0.0, 0 for clients and 0.0.0.0, port for servers</summary>
@ -95,18 +102,24 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
/// <summary>Send a reliable packet, meaning it will reliably be delivered.</summary> /// <summary>Send a reliable packet, meaning it will reliably be delivered.</summary>
public void SendReliable(ulong uid, Packet packet) { public void SendReliable(ulong uid, Packet packet) {
ConnectionManager.AddPacketToQueue(uid, packet); if (Running) {
ConnectionManager.SendPacketQueue(uid); ConnectionManager.AddPacketToQueue(uid, packet);
ConnectionManager.SendPacketQueue(uid);
}
} }
/// <summary>Add reliable packet to queue, so that it will be sent on the next update.</summary> /// <summary>Add reliable packet to queue, so that it will be sent on the next update.</summary>
public void SendReliableLater(ulong uid, Packet packet) { public void SendReliableLater(ulong uid, Packet packet) {
ConnectionManager.AddPacketToQueue(uid, packet); if (Running) {
ConnectionManager.AddPacketToQueue(uid, packet);
}
} }
/// <summary>Send an unreliable packet, meaning its delivery is not reliable.</summary> /// <summary>Send an unreliable packet, meaning its delivery is not reliable.</summary>
public void SendUnreliable(ulong uid, Packet packet) { public void SendUnreliable(ulong uid, Packet packet) {
ConnectionManager.SendSingleUnreliable(uid, packet); if (Running) {
ConnectionManager.SendSingleUnreliable(uid, packet);
}
} }
/// <summary>Get a Connection instance from the given uid, if such exists. Null otherwise.</summary> /// <summary>Get a Connection instance from the given uid, if such exists. Null otherwise.</summary>