2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-06-26 21:13:13 +00:00
This commit is contained in:
2019-08-07 05:18:27 +03:00
parent 2caae61910
commit 8d06fd05ad
74 changed files with 2302 additions and 1336 deletions

View File

@ -27,7 +27,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Esiur.Engine;
using Esiur.Core;
using System.Reflection;
namespace Esiur.Data

View File

@ -28,6 +28,7 @@ using System.Linq;
using System.Text;
using Esiur.Misc;
using System.Reflection;
using Esiur.Core;
namespace Esiur.Data
{
@ -36,7 +37,7 @@ namespace Esiur.Data
/// </summary>
public class BinaryList
{
private List<byte> held = new List<byte>();
private List<byte> list = new List<byte>();
/// <summary>
/// Create an instance of BinaryList
@ -46,18 +47,19 @@ namespace Esiur.Data
}
/*
/// <summary>
/// Converts parameters to binary in same order
/// </summary>
/// <param name="values">Variables to convert</param>
public static byte[] ToBytes(params object[] values)
{
var held = new List<byte>();
var list = new List<byte>();
foreach (var i in values)
{
if (i is byte)
held.Add((byte)i);
list.Add((byte)i);
else
{
#if NETSTANDARD1_5
@ -68,13 +70,14 @@ namespace Esiur.Data
if (mi != null)
{
var b = (byte[])mi.Invoke(null, new object[] { i });
held.AddRange(b);
list.AddRange(b);
}
}
}
return held.ToArray();
return list.ToArray();
}
/// <summary>
/// Create a new instance of BinaryList
@ -94,7 +97,7 @@ namespace Esiur.Data
foreach (var i in values)
{
if (i is byte)
held.Add((byte)i);
list.Add((byte)i);
else
{
#if NETSTANDARD1_5
@ -105,7 +108,7 @@ namespace Esiur.Data
if (mi != null)
{
var b = (byte[])mi.Invoke(null, new object[] {i});
held.AddRange(b);
list.AddRange(b);
}
}
}
@ -131,7 +134,7 @@ namespace Esiur.Data
{
if (i is byte)
{
held.Insert(offset++, (byte)i);
list.Insert(offset++, (byte)i);
}
else
{
@ -143,7 +146,7 @@ namespace Esiur.Data
if (mi != null)
{
var b = (byte[])mi.Invoke(null, new object[] { i });
held.InsertRange(offset, b);
list.InsertRange(offset, b);
offset += b.Length;
}
}
@ -157,64 +160,555 @@ namespace Esiur.Data
{
get
{
return held.Count;
return list.Count;
}
}
/*
public void Append(byte data)
{
held.Add(data);
list.Add(data);
}
public void Append(byte[] data)
{
held.AddRange(data);
list.AddRange(data);
}
public void Append(int data)
{
held.AddRange(DC.ToBytes(data));
list.AddRange(DC.ToBytes(data));
}
public void Append(uint data)
{
held.AddRange(DC.ToBytes(data));
list.AddRange(DC.ToBytes(data));
}
public void Append(float data)
{
held.AddRange(DC.ToBytes(data));
list.AddRange(DC.ToBytes(data));
}
public void Append(short data)
{
held.AddRange(DC.ToBytes(data));
list.AddRange(DC.ToBytes(data));
}
public void Append(ushort data)
{
held.AddRange(DC.ToBytes(data));
list.AddRange(DC.ToBytes(data));
}
public void Append(double data)
{
held.AddRange(DC.ToBytes(data));
list.AddRange(DC.ToBytes(data));
}
public void Append(sbyte data)
{
held.Add((byte)data);
list.Add((byte)data);
}
*/
public int Length => list.Count;
public BinaryList AddDateTime(DateTime value)
{
list.AddRange(DC.ToBytes(value));
return this;
}
public BinaryList InsertDateTime(int position, DateTime value)
{
list.InsertRange(position, DC.ToBytes(value));
return this;
}
public BinaryList AddDateTimeArray(DateTime[] value)
{
list.AddRange(DC.ToBytes(value));
return this;
}
public BinaryList InsertDateTimeArray(int position, DateTime[] value)
{
list.InsertRange(position, DC.ToBytes(value));
return this;
}
public BinaryList AddGuid(Guid value)
{
list.AddRange(DC.ToBytes(value));
return this;
}
public BinaryList InsertGuid(int position, Guid value)
{
list.InsertRange(position, DC.ToBytes(value));
return this;
}
public BinaryList AddGuidArray(Guid[] value)
{
list.AddRange(DC.ToBytes(value));
return this;
}
public BinaryList InsertGuidArray(int position, Guid[] value)
{
list.InsertRange(position, DC.ToBytes(value));
return this;
}
public BinaryList AddUInt8Array(byte[] value)
{
list.AddRange(value);
return this;
}
public BinaryList InsertUInt8Array(int position, byte[] value)
{
list.InsertRange(position, value);
return this;
}
public BinaryList AddHex(string value)
{
return this.AddUInt8Array(DC.FromHex(value, null));
}
public BinaryList InsertHex(int position, string value)
{
return this.InsertUInt8Array(position, DC.FromHex(value, null));
}
public BinaryList AddString(string value)
{
list.AddRange(DC.ToBytes(value));
return this;
}
public BinaryList InsertString(int position, string value)
{
list.InsertRange(position, DC.ToBytes(value));
return this;
}
public BinaryList AddStringArray(string[] value)
{
list.AddRange(DC.ToBytes(value));
return this;
}
public BinaryList InsertStringArray(int position, string[] value)
{
list.InsertRange(position, DC.ToBytes(value));
return this;
}
public BinaryList InsertUInt8(int position, byte value)
{
list.Insert(position, value);
return this;
}
public BinaryList AddUInt8(byte value)
{
list.Add(value);
return this;
}
public BinaryList AddInt8(sbyte value)
{
list.Add((byte)value);
return this;
}
public BinaryList InsertInt8(int position, sbyte value)
{
list.Insert(position, (byte)value);
return this;
}
public BinaryList AddInt8Array(sbyte[] value)
{
list.AddRange(DC.ToBytes(value));
return this;
}
public BinaryList InsertInt8Array(int position, sbyte[] value)
{
list.InsertRange(position, DC.ToBytes(value));
return this;
}
public BinaryList AddChar(char value)
{
list.AddRange(DC.ToBytes(value));
return this;
}
public BinaryList InsertChar(int position, char value)
{
list.InsertRange(position, DC.ToBytes(value));
return this;
}
public BinaryList AddCharArray(char[] value)
{
list.AddRange(DC.ToBytes(value));
return this;
}
public BinaryList InsertCharArray(int position, char[] value)
{
list.InsertRange(position, DC.ToBytes(value));
return this;
}
public BinaryList AddBoolean(bool value)
{
list.AddRange(DC.ToBytes(value));
return this;
}
public BinaryList InsertBoolean(int position, bool value)
{
list.InsertRange(position, DC.ToBytes(value));
return this;
}
public BinaryList AddBooleanArray(bool[] value)
{
list.AddRange(DC.ToBytes(value));
return this;
}
public BinaryList InsertBooleanArray(int position, bool[] value)
{
list.InsertRange(position, DC.ToBytes(value));
return this;
}
public BinaryList AddUInt16(ushort value)
{
list.AddRange(DC.ToBytes(value));
return this;
}
public BinaryList InsertUInt16(int position, ushort value)
{
list.InsertRange(position, DC.ToBytes(value));
return this;
}
public BinaryList AddUInt16Array(ushort[] value)
{
list.AddRange(DC.ToBytes(value));
return this;
}
public BinaryList InsertUInt16Array(int position, ushort[] value)
{
list.InsertRange(position, DC.ToBytes(value));
return this;
}
public BinaryList AddInt16(short value)
{
list.AddRange(DC.ToBytes(value));
return this;
}
public BinaryList InsertInt16(int position, short value)
{
list.InsertRange(position, DC.ToBytes(value));
return this;
}
public BinaryList AddInt16Array(short[] value)
{
list.AddRange(DC.ToBytes(value));
return this;
}
public BinaryList InsertInt16Array(int position, short[] value)
{
list.InsertRange(position, DC.ToBytes(value));
return this;
}
public BinaryList AddUInt32(uint value)
{
list.AddRange(DC.ToBytes(value));
return this;
}
public BinaryList InsertUInt32(int position, uint value)
{
list.InsertRange(position, DC.ToBytes(value));
return this;
}
public BinaryList AddUInt32Array(uint[] value)
{
list.AddRange(DC.ToBytes(value));
return this;
}
public BinaryList InsertUInt32Array(int position, uint[] value)
{
list.InsertRange(position, DC.ToBytes(value));
return this;
}
public BinaryList AddInt32(int value)
{
list.AddRange(DC.ToBytes(value));
return this;
}
public BinaryList InsertInt32(int position, int value)
{
list.InsertRange(position, DC.ToBytes(value));
return this;
}
public BinaryList AddInt32Array(int[] value)
{
list.AddRange(DC.ToBytes(value));
return this;
}
public BinaryList InsertInt32Array(int position, int[] value)
{
list.InsertRange(position, DC.ToBytes(value));
return this;
}
public BinaryList AddUInt64(ulong value)
{
list.AddRange(DC.ToBytes(value));
return this;
}
public BinaryList InsertUInt64(int position, ulong value)
{
list.InsertRange(position, DC.ToBytes(value));
return this;
}
public BinaryList AddUInt64Array(ulong[] value)
{
list.AddRange(DC.ToBytes(value));
return this;
}
public BinaryList InsertUInt64Array(int position, ulong[] value)
{
list.InsertRange(position, DC.ToBytes(value));
return this;
}
public BinaryList AddInt64(long value)
{
list.AddRange(DC.ToBytes(value));
return this;
}
public BinaryList InsertInt64(int position, long value)
{
list.InsertRange(position, DC.ToBytes(value));
return this;
}
public BinaryList AddInt64Array(long[] value)
{
list.AddRange(DC.ToBytes(value));
return this;
}
public BinaryList InsertInt64Array(int position, long[] value)
{
list.InsertRange(position, DC.ToBytes(value));
return this;
}
public BinaryList AddFloat32(float value)
{
list.AddRange(DC.ToBytes(value));
return this;
}
public BinaryList InsertFloat32(int position, float value)
{
list.InsertRange(position, DC.ToBytes(value));
return this;
}
public BinaryList AddFloat32Array(float[] value)
{
list.AddRange(DC.ToBytes(value));
return this;
}
public BinaryList InsertFloat32Array(int position, float[] value)
{
list.InsertRange(position, DC.ToBytes(value));
return this;
}
public BinaryList AddFloat64(double value)
{
list.AddRange(DC.ToBytes(value));
return this;
}
public BinaryList InsertFloat64(int position, double value)
{
list.InsertRange(position, DC.ToBytes(value));
return this;
}
public BinaryList AddFloat64Array(double[] value)
{
list.AddRange(DC.ToBytes(value));
return this;
}
public BinaryList InsertFloat64Array(int position, double[] value)
{
list.InsertRange(position, DC.ToBytes(value));
return this;
}
public BinaryList Add(DataType type, object value)
{
switch (type)
{
case DataType.Bool:
AddBoolean((bool)value);
return this;
case DataType.BoolArray:
AddBooleanArray((bool[])value);
return this;
case DataType.UInt8:
AddUInt8((byte)value);
return this;
case DataType.UInt8Array:
AddUInt8Array((byte[])value);
return this;
case DataType.Int8:
AddInt8((sbyte)value);
return this;
case DataType.Int8Array:
AddInt8Array((sbyte[])value);
return this;
case DataType.Char:
AddChar((char)value);
return this;
case DataType.CharArray:
AddCharArray((char[])value);
return this;
case DataType.UInt16:
AddUInt16((ushort)value);
return this;
case DataType.UInt16Array:
AddUInt16Array((ushort[])value);
return this;
case DataType.Int16:
AddInt16((short)value);
return this;
case DataType.Int16Array:
AddInt16Array((short[])value);
return this;
case DataType.UInt32:
AddUInt32((uint)value);
return this;
case DataType.UInt32Array:
AddUInt32Array((uint[])value);
return this;
case DataType.Int32:
AddInt32((int)value);
return this;
case DataType.Int32Array:
AddInt32Array((int[])value);
return this;
case DataType.UInt64:
AddUInt64((ulong)value);
return this;
case DataType.UInt64Array:
AddUInt64Array((ulong[])value);
return this;
case DataType.Int64:
AddInt64((long)value);
return this;
case DataType.Int64Array:
AddInt64Array((long[])value);
return this;
case DataType.Float32:
AddFloat32((float)value);
return this;
case DataType.Float32Array:
AddFloat32Array((float[])value);
return this;
case DataType.Float64:
AddFloat64((double)value);
return this;
case DataType.Float64Array:
AddFloat64Array((double[])value);
return this;
case DataType.String:
AddString((string)value);
return this;
case DataType.StringArray:
AddStringArray((string[])value);
return this;
case DataType.DateTime:
AddDateTime((DateTime)value);
return this;
case DataType.DateTimeArray:
AddDateTimeArray((DateTime[])value);
return this;
default:
throw new Exception("Not Implemented " + type.ToString());
//return this;
}
}
/// <summary>
/// Convert the list to an array of bytes
/// </summary>
/// <returns>Bytes array</returns>
public byte[] ToArray()
{
return held.ToArray();
return list.ToArray();
}
public virtual IAsyncReply<object[]> Done()
{
return null;
//
}
}
}

