Append new functions

Seriazlization: append types DateOnly and TimeOnly
FSUtils: append RemoveFolderAsync method
pull/1/head
unknown 3 years ago
parent 2253dd7fdc
commit ec159be018

@ -3,6 +3,7 @@ using System.IO;
using System.IO.Compression; using System.IO.Compression;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks;
namespace ZeroLevel.Services.FileSystem namespace ZeroLevel.Services.FileSystem
{ {
@ -259,6 +260,34 @@ namespace ZeroLevel.Services.FileSystem
} while (deleted == false && fault_retrying_count < 5); } while (deleted == false && fault_retrying_count < 5);
} }
public static async Task RemoveFolderAsync(string path, int fault_retrying_count = 5, int fault_timeout_period = 1000)
{
bool deleted = false;
int try_counter = 0;
do
{
try
{
if (Directory.Exists(path) == false)
{
deleted = true;
break;
}
else
{
await Task.Factory.StartNew(p => Directory.Delete((string)p, true), path);
deleted = true;
}
}
catch (Exception ex)
{
Log.SystemError(ex, $"[FSUtils.RemoveFolderAsync] Fault remove folder {path}");
try_counter++;
await Task.Delay(fault_timeout_period);
}
} while (deleted == false && fault_retrying_count < 5);
}
public static void CleanAndTestFolder(string path) public static void CleanAndTestFolder(string path)
{ {
if (Directory.Exists(path)) if (Directory.Exists(path))

@ -38,6 +38,10 @@ namespace ZeroLevel.Services.Serialization
DateTime? ReadDateTime(); DateTime? ReadDateTime();
TimeOnly? ReadTime();
DateOnly? ReadDate();
decimal ReadDecimal(); decimal ReadDecimal();
TimeSpan ReadTimeSpan(); TimeSpan ReadTimeSpan();

@ -39,6 +39,10 @@ namespace ZeroLevel.Services.Serialization
void WriteDateTime(DateTime? datetime); void WriteDateTime(DateTime? datetime);
void WriteTime(TimeOnly? time);
void WriteDate(DateOnly? date);
void WriteDecimal(Decimal number); void WriteDecimal(Decimal number);
void WriteTimeSpan(TimeSpan period); void WriteTimeSpan(TimeSpan period);

@ -212,7 +212,21 @@ namespace ZeroLevel.Services.Serialization
long deserialized = BitConverter.ToInt64(buffer, 0); long deserialized = BitConverter.ToInt64(buffer, 0);
return DateTime.FromBinary(deserialized); return DateTime.FromBinary(deserialized);
} }
public TimeOnly? ReadTime()
{
var is_null = ReadByte();
if (is_null == 0) return null;
var ts = ReadTimeSpan();
return TimeOnly.FromTimeSpan(ts);
}
public DateOnly? ReadDate()
{
var is_null = ReadByte();
if (is_null == 0) return null;
var days = ReadInt32();
return DateOnly.FromDayNumber(days);
}
public IPAddress ReadIP() public IPAddress ReadIP()
{ {
var exists = ReadByte(); var exists = ReadByte();
@ -1119,6 +1133,7 @@ namespace ZeroLevel.Services.Serialization
_stream.Dispose(); _stream.Dispose();
} }
public Stream Stream => _stream; public Stream Stream => _stream;
} }
} }

@ -185,6 +185,34 @@ namespace ZeroLevel.Services.Serialization
} }
} }
public void WriteTime(TimeOnly? time)
{
if (time == null)
{
WriteByte(0);
}
else
{
WriteByte(1);
var ts = time.Value.ToTimeSpan();
WriteTimeSpan(ts);
}
}
public void WriteDate(DateOnly? date)
{
if (date == null)
{
WriteByte(0);
}
else
{
WriteByte(1);
var days = date.Value.DayNumber;
WriteInt32(days);
}
}
public void WriteIP(IPAddress ip) public void WriteIP(IPAddress ip)
{ {
if (ip == null) if (ip == null)
@ -802,6 +830,7 @@ namespace ZeroLevel.Services.Serialization
} }
} }
} }
#endregion Extension #endregion Extension
} }
} }
Loading…
Cancel
Save

Powered by TurnKey Linux.