Added DownloadManager for the server (NOT DONE YET!)
This commit is contained in:
@ -19,7 +19,9 @@ namespace CoopClient
|
|||||||
Directory.CreateDirectory(downloadFolder);
|
Directory.CreateDirectory(downloadFolder);
|
||||||
if (FileAlreadyExists(downloadFolder))
|
if (FileAlreadyExists(downloadFolder))
|
||||||
{
|
{
|
||||||
Finish();
|
// Send the server we are already done
|
||||||
|
Main.MainNetworking.SendDownloadFinish(FileID);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,21 +55,10 @@ namespace CoopClient
|
|||||||
{
|
{
|
||||||
_stream.Write(data, 0, data.Length);
|
_stream.Write(data, 0, data.Length);
|
||||||
if (data.Length >= FileLength)
|
if (data.Length >= FileLength)
|
||||||
{
|
|
||||||
Finish();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void Finish()
|
|
||||||
{
|
|
||||||
if (_stream != null)
|
|
||||||
{
|
{
|
||||||
_stream.Close();
|
_stream.Close();
|
||||||
_stream.Dispose();
|
_stream.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send the server we are done
|
|
||||||
Main.MainNetworking.SendDownloadFinish(FileID);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
101
Server/DownloadManager.cs
Normal file
101
Server/DownloadManager.cs
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace CoopServer
|
||||||
|
{
|
||||||
|
internal class DownloadManager
|
||||||
|
{
|
||||||
|
const int MAX_BUFFER = 1048576; // 1MB
|
||||||
|
|
||||||
|
// Key = Nethandle
|
||||||
|
// Value = List of Files
|
||||||
|
private Dictionary<long, List<DownloadFile>> _files = new();
|
||||||
|
|
||||||
|
public void Create(long nethandle)
|
||||||
|
{
|
||||||
|
if (!DirectoryAndFilesExists())
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<DownloadFile> files = new();
|
||||||
|
|
||||||
|
foreach (string file in Directory.GetFiles("clientside"))
|
||||||
|
{
|
||||||
|
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!");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Logging.Debug($"===== {fileInfo.Name} =====");
|
||||||
|
|
||||||
|
byte[] buffer = new byte[MAX_BUFFER];
|
||||||
|
int bytesRead = 0;
|
||||||
|
bool fileCreated = false;
|
||||||
|
DownloadFile newFile = null;
|
||||||
|
byte fileCount = 0;
|
||||||
|
|
||||||
|
using (FileStream fs = File.Open(file, FileMode.Open, FileAccess.Read))
|
||||||
|
using (BufferedStream bs = new(fs))
|
||||||
|
{
|
||||||
|
while ((bytesRead = bs.Read(buffer, 0, MAX_BUFFER)) != 0) // Reading 1MB chunks at time
|
||||||
|
{
|
||||||
|
if (!fileCreated)
|
||||||
|
{
|
||||||
|
newFile = new() { FileID = fileCount, FileName = fileInfo.Name, FileLength = fileInfo.Length };
|
||||||
|
fileCreated = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
newFile.AddData(buffer);
|
||||||
|
|
||||||
|
Logging.Debug($"{bytesRead}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
files.Add(newFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
_files.Add(nethandle, files);
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool DirectoryAndFilesExists()
|
||||||
|
{
|
||||||
|
if (!Directory.Exists("clientside") || Directory.GetFiles("clientside").Length == 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal class DownloadFile
|
||||||
|
{
|
||||||
|
public int FileID { get; set; }
|
||||||
|
public string FileName { get; set; }
|
||||||
|
public long FileLength { get; set; }
|
||||||
|
|
||||||
|
private List<byte[]> _data = new();
|
||||||
|
private long _sent = 0;
|
||||||
|
|
||||||
|
public void Upload()
|
||||||
|
{
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddData(byte[] data)
|
||||||
|
{
|
||||||
|
_data.Add(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool DownloadFinished()
|
||||||
|
{
|
||||||
|
return _sent >= FileLength;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -36,6 +36,8 @@ namespace CoopServer
|
|||||||
|
|
||||||
public static readonly List<Client> Clients = new();
|
public static readonly List<Client> Clients = new();
|
||||||
|
|
||||||
|
public static DownloadManager MainDownloadManager = new();
|
||||||
|
|
||||||
public Server()
|
public Server()
|
||||||
{
|
{
|
||||||
Logging.Info("================");
|
Logging.Info("================");
|
||||||
@ -204,6 +206,8 @@ namespace CoopServer
|
|||||||
Logging.Info("Listening for clients");
|
Logging.Info("Listening for clients");
|
||||||
Logging.Info("Please use CTRL + C if you want to stop the server!");
|
Logging.Info("Please use CTRL + C if you want to stop the server!");
|
||||||
|
|
||||||
|
MainDownloadManager.Create(0);
|
||||||
|
|
||||||
while (!Program.ReadyToStop)
|
while (!Program.ReadyToStop)
|
||||||
{
|
{
|
||||||
if (RunningResource != null)
|
if (RunningResource != null)
|
||||||
|
Reference in New Issue
Block a user