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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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