first steps in writing DtsodV24 wrapper

This commit is contained in:
Timerix22 2022-03-28 00:34:16 +03:00
parent d3cc21e2ac
commit 18ae9a3ccc
11 changed files with 179 additions and 46 deletions

View File

@ -8,6 +8,7 @@
<ProduceReferenceAssembly>False</ProduceReferenceAssembly> <ProduceReferenceAssembly>False</ProduceReferenceAssembly>
<DebugType>portable</DebugType> <DebugType>portable</DebugType>
<Configurations>Debug;Release;Release-net48</Configurations> <Configurations>Debug;Release;Release-net48</Configurations>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
@ -26,4 +27,10 @@
<Compile Remove="V30\**" /> <Compile Remove="V30\**" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Update="Dependencies\kerep.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,77 @@
using System.Runtime.InteropServices;
using DtsodPtr=System.IntPtr;
namespace DTLib.Dtsod.V24;
public static unsafe class KerepFunctions
{
[DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void pinvoke_print(string msg);
[DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void test_marshalling(string text, out KVPair* k);
public static void TestMarshalling()
{
pinvoke_print("UwU");
string msg = "hello!";
test_marshalling(msg, out KVPair* kptr);
Log("kptr: " + kptr->ToString());
KVPair k = *kptr;
Log("y",
$"{{{Marshal.PtrToStringAnsi(k.key)}, {{{k.value.type.ToString()}, {Marshal.PtrToStringAnsi(k.value.VoidPtr)} }} }}");
}
//parses text to binary values
[DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void kerep_DtsodV24_deserialize(string text, out DtsodPtr output);
internal static DtsodPtr Deserialize(string text)
{
kerep_DtsodV24_deserialize(text, out var dtsod);
return dtsod;
}
//creates text representation of dtsod
[DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void kerep_DtsodV24_serialize(DtsodPtr dtsod, out string output);
internal static string Serialize(DtsodPtr dtsod)
{
kerep_DtsodV24_serialize(dtsod, out var text);
return text;
}
//returns value or UniNull if key not found
[DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void kerep_DtsodV24_get(DtsodPtr dtsod, string key, out Unitype output);
internal static Unitype Get(DtsodPtr dtsod, string key)
{
kerep_DtsodV24_get(dtsod, key, out var output);
return output;
}
//adds or sets value
[DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void kerep_DtsodV24_addOrSet(DtsodPtr dtsod, string key, Unitype value);
internal static void AddOrSet(DtsodPtr dtsod, string key, Unitype value)
{
kerep_DtsodV24_addOrSet(dtsod, key, value);
}
//checks for dtsod contains value or dont
[DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void kerep_DtsodV24_contains(DtsodPtr dtsod, string key, [MarshalAs(UnmanagedType.I1)] out bool output);
internal static bool Contains(DtsodPtr dtsod, string key)
{
kerep_DtsodV24_contains(dtsod, key, out var output);
return output;
}
//replaces value with UniNull if key exists in dtsod
[DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void kerep_DtsodV24_remove(DtsodPtr dtsod, string key, [MarshalAs(UnmanagedType.I1)] out bool output);
internal static bool Remove(DtsodPtr dtsod, string key)
{
kerep_DtsodV24_remove(dtsod, key, out var output);
return output;
}
}

View File

@ -0,0 +1,35 @@
using System.Runtime.InteropServices;
namespace DTLib.Dtsod.V24;
internal enum my_type : byte
{
Null, Float, Double, Char, Bool,
UInt8, Int8, UInt16, Int16, UInt32, Int32, UInt64, Int64,
UInt8Ptr, Int8Ptr, UInt16Ptr, Int16Ptr, UInt32Ptr, Int32Ptr, UInt64Ptr, Int64Ptr,
CharPtr, STNodePtr, HashtablePtr,
UniversalType,
AutoarrInt8Ptr, AutoarrUInt8Ptr, AutoarrInt16Ptr, AutoarrUInt16Ptr,
AutoarrInt32Ptr, AutoarrUInt32Ptr, AutoarrInt64Ptr, AutoarrUInt64Ptr,
AutoarrUnitypePtr, AutoarrKVPairPtr
}
[StructLayout(LayoutKind.Explicit)]
internal struct Unitype
{
[FieldOffset(0)] internal long Int64;
[FieldOffset(0)] internal ulong UInt64;
[FieldOffset(0)] internal double Double;
[FieldOffset(0)] internal byte Char;
[MarshalAs(UnmanagedType.I1)]
[FieldOffset(0)] internal bool Bool;
[FieldOffset(0)] internal IntPtr VoidPtr;
[FieldOffset(8)] internal my_type type;
}
[StructLayout(LayoutKind.Sequential)]
internal struct KVPair
{
internal IntPtr key;
internal Unitype value;
}

View File

@ -18,6 +18,8 @@ public class FSP
// скачивает файл с помощью FSP протокола // скачивает файл с помощью FSP протокола
public void DownloadFile(string filePath_server, string filePath_client) public void DownloadFile(string filePath_server, string filePath_client)
{ {
Путь.Предупредить(filePath_server);
Путь.Предупредить(filePath_client);
lock (MainSocket) lock (MainSocket)
{ {
Debug("b", $"requesting file download: {filePath_server}"); Debug("b", $"requesting file download: {filePath_server}");
@ -29,6 +31,7 @@ public class FSP
public void DownloadFile(string filePath_client) public void DownloadFile(string filePath_client)
{ {
Путь.Предупредить(filePath_client);
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();
@ -37,6 +40,7 @@ public class FSP
public byte[] DownloadFileToMemory(string filePath_server) public byte[] DownloadFileToMemory(string filePath_server)
{ {
Путь.Предупредить(filePath_server);
lock (MainSocket) lock (MainSocket)
{ {
Debug("b", $"requesting file download: {filePath_server}"); Debug("b", $"requesting file download: {filePath_server}");
@ -79,8 +83,7 @@ public class FSP
fileStream.Flush(); fileStream.Flush();
n = 0; n = 0;
} }
else else n++;
n++;
} }
} }
// получение остатка // получение остатка
@ -99,6 +102,7 @@ public class FSP
// отдаёт файл с помощью FSP протокола // отдаёт файл с помощью FSP протокола
public void UploadFile(string filePath) public void UploadFile(string filePath)
{ {
Путь.Предупредить(filePath);
BytesUploaded = 0; BytesUploaded = 0;
Debug("b", $"uploading file {filePath}"); Debug("b", $"uploading file {filePath}");
using System.IO.FileStream fileStream = File.OpenRead(filePath); using System.IO.FileStream fileStream = File.OpenRead(filePath);

View File

@ -15,7 +15,7 @@ public static class TestDtsodV23
public static void TestBaseTypes() public static void TestBaseTypes()
{ {
Info.Log("c", "-----[TestDtsodV23/TestBaseTypes]------"); Info.Log("c", "-----[TestDtsodV23/TestBaseTypes]------");
DtsodV23 dtsod = new(File.ReadAllText($"DtsodV2X{Path.Sep}base_types.dtsod")); DtsodV23 dtsod = new(File.ReadAllText($"DtsodV2X{Путь.Разд}base_types.dtsod"));
foreach (var pair in dtsod) foreach (var pair in dtsod)
Info.LogNoTime("b", pair.Value.GetType().Name + ' ', "w", pair.Key + ' ', "c", pair.Value.ToString()); Info.LogNoTime("b", pair.Value.GetType().Name + ' ', "w", pair.Key + ' ', "c", pair.Value.ToString());
Info.Log("g", "test completed"); Info.Log("g", "test completed");
@ -23,7 +23,7 @@ public static class TestDtsodV23
public static void TestLists() public static void TestLists()
{ {
Info.Log("c", "-------[TestDtsodV23/TestLists]--------"); Info.Log("c", "-------[TestDtsodV23/TestLists]--------");
DtsodV23 dtsod = new(File.ReadAllText($"DtsodV2X{Path.Sep}lists.dtsod")); DtsodV23 dtsod = new(File.ReadAllText($"DtsodV2X{Путь.Разд}lists.dtsod"));
foreach (var pair in dtsod) foreach (var pair in dtsod)
{ {
Info.LogNoTime("b", pair.Value.GetType().Name + ' ', "w", pair.Key, "c", Info.LogNoTime("b", pair.Value.GetType().Name + ' ', "w", pair.Key, "c",
@ -37,7 +37,7 @@ public static class TestDtsodV23
public static void TestComplexes() public static void TestComplexes()
{ {
Info.Log("c", "-----[TestDtsodV23/TestComplexes]------"); Info.Log("c", "-----[TestDtsodV23/TestComplexes]------");
DtsodV23 dtsod = new(File.ReadAllText($"DtsodV2X{Path.Sep}complexes.dtsod")); DtsodV23 dtsod = new(File.ReadAllText($"DtsodV2X{Путь.Разд}complexes.dtsod"));
foreach (var pair in dtsod) foreach (var pair in dtsod)
{ {
Info.LogNoTime("b", pair.Value.GetType().Name + ' ', "w", pair.Key, Info.LogNoTime("b", pair.Value.GetType().Name + ' ', "w", pair.Key,
@ -51,7 +51,7 @@ public static class TestDtsodV23
{ {
Info.Log("c", "---[TestDtsodV23/TestReSerialization]--"); Info.Log("c", "---[TestDtsodV23/TestReSerialization]--");
var dtsod = new DtsodV23(new DtsodV23(new DtsodV23( var dtsod = new DtsodV23(new DtsodV23(new DtsodV23(
new DtsodV23(File.ReadAllText($"DtsodV2X{Path.Sep}complexes.dtsod")).ToString()).ToString()).ToString()); new DtsodV23(File.ReadAllText($"DtsodV2X{Путь.Разд}complexes.dtsod")).ToString()).ToString()).ToString());
Info.Log("y", dtsod.ToString()); Info.Log("y", dtsod.ToString());
Info.Log("g", "test completed"); Info.Log("g", "test completed");
} }
@ -60,7 +60,7 @@ public static class TestDtsodV23
{ {
Info.Log("c", "--------[TestDtsodV23/TestSpeed]-------"); Info.Log("c", "--------[TestDtsodV23/TestSpeed]-------");
IDtsod dtsod=null; IDtsod dtsod=null;
string text = File.ReadAllText($"DtsodV2X{Path.Sep}messages.dtsod"); string text = File.ReadAllText($"DtsodV2X{Путь.Разд}messages.dtsod");
LogOperationTime("V21 deserialization",100,()=>dtsod=new DtsodV21(text)); LogOperationTime("V21 deserialization",100,()=>dtsod=new DtsodV21(text));
LogOperationTime("V21 serialization", 100, () => _=dtsod.ToString()); LogOperationTime("V21 serialization", 100, () => _=dtsod.ToString());
LogOperationTime("V23 deserialization", 100, () => dtsod = new DtsodV23(text)); LogOperationTime("V23 deserialization", 100, () => dtsod = new DtsodV23(text));
@ -71,7 +71,7 @@ public static class TestDtsodV23
public static void TestMemoryConsumption() public static void TestMemoryConsumption()
{ {
Info.Log("c", "-----[TestDtsodV23/TestMemConsumpt]----"); Info.Log("c", "-----[TestDtsodV23/TestMemConsumpt]----");
string text = File.ReadAllText($"DtsodV2X{Path.Sep}messages.dtsod"); string text = File.ReadAllText($"DtsodV2X{Путь.Разд}messages.dtsod");
var a = GC.GetTotalMemory(true); var a = GC.GetTotalMemory(true);
var dtsods = new DtsodV23[100]; var dtsods = new DtsodV23[100];
for (int i = 0; i < dtsods.Length; i++) for (int i = 0; i < dtsods.Length; i++)

View File

@ -21,7 +21,6 @@ public static class Program
public static readonly DefaultLogger Info = new(); public static readonly DefaultLogger Info = new();
public static void Main() public static void Main()
{ {
Info.Enable(); Info.Enable();
PublicLog.LogEvent += Info.Log; PublicLog.LogEvent += Info.Log;
PublicLog.LogNoTimeEvent += Info.LogNoTime; PublicLog.LogNoTimeEvent += Info.LogNoTime;
@ -32,7 +31,12 @@ public static class Program
{ {
Info.Log("c", "-------------[DTLib.Tests]-------------"); Info.Log("c", "-------------[DTLib.Tests]-------------");
//TestDtsodV23.TestAll(); //TestDtsodV23.TestAll();
DictTest.Test(); //DictTest.Test();
for (uint i = 0; i < 10; i++)
{
Dtsod.V24.KerepFunctions.TestMarshalling();
Info.Log($"{i}");
}
} }
catch (Exception ex) catch (Exception ex)
{ Info.Log("r", ex.ToString()); } { Info.Log("r", ex.ToString()); }

View File

@ -10,8 +10,8 @@ public static class Directory
if (!Directory.Exists(dir)) if (!Directory.Exists(dir))
{ {
// проверяет существование папки, в которой нужно создать dir // проверяет существование папки, в которой нужно создать dir
if (dir.Contains(Path.Sep) && !Directory.Exists(dir.Remove(dir.LastIndexOf(Path.Sep)))) if (dir.Contains(Путь.Разд) && !Directory.Exists(dir.Remove(dir.LastIndexOf(Путь.Разд))))
Create(dir.Remove(dir.LastIndexOf(Path.Sep))); Create(dir.Remove(dir.LastIndexOf(Путь.Разд)));
System.IO.Directory.CreateDirectory(dir); System.IO.Directory.CreateDirectory(dir);
} }
} }
@ -100,8 +100,8 @@ public static class Directory
public static void CreateSymlink(string sourceName, string symlinkName) public static void CreateSymlink(string sourceName, string symlinkName)
{ {
if (symlinkName.Contains(Path.Sep)) if (symlinkName.Contains(Путь.Разд))
Directory.Create(symlinkName.Remove(symlinkName.LastIndexOf(Path.Sep))); Directory.Create(symlinkName.Remove(symlinkName.LastIndexOf(Путь.Разд)));
if (!Symlink.CreateSymbolicLink(symlinkName, sourceName, Symlink.SymlinkTarget.Directory)) if (!Symlink.CreateSymbolicLink(symlinkName, sourceName, Symlink.SymlinkTarget.Directory))
throw new InvalidOperationException($"some error occured while creating symlink\nDirectory.CreateSymlink({symlinkName}, {sourceName})"); throw new InvalidOperationException($"some error occured while creating symlink\nDirectory.CreateSymlink({symlinkName}, {sourceName})");
} }
@ -110,8 +110,8 @@ public static class Directory
public static int SymCopy(string srcdir, string newdir) public static int SymCopy(string srcdir, string newdir)
{ {
List<string> files = Directory.GetAllFiles(srcdir); List<string> files = Directory.GetAllFiles(srcdir);
if (!srcdir.EndsWith(Path.Sep)) srcdir += Path.Sep; if (!srcdir.EndsWith(Путь.Разд)) srcdir += Путь.Разд;
if (!newdir.EndsWith(Path.Sep)) newdir += Path.Sep; if (!newdir.EndsWith(Путь.Разд)) newdir += Путь.Разд;
int i = 0; int i = 0;
for (; i < files.Count; i++) for (; i < files.Count; i++)
File.CreateSymlink(files[i], files[i].Replace(srcdir, newdir)); File.CreateSymlink(files[i], files[i].Replace(srcdir, newdir));

View File

@ -11,8 +11,8 @@ public static class File
{ {
if (!File.Exists(file)) if (!File.Exists(file))
{ {
if (file.Contains(Path.Sep)) if (file.Contains(Путь.Разд))
Directory.Create(file.Remove(file.LastIndexOf(Path.Sep))); Directory.Create(file.Remove(file.LastIndexOf(Путь.Разд)));
using System.IO.FileStream stream = System.IO.File.Create(file); using System.IO.FileStream stream = System.IO.File.Create(file);
stream.Close(); stream.Close();
} }
@ -75,8 +75,8 @@ public static class File
public static void CreateSymlink(string sourceName, string symlinkName) public static void CreateSymlink(string sourceName, string symlinkName)
{ {
if (symlinkName.Contains(Path.Sep)) if (symlinkName.Contains(Путь.Разд))
Directory.Create(symlinkName.Remove(symlinkName.LastIndexOf(Path.Sep))); Directory.Create(symlinkName.Remove(symlinkName.LastIndexOf(Путь.Разд)));
if (!Symlink.CreateSymbolicLink(symlinkName, sourceName, Symlink.SymlinkTarget.File)) if (!Symlink.CreateSymbolicLink(symlinkName, sourceName, Symlink.SymlinkTarget.File))
throw new InvalidOperationException($"some error occured while creating symlink\nFile.CreateSymlink({symlinkName}, {sourceName})"); throw new InvalidOperationException($"some error occured while creating symlink\nFile.CreateSymlink({symlinkName}, {sourceName})");
} }

View File

@ -1,25 +0,0 @@
namespace DTLib.Filesystem;
public static class Path
{
public static readonly char Sep = Environment.OSVersion.Platform == PlatformID.Win32NT ? '\\' : '/';
public static string CorrectSeparator(this string path)
{
if (Sep == '\\')
{
if (path.Contains('/'))
path = path.Replace('/', '\\');
}
else
{
if (path.Contains('\\'))
path = path.Replace('\\', '/');
}
return path;
}
// replaces wrong characters to use string as path
public static string NormalizeAsPath(this string str) =>
str.Replace(':', '-').Replace(' ', '_');
}

View File

@ -0,0 +1,31 @@
namespace DTLib.Filesystem;
public static class Путь
{
public static readonly char Разд = Environment.OSVersion.Platform == PlatformID.Win32NT ? '\\' : '/';
public static string ИсправитьРазд(this string путь)
{
if (Разд == '\\')
{
if (путь.Contains('/'))
путь = путь.Replace('/', '\\');
}
else
{
if (путь.Contains('\\'))
путь = путь.Replace('\\', '/');
}
return путь;
}
// replaces wrong characters to use string as путь
public static string НормализоватьДляПути(this string путь) =>
путь.Replace(':', '-').Replace(' ', '_');
public static void Предупредить(string может_быть_сочками)
{
if(может_быть_сочками.Contains(".."))
throw new Exception($"лее точки двойные убери: {может_быть_сочками}");
}
}

2
kerep

@ -1 +1 @@
Subproject commit 20ce758528fe57fdacac1960682ac8759c5a4c9c Subproject commit c1d004f411c5c2963bbbcb6172f08ad55188ed88