DownloadManager update (NOT DONE YET!)
This commit is contained in:
@ -26,6 +26,7 @@ namespace CoopServer
|
||||
private readonly Dictionary<string, object> CustomData = new();
|
||||
private long CallbacksCount = 0;
|
||||
internal readonly Dictionary<long, Action<object>> Callbacks = new();
|
||||
internal bool FilesSent = false;
|
||||
|
||||
#region CUSTOMDATA FUNCTIONS
|
||||
public void SetData<T>(string name, T data)
|
||||
|
@ -4,31 +4,42 @@ using System.Collections.Generic;
|
||||
|
||||
namespace CoopServer
|
||||
{
|
||||
internal class DownloadManager
|
||||
internal static class DownloadManager
|
||||
{
|
||||
const int MAX_BUFFER = 1048576; // 1MB
|
||||
|
||||
// Key = Nethandle
|
||||
// Value = List of Files
|
||||
private Dictionary<long, List<DownloadFile>> _files = new();
|
||||
private static readonly List<DownloadClient> _clients = new();
|
||||
private static readonly List<DownloadFile> _files = new();
|
||||
public static bool AnyFileExists = false;
|
||||
|
||||
public void Create(long nethandle)
|
||||
public static void InsertClient(long nethandle)
|
||||
{
|
||||
if (!DirectoryAndFilesExists())
|
||||
if (!AnyFileExists)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
List<DownloadFile> files = new();
|
||||
_clients.Add(new DownloadClient() { NetHandle = nethandle, FilesCount = _files.Count });
|
||||
}
|
||||
|
||||
foreach (string file in Directory.GetFiles("clientside"))
|
||||
public static bool CheckForDirectoryAndFiles()
|
||||
{
|
||||
string[] filePaths = Directory.GetFiles("clientside");
|
||||
|
||||
if (!Directory.Exists("clientside") || filePaths.Length == 0)
|
||||
{
|
||||
AnyFileExists = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (string file in filePaths)
|
||||
{
|
||||
FileInfo fileInfo = new(file);
|
||||
|
||||
// ONLY JAVASCRIPT AND JSON FILES!
|
||||
if (!new string[] { ".js", ".json" }.Any(x => x == fileInfo.Extension))
|
||||
{
|
||||
Logging.Error("Only files with \"*.js\" and \"*.json\" can be sent!");
|
||||
Logging.Warning("Only files with \"*.js\" and \"*.json\" can be sent!");
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -57,19 +68,76 @@ namespace CoopServer
|
||||
}
|
||||
}
|
||||
|
||||
files.Add(newFile);
|
||||
_files.Add(newFile);
|
||||
}
|
||||
|
||||
_files.Add(nethandle, files);
|
||||
Logging.Info($"{_files.Count} files found!");
|
||||
|
||||
AnyFileExists = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool DirectoryAndFilesExists()
|
||||
public static void Tick()
|
||||
{
|
||||
if (!Directory.Exists("clientside") || Directory.GetFiles("clientside").Length == 0)
|
||||
_clients.ForEach(client =>
|
||||
{
|
||||
if (!client.SendFiles(_files))
|
||||
{
|
||||
Client x = Server.Clients.FirstOrDefault(x => x.NetHandle == client.NetHandle);
|
||||
if (x != null)
|
||||
{
|
||||
x.FilesSent = true;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void RemoveClient(long nethandle)
|
||||
{
|
||||
DownloadClient client = _clients.FirstOrDefault(x => x.NetHandle == nethandle);
|
||||
if (client != null)
|
||||
{
|
||||
_clients.Remove(client);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal class DownloadClient
|
||||
{
|
||||
public long NetHandle { get; set; }
|
||||
public int FilesCount { get; set; }
|
||||
public int FilesSent = 0;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <returns>true if files should be sent otherwise false</returns>
|
||||
public bool SendFiles(List<DownloadFile> files)
|
||||
{
|
||||
if (FilesSent >= FilesCount)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
DownloadFile file = files.FirstOrDefault(x => !x.DownloadFinished());
|
||||
if (file == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
file.Send(NetHandle);
|
||||
|
||||
// Check it again, maybe this file is finish now
|
||||
if (file.DownloadFinished())
|
||||
{
|
||||
FilesSent++;
|
||||
|
||||
if (FilesSent >= FilesCount)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -80,10 +148,10 @@ namespace CoopServer
|
||||
public string FileName { get; set; }
|
||||
public long FileLength { get; set; }
|
||||
|
||||
private List<byte[]> _data = new();
|
||||
private long _sent = 0;
|
||||
private readonly List<byte[]> _data = new();
|
||||
private readonly long _sent = 0;
|
||||
|
||||
public void Upload()
|
||||
public void Send(long nethandle)
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
|
@ -36,8 +36,6 @@ namespace CoopServer
|
||||
|
||||
public static readonly List<Client> Clients = new();
|
||||
|
||||
public static DownloadManager MainDownloadManager = new();
|
||||
|
||||
public Server()
|
||||
{
|
||||
Logging.Info("================");
|
||||
@ -198,6 +196,9 @@ namespace CoopServer
|
||||
}
|
||||
}
|
||||
|
||||
Logging.Info("Client-side files are checked...");
|
||||
DownloadManager.CheckForDirectoryAndFiles();
|
||||
|
||||
Listen();
|
||||
}
|
||||
|
||||
@ -206,8 +207,6 @@ namespace CoopServer
|
||||
Logging.Info("Listening for clients");
|
||||
Logging.Info("Please use CTRL + C if you want to stop the server!");
|
||||
|
||||
MainDownloadManager.Create(0);
|
||||
|
||||
while (!Program.ReadyToStop)
|
||||
{
|
||||
if (RunningResource != null)
|
||||
@ -215,6 +214,23 @@ namespace CoopServer
|
||||
RunningResource.InvokeTick(++CurrentTick);
|
||||
}
|
||||
|
||||
// Only new clients that did not receive files on connection will receive the current files in "clientside"
|
||||
if (DownloadManager.AnyFileExists)
|
||||
{
|
||||
lock (Clients)
|
||||
{
|
||||
Clients.ForEach(client =>
|
||||
{
|
||||
if (!client.FilesSent)
|
||||
{
|
||||
DownloadManager.InsertClient(client.NetHandle);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
DownloadManager.Tick();
|
||||
}
|
||||
|
||||
NetIncomingMessage message;
|
||||
|
||||
while ((message = MainNetServer.ReadMessage()) != null)
|
||||
|
Reference in New Issue
Block a user