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> <PropertyGroup>
<!--package info--> <!--package info-->
<PackageId>DTLib.Network</PackageId> <PackageId>DTLib.Network</PackageId>
<Version>1.3.0</Version> <Version>1.3.1</Version>
<Authors>Timerix</Authors> <Authors>Timerix</Authors>
<Description>Some sick network protocols</Description> <Description>Some sick network protocols</Description>
<RepositoryType>GIT</RepositoryType> <RepositoryType>GIT</RepositoryType>

View File

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