DownloadManager updated [Server]

This commit is contained in:
EntenKoeniq
2022-04-03 18:46:57 +02:00
parent c8923359f1
commit 7777310267
2 changed files with 26 additions and 51 deletions

View File

@ -1,5 +1,4 @@
using System; using System.IO;
using System.IO;
using System.Linq; using System.Linq;
using System.Collections.Generic; using System.Collections.Generic;
@ -20,13 +19,7 @@ namespace CoopServer
return; return;
} }
List<DownloadFile> DeepCopy = new(); _clients.Add(new DownloadClient(nethandle, new(_files)));
foreach (DownloadFile file in _files)
{
DeepCopy.Add((DownloadFile)file.Clone());
}
_clients.Add(new DownloadClient(nethandle, DeepCopy));
} }
public static bool CheckForDirectoryAndFiles() public static bool CheckForDirectoryAndFiles()
@ -35,14 +28,12 @@ namespace CoopServer
if (!Directory.Exists("clientside")) if (!Directory.Exists("clientside"))
{ {
AnyFileExists = false;
return false; return false;
} }
filePaths = Directory.GetFiles("clientside"); filePaths = Directory.GetFiles("clientside");
if (filePaths.Length == 0) if (filePaths.Length == 0)
{ {
AnyFileExists = false;
return false; return false;
} }
@ -69,10 +60,9 @@ namespace CoopServer
{ {
while (bs.Read(buffer, 0, MAX_BUFFER) != 0) // Reading 5KB chunks at time while (bs.Read(buffer, 0, MAX_BUFFER) != 0) // Reading 5KB chunks at time
{ {
if (!fileCreated) if (!fileCreated && (fileCreated = true))
{ {
newFile = new() { FileID = fileCount, FileName = fileInfo.Name, FileLength = fileInfo.Length }; newFile = new() { FileID = fileCount, FileName = fileInfo.Name, FileLength = fileInfo.Length, FileData = new() };
fileCreated = true;
} }
newFile.FileData.Add(buffer); newFile.FileData.Add(buffer);
@ -83,8 +73,6 @@ namespace CoopServer
fileCount++; fileCount++;
} }
Logging.Info($"{_files.Count} files found!");
AnyFileExists = true; AnyFileExists = true;
return true; return true;
} }
@ -138,18 +126,19 @@ namespace CoopServer
internal class DownloadClient internal class DownloadClient
{ {
public long NetHandle = 0; public long NetHandle = 0;
private readonly List<DownloadFile> Files = null; private readonly List<DownloadFile> _files = null;
public int FilePosition = 0; public int FilePosition = 0;
private int _fileDataPosition = 0;
public DownloadClient(long nethandle, List<DownloadFile> files) public DownloadClient(long nethandle, List<DownloadFile> files)
{ {
NetHandle = nethandle; NetHandle = nethandle;
Files = files; _files = files;
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)
{ {
Files.ForEach(file => _files.ForEach(file =>
{ {
NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage(); NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage();
@ -177,13 +166,14 @@ namespace CoopServer
return false; return false;
} }
DownloadFile file = Files[FilePosition]; DownloadFile file = _files[FilePosition];
file.Send(NetHandle); Send(NetHandle, file);
if (file.FileDataPosition >= file.FileData.Count) if (_fileDataPosition >= file.FileData.Count)
{ {
FilePosition++; FilePosition++;
_fileDataPosition = 0;
return DownloadComplete(); return DownloadComplete();
} }
@ -191,22 +181,7 @@ namespace CoopServer
return true; return true;
} }
public bool DownloadComplete() private void Send(long nethandle, DownloadFile file)
{
return FilePosition >= Files.Count;
}
}
internal class DownloadFile : ICloneable
{
public byte FileID { get; set; }
public string FileName { get; set; }
public long FileLength { get; set; }
public List<byte[]> FileData { get; set; } = new List<byte[]>();
public int FileDataPosition { get; set; } = 0;
public void Send(long nethandle)
{ {
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)
@ -216,23 +191,23 @@ namespace CoopServer
NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage(); NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage();
new Packets.FileTransferTick() { ID = FileID, FileChunk = FileData[FileDataPosition] }.PacketToNetOutGoingMessage(outgoingMessage); new Packets.FileTransferTick() { ID = file.FileID, FileChunk = file.FileData[_fileDataPosition++] }.PacketToNetOutGoingMessage(outgoingMessage);
Server.MainNetServer.SendMessage(outgoingMessage, conn, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.File); Server.MainNetServer.SendMessage(outgoingMessage, conn, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.File);
FileDataPosition++;
} }
public object Clone() public bool DownloadComplete()
{ {
return new DownloadFile return FilePosition >= _files.Count;
{
FileID = FileID,
FileName = FileName,
FileLength = FileLength,
FileData = FileData,
FileDataPosition = 0
};
} }
} }
internal class DownloadFile
{
public byte FileID { get; set; } = 0;
public string FileName { get; set; } = string.Empty;
public long FileLength { get; set; } = 0;
public List<byte[]> FileData { get; set; } = null;
}
} }

View File

@ -196,7 +196,7 @@ namespace CoopServer
} }
} }
Logging.Info("Client-side files are checked..."); Logging.Info("Searching for client-side files...");
DownloadManager.CheckForDirectoryAndFiles(); DownloadManager.CheckForDirectoryAndFiles();
Listen(); Listen();