More bug fixes and changes for DownloadManager
This commit is contained in:
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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)
|
||||||
|
@ -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;
|
||||||
|
@ -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}]");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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>
|
||||||
|
@ -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,8 +20,11 @@ namespace CoopServer
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lock (_clients)
|
||||||
|
{
|
||||||
_clients.Add(new DownloadClient(nethandle, new(_files)));
|
_clients.Add(new DownloadClient(nethandle, new(_files)));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static bool CheckForDirectoryAndFiles()
|
public static bool CheckForDirectoryAndFiles()
|
||||||
{
|
{
|
||||||
@ -79,6 +82,8 @@ namespace CoopServer
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void Tick()
|
public static void Tick()
|
||||||
|
{
|
||||||
|
lock (_clients)
|
||||||
{
|
{
|
||||||
lock (ClientsToDelete)
|
lock (ClientsToDelete)
|
||||||
{
|
{
|
||||||
@ -95,7 +100,7 @@ namespace CoopServer
|
|||||||
|
|
||||||
_clients.ForEach(client =>
|
_clients.ForEach(client =>
|
||||||
{
|
{
|
||||||
if (!client.SendFiles())
|
if (client.SendFiles())
|
||||||
{
|
{
|
||||||
lock (Server.Clients)
|
lock (Server.Clients)
|
||||||
{
|
{
|
||||||
@ -105,10 +110,11 @@ namespace CoopServer
|
|||||||
x.FilesReceived = true;
|
x.FilesReceived = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
AddClientToRemove(client.NetHandle);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
Logging.Debug($"Clients [{_clients.Count}]");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void RemoveClient(long nethandle)
|
public static void RemoveClient(long nethandle)
|
||||||
@ -126,6 +132,8 @@ namespace CoopServer
|
|||||||
/// <param name="nethandle"></param>
|
/// <param name="nethandle"></param>
|
||||||
/// <param name="id"></param>
|
/// <param name="id"></param>
|
||||||
public static void TryToRemoveClient(long nethandle, int id)
|
public static void TryToRemoveClient(long nethandle, int id)
|
||||||
|
{
|
||||||
|
lock (_clients)
|
||||||
{
|
{
|
||||||
DownloadClient client = _clients.FirstOrDefault(x => x.NetHandle == nethandle);
|
DownloadClient client = _clients.FirstOrDefault(x => x.NetHandle == nethandle);
|
||||||
if (client == null)
|
if (client == null)
|
||||||
@ -142,6 +150,15 @@ namespace CoopServer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void AddClientToRemove(long nethandle)
|
||||||
|
{
|
||||||
|
lock (ClientsToDelete)
|
||||||
|
{
|
||||||
|
ClientsToDelete.Add(nethandle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
internal class DownloadClient
|
internal class DownloadClient
|
||||||
{
|
{
|
||||||
public long NetHandle = 0;
|
public long NetHandle = 0;
|
||||||
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user