IOPath in FSP

This commit is contained in:
Timerix22 2024-01-05 23:09:13 +06:00
parent 34ae7c2e2b
commit d54316ca61
3 changed files with 28 additions and 65 deletions

View File

@ -2,7 +2,7 @@
<PropertyGroup>
<!--package info-->
<PackageId>DTLib.Network</PackageId>
<Version>1.3.0</Version>
<Version>1.3.1</Version>
<Authors>Timerix</Authors>
<Description>Some sick network protocols</Description>
<RepositoryType>GIT</RepositoryType>

View File

@ -8,44 +8,40 @@ namespace DTLib.Network;
public class FSP
{
Socket MainSocket { get; init; }
public static bool debug = false;
public FSP(Socket _mainSocket) => MainSocket = _mainSocket;
public uint BytesDownloaded = 0;
public uint BytesUploaded = 0;
public uint Filesize = 0;
public uint BytesDownloaded;
public uint BytesUploaded;
public uint Filesize;
// скачивает файл с помощью FSP протокола
public void DownloadFile(string filePath_server, string filePath_client)
public void DownloadFile(IOPath filePath_server, IOPath filePath_client)
{
Path.ThrowIfEscapes(filePath_server);
Path.ThrowIfEscapes(filePath_client);
filePath_server.ThrowIfEscapes();
filePath_client.ThrowIfEscapes();
lock (MainSocket)
{
Debug("b", $"requesting file download: {filePath_server}");
MainSocket.SendPackage("requesting file download".ToBytes(StringConverter.UTF8));
MainSocket.SendPackage(filePath_server.ToBytes(StringConverter.UTF8));
MainSocket.SendPackage(filePath_server.Str.ToBytes(StringConverter.UTF8));
}
DownloadFile(filePath_client);
}
public void DownloadFile(string filePath_client)
public void DownloadFile(IOPath filePath_client)
{
Path.ThrowIfEscapes(filePath_client);
filePath_client.ThrowIfEscapes();
using System.IO.Stream fileStream = File.OpenWrite(filePath_client);
Download_SharedCode(fileStream, true);
fileStream.Close();
Debug("g", $" downloaded {BytesDownloaded} of {Filesize} bytes");
}
public byte[] DownloadFileToMemory(string filePath_server)
public byte[] DownloadFileToMemory(IOPath filePath_server)
{
Path.ThrowIfEscapes(filePath_server);
filePath_server.ThrowIfEscapes();
lock (MainSocket)
{
Debug("b", $"requesting file download: {filePath_server}");
MainSocket.SendPackage("requesting file download".ToBytes(StringConverter.UTF8));
MainSocket.SendPackage(filePath_server.ToBytes(StringConverter.UTF8));
MainSocket.SendPackage(filePath_server.Str.ToBytes(StringConverter.UTF8));
}
return DownloadFileToMemory();
}
@ -56,11 +52,10 @@ public class FSP
Download_SharedCode(fileStream, false);
byte[] output = fileStream.ToArray();
fileStream.Close();
Debug("g", $" downloaded {BytesDownloaded} of {Filesize} bytes");
return output;
}
void Download_SharedCode(System.IO.Stream fileStream, bool requiresFlushing)
private void Download_SharedCode(System.IO.Stream fileStream, bool requiresFlushing)
{
lock (MainSocket)
{
@ -100,11 +95,10 @@ public class FSP
}
// отдаёт файл с помощью FSP протокола
public void UploadFile(string filePath)
public void UploadFile(IOPath filePath)
{
Path.ThrowIfEscapes(filePath);
filePath.ThrowIfEscapes();
BytesUploaded = 0;
Debug("b", $"uploading file {filePath}");
using System.IO.FileStream fileStream = File.OpenRead(filePath);
Filesize = File.GetSize(filePath).ToUInt();
lock (MainSocket)
@ -132,79 +126,50 @@ public class FSP
}
}
fileStream.Close();
Debug("g", $" uploaded {BytesUploaded} of {Filesize} bytes");
}
public void DownloadByManifest(string dirOnServer, string dirOnClient, bool overwrite = false, bool delete_excess = false)
public void DownloadByManifest(IOPath dirOnServer, IOPath dirOnClient, bool overwrite = false, bool delete_excess = false)
{
if (!dirOnClient.EndsWith(Path.Sep))
dirOnClient += Path.Sep;
if (!dirOnServer.EndsWith(Path.Sep))
dirOnServer += Path.Sep;
Debug("b", "downloading manifest <", "c", dirOnServer + "manifest.dtsod", "b", ">");
var manifest = new DtsodV23(DownloadFileToMemory(dirOnServer + "manifest.dtsod").BytesToString(StringConverter.UTF8));
Debug("g", $"found {manifest.Values.Count} files in manifest");
var hasher = new Hasher();
foreach (string fileOnServer in manifest.Keys)
foreach (var fileOnServer in manifest.Keys)
{
string fileOnClient = dirOnClient + fileOnServer;
Debug("b", "file <", "c", fileOnClient, "b", ">... ");
if (!File.Exists(fileOnClient))
{
Debug("y", "doesn't exist");
DownloadFile(dirOnServer + fileOnServer, fileOnClient);
}
else if (overwrite && hasher.HashFile(fileOnClient).HashToString() != manifest[fileOnServer])
{
Debug("y", "outdated");
DownloadFile(dirOnServer + fileOnServer, fileOnClient);
}
else Debug("g", "without changes");
IOPath fileOnClient = Path.Concat(dirOnClient, fileOnServer);
if (!File.Exists(fileOnClient) || (overwrite && hasher.HashFile(fileOnClient).HashToString() != manifest[fileOnServer]))
DownloadFile(Path.Concat(dirOnServer, fileOnServer), fileOnClient);
}
// удаление лишних файлов
if (delete_excess)
{
foreach (string file in Directory.GetAllFiles(dirOnClient))
foreach (var file in Directory.GetAllFiles(dirOnClient))
{
if (!manifest.ContainsKey(file.Remove(0, dirOnClient.Length)))
{
Debug("y", $"deleting excess file: {file}");
if (!manifest.ContainsKey(file.Str.Remove(0, dirOnClient.Length)))
File.Delete(file);
}
}
}
}
public static void CreateManifest(string dir)
public static void CreateManifest(IOPath dir)
{
if(!Directory.Exists(dir))
{
Directory.Create(dir);
Log("y", $"can't create manifest, dir <{dir}> doesn't exist");
return;
}
if (!dir.EndsWith(Path.Sep))
dir += Path.Sep;
Log($"b", $"creating manifest of {dir}");
StringBuilder manifestBuilder = new();
Hasher hasher = new();
if (Directory.GetFiles(dir).Contains(dir + "manifest.dtsod"))
File.Delete(dir + "manifest.dtsod");
foreach (string _file in Directory.GetAllFiles(dir))
foreach (var _file in Directory.GetAllFiles(dir))
{
string file = _file.Remove(0, dir.Length);
var file = _file.Remove(0, dir.Length);
manifestBuilder.Append(file);
manifestBuilder.Append(": \"");
byte[] hash = hasher.HashFile(dir + file);
byte[] hash = hasher.HashFile(Path.Concat(dir, file));
manifestBuilder.Append(hash.HashToString());
manifestBuilder.Append("\";\n");
}
Debug($"g", $" manifest of {dir} created");
File.WriteAllText(dir + "manifest.dtsod", manifestBuilder.ToString());
}
static void Debug(params string[] msg)
{
if (debug) Log(msg);
}
}

View File

@ -6,8 +6,6 @@ global using System.Linq;
global using System.Text;
global using DTLib.Extensions;
global using DTLib.Filesystem;
global using static DTLib.Logging.InternalLog;
using System.Diagnostics;
using System.Net.Http;
namespace DTLib.Network;