More bug fixes and changes for DownloadManager

This commit is contained in:
EntenKoeniq
2022-04-06 05:54:03 +02:00
parent a6d2acac79
commit 1e1b22462d
7 changed files with 95 additions and 59 deletions

View File

@ -9,6 +9,7 @@ namespace CoopClient
private static readonly List<DownloadFile> _downloadFiles = new List<DownloadFile>();
private static readonly Dictionary<byte, FileStream> _streams = new Dictionary<byte, FileStream>();
private static readonly List<byte> _filesFinished = new List<byte>();
public static bool DownloadComplete = false;
public static void AddFile(byte id, Packets.DataFileType type, string name, long length)
{
@ -143,6 +144,8 @@ namespace CoopClient
_downloadFiles.Clear();
_filesFinished.Clear();
}
DownloadComplete = false;
}
}

View File

@ -17,6 +17,7 @@ namespace CoopClient
public class JavascriptHook : Script
{
private static readonly List<V8ScriptEngine> ScriptEngines = new List<V8ScriptEngine>();
internal static bool JavascriptLoaded { get; private set; } = false;
/// <summary>
/// Don't use this!
@ -88,15 +89,23 @@ namespace CoopClient
}
}
}
JavascriptLoaded = true;
}
internal static void StopAll()
{
lock (ScriptEngines)
{
ScriptEngines.ForEach(engine => engine.Script.API.InvokeStop());
ScriptEngines.ForEach(engine =>
{
engine.Script.API.InvokeStop();
engine.Dispose();
});
ScriptEngines.Clear();
}
JavascriptLoaded = false;
}
internal static void InvokePlayerConnect(string username, long nethandle)

View File

@ -110,6 +110,11 @@ namespace CoopClient
MainNetworking.ReceiveMessages();
if (!JavascriptHook.JavascriptLoaded && DownloadManager.DownloadComplete)
{
JavascriptHook.LoadAll();
}
if (IsGoingToCar && Game.Player.Character.IsInVehicle())
{
IsGoingToCar = false;

View File

@ -129,7 +129,6 @@ namespace CoopClient
COOPAPI.Connected();
GTA.UI.Notification.Show("~g~Connected!");
JavascriptHook.LoadAll();
Logger.Write(">> Connected <<", Logger.LogLevel.Server);
}
break;
@ -453,6 +452,27 @@ namespace CoopClient
}
}
break;
case (byte)PacketTypes.FileTransferTick:
{
try
{
int len = message.ReadInt32();
byte[] data = message.ReadBytes(len);
Packets.FileTransferTick packet = new Packets.FileTransferTick();
packet.NetIncomingMessageToPacket(data);
DownloadManager.Write(packet.ID, packet.FileChunk);
}
catch (Exception ex)
{
GTA.UI.Notification.Show("~r~~h~Packet Error");
Logger.Write($"[{packetType}] {ex.Message}", Logger.LogLevel.Server);
Logger.Write($"[{packetType}] {ex.Source}", Logger.LogLevel.Server);
Logger.Write($"[{packetType}] {ex.StackTrace}", Logger.LogLevel.Server);
Client.Disconnect($"Packet Error [{packetType}]");
}
}
break;
case (byte)PacketTypes.FileTransferRequest:
{
try
@ -472,23 +492,21 @@ namespace CoopClient
}
}
break;
case (byte)PacketTypes.FileTransferTick:
case (byte)PacketTypes.FileTransferComplete:
{
try
{
int len = message.ReadInt32();
byte[] data = message.ReadBytes(len);
Packets.FileTransferTick packet = new Packets.FileTransferTick();
Packets.FileTransferComplete packet = new Packets.FileTransferComplete();
packet.NetIncomingMessageToPacket(data);
DownloadManager.Write(packet.ID, packet.FileChunk);
DownloadManager.DownloadComplete = true;
}
catch (Exception ex)
{
GTA.UI.Notification.Show("~r~~h~Packet Error");
Logger.Write($"[{packetType}] {ex.Message}", Logger.LogLevel.Server);
Logger.Write($"[{packetType}] {ex.Source}", Logger.LogLevel.Server);
Logger.Write($"[{packetType}] {ex.StackTrace}", Logger.LogLevel.Server);
Client.Disconnect($"Packet Error [{packetType}]");
}
}

View File

@ -3,7 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<AssemblyVersion>1.47.0.0001</AssemblyVersion>
<AssemblyVersion>1.47.2.0001</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>
<RepositoryUrl>https://github.com/GTACOOP-R/GTACoop-R</RepositoryUrl>
</PropertyGroup>

View File

