Updated DownloadManager
This commit is contained in:
@ -84,6 +84,7 @@
|
|||||||
<Compile Include="BitReader.cs" />
|
<Compile Include="BitReader.cs" />
|
||||||
<Compile Include="Chat.cs" />
|
<Compile Include="Chat.cs" />
|
||||||
<Compile Include="COOPAPI.cs" />
|
<Compile Include="COOPAPI.cs" />
|
||||||
|
<Compile Include="DownloadManager.cs" />
|
||||||
<Compile Include="Entities\NPC\EntitiesNPC.cs" />
|
<Compile Include="Entities\NPC\EntitiesNPC.cs" />
|
||||||
<Compile Include="Entities\NPC\Sync\OnFootSync.cs" />
|
<Compile Include="Entities\NPC\Sync\OnFootSync.cs" />
|
||||||
<Compile Include="Entities\NPC\Sync\VehicleSync.cs" />
|
<Compile Include="Entities\NPC\Sync\VehicleSync.cs" />
|
||||||
@ -98,6 +99,7 @@
|
|||||||
<Compile Include="Menus\Sub\Servers.cs" />
|
<Compile Include="Menus\Sub\Servers.cs" />
|
||||||
<Compile Include="Menus\Sub\Settings.cs" />
|
<Compile Include="Menus\Sub\Settings.cs" />
|
||||||
<Compile Include="Networking.cs" />
|
<Compile Include="Networking.cs" />
|
||||||
|
<Compile Include="Packets\DownloadPackets.cs" />
|
||||||
<Compile Include="Packets\NPCPackets.cs" />
|
<Compile Include="Packets\NPCPackets.cs" />
|
||||||
<Compile Include="Packets\Packets.cs" />
|
<Compile Include="Packets\Packets.cs" />
|
||||||
<Compile Include="Packets\PlayerPackets.cs" />
|
<Compile Include="Packets\PlayerPackets.cs" />
|
||||||
|
@ -1,35 +1,73 @@
|
|||||||
using System;
|
using System.IO;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace CoopClient
|
namespace CoopClient
|
||||||
{
|
{
|
||||||
internal class DownloadManager
|
internal class DownloadManager
|
||||||
{
|
{
|
||||||
public int FileID { get; set; }
|
public byte FileID { get; set; }
|
||||||
public Packets.DataFileType FileType { get; set; }
|
public Packets.DataFileType FileType { get; set; }
|
||||||
public string FileName { get; set; }
|
public string FileName { get; set; }
|
||||||
public int FileLength { get; set; }
|
public long FileLength { get; set; }
|
||||||
public int Downloaded { get; set; } = 0;
|
|
||||||
|
|
||||||
public DownloadManager(int id, Packets.DataFileType type, string name, int length)
|
private readonly FileStream _stream;
|
||||||
|
|
||||||
|
public DownloadManager()
|
||||||
{
|
{
|
||||||
FileID = id;
|
string downloadFolder = $"scripts\\{Main.MainSettings.LastServerAddress.Replace(":", ".")}";
|
||||||
FileType = type;
|
if (!Directory.Exists(downloadFolder))
|
||||||
FileName = name;
|
{
|
||||||
FileLength = length;
|
Directory.CreateDirectory(downloadFolder);
|
||||||
|
if (FileAlreadyExists(downloadFolder))
|
||||||
|
{
|
||||||
|
Finish();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DownloadPart(byte[] bytes)
|
_stream = new FileStream(downloadFolder + "\\" + FileName, FileMode.CreateNew);
|
||||||
{
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Finish()
|
/// <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 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Linq;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
using CoopClient.Entities.Player;
|
using CoopClient.Entities.Player;
|
||||||
@ -21,6 +22,8 @@ namespace CoopClient
|
|||||||
public int BytesReceived = 0;
|
public int BytesReceived = 0;
|
||||||
public int BytesSend = 0;
|
public int BytesSend = 0;
|
||||||
|
|
||||||
|
private Dictionary<byte, DownloadManager> _downloads = new Dictionary<byte, DownloadManager>();
|
||||||
|
|
||||||
public void DisConnectFromServer(string address)
|
public void DisConnectFromServer(string address)
|
||||||
{
|
{
|
||||||
if (IsOnServer())
|
if (IsOnServer())
|
||||||
@ -450,6 +453,54 @@ namespace CoopClient
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
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;
|
break;
|
||||||
case NetIncomingMessageType.ConnectionLatencyUpdated:
|
case NetIncomingMessageType.ConnectionLatencyUpdated:
|
||||||
@ -1143,6 +1194,23 @@ namespace CoopClient
|
|||||||
}
|
}
|
||||||
#endif
|
#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
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@ namespace CoopClient
|
|||||||
Map = 1
|
Map = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
public class FileDownload : Packet
|
public class FileRequest : Packet
|
||||||
{
|
{
|
||||||
public byte ID { get; set; }
|
public byte ID { get; set; }
|
||||||
|
|
||||||
@ -22,7 +22,7 @@ namespace CoopClient
|
|||||||
|
|
||||||
public string FileName { get; set; }
|
public string FileName { get; set; }
|
||||||
|
|
||||||
public int FileLength { get; set; }
|
public long FileLength { get; set; }
|
||||||
|
|
||||||
public override void PacketToNetOutGoingMessage(NetOutgoingMessage message)
|
public override void PacketToNetOutGoingMessage(NetOutgoingMessage message)
|
||||||
{
|
{
|
||||||
@ -61,7 +61,7 @@ namespace CoopClient
|
|||||||
FileType = reader.ReadByte();
|
FileType = reader.ReadByte();
|
||||||
int nameArrayLength = reader.ReadInt();
|
int nameArrayLength = reader.ReadInt();
|
||||||
FileName = reader.ReadString(nameArrayLength);
|
FileName = reader.ReadString(nameArrayLength);
|
||||||
FileLength = reader.ReadInt();
|
FileLength = reader.ReadLong();
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -128,7 +128,8 @@ namespace CoopClient
|
|||||||
NPCFull = 4,
|
NPCFull = 4,
|
||||||
Chat = 5,
|
Chat = 5,
|
||||||
Native = 6,
|
Native = 6,
|
||||||
Mod = 7
|
Mod = 7,
|
||||||
|
File = 8
|
||||||
}
|
}
|
||||||
|
|
||||||
[Flags]
|
[Flags]
|
||||||
|
Reference in New Issue
Block a user