Small changes and stopping sending own disconnect packet

This commit is contained in:
EntenKoeniq
2022-04-03 19:48:51 +02:00
parent fffe619f1e
commit ebba1e5d28
3 changed files with 54 additions and 24 deletions

View File

@ -27,7 +27,7 @@ namespace CoopServer
private long CallbacksCount = 0; private long CallbacksCount = 0;
internal readonly Dictionary<long, Action<object>> Callbacks = new(); internal readonly Dictionary<long, Action<object>> Callbacks = new();
internal bool FilesReceived = false; internal bool FilesReceived = false;
public bool FilesSent = false; internal bool FilesSent = false;
#region CUSTOMDATA FUNCTIONS #region CUSTOMDATA FUNCTIONS
public void SetData<T>(string name, T data) public void SetData<T>(string name, T data)

View File

@ -8,7 +8,8 @@ namespace CoopServer
{ {
internal static class DownloadManager internal static class DownloadManager
{ {
private static readonly List<DownloadClient> _clients = new(); public static readonly List<long> ClientsToDelete = new();
private static List<DownloadClient> _clients = new();
private static readonly List<DownloadFile> _files = new(); private static readonly List<DownloadFile> _files = new();
public static bool AnyFileExists = false; public static bool AnyFileExists = false;
@ -79,9 +80,24 @@ namespace CoopServer
public static void Tick() public static void Tick()
{ {
lock (ClientsToDelete)
{
foreach (long nethandle in ClientsToDelete)
{
DownloadClient client = _clients.FirstOrDefault(x => x.NetHandle == nethandle);
if (client != null)
{
_clients.Remove(client);
}
}
ClientsToDelete.Clear();
}
_clients.ForEach(client => _clients.ForEach(client =>
{ {
if (!client.SendFiles()) if (!client.SendFiles())
{
lock (Server.Clients)
{ {
Client x = Server.Clients.FirstOrDefault(x => x.NetHandle == client.NetHandle); Client x = Server.Clients.FirstOrDefault(x => x.NetHandle == client.NetHandle);
if (x != null) if (x != null)
@ -89,6 +105,7 @@ namespace CoopServer
x.FilesReceived = true; x.FilesReceived = true;
} }
} }
}
}); });
} }
@ -136,8 +153,15 @@ namespace CoopServer
_files = files; _files = files;
NetConnection conn = Server.MainNetServer.Connections.FirstOrDefault(x => x.RemoteUniqueIdentifier == NetHandle); NetConnection conn = Server.MainNetServer.Connections.FirstOrDefault(x => x.RemoteUniqueIdentifier == NetHandle);
if (conn != null) if (conn == null)
{ {
lock (DownloadManager.ClientsToDelete)
{
DownloadManager.ClientsToDelete.Add(NetHandle);
}
return;
}
_files.ForEach(file => _files.ForEach(file =>
{ {
NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage(); NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage();
@ -153,7 +177,6 @@ namespace CoopServer
Server.MainNetServer.SendMessage(outgoingMessage, conn, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.File); Server.MainNetServer.SendMessage(outgoingMessage, conn, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.File);
}); });
} }
}
/// <summary> /// <summary>
/// ///
@ -186,6 +209,10 @@ namespace CoopServer
NetConnection conn = Server.MainNetServer.Connections.FirstOrDefault(x => x.RemoteUniqueIdentifier == nethandle); NetConnection conn = Server.MainNetServer.Connections.FirstOrDefault(x => x.RemoteUniqueIdentifier == nethandle);
if (conn == null) if (conn == null)
{ {
lock (DownloadManager.ClientsToDelete)
{
DownloadManager.ClientsToDelete.Add(NetHandle);
}
return; return;
} }

View File

@ -271,7 +271,10 @@ namespace CoopServer
{ {
long nethandle = message.SenderConnection.RemoteUniqueIdentifier; long nethandle = message.SenderConnection.RemoteUniqueIdentifier;
DownloadManager.RemoveClient(nethandle); lock (DownloadManager.ClientsToDelete)
{
DownloadManager.ClientsToDelete.Add(nethandle);
}
SendPlayerDisconnectPacket(nethandle); SendPlayerDisconnectPacket(nethandle);
} }
@ -736,20 +739,20 @@ namespace CoopServer
} }
// Send all players a message that someone has left the server // Send all players a message that someone has left the server
private static void SendPlayerDisconnectPacket(long clientID) private static void SendPlayerDisconnectPacket(long nethandle)
{ {
List<NetConnection> clients = MainNetServer.Connections; List<NetConnection> clients = MainNetServer.Connections.FindAll(x => x.RemoteUniqueIdentifier != nethandle);
if (clients.Count > 0) if (clients.Count > 0)
{ {
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage(); NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
new Packets.PlayerDisconnect() new Packets.PlayerDisconnect()
{ {
NetHandle = clientID NetHandle = nethandle
}.PacketToNetOutGoingMessage(outgoingMessage); }.PacketToNetOutGoingMessage(outgoingMessage);
MainNetServer.SendMessage(outgoingMessage, clients, NetDeliveryMethod.ReliableOrdered, 0); MainNetServer.SendMessage(outgoingMessage, clients, NetDeliveryMethod.ReliableOrdered, 0);
} }
Client localClient = Clients.Find(x => x.NetHandle == clientID); Client localClient = Clients.FirstOrDefault(x => x.NetHandle == nethandle);
if (localClient == null) if (localClient == null)
{ {
return; return;