@ -8,7 +8,7 @@ namespace CoopServer
{
internal static class DownloadManager
{
public static readonly List<long> ClientsToDelete = new();
private static readonly List<long> ClientsToDelete = new();
private static List<DownloadClient> _clients = new();
private static readonly List<DownloadFile> _files = new();
public static bool AnyFileExists = false;
@ -20,7 +20,10 @@ namespace CoopServer
return;
}
_clients.Add(new DownloadClient(nethandle, new(_files)));
lock (_clients)
{
_clients.Add(new DownloadClient(nethandle, new(_files)));
}
}
public static bool CheckForDirectoryAndFiles()
@ -80,35 +83,38 @@ namespace CoopServer
public static void Tick()
{
lock (ClientsToDelete)
lock (_clients)
{
foreach (long nethandle in ClientsToDelete)
lock (ClientsToDelete)
{
DownloadClient client = _clients.FirstOrDefault(x => x.NetHandle == nethandle);
if (client != null)
foreach (long nethandle in ClientsToDelete)
{
_clients.Remove(client);
}
}
ClientsToDelete.Clear();
}
_clients.ForEach(client =>
{
if (!client.SendFiles())
{
lock (Server.Clients)
{
Client x = Server.Clients.FirstOrDefault(x => x.NetHandle == client.NetHandle);
if (x != null)
DownloadClient client = _clients.FirstOrDefault(x => x.NetHandle == nethandle);
if (client != null)
{
x.FilesReceived = true;
_clients.Remove(client);
}
}
ClientsToDelete.Clear();
}
});
Logging.Debug($"Clients [{_clients.Count}]");
_clients.ForEach(client =>
{
if (client.SendFiles())
{
lock (Server.Clients)
{
Client x = Server.Clients.FirstOrDefault(x => x.NetHandle == client.NetHandle);
if (x != null)
{
x.FilesReceived = true;
}
}
AddClientToRemove(client.NetHandle);
}
});
}
}
public static void RemoveClient(long nethandle)
@ -127,17 +133,28 @@ namespace CoopServer
/// <param name="id"></param>
public static void TryToRemoveClient(long nethandle, int id)
{
DownloadClient client = _clients.FirstOrDefault(x => x.NetHandle == nethandle);
if (client == null)
lock (_clients)
{
return;
DownloadClient client = _clients.FirstOrDefault(x => x.NetHandle == nethandle);
if (client == null)
{
return;
}
client.FilePosition++;
if (client.DownloadComplete())
{
_clients.Remove(client);
}
}
}
client.FilePosition++;
if (client.DownloadComplete())
public static void AddClientToRemove(long nethandle)
{
lock (ClientsToDelete)
{
_clients.Remove(client);
ClientsToDelete.Add(nethandle);
}
}
}
@ -157,10 +174,7 @@ namespace CoopServer
NetConnection conn = Server.MainNetServer.Connections.FirstOrDefault(x => x.RemoteUniqueIdentifier == NetHandle);
if (conn == null)
{
lock (DownloadManager.ClientsToDelete)
{
DownloadManager.ClientsToDelete.Add(NetHandle);
}
DownloadManager.AddClientToRemove(NetHandle);
return;
}
@ -201,14 +215,9 @@ namespace CoopServer
/// <summary>
///
/// </summary>
/// <returns>true if files should be sent otherwise false</returns>
/// <returns>true if we are done otherwise false</returns>
public bool SendFiles()
{
if (DownloadComplete())
{
return false;
}
DownloadFile file = _files[FilePosition];
Send(NetHandle, file);
@ -217,11 +226,9 @@ namespace CoopServer
{
FilePosition++;
_fileDataPosition = 0;
return DownloadComplete();
}
return true;
return DownloadComplete();
}
private void Send(long nethandle, DownloadFile file)
@ -229,10 +236,7 @@ namespace CoopServer
NetConnection conn = Server.MainNetServer.Connections.FirstOrDefault(x => x.RemoteUniqueIdentifier == nethandle);
if (conn == null)
{
lock (DownloadManager.ClientsToDelete)
{
DownloadManager.ClientsToDelete.Add(NetHandle);
}
DownloadManager.AddClientToRemove(NetHandle);
return;
}

View File

@ -271,10 +271,7 @@ namespace CoopServer
{
long nethandle = message.SenderConnection.RemoteUniqueIdentifier;
lock (DownloadManager.ClientsToDelete)
{
DownloadManager.ClientsToDelete.Add(nethandle);
}
DownloadManager.AddClientToRemove(nethandle);
SendPlayerDisconnectPacket(nethandle);
}