Updated DownloadManager

This commit is contained in:
EntenKoeniq
2022-04-02 18:11:30 +02:00
parent 7e1764d526
commit cc3e0829c0
5 changed files with 128 additions and 19 deletions

View File

@ -84,6 +84,7 @@
<Compile Include="BitReader.cs" />
<Compile Include="Chat.cs" />
<Compile Include="COOPAPI.cs" />
<Compile Include="DownloadManager.cs" />
<Compile Include="Entities\NPC\EntitiesNPC.cs" />
<Compile Include="Entities\NPC\Sync\OnFootSync.cs" />
<Compile Include="Entities\NPC\Sync\VehicleSync.cs" />
@ -98,6 +99,7 @@
<Compile Include="Menus\Sub\Servers.cs" />
<Compile Include="Menus\Sub\Settings.cs" />
<Compile Include="Networking.cs" />
<Compile Include="Packets\DownloadPackets.cs" />
<Compile Include="Packets\NPCPackets.cs" />
<Compile Include="Packets\Packets.cs" />
<Compile Include="Packets\PlayerPackets.cs" />

View File

@ -1,35 +1,73 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace CoopClient
{
internal class DownloadManager
{
public int FileID { get; set; }
public byte FileID { get; set; }
public Packets.DataFileType FileType { get; set; }
public string FileName { get; set; }
public int FileLength { get; set; }
public int Downloaded { get; set; } = 0;
public long FileLength { get; set; }
public DownloadManager(int id, Packets.DataFileType type, string name, int length)
private readonly FileStream _stream;
public DownloadManager()
{
FileID = id;
FileType = type;
FileName = name;
FileLength = length;
string downloadFolder = $"scripts\\{Main.MainSettings.LastServerAddress.Replace(":", ".")}";
if (!Directory.Exists(downloadFolder))
{
Directory.CreateDirectory(downloadFolder);
if (FileAlreadyExists(downloadFolder))
{
Finish();
}
}
_stream = new FileStream(downloadFolder + "\\" + FileName, FileMode.CreateNew);
}
public void DownloadPart(byte[] bytes)
/// <summary>
/// Check if the file already exists and if the size correct otherwise delete this file
/// </summary>
/// <param name="folder"></param>
private bool FileAlreadyExists(string folder)
{
string filePath = $"{folder}\\{FileName}";
if (File.Exists(filePath))
{
if (new FileInfo(filePath).Length == FileLength)
{
return true;
}
else
{
// Delete the file because the length is wrong (maybe the file was updated)
File.Delete(filePath);
}
}
return false;
}
public void Finish()
public void DownloadPart(byte[] data)
{
_stream.Write(data, 0, data.Length);
if (data.Length >= FileLength)
{
Finish();
}
}
private void Finish()
{
if (_stream != null)
{
_stream.Close();
_stream.Dispose();
}
// Send the server we are done
Main.MainNetworking.SendDownloadFinish(FileID);
}
}
}

View File

@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Collections.Generic;
using CoopClient.Entities.Player;
@ -21,6 +22,8 @@ namespace CoopClient
public int BytesReceived = 0;
public int BytesSend = 0;
private Dictionary<byte, DownloadManager> _downloads = new Dictionary<byte, DownloadManager>();
public void DisConnectFromServer(string address)
{
if (IsOnServer())
@ -450,6 +453,54 @@ namespace CoopClient
}
}
break;
case (byte)PacketTypes.FileTransferRequest:
{
try
{
int len = message.ReadInt32();
byte[] data = message.ReadBytes(len);
Packets.FileRequest packet = new Packets.FileRequest();
packet.NetIncomingMessageToPacket(data);
_downloads.Add(packet.ID, new DownloadManager()
{
FileID = packet.ID,
FileType = (Packets.DataFileType)packet.FileType,
FileName = packet.FileName,
FileLength = packet.FileLength
});
}
catch (Exception ex)
{
GTA.UI.Notification.Show("~r~~h~Packet Error");
Logger.Write($"[{packetType}] {ex.Message}", Logger.LogLevel.Server);
Client.Disconnect($"Packet Error [{packetType}]");
}
}
break;
case (byte)PacketTypes.FileTransferTick:
{
try
{
int len = message.ReadInt32();
byte[] data = message.ReadBytes(len);
Packets.FileTransferTick packet = new Packets.FileTransferTick();
packet.NetIncomingMessageToPacket(data);
KeyValuePair<byte, DownloadManager> dm = _downloads.FirstOrDefault(x => x.Key == packet.ID);
if (dm.Value != null)
{
dm.Value.DownloadPart(packet.FileChunk);
}
}
catch (Exception ex)
{
GTA.UI.Notification.Show("~r~~h~Packet Error");
Logger.Write($"[{packetType}] {ex.Message}", Logger.LogLevel.Server);
Client.Disconnect($"Packet Error [{packetType}]");
}
}
break;
}
break;
case NetIncomingMessageType.ConnectionLatencyUpdated:
@ -1143,6 +1194,23 @@ namespace CoopClient
}
#endif
}
public void SendDownloadFinish(byte id)
{
NetOutgoingMessage outgoingMessage = Client.CreateMessage();
new Packets.FileTransferComplete() { ID = id }.PacketToNetOutGoingMessage(outgoingMessage);
Client.SendMessage(outgoingMessage, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.File);
Client.FlushSendQueue();
#if DEBUG
if (ShowNetworkInfo)
{
BytesSend += outgoingMessage.LengthBytes;
}
#endif
}
#endregion
}
}

View File

@ -14,7 +14,7 @@ namespace CoopClient
Map = 1
}
public class FileDownload : Packet
public class FileRequest : Packet
{
public byte ID { get; set; }
@ -22,7 +22,7 @@ namespace CoopClient
public string FileName { get; set; }
public int FileLength { get; set; }
public long FileLength { get; set; }
public override void PacketToNetOutGoingMessage(NetOutgoingMessage message)
{
@ -61,7 +61,7 @@ namespace CoopClient
FileType = reader.ReadByte();
int nameArrayLength = reader.ReadInt();
FileName = reader.ReadString(nameArrayLength);
FileLength = reader.ReadInt();
FileLength = reader.ReadLong();
#endregion
}
}

View File

@ -128,7 +128,8 @@ namespace CoopClient
NPCFull = 4,
Chat = 5,
Native = 6,
Mod = 7
Mod = 7,
File = 8
}
[Flags]