removed SafeMutex, because i found lock expression
This commit is contained in:
parent
9762d7f528
commit
5566170210
@ -51,7 +51,6 @@
|
||||
<Compile Include="Network\OldNetwork.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Hasher.cs" />
|
||||
<Compile Include="SafeMutex.cs" />
|
||||
<Compile Include="SecureRandom.cs" />
|
||||
<Compile Include="FrameworkFix.cs" />
|
||||
<Compile Include="TImer.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());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -68,7 +68,7 @@ namespace DTLib.Dtsod
|
||||
Add(pair.Key, pair.Value);
|
||||
}
|
||||
|
||||
public DtsodV22(Dictionary<string, DtsodV22.ValueStruct> dict)
|
||||
public DtsodV22(Dictionary<string, ValueStruct> dict)
|
||||
{
|
||||
foreach (KeyValuePair<string, ValueStruct> pair in dict)
|
||||
Add(pair.Key, pair.Value);
|
||||
|
||||
@ -3,4 +3,5 @@
|
||||
namespace DTLib
|
||||
{
|
||||
public delegate Task EventHandlerAsync<TEventArgs>(object sender, TEventArgs e);
|
||||
public delegate Task EventHandlerAsync();
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
43
SafeMutex.cs
43
SafeMutex.cs
@ -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<T>(Func<T> action)
|
||||
{
|
||||
Mutex.WaitOne();
|
||||
T rezult = action();
|
||||
Mutex.ReleaseMutex();
|
||||
isReleased = true;
|
||||
return rezult;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user