From 65ccd5713629ce86304c64bd4e239f0513496917 Mon Sep 17 00:00:00 2001 From: Timerix Date: Mon, 13 Sep 2021 17:04:23 +0300 Subject: [PATCH] AAAAAAAA --- ConsoleGUI/Container.cs | 2 +- DTLib.csproj | 2 ++ Dtsod/DtsodV22.cs | 1 - Dtsod/DtsodV23.cs | 1 - Network/FSP.cs | 67 +++++++++++++++++++++++++++++++---------- Network/Package.cs | 6 ++++ TImer.cs | 3 +- 7 files changed, 62 insertions(+), 20 deletions(-) diff --git a/ConsoleGUI/Container.cs b/ConsoleGUI/Container.cs index 981eae3..5a0bcbc 100644 --- a/ConsoleGUI/Container.cs +++ b/ConsoleGUI/Container.cs @@ -22,7 +22,7 @@ namespace DTLib.ConsoleGUI void ParseLayoutFile(string layout_file) { - DtsodV22 layout = new(File.ReadAllText(layout_file)); + DtsodV23 layout = new(File.ReadAllText(layout_file)); AnchorPoint = (layout[Name]["anchor"][0], layout[Name]["anchor"][1]); Width = layout[Name]["width"]; Height = layout[Name]["height"]; diff --git a/DTLib.csproj b/DTLib.csproj index 1ff8a22..97598e4 100644 --- a/DTLib.csproj +++ b/DTLib.csproj @@ -40,6 +40,7 @@ + @@ -53,6 +54,7 @@ + diff --git a/Dtsod/DtsodV22.cs b/Dtsod/DtsodV22.cs index e84a9c2..9c3ce2a 100644 --- a/Dtsod/DtsodV22.cs +++ b/Dtsod/DtsodV22.cs @@ -3,7 +3,6 @@ using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; -using static System.Net.Mime.MediaTypeNames; namespace DTLib.Dtsod { diff --git a/Dtsod/DtsodV23.cs b/Dtsod/DtsodV23.cs index 584c939..b0dc470 100644 --- a/Dtsod/DtsodV23.cs +++ b/Dtsod/DtsodV23.cs @@ -3,7 +3,6 @@ using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; -using static System.Net.Mime.MediaTypeNames; namespace DTLib.Dtsod { diff --git a/Network/FSP.cs b/Network/FSP.cs index 0100036..61d597b 100644 --- a/Network/FSP.cs +++ b/Network/FSP.cs @@ -1,6 +1,5 @@ using DTLib.Dtsod; using DTLib.Filesystem; -using System; using System.Collections.Generic; using System.Net.Sockets; using System.Text; @@ -130,7 +129,7 @@ namespace DTLib.Network var fileHash = new Hasher().HashFile(filePath); mainSocket.SendPackage(Filesize.ToString().ToBytes()); mainSocket.SendPackage(fileHash); - if (mainSocket.GetPackage().ToStr() != "ready") throw new Exception("user socket isn't ready"); + mainSocket.GetAnswer("ready"); byte[] buffer = new byte[5120]; var hashstr = fileHash.HashToString(); int packagesCount = 0; @@ -163,6 +162,52 @@ namespace DTLib.Network Debug(new string[] { "g", $" uploaded {BytesUploaded} of {Filesize} bytes\n" }); } + /*public void DownloadByManifest(string manifestString, string dirOnClient, bool overwrite = false, bool delete_excess = false) + { + if (!dirOnClient.EndsWith("\\")) dirOnClient += "\\"; + var manifest = new DtsodV23(manifestString); + Debug("g", $"found {manifest.Values.Count} files in manifest\n"); + var hasher = new Hasher(); + foreach (string fileOnServer in manifest.Keys) + { + string fileOnClient = dirOnClient + fileOnServer; + Debug("b", "file <", "c", fileOnClient, "b", ">... "); + if (!File.Exists(fileOnClient)) + { + DebugNoTime("y", "doesn't exist\n"); + DownloadFile(fileOnServer, fileOnClient); + } + else if (overwrite && hasher.HashFile(fileOnClient).HashToString() != manifest[fileOnServer]) + { + DebugNoTime("y", "outdated\n"); + DownloadFile(fileOnServer, fileOnClient); + } + else DebugNoTime("g", "without changes\n"); + } + // удаление лишних файлов + if (delete_excess) + { + List dirs = new(); + foreach (string file in Directory.GetAllFiles(dirOnClient, ref dirs)) + { + if (!manifest.ContainsKey(file.Remove(0, dirOnClient.Length))) + { + Debug("y", $"deleting excess file: {file}\n"); + File.Delete(file); + } + } + // удаление пустых папок + /*foreach (string dir in dirs) + { + if (Directory.Exists(dir) && Directory.GetAllFiles(dir).Count == 0) + { + Debug("y", $"deleting empty dir: {dir}\n"); + Directory.Delete(dir); + } + }*/ + /*} + }*/ + public void DownloadByManifest(string dirOnServer, string dirOnClient, bool overwrite = false, bool delete_excess = false) { if (!dirOnClient.EndsWith("\\")) dirOnClient += "\\"; @@ -190,8 +235,7 @@ namespace DTLib.Network // удаление лишних файлов if (delete_excess) { - List dirs = new(); - foreach (string file in Directory.GetAllFiles(dirOnClient, ref dirs)) + foreach (string file in Directory.GetAllFiles(dirOnClient)) { if (!manifest.ContainsKey(file.Remove(0, dirOnClient.Length))) { @@ -199,15 +243,6 @@ namespace DTLib.Network File.Delete(file); } } - // удаление пустых папок - /*foreach (string dir in dirs) - { - if (Directory.Exists(dir) && Directory.GetAllFiles(dir).Count == 0) - { - Debug("y", $"deleting empty dir: {dir}\n"); - Directory.Delete(dir); - } - }*/ } } @@ -227,15 +262,15 @@ namespace DTLib.Network manifestBuilder.Append(hash.HashToString()); manifestBuilder.Append("\";\n"); } + Debug($"g", $" manifest of {dir} created\n"); File.WriteAllText(dir + "manifest.dtsod", manifestBuilder.ToString()); - Log($"g", $" manifest of {dir} created\n"); } - void Debug(params string[] msg) + static void Debug(params string[] msg) { if (debug) Log(msg); } - void DebugNoTime(params string[] msg) + static void DebugNoTime(params string[] msg) { if (debug) LogNoTime(msg); } diff --git a/Network/Package.cs b/Network/Package.cs index 18ff8a8..87aabac 100644 --- a/Network/Package.cs +++ b/Network/Package.cs @@ -53,5 +53,11 @@ namespace DTLib.Network var rec = socket.GetPackage().ToStr(); if (rec != answer) throw new Exception($"GetAnswer() error: invalid answer: <{rec}>"); } + + public static byte[] RequestPackage(this Socket socket, byte[] request) + { + socket.SendPackage(request); + return socket.GetPackage(); + } } } diff --git a/TImer.cs b/TImer.cs index cb154ec..90c4d42 100644 --- a/TImer.cs +++ b/TImer.cs @@ -23,9 +23,10 @@ namespace DTLib method(); } while (Repeat); }); - TimerThread.Start(); } + public void Start() => TimerThread.Start(); + // завершение потока public void Stop() {