diff --git a/DTLib.csproj b/DTLib.csproj index edc6e01..52c0406 100644 --- a/DTLib.csproj +++ b/DTLib.csproj @@ -51,7 +51,6 @@ - diff --git a/DefaultLogger.cs b/DefaultLogger.cs index d0d0423..4d65fca 100644 --- a/DefaultLogger.cs +++ b/DefaultLogger.cs @@ -8,28 +8,29 @@ namespace DTLib public static class DefaultLogger { public static void SetLogfile(string dir, string programName) - => logfile = $"{dir}\\{programName}_{DateTime.Now}.log".Replace(':', '-').Replace(' ', '_'); + => Logfile = $"{dir}\\{programName}_{DateTime.Now}.log".Replace(':', '-').Replace(' ', '_'); - static string logfile; - static readonly SafeMutex LogMutex = new(); + static public string Logfile { get; set; } public static void Log(params string[] msg) { if (msg.Length == 1) msg[0] = "[" + DateTime.Now.ToString() + "]: " + msg[0]; else msg[1] = "[" + DateTime.Now.ToString() + "]: " + msg[1]; LogNoTime(msg); } - public static void LogNoTime(params string[] msg) => - LogMutex.Execute(() => + public static void LogNoTime(params string[] msg) + { + lock (Logfile) { ColoredConsole.Write(msg); - if (msg.Length == 1) File.AppendAllText(logfile, msg[0]); + if (msg.Length == 1) File.AppendAllText(Logfile, msg[0]); else { StringBuilder strB = new(); for (ushort i = 0; i < msg.Length; i++) strB.Append(msg[++i]); - File.AppendAllText(logfile, strB.ToString()); + File.AppendAllText(Logfile, strB.ToString()); } - }); + } + } } } diff --git a/Dtsod/DtsodV22.cs b/Dtsod/DtsodV22.cs index ca03755..051201b 100644 --- a/Dtsod/DtsodV22.cs +++ b/Dtsod/DtsodV22.cs @@ -68,7 +68,7 @@ namespace DTLib.Dtsod Add(pair.Key, pair.Value); } - public DtsodV22(Dictionary dict) + public DtsodV22(Dictionary dict) { foreach (KeyValuePair pair in dict) Add(pair.Key, pair.Value); diff --git a/EventHandlerAsync.cs b/EventHandlerAsync.cs index 13361dc..f874a22 100644 --- a/EventHandlerAsync.cs +++ b/EventHandlerAsync.cs @@ -3,4 +3,5 @@ namespace DTLib { public delegate Task EventHandlerAsync(object sender, TEventArgs e); + public delegate Task EventHandlerAsync(); } diff --git a/Network/FSP.cs b/Network/FSP.cs index 281623b..35d1d3a 100644 --- a/Network/FSP.cs +++ b/Network/FSP.cs @@ -19,17 +19,15 @@ namespace DTLib.Network public uint BytesUploaded = 0; public uint Filesize = 0; - public SafeMutex Mutex = new(); - // скачивает файл с помощью FSP протокола public void DownloadFile(string filePath_server, string filePath_client) { - Mutex.Execute(() => + lock (MainSocket) { Debug("b", $"requesting file download: {filePath_server}\n"); MainSocket.SendPackage("requesting file download".ToBytes()); MainSocket.SendPackage(filePath_server.ToBytes()); - }); + } DownloadFile(filePath_client); } @@ -38,17 +36,17 @@ namespace DTLib.Network using System.IO.Stream fileStream = File.OpenWrite(filePath_client); Download_SharedCode(fileStream, true); fileStream.Close(); - Debug(new string[] { "g", $" downloaded {BytesDownloaded} of {Filesize} bytes\n" }); + Debug("g", $" downloaded {BytesDownloaded} of {Filesize} bytes\n"); } public byte[] DownloadFileToMemory(string filePath_server) { - Mutex.Execute(() => + lock (MainSocket) { Debug("b", $"requesting file download: {filePath_server}\n"); MainSocket.SendPackage("requesting file download".ToBytes()); MainSocket.SendPackage(filePath_server.ToBytes()); - }); + } return DownloadFileToMemory(); } @@ -58,13 +56,13 @@ namespace DTLib.Network Download_SharedCode(fileStream, false); byte[] output = fileStream.GetBuffer(); fileStream.Close(); - Debug(new string[] { "g", $" downloaded {BytesDownloaded} of {Filesize} bytes\n" }); + Debug("g", $" downloaded {BytesDownloaded} of {Filesize} bytes\n"); return output; } void Download_SharedCode(System.IO.Stream fileStream, bool requiresFlushing) { - Mutex.Execute(() => + lock (MainSocket) { BytesDownloaded = 0; Filesize = MainSocket.GetPackage().BytesToString().ToUInt(); @@ -97,7 +95,7 @@ namespace DTLib.Network BytesDownloaded += (uint)buffer.Length; fileStream.Write(buffer, 0, buffer.Length); } - }); + } if (requiresFlushing) fileStream.Flush(); } @@ -109,7 +107,7 @@ namespace DTLib.Network Debug("b", $"uploading file {filePath}\n"); using System.IO.FileStream fileStream = File.OpenRead(filePath); Filesize = File.GetSize(filePath).ToUInt(); - Mutex.Execute(() => + lock (MainSocket) { MainSocket.SendPackage(Filesize.ToString().ToBytes()); MainSocket.GetAnswer("ready"); @@ -132,9 +130,9 @@ namespace DTLib.Network MainSocket.SendPackage(buffer); BytesUploaded += (uint)buffer.Length; } - }); + } fileStream.Close(); - Debug(new string[] { "g", $" uploaded {BytesUploaded} of {Filesize} bytes\n" }); + Debug("g", $" uploaded {BytesUploaded} of {Filesize} bytes\n"); } public void DownloadByManifest(string dirOnServer, string dirOnClient, bool overwrite = false, bool delete_excess = false) @@ -144,7 +142,7 @@ namespace DTLib.Network if (!dirOnServer.EndsWith("\\")) dirOnServer += "\\"; Debug("b", "downloading manifest <", "c", dirOnServer + "manifest.dtsod", "b", ">\n"); - DtsodV22 manifest = new Dtsod.DtsodV22(DownloadFileToMemory(dirOnServer + "manifest.dtsod").BytesToString()); + DtsodV22 manifest = new DtsodV22(DownloadFileToMemory(dirOnServer + "manifest.dtsod").BytesToString()); Debug("g", $"found {manifest.Values.Count} files in manifest\n"); Hasher hasher = new Hasher(); foreach (string fileOnServer in manifest.Keys) @@ -202,13 +200,11 @@ namespace DTLib.Network static void Debug(params string[] msg) { - if (debug) - Log(msg); + if (debug) Log(msg); } static void DebugNoTime(params string[] msg) { - if (debug) - LogNoTime(msg); + if (debug) LogNoTime(msg); } } } diff --git a/SafeMutex.cs b/SafeMutex.cs deleted file mode 100644 index d1d72ff..0000000 --- a/SafeMutex.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System; -using System.Threading; - -namespace DTLib -{ - public class SafeMutex - { - readonly Mutex Mutex = new(); - bool isReleased = false; - - // тут выполняется отправленный код - public void Execute(Action action, out Exception exception) - { - try - { - exception = null; - Execute(action); - } - catch (Exception ex) - { - exception = ex; - if (!isReleased) - Mutex.ReleaseMutex(); - } - } - - public void Execute(Action action) - { - Mutex.WaitOne(); - action(); - Mutex.ReleaseMutex(); - isReleased = true; - } - public T Execute(Func action) - { - Mutex.WaitOne(); - T rezult = action(); - Mutex.ReleaseMutex(); - isReleased = true; - return rezult; - } - } -}