Make disconnecting work good and add Peer.Running
This commit is contained in:
parent
d5edcab211
commit
fc2e737c8a
@ -23,7 +23,7 @@ namespace NeonTea.Quakeball.Networking.Instances {
|
||||
private byte LastPingIdent;
|
||||
|
||||
public override void Start(string address, int port, PeerMessageListener listener) {
|
||||
if (Peer != null) {
|
||||
if (Peer.Running) {
|
||||
return;
|
||||
}
|
||||
Peer = new Peer(Fingerprint);
|
||||
@ -38,6 +38,19 @@ namespace NeonTea.Quakeball.Networking.Instances {
|
||||
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) {
|
||||
Terminal.Singleton.Println($"{conn.uid} Connected");
|
||||
if (Server == null) {
|
||||
@ -51,6 +64,14 @@ namespace NeonTea.Quakeball.Networking.Instances {
|
||||
Terminal.Singleton.Println($"{conn.uid} Disconnected: {conn.ClosingReason}");
|
||||
if (Server == conn) {
|
||||
Server = null;
|
||||
foreach (NetPlayer p in Players.Values) {
|
||||
if (p.Id != LocalPlayer.Id) {
|
||||
GameObject.Destroy(p.Controlled.gameObject);
|
||||
}
|
||||
}
|
||||
Players.Clear();
|
||||
Peer.Stop();
|
||||
Ping = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -13,7 +13,12 @@ namespace NeonTea.Quakeball.Networking.Instances {
|
||||
public Peer Peer;
|
||||
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 OnStop();
|
||||
public abstract void Connected(Connection conn);
|
||||
public abstract void Disconnected(Connection conn);
|
||||
public abstract void Handle(Connection conn, Packet packet);
|
||||
@ -25,10 +30,10 @@ namespace NeonTea.Quakeball.Networking.Instances {
|
||||
public virtual void Update() { }
|
||||
|
||||
public void Stop() {
|
||||
if (Peer != null) {
|
||||
if (Peer.Running) {
|
||||
OnStop();
|
||||
Peer.Stop();
|
||||
Peer.MessageListener.Message("Stopping");
|
||||
Peer = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -23,7 +23,7 @@ namespace NeonTea.Quakeball.Networking.Instances {
|
||||
public static float PingInterval = 1;
|
||||
|
||||
public override void Start(string address, int port, PeerMessageListener listener) {
|
||||
if (Peer != null) {
|
||||
if (Peer.Running) {
|
||||
return;
|
||||
}
|
||||
Peer = new Peer(Fingerprint);
|
||||
@ -39,6 +39,17 @@ namespace NeonTea.Quakeball.Networking.Instances {
|
||||
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) {
|
||||
Terminal.Singleton.Println($"{conn.uid} Connected");
|
||||
foreach (NetPlayer p in Players.Values) {
|
||||
@ -61,6 +72,7 @@ namespace NeonTea.Quakeball.Networking.Instances {
|
||||
public override void Disconnected(Connection conn) {
|
||||
Terminal.Singleton.Println($"{conn.uid} Disconnected: {conn.ClosingReason}");
|
||||
if (Players.ContainsKey(conn.uid)) {
|
||||
GameObject.Destroy(Players[conn.uid].Controlled.gameObject);
|
||||
RemovePlayer(Players[conn.uid]);
|
||||
}
|
||||
}
|
||||
|
@ -26,6 +26,7 @@ namespace NeonTea.Quakeball.Networking {
|
||||
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("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;
|
||||
}
|
||||
|
||||
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) {
|
||||
MessageQueue.Enqueue(msg);
|
||||
}
|
||||
|
@ -31,6 +31,8 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
|
||||
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 ConnectionManager ConnectionManager;
|
||||
@ -48,8 +50,12 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
|
||||
|
||||
/// <summary>Starts the UdpClient, but does no networking as is.</summary>
|
||||
public void Start(int sending_port) {
|
||||
if (Running) {
|
||||
return;
|
||||
}
|
||||
UdpClient = new UdpClient(sending_port);
|
||||
MessageListener.Message("UdpClient Started");
|
||||
Running = true;
|
||||
}
|
||||
|
||||
/// <summary>Abruptly stops the UdpClient and all relevant threads.</summary>
|
||||
@ -60,6 +66,7 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
|
||||
}
|
||||
UdpClient.Dispose();
|
||||
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>
|
||||
@ -95,19 +102,25 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
|
||||
|
||||
/// <summary>Send a reliable packet, meaning it will reliably be delivered.</summary>
|
||||
public void SendReliable(ulong uid, Packet packet) {
|
||||
if (Running) {
|
||||
ConnectionManager.AddPacketToQueue(uid, packet);
|
||||
ConnectionManager.SendPacketQueue(uid);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Add reliable packet to queue, so that it will be sent on the next update.</summary>
|
||||
public void SendReliableLater(ulong uid, Packet packet) {
|
||||
if (Running) {
|
||||
ConnectionManager.AddPacketToQueue(uid, packet);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Send an unreliable packet, meaning its delivery is not reliable.</summary>
|
||||
public void SendUnreliable(ulong uid, Packet packet) {
|
||||
if (Running) {
|
||||
ConnectionManager.SendSingleUnreliable(uid, packet);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Get a Connection instance from the given uid, if such exists. Null otherwise.</summary>
|
||||
public Connection GetConnection(ulong uid) {
|
||||
|
Loading…
Reference in New Issue
Block a user