View File

@ -28,7 +28,7 @@ using System.Text;
using Esiur.Misc;
using System.ComponentModel;
using Esiur.Data;
using Esiur.Engine;
using Esiur.Core;
using Esiur.Net.IIP;
using Esiur.Resource;
using System.Linq;
@ -128,24 +128,24 @@ namespace Esiur.Data
var rt = new BinaryList();
var comparsion = StructureComparisonResult.Structure;
rt.Append((byte)comparsion);
rt.Append(ComposeStructure(structures[0], connection, true, true, true));
rt.AddUInt8((byte)comparsion)
.AddUInt8Array(ComposeStructure(structures[0], connection, true, true, true));
for (var i = 1; i < structures.Length; i++)
{
comparsion = Compare(structures[i - 1], structures[i], connection);
rt.Append((byte)comparsion);
rt.AddUInt8((byte)comparsion);
if (comparsion == StructureComparisonResult.Structure)
rt.Append(ComposeStructure(structures[i], connection, true, true, true));
rt.AddUInt8Array(ComposeStructure(structures[i], connection, true, true, true));
else if (comparsion == StructureComparisonResult.StructureSameKeys)
rt.Append(ComposeStructure(structures[i], connection, false, true, true));
rt.AddUInt8Array(ComposeStructure(structures[i], connection, false, true, true));
else if (comparsion == StructureComparisonResult.StructureSameTypes)
rt.Append(ComposeStructure(structures[i], connection, false, false, true));
rt.AddUInt8Array(ComposeStructure(structures[i], connection, false, false, true));
}
if (prependLength)
rt.Insert(0, rt.Length);
rt.InsertInt32(0, rt.Length);
return rt.ToArray();
}
@ -244,17 +244,19 @@ namespace Esiur.Data
foreach (var i in value)
{
var key = DC.ToBytes(i.Key);
rt.Append((byte)key.Length, key, Compose(i.Value, connection));
rt.AddUInt8((byte)key.Length)
.AddUInt8Array(key)
.AddUInt8Array(Compose(i.Value, connection));
}
}
else
{
foreach (var i in value)
rt.Append(Compose(i.Value, connection, includeTypes));
rt.AddUInt8Array(Compose(i.Value, connection, includeTypes));
}
if (prependLength)
rt.Insert(0, rt.Length);
rt.InsertInt32(0, rt.Length);
return rt.ToArray();
}
@ -587,7 +589,7 @@ namespace Esiur.Data
/// <param name="resource">Resource to check.</param>
/// <param name="connection">DistributedConnection to check if the resource is local to it.</param>
/// <returns>True, if the resource owner is the given connection, otherwise False.</returns>
static bool IsLocalResource(IResource resource, DistributedConnection connection)
public static bool IsLocalResource(IResource resource, DistributedConnection connection)
{
if (resource is DistributedResource)
if ((resource as DistributedResource).Connection == connection)
@ -628,7 +630,8 @@ namespace Esiur.Data
return DC.ToBytes((resource as DistributedResource).Id);
else
{
return BinaryList.ToBytes(resource.Instance.Template.ClassId, resource.Instance.Id);
return new BinaryList().AddGuid(resource.Instance.Template.ClassId).AddUInt32(resource.Instance.Id).ToArray();
//return BinaryList.ToBytes(resource.Instance.Template.ClassId, resource.Instance.Id);
}
}
@ -648,25 +651,25 @@ namespace Esiur.Data
var rt = new BinaryList();
var comparsion = Compare(null, resources[0], connection);
rt.Append((byte)comparsion);
rt.AddUInt8((byte)comparsion);
if (comparsion == ResourceComparisonResult.Local)
rt.Append((resources[0] as DistributedResource).Id);
rt.AddUInt32((resources[0] as DistributedResource).Id);
else if (comparsion == ResourceComparisonResult.Distributed)
rt.Append(resources[0].Instance.Id);
rt.AddUInt32(resources[0].Instance.Id);
for (var i = 1; i < resources.Length; i++)
{
comparsion = Compare(resources[i - 1], resources[i], connection);
rt.Append((byte)comparsion);
rt.AddUInt8((byte)comparsion);
if (comparsion == ResourceComparisonResult.Local)
rt.Append((resources[i] as DistributedResource).Id);
rt.AddUInt32((resources[i] as DistributedResource).Id);
else if (comparsion == ResourceComparisonResult.Distributed)
rt.Append(resources[i].Instance.Id);
rt.AddUInt32(resources[i].Instance.Id);
}
if (prependLength)
rt.Insert(0, rt.Length);
rt.InsertInt32(0, rt.Length);
return rt.ToArray();
}
@ -831,9 +834,16 @@ namespace Esiur.Data
/// <returns>Array of bytes in the network byte order.</returns>
public static byte[] ComposePropertyValue(PropertyValue propertyValue, DistributedConnection connection)//, bool includeAge = true)
{
return new BinaryList()
.AddUInt64(propertyValue.Age)
.AddDateTime(propertyValue.Date)
.AddUInt8Array(Compose(propertyValue.Value, connection))
.ToArray();
// age, date, value
//if (includeAge)
return BinaryList.ToBytes(propertyValue.Age, propertyValue.Date, Compose(propertyValue.Value, connection));
// return BinaryList.ToBytes(propertyValue.Age, propertyValue.Date, Compose(propertyValue.Value, connection));
//else
// return BinaryList.ToBytes(propertyValue.Date, Compose(propertyValue.Value, connection));
@ -905,7 +915,7 @@ namespace Esiur.Data
while (offset < ends)
{
var index = data[offset++];
var pt = resource.Instance.Template.GetPropertyTemplate(index);
var pt = resource.Instance.Template.GetPropertyTemplateByIndex(index);
list.Add(pt, null);
var cs = DC.GetUInt32(data, offset);
offset += 4;
@ -933,16 +943,20 @@ namespace Esiur.Data
/// <param name="history">History</param>
/// <param name="connection">DistributedConnection is required to fetch resources.</param>
/// <returns></returns>
public static byte[] ComposeHistory(KeyList<PropertyTemplate, PropertyValue[]> history, DistributedConnection connection, bool prependLength = false)
public static byte[] ComposeHistory(KeyList<PropertyTemplate, PropertyValue[]> history,
DistributedConnection connection, bool prependLength = false)
{
var rt = new BinaryList();
for (var i = 0; i < history.Count; i++)
rt.Append((byte)history.Keys.ElementAt(i).Index,
ComposePropertyValueArray(history.Values.ElementAt(i), connection, true));
rt.AddUInt8(history.Keys.ElementAt(i).Index)
.AddUInt8Array(ComposePropertyValueArray(history.Values.ElementAt(i), connection, true));
// rt.Append((byte)history.Keys.ElementAt(i).Index,
// ComposePropertyValueArray(history.Values.ElementAt(i), connection, true));
if (prependLength)
rt.Insert(0, (uint)rt.Length);
rt.InsertInt32(0, rt.Length);
return rt.ToArray();
}
@ -1005,47 +1019,47 @@ namespace Esiur.Data
case DataType.String:
var st = DC.ToBytes((string)value);
rt.Append(st.Length, st);
rt.AddInt32(st.Length).AddUInt8Array(st);
break;
case DataType.Resource:
rt.Append((value as DistributedResource).Id);
rt.AddUInt32((value as DistributedResource).Id);
break;
case DataType.DistributedResource:
//rt.Append((value as IResource).Instance.Template.ClassId, (value as IResource).Instance.Id);
rt.Append((value as IResource).Instance.Id);
rt.AddUInt32((value as IResource).Instance.Id);
break;
case DataType.Structure:
rt.Append(ComposeStructure((Structure)value, connection, true, true, true));
rt.AddUInt8Array(ComposeStructure((Structure)value, connection, true, true, true));
break;
case DataType.VarArray:
rt.Append(ComposeVarArray((object[])value, connection, true));
rt.AddUInt8Array(ComposeVarArray((object[])value, connection, true));
break;
case DataType.ResourceArray:
if (value is IResource[])
rt.Append(ComposeResourceArray((IResource[])value, connection, true));
rt.AddUInt8Array(ComposeResourceArray((IResource[])value, connection, true));
else
rt.Append(ComposeResourceArray((IResource[])DC.CastConvert(value, typeof(IResource[])), connection, true));
rt.AddUInt8Array(ComposeResourceArray((IResource[])DC.CastConvert(value, typeof(IResource[])), connection, true));
break;
case DataType.StructureArray:
rt.Append(ComposeStructureArray((Structure[])value, connection, true));
rt.AddUInt8Array(ComposeStructureArray((Structure[])value, connection, true));
break;
default:
rt.Append(value);
rt.Add(type, value);
if (type.IsArray())
rt.Insert(0, rt.Length);
rt.InsertInt32(0, rt.Length);
break;
}
if (prependType)
rt.Insert(0, (byte)type);
rt.InsertUInt8(0, (byte)type);
return rt.ToArray();
}
@ -1058,23 +1072,56 @@ namespace Esiur.Data
/// <returns>True, if <paramref name="type"/> implements <paramref name="iface"/>.</returns>
public static bool ImplementsInterface(Type type, Type iface)
{
while (type != null)
/*
if (iface.GetTypeInfo().IsGenericType)
{
if (type == iface)
return true;
//var x = (type.GetTypeInfo().GetInterfaces().Any(x => x.GetTypeInfo().IsGenericType Contains(iface))
iface = iface.GetTypeInfo().GetGenericTypeDefinition();
//if (type.GetTypeInfo().IsGenericType)
// type =
while (type != null)
{
if (type == iface)
return true;
#if NETSTANDARD1_5
if (type.GetTypeInfo().GetInterfaces().Contains(iface))
return true;
type = type.GetTypeInfo().BaseType;
if (type.GetTypeInfo().GetInterfaces().Contains(iface))// (x=>x.GetTypeInfo().IsGenericType (iface))
return true;
type = type.GetTypeInfo().BaseType;
#else
if (type.GetInterfaces().Contains(iface))
return true;
type = type.BaseType;
#endif
}
}
else
*/
//{
while (type != null)
{
if (type == iface)
return true;
#if NETSTANDARD1_5
if (type.GetTypeInfo().GetInterfaces().Contains(iface))
return true;
type = type.GetTypeInfo().BaseType;
#else
if (type.GetInterfaces().Contains(iface))
return true;
type = type.BaseType;
#endif
}
//}
return false;
}

File diff suppressed because it is too large Load Diff

View File

@ -31,7 +31,7 @@ using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Esiur.Engine;
using Esiur.Core;
namespace Esiur.Data
{

View File

@ -30,7 +30,7 @@ using System.Text;
using System.Threading.Tasks;
using Esiur.Data;
using Esiur.Misc;
using Esiur.Engine;
using Esiur.Core;
namespace Esiur.Data
{