mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2025-09-13 12:43:17 +00:00
codec
This commit is contained in:
@@ -180,13 +180,13 @@ public static class Codec
|
||||
/// <param name="connection">DistributedConnection is required in case a structure in the array holds items at the other end.</param>
|
||||
/// <param name="dataType">DataType, in case the data is not prepended with DataType</param>
|
||||
/// <returns>Value</returns>
|
||||
public static (uint, object) ParseAsync(byte[] data, uint offset, DistributedConnection connection, uint[] requestSequence, TransmissionType? dataType = null)
|
||||
public static (uint, object) ParseAsync(byte[] data, uint offset, DistributedConnection connection, uint[] requestSequence, TransmissionDataUnit? dataType = null)
|
||||
{
|
||||
uint len = 0;
|
||||
|
||||
if (dataType == null)
|
||||
{
|
||||
(var longLen, dataType) = TransmissionType.Parse(data, offset, (uint)data.Length);
|
||||
(var longLen, dataType) = TransmissionDataUnit.Parse(data, offset, (uint)data.Length);
|
||||
|
||||
if (dataType == null)
|
||||
throw new NullReferenceException("DataType can't be parsed.");
|
||||
@@ -201,27 +201,27 @@ public static class Codec
|
||||
|
||||
//Console.WriteLine("Parsing " + tt.Class + " " + tt.Identifier);
|
||||
|
||||
if (tt.Class == TransmissionTypeClass.Fixed)
|
||||
if (tt.Class == TransmissionDataUnitClass.Fixed)
|
||||
{
|
||||
return (len, FixedAsyncParsers[tt.Exponent][tt.Index](data, dataType.Value.Offset, (uint)tt.ContentLength, connection, requestSequence));
|
||||
}
|
||||
else if (tt.Class == TransmissionTypeClass.Dynamic)
|
||||
else if (tt.Class == TransmissionDataUnitClass.Dynamic)
|
||||
{
|
||||
return (len, DynamicAsyncParsers[tt.Index](data, dataType.Value.Offset, (uint)tt.ContentLength, connection, requestSequence));
|
||||
}
|
||||
else //if (tt.Class == TransmissionTypeClass.Typed)
|
||||
else //if (tt.Class == TransmissionDataUnitClass.Typed)
|
||||
{
|
||||
return (len, TypedAsyncParsers[tt.Index](data, dataType.Value.Offset, (uint)tt.ContentLength, connection, requestSequence));
|
||||
}
|
||||
}
|
||||
|
||||
public static (uint, object) ParseSync(byte[] data, uint offset, Warehouse warehouse, TransmissionType? dataType = null)
|
||||
public static (uint, object) ParseSync(byte[] data, uint offset, Warehouse warehouse, TransmissionDataUnit? dataType = null)
|
||||
{
|
||||
uint len = 0;
|
||||
|
||||
if (dataType == null)
|
||||
{
|
||||
(var longLen, dataType) = TransmissionType.Parse(data, offset, (uint)data.Length);
|
||||
(var longLen, dataType) = TransmissionDataUnit.Parse(data, offset, (uint)data.Length);
|
||||
|
||||
if (dataType == null)
|
||||
throw new NullReferenceException("DataType can't be parsed.");
|
||||
@@ -234,15 +234,15 @@ public static class Codec
|
||||
|
||||
var tt = dataType.Value;
|
||||
|
||||
if (tt.Class == TransmissionTypeClass.Fixed)
|
||||
if (tt.Class == TransmissionDataUnitClass.Fixed)
|
||||
{
|
||||
return (len, FixedParsers[tt.Exponent][tt.Index](data, dataType.Value.Offset, (uint)tt.ContentLength, warehouse));
|
||||
}
|
||||
else if (tt.Class == TransmissionTypeClass.Dynamic)
|
||||
else if (tt.Class == TransmissionDataUnitClass.Dynamic)
|
||||
{
|
||||
return (len, DynamicParsers[tt.Index](data, dataType.Value.Offset, (uint)tt.ContentLength, warehouse));
|
||||
}
|
||||
else //if (tt.Class == TransmissionTypeClass.Typed)
|
||||
else //if (tt.Class == TransmissionDataUnitClass.Typed)
|
||||
{
|
||||
return (len, TypedParsers[tt.Index](data, dataType.Value.Offset, (uint)tt.ContentLength, warehouse));
|
||||
}
|
||||
@@ -265,7 +265,7 @@ public static class Codec
|
||||
return false;
|
||||
}
|
||||
|
||||
public delegate (TransmissionTypeIdentifier, byte[]) Composer(object value, Warehouse warehouse, DistributedConnection connection);
|
||||
public delegate (TransmissionDataUnitIdentifier, byte[]) Composer(object value, Warehouse warehouse, DistributedConnection connection);
|
||||
|
||||
public static Dictionary<Type, Composer> Composers = new Dictionary<Type, Composer>()
|
||||
{
|
||||
@@ -328,21 +328,21 @@ public static class Codec
|
||||
[typeof(PropertyValue[])] = DataSerializer.PropertyValueArrayComposer
|
||||
// Typed
|
||||
// [typeof(bool[])] = (value, con) => DataSerializer.TypedListComposer((IEnumerable)value, typeof(bool), con),
|
||||
// [typeof(bool?[])] = (value, con) => (TransmissionTypeIdentifier.TypedList, new byte[] { (byte)value }),
|
||||
// [typeof(List<bool>)] = (value, con) => (TransmissionTypeIdentifier.TypedList, new byte[] { (byte)value }),
|
||||
// [typeof(List<bool?>)] = (value, con) => (TransmissionTypeIdentifier.TypedList, new byte[] { (byte)value }),
|
||||
// [typeof(bool?[])] = (value, con) => (TransmissionDataUnitIdentifier.TypedList, new byte[] { (byte)value }),
|
||||
// [typeof(List<bool>)] = (value, con) => (TransmissionDataUnitIdentifier.TypedList, new byte[] { (byte)value }),
|
||||
// [typeof(List<bool?>)] = (value, con) => (TransmissionDataUnitIdentifier.TypedList, new byte[] { (byte)value }),
|
||||
|
||||
// [typeof(byte?[])] = (value, con) => (TransmissionTypeIdentifier.TypedList, new byte[] { (byte)value }),
|
||||
// [typeof(List<bool?>)] = (value, con) => (TransmissionTypeIdentifier.TypedList, new byte[] { (byte)value }),
|
||||
// [typeof(byte?[])] = (value, con) => (TransmissionDataUnitIdentifier.TypedList, new byte[] { (byte)value }),
|
||||
// [typeof(List<bool?>)] = (value, con) => (TransmissionDataUnitIdentifier.TypedList, new byte[] { (byte)value }),
|
||||
|
||||
};
|
||||
|
||||
|
||||
internal static (TransmissionTypeIdentifier identifier, byte[])
|
||||
internal static (TransmissionDataUnitIdentifier identifier, byte[])
|
||||
ComposeInternal(object valueOrSource, Warehouse warehouse, DistributedConnection connection)
|
||||
{
|
||||
if (valueOrSource == null)
|
||||
return (TransmissionTypeIdentifier.Null, null);
|
||||
return (TransmissionDataUnitIdentifier.Null, null);
|
||||
|
||||
var type = valueOrSource.GetType();
|
||||
|
||||
@@ -369,7 +369,7 @@ public static class Codec
|
||||
valueOrSource = ((IUserType)valueOrSource).Get();
|
||||
|
||||
if (valueOrSource == null)
|
||||
return (TransmissionTypeIdentifier.Null, null);
|
||||
return (TransmissionDataUnitIdentifier.Null, null);
|
||||
|
||||
|
||||
type = valueOrSource.GetType();
|
||||
@@ -454,7 +454,7 @@ public static class Codec
|
||||
|
||||
}
|
||||
|
||||
return (TransmissionTypeIdentifier.Null, null);
|
||||
return (TransmissionDataUnitIdentifier.Null, null);
|
||||
|
||||
}
|
||||
|
||||
@@ -469,7 +469,7 @@ public static class Codec
|
||||
public static byte[] Compose(object valueOrSource, Warehouse warehouse, DistributedConnection connection)//, bool prependType = true)
|
||||
{
|
||||
var (hdr, data) = ComposeInternal(valueOrSource, warehouse, connection);
|
||||
return TransmissionType.Compose(hdr, data);
|
||||
return TransmissionDataUnit.Compose(hdr, data);
|
||||
}
|
||||
|
||||
public static bool IsAnonymous(Type type)
|
||||
|
@@ -677,16 +677,16 @@ public static class DataDeserializer
|
||||
{
|
||||
var rt = new List<object>();
|
||||
|
||||
//TransmissionTypeIdentifier? previous = null;
|
||||
//TransmissionDataUnitIdentifier? previous = null;
|
||||
//byte[]? previousUUID = null;
|
||||
|
||||
TransmissionType? previous = null;
|
||||
TransmissionDataUnit? previous = null;
|
||||
|
||||
while (length > 0)
|
||||
{
|
||||
var (longLen, dataType) = TransmissionType.Parse(data, offset, (uint)data.Length);
|
||||
var (longLen, dataType) = TransmissionDataUnit.Parse(data, offset, (uint)data.Length);
|
||||
|
||||
if (dataType.Value.Identifier == TransmissionTypeIdentifier.Same)
|
||||
if (dataType.Value.Identifier == TransmissionDataUnitIdentifier.Same)
|
||||
{
|
||||
// Add UUID
|
||||
}
|
||||
|
@@ -14,108 +14,108 @@ public static class DataSerializer
|
||||
{
|
||||
public delegate byte[] Serializer(object value);
|
||||
|
||||
public static unsafe (TransmissionTypeIdentifier, byte[]) Int32Composer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
public static unsafe (TransmissionDataUnitIdentifier, byte[]) Int32Composer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
{
|
||||
var v = (int)value;
|
||||
var rt = new byte[4];
|
||||
fixed (byte* ptr = rt)
|
||||
*((int*)ptr) = v;
|
||||
return (TransmissionTypeIdentifier.Int32, rt);
|
||||
return (TransmissionDataUnitIdentifier.Int32, rt);
|
||||
}
|
||||
|
||||
public static unsafe (TransmissionTypeIdentifier, byte[]) UInt32Composer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
public static unsafe (TransmissionDataUnitIdentifier, byte[]) UInt32Composer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
{
|
||||
var v = (uint)value;
|
||||
var rt = new byte[4];
|
||||
fixed (byte* ptr = rt)
|
||||
*((uint*)ptr) = v;
|
||||
return (TransmissionTypeIdentifier.UInt32, rt);
|
||||
return (TransmissionDataUnitIdentifier.UInt32, rt);
|
||||
}
|
||||
|
||||
public static unsafe (TransmissionTypeIdentifier, byte[]) Int16Composer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
public static unsafe (TransmissionDataUnitIdentifier, byte[]) Int16Composer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
{
|
||||
var v = (short)value;
|
||||
var rt = new byte[2];
|
||||
fixed (byte* ptr = rt)
|
||||
*((short*)ptr) = v;
|
||||
return (TransmissionTypeIdentifier.Int16, rt);
|
||||
return (TransmissionDataUnitIdentifier.Int16, rt);
|
||||
}
|
||||
|
||||
public static unsafe (TransmissionTypeIdentifier, byte[]) UInt16Composer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
public static unsafe (TransmissionDataUnitIdentifier, byte[]) UInt16Composer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
{
|
||||
var v = (ushort)value;
|
||||
var rt = new byte[2];
|
||||
fixed (byte* ptr = rt)
|
||||
*((ushort*)ptr) = v;
|
||||
return (TransmissionTypeIdentifier.UInt16, rt);
|
||||
return (TransmissionDataUnitIdentifier.UInt16, rt);
|
||||
}
|
||||
|
||||
public static unsafe (TransmissionTypeIdentifier, byte[]) Float32Composer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
public static unsafe (TransmissionDataUnitIdentifier, byte[]) Float32Composer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
{
|
||||
var v = (float)value;
|
||||
var rt = new byte[4];
|
||||
fixed (byte* ptr = rt)
|
||||
*((float*)ptr) = v;
|
||||
return (TransmissionTypeIdentifier.Float32, rt);
|
||||
return (TransmissionDataUnitIdentifier.Float32, rt);
|
||||
}
|
||||
|
||||
public static unsafe (TransmissionTypeIdentifier, byte[]) Float64Composer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
public static unsafe (TransmissionDataUnitIdentifier, byte[]) Float64Composer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
{
|
||||
var v = (double)value;
|
||||
var rt = new byte[8];
|
||||
fixed (byte* ptr = rt)
|
||||
*((double*)ptr) = v;
|
||||
return (TransmissionTypeIdentifier.Float64, rt);
|
||||
return (TransmissionDataUnitIdentifier.Float64, rt);
|
||||
}
|
||||
|
||||
public static unsafe (TransmissionTypeIdentifier, byte[]) Int64Composer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
public static unsafe (TransmissionDataUnitIdentifier, byte[]) Int64Composer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
{
|
||||
var v = (long)value;
|
||||
var rt = new byte[8];
|
||||
fixed (byte* ptr = rt)
|
||||
*((long*)ptr) = v;
|
||||
return (TransmissionTypeIdentifier.Int64, rt);
|
||||
return (TransmissionDataUnitIdentifier.Int64, rt);
|
||||
}
|
||||
|
||||
public static unsafe (TransmissionTypeIdentifier, byte[]) UIn64Composer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
public static unsafe (TransmissionDataUnitIdentifier, byte[]) UIn64Composer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
{
|
||||
var v = (ulong)value;
|
||||
var rt = new byte[8];
|
||||
fixed (byte* ptr = rt)
|
||||
*((ulong*)ptr) = v;
|
||||
return (TransmissionTypeIdentifier.UInt64, rt);
|
||||
return (TransmissionDataUnitIdentifier.UInt64, rt);
|
||||
}
|
||||
|
||||
|
||||
public static unsafe (TransmissionTypeIdentifier, byte[]) DateTimeComposer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
public static unsafe (TransmissionDataUnitIdentifier, byte[]) DateTimeComposer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
{
|
||||
var v = ((DateTime)value).ToUniversalTime().Ticks;
|
||||
var rt = new byte[8];
|
||||
fixed (byte* ptr = rt)
|
||||
*((long*)ptr) = v;
|
||||
return (TransmissionTypeIdentifier.DateTime, rt);
|
||||
return (TransmissionDataUnitIdentifier.DateTime, rt);
|
||||
}
|
||||
|
||||
public static unsafe (TransmissionTypeIdentifier, byte[]) Float128Composer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
public static unsafe (TransmissionDataUnitIdentifier, byte[]) Float128Composer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
{
|
||||
var v = (decimal)value;
|
||||
var rt = new byte[16];
|
||||
fixed (byte* ptr = rt)
|
||||
*((decimal*)ptr) = v;
|
||||
return (TransmissionTypeIdentifier.Decimal128, rt);
|
||||
return (TransmissionDataUnitIdentifier.Decimal128, rt);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static (TransmissionTypeIdentifier, byte[]) StringComposer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
public static (TransmissionDataUnitIdentifier, byte[]) StringComposer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
{
|
||||
return (TransmissionTypeIdentifier.String, Encoding.UTF8.GetBytes((string)value));
|
||||
return (TransmissionDataUnitIdentifier.String, Encoding.UTF8.GetBytes((string)value));
|
||||
}
|
||||
|
||||
public static (TransmissionTypeIdentifier, byte[]) EnumComposer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
public static (TransmissionDataUnitIdentifier, byte[]) EnumComposer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
{
|
||||
if (value == null)
|
||||
return (TransmissionTypeIdentifier.Null, new byte[0]);
|
||||
return (TransmissionDataUnitIdentifier.Null, new byte[0]);
|
||||
|
||||
//var warehouse = connection?.Instance?.Warehouse ?? connection?.Server?.Instance?.Warehouse;
|
||||
//if (warehouse == null)
|
||||
@@ -128,64 +128,64 @@ public static class DataSerializer
|
||||
var ct = template.Constants.FirstOrDefault(x => x.Value.Equals(intVal));
|
||||
|
||||
if (ct == null)
|
||||
return (TransmissionTypeIdentifier.Null, new byte[0]);
|
||||
return (TransmissionDataUnitIdentifier.Null, new byte[0]);
|
||||
|
||||
|
||||
var rt = new List<byte>();
|
||||
rt.AddRange(template.ClassId.Data);
|
||||
rt.Add(ct.Index);
|
||||
|
||||
return (TransmissionTypeIdentifier.TypedEnum, rt.ToArray());
|
||||
return (TransmissionDataUnitIdentifier.TypedEnum, rt.ToArray());
|
||||
}
|
||||
|
||||
public static (TransmissionTypeIdentifier, byte[]) UInt8Composer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
public static (TransmissionDataUnitIdentifier, byte[]) UInt8Composer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
{
|
||||
return (TransmissionTypeIdentifier.UInt8, new byte[] { (byte)value });
|
||||
return (TransmissionDataUnitIdentifier.UInt8, new byte[] { (byte)value });
|
||||
}
|
||||
|
||||
public static (TransmissionTypeIdentifier, byte[]) Int8Composer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
public static (TransmissionDataUnitIdentifier, byte[]) Int8Composer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
{
|
||||
return (TransmissionTypeIdentifier.Int8, new byte[] { (byte)(sbyte)value });
|
||||
return (TransmissionDataUnitIdentifier.Int8, new byte[] { (byte)(sbyte)value });
|
||||
}
|
||||
|
||||
public static unsafe (TransmissionTypeIdentifier, byte[]) Char8Composer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
public static unsafe (TransmissionDataUnitIdentifier, byte[]) Char8Composer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
{
|
||||
return (TransmissionTypeIdentifier.Char8, new byte[] { (byte)(char)value });
|
||||
return (TransmissionDataUnitIdentifier.Char8, new byte[] { (byte)(char)value });
|
||||
}
|
||||
|
||||
public static unsafe (TransmissionTypeIdentifier, byte[]) Char16Composer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
public static unsafe (TransmissionDataUnitIdentifier, byte[]) Char16Composer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
{
|
||||
|
||||
var v = (char)value;
|
||||
var rt = new byte[2];
|
||||
fixed (byte* ptr = rt)
|
||||
*((char*)ptr) = v;
|
||||
return (TransmissionTypeIdentifier.Char16, rt);
|
||||
return (TransmissionDataUnitIdentifier.Char16, rt);
|
||||
|
||||
}
|
||||
|
||||
public static (TransmissionTypeIdentifier, byte[]) BoolComposer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
public static (TransmissionDataUnitIdentifier, byte[]) BoolComposer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
{
|
||||
return ((bool)value ? TransmissionTypeIdentifier.True : TransmissionTypeIdentifier.False, new byte[0]);
|
||||
return ((bool)value ? TransmissionDataUnitIdentifier.True : TransmissionDataUnitIdentifier.False, new byte[0]);
|
||||
}
|
||||
|
||||
|
||||
public static (TransmissionTypeIdentifier, byte[]) NotModifiedComposer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
public static (TransmissionDataUnitIdentifier, byte[]) NotModifiedComposer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
{
|
||||
return (TransmissionTypeIdentifier.NotModified, new byte[0]);
|
||||
return (TransmissionDataUnitIdentifier.NotModified, new byte[0]);
|
||||
}
|
||||
|
||||
public static (TransmissionTypeIdentifier, byte[]) RawDataComposerFromArray(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
public static (TransmissionDataUnitIdentifier, byte[]) RawDataComposerFromArray(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
{
|
||||
return (TransmissionTypeIdentifier.RawData, (byte[])value);
|
||||
return (TransmissionDataUnitIdentifier.RawData, (byte[])value);
|
||||
}
|
||||
|
||||
public static (TransmissionTypeIdentifier, byte[]) RawDataComposerFromList(dynamic value, Warehouse warehouse, DistributedConnection connection)
|
||||
public static (TransmissionDataUnitIdentifier, byte[]) RawDataComposerFromList(dynamic value, Warehouse warehouse, DistributedConnection connection)
|
||||
{
|
||||
return (TransmissionTypeIdentifier.RawData, (value as List<byte>).ToArray());
|
||||
return (TransmissionDataUnitIdentifier.RawData, (value as List<byte>).ToArray());
|
||||
}
|
||||
|
||||
//public static (TransmissionTypeIdentifier, byte[]) ListComposerFromArray(dynamic value, DistributedConnection connection)
|
||||
//public static (TransmissionDataUnitIdentifier, byte[]) ListComposerFromArray(dynamic value, DistributedConnection connection)
|
||||
//{
|
||||
// var rt = new List<byte>();
|
||||
// var array = (object[])value;
|
||||
@@ -193,18 +193,18 @@ public static class DataSerializer
|
||||
// for (var i = 0; i < array.Length; i++)
|
||||
// rt.AddRange(Codec.Compose(array[i], connection));
|
||||
|
||||
// return (TransmissionTypeIdentifier.List, rt.ToArray());
|
||||
// return (TransmissionDataUnitIdentifier.List, rt.ToArray());
|
||||
//}
|
||||
|
||||
public static (TransmissionTypeIdentifier, byte[]) ListComposer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
public static (TransmissionDataUnitIdentifier, byte[]) ListComposer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
{
|
||||
|
||||
var rt = ArrayComposer((IEnumerable)value, warehouse, connection);
|
||||
|
||||
if (rt == null)
|
||||
return (TransmissionTypeIdentifier.Null, new byte[0]);
|
||||
return (TransmissionDataUnitIdentifier.Null, new byte[0]);
|
||||
else
|
||||
return (TransmissionTypeIdentifier.List, rt);
|
||||
return (TransmissionDataUnitIdentifier.List, rt);
|
||||
|
||||
|
||||
//var rt = new List<byte>();
|
||||
@@ -213,16 +213,16 @@ public static class DataSerializer
|
||||
//foreach (var o in list)
|
||||
// rt.AddRange(Codec.Compose(o, connection));
|
||||
|
||||
//return (TransmissionTypeIdentifier.List, rt.ToArray());
|
||||
//return (TransmissionDataUnitIdentifier.List, rt.ToArray());
|
||||
}
|
||||
|
||||
|
||||
public static (TransmissionTypeIdentifier, byte[]) TypedListComposer(IEnumerable value, Type type, Warehouse warehouse, DistributedConnection connection)
|
||||
public static (TransmissionDataUnitIdentifier, byte[]) TypedListComposer(IEnumerable value, Type type, Warehouse warehouse, DistributedConnection connection)
|
||||
{
|
||||
var composed = ArrayComposer((IEnumerable)value, warehouse, connection);
|
||||
|
||||
if (composed == null)
|
||||
return (TransmissionTypeIdentifier.Null, new byte[0]);
|
||||
return (TransmissionDataUnitIdentifier.Null, new byte[0]);
|
||||
|
||||
var header = RepresentationType.FromType(type).Compose();
|
||||
|
||||
@@ -231,7 +231,7 @@ public static class DataSerializer
|
||||
rt.AddRange(header);
|
||||
rt.AddRange(composed);
|
||||
|
||||
return (TransmissionTypeIdentifier.TypedList, rt.ToArray());
|
||||
return (TransmissionDataUnitIdentifier.TypedList, rt.ToArray());
|
||||
}
|
||||
|
||||
//public static byte[] PropertyValueComposer(PropertyValue propertyValue, DistributedConnection connection)//, bool includeAge = true)
|
||||
@@ -245,10 +245,10 @@ public static class DataSerializer
|
||||
// .ToArray();
|
||||
//}
|
||||
|
||||
public static (TransmissionTypeIdentifier, byte[]) PropertyValueArrayComposer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
public static (TransmissionDataUnitIdentifier, byte[]) PropertyValueArrayComposer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
{
|
||||
if (value == null)
|
||||
return (TransmissionTypeIdentifier.Null, new byte[0]);
|
||||
return (TransmissionDataUnitIdentifier.Null, new byte[0]);
|
||||
|
||||
var rt = new List<byte>();
|
||||
var ar = value as PropertyValue[];
|
||||
@@ -260,13 +260,13 @@ public static class DataSerializer
|
||||
rt.AddRange(Codec.Compose(pv.Value, warehouse, connection));
|
||||
}
|
||||
|
||||
return (TransmissionTypeIdentifier.RawData, rt.ToArray());
|
||||
return (TransmissionDataUnitIdentifier.RawData, rt.ToArray());
|
||||
}
|
||||
|
||||
public static (TransmissionTypeIdentifier, byte[]) TypedMapComposer(object value, Type keyType, Type valueType, Warehouse warehouse, DistributedConnection connection)
|
||||
public static (TransmissionDataUnitIdentifier, byte[]) TypedMapComposer(object value, Type keyType, Type valueType, Warehouse warehouse, DistributedConnection connection)
|
||||
{
|
||||
if (value == null)
|
||||
return (TransmissionTypeIdentifier.Null, new byte[0]);
|
||||
return (TransmissionDataUnitIdentifier.Null, new byte[0]);
|
||||
|
||||
var kt = RepresentationType.FromType(keyType).Compose();
|
||||
var vt = RepresentationType.FromType(valueType).Compose();
|
||||
@@ -282,12 +282,12 @@ public static class DataSerializer
|
||||
rt.AddRange(Codec.Compose(el, warehouse, connection));
|
||||
|
||||
|
||||
return (TransmissionTypeIdentifier.TypedMap, rt.ToArray());
|
||||
return (TransmissionDataUnitIdentifier.TypedMap, rt.ToArray());
|
||||
}
|
||||
public static (TransmissionTypeIdentifier, byte[]) TypedDictionaryComposer(object value, Type keyType, Type valueType, Warehouse warehouse, DistributedConnection connection)
|
||||
public static (TransmissionDataUnitIdentifier, byte[]) TypedDictionaryComposer(object value, Type keyType, Type valueType, Warehouse warehouse, DistributedConnection connection)
|
||||
{
|
||||
if (value == null)
|
||||
return (TransmissionTypeIdentifier.Null, new byte[0]);
|
||||
return (TransmissionDataUnitIdentifier.Null, new byte[0]);
|
||||
|
||||
var kt = RepresentationType.FromType(keyType).Compose();
|
||||
var vt = RepresentationType.FromType(valueType).Compose();
|
||||
@@ -310,7 +310,7 @@ public static class DataSerializer
|
||||
rt.AddRange(Codec.Compose(el, warehouse, connection));
|
||||
|
||||
|
||||
return (TransmissionTypeIdentifier.TypedMap, rt.ToArray());
|
||||
return (TransmissionDataUnitIdentifier.TypedMap, rt.ToArray());
|
||||
}
|
||||
|
||||
public static byte[] ArrayComposer(IEnumerable value, Warehouse warehouse, DistributedConnection connection)
|
||||
@@ -320,7 +320,7 @@ public static class DataSerializer
|
||||
|
||||
var rt = new List<byte>();
|
||||
|
||||
TransmissionTypeIdentifier? previous = null;
|
||||
TransmissionDataUnitIdentifier? previous = null;
|
||||
byte[]? previousUUID = null;
|
||||
|
||||
foreach (var i in value)
|
||||
@@ -330,15 +330,15 @@ public static class DataSerializer
|
||||
previous = hdr;
|
||||
else if (hdr == previous)
|
||||
{
|
||||
if (hdr == TransmissionTypeIdentifier.Record)
|
||||
if (hdr == TransmissionDataUnitIdentifier.Record)
|
||||
{
|
||||
var newUUID = data.Take(16).ToArray();
|
||||
// check same uuid
|
||||
if (newUUID.SequenceEqual(previousUUID))
|
||||
rt.AddRange(TransmissionType.Compose(TransmissionTypeIdentifier.Same,
|
||||
rt.AddRange(TransmissionDataUnit.Compose(TransmissionDataUnitIdentifier.Same,
|
||||
data.Skip(16).ToArray()));
|
||||
else
|
||||
rt.AddRange(TransmissionType.Compose(hdr, data));
|
||||
rt.AddRange(TransmissionDataUnit.Compose(hdr, data));
|
||||
|
||||
previous = hdr;
|
||||
previousUUID = newUUID;
|
||||
@@ -351,32 +351,32 @@ public static class DataSerializer
|
||||
return rt.ToArray();
|
||||
}
|
||||
|
||||
public static (TransmissionTypeIdentifier, byte[]) ResourceListComposer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
public static (TransmissionDataUnitIdentifier, byte[]) ResourceListComposer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
{
|
||||
if (value == null)
|
||||
return (TransmissionTypeIdentifier.Null, new byte[0]);
|
||||
return (TransmissionDataUnitIdentifier.Null, new byte[0]);
|
||||
|
||||
|
||||
return (TransmissionTypeIdentifier.ResourceList, ArrayComposer((IEnumerable)value, warehouse, connection));
|
||||
return (TransmissionDataUnitIdentifier.ResourceList, ArrayComposer((IEnumerable)value, warehouse, connection));
|
||||
}
|
||||
|
||||
public static (TransmissionTypeIdentifier, byte[]) RecordListComposer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
public static (TransmissionDataUnitIdentifier, byte[]) RecordListComposer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
{
|
||||
if (value == null)
|
||||
return (TransmissionTypeIdentifier.Null, new byte[0]);
|
||||
return (TransmissionDataUnitIdentifier.Null, new byte[0]);
|
||||
|
||||
|
||||
return (TransmissionTypeIdentifier.RecordList, ArrayComposer((IEnumerable)value, warehouse, connection));
|
||||
return (TransmissionDataUnitIdentifier.RecordList, ArrayComposer((IEnumerable)value, warehouse, connection));
|
||||
}
|
||||
|
||||
|
||||
public static unsafe (TransmissionTypeIdentifier, byte[]) ResourceComposer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
public static unsafe (TransmissionDataUnitIdentifier, byte[]) ResourceComposer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
{
|
||||
var resource = (IResource)value;
|
||||
|
||||
if (resource.Instance == null || resource.Instance.IsDestroyed)
|
||||
{
|
||||
return (TransmissionTypeIdentifier.Null, new byte[0]);
|
||||
return (TransmissionDataUnitIdentifier.Null, new byte[0]);
|
||||
}
|
||||
|
||||
if (Codec.IsLocalResource(resource, connection))
|
||||
@@ -384,21 +384,21 @@ public static class DataSerializer
|
||||
var rid = (resource as DistributedResource).DistributedResourceInstanceId;
|
||||
|
||||
if (rid <= 0xFF)
|
||||
return (TransmissionTypeIdentifier.LocalResource8, new byte[] { (byte)rid });
|
||||
return (TransmissionDataUnitIdentifier.LocalResource8, new byte[] { (byte)rid });
|
||||
else if (rid <= 0xFFFF)
|
||||
{
|
||||
var rt = new byte[2];
|
||||
fixed (byte* ptr = rt)
|
||||
*((ushort*)ptr) = (ushort)rid;
|
||||
|
||||
return (TransmissionTypeIdentifier.LocalResource16, rt);
|
||||
return (TransmissionDataUnitIdentifier.LocalResource16, rt);
|
||||
}
|
||||
else
|
||||
{
|
||||
var rt = new byte[4];
|
||||
fixed (byte* ptr = rt)
|
||||
*((uint*)ptr) = rid;
|
||||
return (TransmissionTypeIdentifier.LocalResource32, rt);
|
||||
return (TransmissionDataUnitIdentifier.LocalResource32, rt);
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -410,29 +410,29 @@ public static class DataSerializer
|
||||
var rid = resource.Instance.Id;
|
||||
|
||||
if (rid <= 0xFF)
|
||||
return (TransmissionTypeIdentifier.RemoteResource8, new byte[] { (byte)rid });
|
||||
return (TransmissionDataUnitIdentifier.RemoteResource8, new byte[] { (byte)rid });
|
||||
else if (rid <= 0xFFFF)
|
||||
{
|
||||
var rt = new byte[2];
|
||||
fixed (byte* ptr = rt)
|
||||
*((ushort*)ptr) = (ushort)rid;
|
||||
|
||||
return (TransmissionTypeIdentifier.RemoteResource16, rt);
|
||||
return (TransmissionDataUnitIdentifier.RemoteResource16, rt);
|
||||
}
|
||||
else
|
||||
{
|
||||
var rt = new byte[4];
|
||||
fixed (byte* ptr = rt)
|
||||
*((uint*)ptr) = rid;
|
||||
return (TransmissionTypeIdentifier.RemoteResource32, rt);
|
||||
return (TransmissionDataUnitIdentifier.RemoteResource32, rt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static unsafe (TransmissionTypeIdentifier, byte[]) MapComposer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
public static unsafe (TransmissionDataUnitIdentifier, byte[]) MapComposer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
{
|
||||
if (value == null)
|
||||
return (TransmissionTypeIdentifier.Null, new byte[0]);
|
||||
return (TransmissionDataUnitIdentifier.Null, new byte[0]);
|
||||
|
||||
var rt = new List<byte>();
|
||||
var map = (IMap)value;
|
||||
@@ -440,16 +440,16 @@ public static class DataSerializer
|
||||
foreach (var el in map.Serialize())
|
||||
rt.AddRange(Codec.Compose(el, warehouse, connection));
|
||||
|
||||
return (TransmissionTypeIdentifier.Map, rt.ToArray());
|
||||
return (TransmissionDataUnitIdentifier.Map, rt.ToArray());
|
||||
}
|
||||
|
||||
public static unsafe (TransmissionTypeIdentifier, byte[]) UUIDComposer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
public static unsafe (TransmissionDataUnitIdentifier, byte[]) UUIDComposer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
{
|
||||
return (TransmissionTypeIdentifier.UUID, ((UUID)value).Data);
|
||||
return (TransmissionDataUnitIdentifier.UUID, ((UUID)value).Data);
|
||||
|
||||
}
|
||||
|
||||
public static unsafe (TransmissionTypeIdentifier, byte[]) RecordComposer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
public static unsafe (TransmissionDataUnitIdentifier, byte[]) RecordComposer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
{
|
||||
var rt = new List<byte>();// BinaryList();
|
||||
var record = (IRecord)value;
|
||||
@@ -465,7 +465,7 @@ public static class DataSerializer
|
||||
rt.AddRange(Codec.Compose(propValue, warehouse, connection));
|
||||
}
|
||||
|
||||
return (TransmissionTypeIdentifier.Record, rt.ToArray());
|
||||
return (TransmissionDataUnitIdentifier.Record, rt.ToArray());
|
||||
}
|
||||
public static byte[] HistoryComposer(KeyList<PropertyTemplate, PropertyValue[]> history, Warehouse warehouse,
|
||||
DistributedConnection connection, bool prependLength = false)
|
||||
@@ -483,10 +483,10 @@ public static class DataSerializer
|
||||
return rt.ToArray();
|
||||
}
|
||||
|
||||
public static (TransmissionTypeIdentifier, byte[]) TupleComposer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
public static (TransmissionDataUnitIdentifier, byte[]) TupleComposer(object value, Warehouse warehouse, DistributedConnection connection)
|
||||
{
|
||||
if (value == null)
|
||||
return (TransmissionTypeIdentifier.Null, new byte[0]);
|
||||
return (TransmissionDataUnitIdentifier.Null, new byte[0]);
|
||||
|
||||
var rt = new List<byte>();
|
||||
|
||||
@@ -502,11 +502,11 @@ public static class DataSerializer
|
||||
var composed = ArrayComposer(list, warehouse, connection);
|
||||
|
||||
if (composed == null)
|
||||
return (TransmissionTypeIdentifier.Null, new byte[0]);
|
||||
return (TransmissionDataUnitIdentifier.Null, new byte[0]);
|
||||
else
|
||||
{
|
||||
rt.AddRange(composed);
|
||||
return (TransmissionTypeIdentifier.TypedTuple, rt.ToArray());
|
||||
return (TransmissionDataUnitIdentifier.TypedTuple, rt.ToArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -76,42 +76,42 @@ namespace Esiur.Data
|
||||
RepresentationTypeIdentifier.TypedResource
|
||||
};
|
||||
|
||||
static Map<TransmissionTypeIdentifier, RepresentationTypeIdentifier> typesMap = new Map<TransmissionTypeIdentifier, RepresentationTypeIdentifier>()
|
||||
static Map<TransmissionDataUnitIdentifier, RepresentationTypeIdentifier> typesMap = new Map<TransmissionDataUnitIdentifier, RepresentationTypeIdentifier>()
|
||||
{
|
||||
[TransmissionTypeIdentifier.UInt8] = RepresentationTypeIdentifier.UInt8,
|
||||
[TransmissionTypeIdentifier.Int8] = RepresentationTypeIdentifier.Int8,
|
||||
[TransmissionTypeIdentifier.UInt16] = RepresentationTypeIdentifier.UInt16,
|
||||
[TransmissionTypeIdentifier.Int16] = RepresentationTypeIdentifier.Int16,
|
||||
[TransmissionTypeIdentifier.UInt32] = RepresentationTypeIdentifier.UInt32,
|
||||
[TransmissionTypeIdentifier.Int32] = RepresentationTypeIdentifier.Int32,
|
||||
[TransmissionTypeIdentifier.UInt64] = RepresentationTypeIdentifier.UInt64,
|
||||
[TransmissionTypeIdentifier.Int64] = RepresentationTypeIdentifier.Int64,
|
||||
[TransmissionTypeIdentifier.UInt128] = RepresentationTypeIdentifier.UInt128,
|
||||
[TransmissionTypeIdentifier.Int128] = RepresentationTypeIdentifier.Int128,
|
||||
[TransmissionTypeIdentifier.Char8] = RepresentationTypeIdentifier.Char,
|
||||
[TransmissionTypeIdentifier.DateTime] = RepresentationTypeIdentifier.DateTime,
|
||||
[TransmissionTypeIdentifier.Float32] = RepresentationTypeIdentifier.Float32,
|
||||
[TransmissionTypeIdentifier.Float64] = RepresentationTypeIdentifier.Float64,
|
||||
[TransmissionTypeIdentifier.Decimal128] = RepresentationTypeIdentifier.Decimal,
|
||||
[TransmissionTypeIdentifier.False] = RepresentationTypeIdentifier.Bool,
|
||||
[TransmissionTypeIdentifier.True] = RepresentationTypeIdentifier.Bool,
|
||||
[TransmissionTypeIdentifier.Map] = RepresentationTypeIdentifier.Map,
|
||||
[TransmissionTypeIdentifier.List] = RepresentationTypeIdentifier.List,
|
||||
[TransmissionTypeIdentifier.RawData] = RepresentationTypeIdentifier.RawData,
|
||||
[TransmissionTypeIdentifier.Record] = RepresentationTypeIdentifier.Record,
|
||||
[TransmissionTypeIdentifier.String] = RepresentationTypeIdentifier.String,
|
||||
[TransmissionDataUnitIdentifier.UInt8] = RepresentationTypeIdentifier.UInt8,
|
||||
[TransmissionDataUnitIdentifier.Int8] = RepresentationTypeIdentifier.Int8,
|
||||
[TransmissionDataUnitIdentifier.UInt16] = RepresentationTypeIdentifier.UInt16,
|
||||
[TransmissionDataUnitIdentifier.Int16] = RepresentationTypeIdentifier.Int16,
|
||||
[TransmissionDataUnitIdentifier.UInt32] = RepresentationTypeIdentifier.UInt32,
|
||||
[TransmissionDataUnitIdentifier.Int32] = RepresentationTypeIdentifier.Int32,
|
||||
[TransmissionDataUnitIdentifier.UInt64] = RepresentationTypeIdentifier.UInt64,
|
||||
[TransmissionDataUnitIdentifier.Int64] = RepresentationTypeIdentifier.Int64,
|
||||
[TransmissionDataUnitIdentifier.UInt128] = RepresentationTypeIdentifier.UInt128,
|
||||
[TransmissionDataUnitIdentifier.Int128] = RepresentationTypeIdentifier.Int128,
|
||||
[TransmissionDataUnitIdentifier.Char8] = RepresentationTypeIdentifier.Char,
|
||||
[TransmissionDataUnitIdentifier.DateTime] = RepresentationTypeIdentifier.DateTime,
|
||||
[TransmissionDataUnitIdentifier.Float32] = RepresentationTypeIdentifier.Float32,
|
||||
[TransmissionDataUnitIdentifier.Float64] = RepresentationTypeIdentifier.Float64,
|
||||
[TransmissionDataUnitIdentifier.Decimal128] = RepresentationTypeIdentifier.Decimal,
|
||||
[TransmissionDataUnitIdentifier.False] = RepresentationTypeIdentifier.Bool,
|
||||
[TransmissionDataUnitIdentifier.True] = RepresentationTypeIdentifier.Bool,
|
||||
[TransmissionDataUnitIdentifier.Map] = RepresentationTypeIdentifier.Map,
|
||||
[TransmissionDataUnitIdentifier.List] = RepresentationTypeIdentifier.List,
|
||||
[TransmissionDataUnitIdentifier.RawData] = RepresentationTypeIdentifier.RawData,
|
||||
[TransmissionDataUnitIdentifier.Record] = RepresentationTypeIdentifier.Record,
|
||||
[TransmissionDataUnitIdentifier.String] = RepresentationTypeIdentifier.String,
|
||||
};
|
||||
|
||||
public bool IsCompatible(TransmissionType tdu)
|
||||
public bool IsCompatible(TransmissionDataUnit tdu)
|
||||
{
|
||||
var tru = typesMap[tdu.Identifier];
|
||||
|
||||
if (tru != Identifier)
|
||||
return false;
|
||||
|
||||
if (tdu.Class == TransmissionTypeClass.Typed)
|
||||
if (tdu.Class == TransmissionDataUnitClass.Typed)
|
||||
{
|
||||
if (tdu.Identifier == TransmissionTypeIdentifier.)
|
||||
if (tdu.Identifier == TransmissionDataUnitIdentifier.)
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
272
Esiur/Data/TransmissionDataUnit.cs
Normal file
272
Esiur/Data/TransmissionDataUnit.cs
Normal file
@@ -0,0 +1,272 @@
|
||||
using Esiur.Net.IIP;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Data;
|
||||
|
||||
|
||||
public struct TransmissionDataUnit
|
||||
{
|
||||
public TransmissionDataUnitIdentifier Identifier;
|
||||
public int Index;
|
||||
public TransmissionDataUnitClass Class;
|
||||
public uint Offset;
|
||||
public ulong ContentLength;
|
||||
public byte Exponent;
|
||||
public byte[] Data;
|
||||
|
||||
public TransmissionDataUnit(byte[] data, TransmissionDataUnitIdentifier identifier,
|
||||
TransmissionDataUnitClass cls, int index, uint offset,
|
||||
ulong contentLength, byte exponent = 0)
|
||||
{
|
||||
Identifier = identifier;
|
||||
Index = index;
|
||||
Class = cls;
|
||||
Offset=offset;
|
||||
ContentLength = contentLength;
|
||||
Exponent = exponent;
|
||||
Data = data;
|
||||
}
|
||||
|
||||
public byte[] GetTypeMetadata()
|
||||
{
|
||||
if (Class != TransmissionDataUnitClass.Typed)
|
||||
throw new Exception("Class has no metadata.");
|
||||
|
||||
var size = Data[Offset];
|
||||
return Data.Clip(Offset + 1, size);
|
||||
|
||||
}
|
||||
|
||||
public static byte[] Compose(TransmissionDataUnitIdentifier identifier, byte[] data, byte[] typeMetadata = null)
|
||||
{
|
||||
|
||||
if (data == null || data.Length == 0)
|
||||
return new byte[] { (byte)identifier };
|
||||
|
||||
var cls = (TransmissionDataUnitClass)((int)identifier >> 6);
|
||||
if (cls == TransmissionDataUnitClass.Fixed)
|
||||
{
|
||||
return DC.Combine(new byte[] { (byte)identifier }, 0, 1, data, 0, (uint)data.Length);
|
||||
}
|
||||
else if (cls == TransmissionDataUnitClass.Dynamic)
|
||||
{
|
||||
var len = (ulong)data.LongLength;
|
||||
|
||||
if (len == 0)
|
||||
{
|
||||
return new byte[1] { (byte) identifier };
|
||||
}
|
||||
else if (len <= 0xFF)
|
||||
{
|
||||
var rt = new byte[2 + len];
|
||||
rt[0] = (byte)((byte)identifier | 0x8);
|
||||
rt[1] = (byte)len;
|
||||
Buffer.BlockCopy(data, 0, rt, 2, (int)len);
|
||||
return rt;
|
||||
}
|
||||
else if (len <= 0xFF_FF)
|
||||
{
|
||||
var rt = new byte[3 + len];
|
||||
rt[0] = (byte)((byte)identifier | 0x10);
|
||||
rt[1] = (byte)((len >> 8) & 0xFF);
|
||||
rt[2] = (byte)(len & 0xFF);
|
||||
Buffer.BlockCopy(data, 0, rt, 3, (int)len);
|
||||
return rt;
|
||||
}
|
||||
else if (len <= 0xFF_FF_FF)
|
||||
{
|
||||
var rt = new byte[4 + len];
|
||||
rt[0] = (byte)((byte)identifier | 0x18);
|
||||
rt[1] = (byte)((len >> 16) & 0xFF);
|
||||
rt[2] = (byte)((len >> 8) & 0xFF);
|
||||
rt[3] = (byte)(len & 0xFF);
|
||||
Buffer.BlockCopy(data, 0, rt, 4, (int)len);
|
||||
return rt;
|
||||
}
|
||||
else if (len <= 0xFF_FF_FF_FF)
|
||||
{
|
||||
var rt = new byte[5 + len];
|
||||
rt[0] = (byte)((byte)identifier | 0x20);
|
||||
rt[1] = (byte)((len >> 24) & 0xFF);
|
||||
rt[2] = (byte)((len >> 16) & 0xFF);
|
||||
rt[3] = (byte)((len >> 8) & 0xFF);
|
||||
rt[4] = (byte)(len & 0xFF);
|
||||
Buffer.BlockCopy(data, 0, rt, 5, (int)len);
|
||||
return rt;
|
||||
}
|
||||
else if (len <= 0xFF_FF_FF_FF_FF)
|
||||
{
|
||||
var rt = new byte[6 + len];
|
||||
rt[0] = (byte)((byte)identifier | 0x28);
|
||||
rt[1] = (byte)((len >> 32) & 0xFF);
|
||||
rt[2] = (byte)((len >> 24) & 0xFF);
|
||||
rt[3] = (byte)((len >> 16) & 0xFF);
|
||||
rt[4] = (byte)((len >> 8) & 0xFF);
|
||||
rt[5] = (byte)(len & 0xFF);
|
||||
Buffer.BlockCopy(data, 0, rt, 6, (int)len);
|
||||
return rt;
|
||||
}
|
||||
else if (len <= 0xFF_FF_FF_FF_FF_FF)
|
||||
{
|
||||
var rt = new byte[7 + len];
|
||||
rt[0] = (byte)((byte)identifier | 0x30);
|
||||
rt[1] = (byte)((len >> 40) & 0xFF);
|
||||
rt[2] = (byte)((len >> 32) & 0xFF);
|
||||
rt[3] = (byte)((len >> 24) & 0xFF);
|
||||
rt[4] = (byte)((len >> 16) & 0xFF);
|
||||
rt[5] = (byte)((len >> 8) & 0xFF);
|
||||
rt[6] = (byte)(len & 0xFF);
|
||||
Buffer.BlockCopy(data, 0, rt, 7, (int)len);
|
||||
return rt;
|
||||
}
|
||||
else //if (len <= 0xFF_FF_FF_FF_FF_FF_FF)
|
||||
{
|
||||
var rt = new byte[8 + len];
|
||||
rt[0] = (byte)((byte)identifier | 0x38);
|
||||
rt[1] = (byte)((len >> 48) & 0xFF);
|
||||
rt[2] = (byte)((len >> 40) & 0xFF);
|
||||
rt[3] = (byte)((len >> 32) & 0xFF);
|
||||
rt[4] = (byte)((len >> 24) & 0xFF);
|
||||
rt[5] = (byte)((len >> 16) & 0xFF);
|
||||
rt[6] = (byte)((len >> 8) & 0xFF);
|
||||
rt[7] = (byte)(len & 0xFF);
|
||||
Buffer.BlockCopy(data, 0, rt, 8, (int)len);
|
||||
return rt;
|
||||
}
|
||||
}
|
||||
else if (cls == TransmissionDataUnitClass.Typed)
|
||||
{
|
||||
|
||||
var len = 1 + (ulong)typeMetadata.LongLength + (ulong)data.LongLength;
|
||||
|
||||
if (len == 0)
|
||||
{
|
||||
return new byte[1] { (byte)identifier };
|
||||
}
|
||||
else if (len <= 0xFF)
|
||||
{
|
||||
var rt = new byte[2 + len];
|
||||
rt[0] = (byte)((byte)identifier | 0x8);
|
||||
rt[1] = (byte)len;
|
||||
|
||||
|
||||
Buffer.BlockCopy(data, 0, rt, 2, (int)len);
|
||||
return rt;
|
||||
}
|
||||
else if (len <= 0xFF_FF)
|
||||
{
|
||||
var rt = new byte[3 + len];
|
||||
rt[0] = (byte)((byte)identifier | 0x10);
|
||||
rt[1] = (byte)((len >> 8) & 0xFF);
|
||||
rt[2] = (byte)(len & 0xFF);
|
||||
Buffer.BlockCopy(data, 0, rt, 3, (int)len);
|
||||
return rt;
|
||||
}
|
||||
else if (len <= 0xFF_FF_FF)
|
||||
{
|
||||
var rt = new byte[4 + len];
|
||||
rt[0] = (byte)((byte)identifier | 0x18);
|
||||
rt[1] = (byte)((len >> 16) & 0xFF);
|
||||
rt[2] = (byte)((len >> 8) & 0xFF);
|
||||
rt[3] = (byte)(len & 0xFF);
|
||||
Buffer.BlockCopy(data, 0, rt, 4, (int)len);
|
||||
return rt;
|
||||
}
|
||||
else if (len <= 0xFF_FF_FF_FF)
|
||||
{
|
||||
var rt = new byte[5 + len];
|
||||
rt[0] = (byte)((byte)identifier | 0x20);
|
||||
rt[1] = (byte)((len >> 24) & 0xFF);
|
||||
rt[2] = (byte)((len >> 16) & 0xFF);
|
||||
rt[3] = (byte)((len >> 8) & 0xFF);
|
||||
rt[4] = (byte)(len & 0xFF);
|
||||
Buffer.BlockCopy(data, 0, rt, 5, (int)len);
|
||||
return rt;
|
||||
}
|
||||
else if (len <= 0xFF_FF_FF_FF_FF)
|
||||
{
|
||||
var rt = new byte[6 + len];
|
||||
rt[0] = (byte)((byte)identifier | 0x28);
|
||||
rt[1] = (byte)((len >> 32) & 0xFF);
|
||||
rt[2] = (byte)((len >> 24) & 0xFF);
|
||||
rt[3] = (byte)((len >> 16) & 0xFF);
|
||||
rt[4] = (byte)((len >> 8) & 0xFF);
|
||||
rt[5] = (byte)(len & 0xFF);
|
||||
Buffer.BlockCopy(data, 0, rt, 6, (int)len);
|
||||
return rt;
|
||||
}
|
||||
else if (len <= 0xFF_FF_FF_FF_FF_FF)
|
||||
{
|
||||
var rt = new byte[7 + len];
|
||||
rt[0] = (byte)((byte)identifier | 0x30);
|
||||
rt[1] = (byte)((len >> 40) & 0xFF);
|
||||
rt[2] = (byte)((len >> 32) & 0xFF);
|
||||
rt[3] = (byte)((len >> 24) & 0xFF);
|
||||
rt[4] = (byte)((len >> 16) & 0xFF);
|
||||
rt[5] = (byte)((len >> 8) & 0xFF);
|
||||
rt[6] = (byte)(len & 0xFF);
|
||||
Buffer.BlockCopy(data, 0, rt, 7, (int)len);
|
||||
return rt;
|
||||
}
|
||||
else //if (len <= 0xFF_FF_FF_FF_FF_FF_FF)
|
||||
{
|
||||
var rt = new byte[8 + len];
|
||||
rt[0] = (byte)((byte)identifier | 0x38);
|
||||
rt[1] = (byte)((len >> 48) & 0xFF);
|
||||
rt[2] = (byte)((len >> 40) & 0xFF);
|
||||
rt[3] = (byte)((len >> 32) & 0xFF);
|
||||
rt[4] = (byte)((len >> 24) & 0xFF);
|
||||
rt[5] = (byte)((len >> 16) & 0xFF);
|
||||
rt[6] = (byte)((len >> 8) & 0xFF);
|
||||
rt[7] = (byte)(len & 0xFF);
|
||||
Buffer.BlockCopy(data, 0, rt, 8, (int)len);
|
||||
return rt;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static (ulong, TransmissionDataUnit?) Parse(byte[] data, uint offset, uint ends)
|
||||
{
|
||||
var h = data[offset++];
|
||||
|
||||
var cls = (TransmissionDataUnitClass)(h >> 6);
|
||||
|
||||
if (cls == TransmissionDataUnitClass.Fixed)
|
||||
{
|
||||
var exp = (h & 0x38) >> 3;
|
||||
|
||||
if (exp == 0)
|
||||
return (1, new TransmissionDataUnit(data, (TransmissionDataUnitIdentifier)h, cls, h & 0x7, 0, (byte)exp));
|
||||
|
||||
ulong cl = (ulong)(1 << (exp -1));
|
||||
|
||||
if (ends - offset < cl)
|
||||
return (cl - (ends - offset), null);
|
||||
|
||||
//offset += (uint)cl;
|
||||
|
||||
return (1 + cl, new TransmissionDataUnit(data, (TransmissionDataUnitIdentifier)h, cls, h & 0x7, offset, cl, (byte)exp));
|
||||
}
|
||||
else
|
||||
{
|
||||
ulong cll = (ulong)(h >> 3) & 0x7;
|
||||
|
||||
if (ends - offset < cll)
|
||||
return (cll - (ends - offset), null);
|
||||
|
||||
ulong cl = 0;
|
||||
|
||||
for (uint i = 0; i < cll; i++)
|
||||
cl = cl << 8 | data[offset++];
|
||||
|
||||
if (ends - offset < cl)
|
||||
return (cl - (ends - offset), null);
|
||||
|
||||
|
||||
return (1 + cl + cll, new TransmissionDataUnit(data, (TransmissionDataUnitIdentifier)(h & 0xC7), cls, h & 0x7, offset, cl));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
14
Esiur/Data/TransmissionDataUnitClass.cs
Normal file
14
Esiur/Data/TransmissionDataUnitClass.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Data
|
||||
{
|
||||
public enum TransmissionDataUnitClass
|
||||
{
|
||||
Fixed = 0x0,
|
||||
Dynamic = 0x1,
|
||||
Typed = 0x2,
|
||||
Extension = 0x3
|
||||
}
|
||||
}
|
63
Esiur/Data/TransmissionDataUnitIdentifier.cs
Normal file
63
Esiur/Data/TransmissionDataUnitIdentifier.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Data
|
||||
{
|
||||
public enum TransmissionDataUnitIdentifier
|
||||
{
|
||||
Null = 0x0,
|
||||
False = 0x1,
|
||||
True = 0x2,
|
||||
NotModified = 0x3,
|
||||
UInt8 = 0x8,
|
||||
Int8 = 0x9,
|
||||
Char8 = 0xA,
|
||||
LocalResource8 = 0xB,
|
||||
RemoteResource8 = 0xC,
|
||||
LocalProcedure8 = 0xD,
|
||||
RemoteProcedure8 = 0xE,
|
||||
UInt16 = 0x10,
|
||||
Int16 = 0x11,
|
||||
Char16 = 0x12,
|
||||
LocalResource16 = 0x13,
|
||||
RemoteResource16 = 0x14,
|
||||
LocalProcedure16 = 0x15,
|
||||
RemoteProcedure16 = 0x16,
|
||||
UInt32 = 0x18,
|
||||
Int32 = 0x19,
|
||||
Float32 = 0x1A,
|
||||
LocalResource32 = 0x1B,
|
||||
RemoteResource32 = 0x1C,
|
||||
LocalProcedure32 = 0x1D,
|
||||
RemoteProcedure32 = 0x1E,
|
||||
UInt64 = 0x20,
|
||||
Int64 = 0x21,
|
||||
Float64 = 0x22,
|
||||
DateTime = 0x23,
|
||||
UInt128 = 0x28,
|
||||
Int128 = 0x29,
|
||||
Decimal128 = 0x2A,
|
||||
UUID = 0x2B,
|
||||
|
||||
RawData = 0x40,
|
||||
String = 0x41,
|
||||
List = 0x42,
|
||||
ResourceList = 0x43,
|
||||
RecordList = 0x44,
|
||||
Map = 0x45,
|
||||
MapList = 0x46,
|
||||
|
||||
Record = 0x80,
|
||||
TypedList = 0x81,
|
||||
TypedMap = 0x82,
|
||||
TypedTuple = 0x83,
|
||||
TypedEnum = 0x84,
|
||||
TypedConstant = 0x85,
|
||||
|
||||
ResourceLink = 0xC0,
|
||||
|
||||
Same = 0xFF
|
||||
|
||||
}
|
||||
}
|
@@ -1,261 +0,0 @@
|
||||
using Esiur.Net.IIP;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Data;
|
||||
|
||||
public enum TransmissionTypeIdentifier : byte
|
||||
{
|
||||
Null = 0x0,
|
||||
False = 0x1,
|
||||
True = 0x2,
|
||||
NotModified = 0x3,
|
||||
UInt8 = 0x8,
|
||||
Int8 = 0x9,
|
||||
Char8 = 0xA,
|
||||
LocalResource8 = 0xB,
|
||||
RemoteResource8 = 0xC,
|
||||
LocalProcedure8 = 0xD,
|
||||
RemoteProcedure8 = 0xE,
|
||||
UInt16 = 0x10,
|
||||
Int16 = 0x11,
|
||||
Char16 = 0x12,
|
||||
LocalResource16 = 0x13,
|
||||
RemoteResource16 = 0x14,
|
||||
LocalProcedure16 = 0x15,
|
||||
RemoteProcedure16 = 0x16,
|
||||
UInt32 = 0x18,
|
||||
Int32 = 0x19,
|
||||
Float32 = 0x1A,
|
||||
LocalResource32 = 0x1B,
|
||||
RemoteResource32 = 0x1C,
|
||||
LocalProcedure32 = 0x1D,
|
||||
RemoteProcedure32 = 0x1E,
|
||||
UInt64 = 0x20,
|
||||
Int64 = 0x21,
|
||||
Float64 = 0x22,
|
||||
DateTime = 0x23,
|
||||
UInt128 = 0x28,
|
||||
Int128 = 0x29,
|
||||
Decimal128 = 0x2A,
|
||||
UUID = 0x2B,
|
||||
|
||||
RawData = 0x40,
|
||||
String = 0x41,
|
||||
List = 0x42,
|
||||
ResourceList = 0x43,
|
||||
RecordList = 0x44,
|
||||
Map = 0x45,
|
||||
MapList = 0x46,
|
||||
|
||||
Record = 0x80,
|
||||
TypedList = 0x81,
|
||||
TypedMap = 0x82,
|
||||
TypedTuple = 0x83,
|
||||
TypedEnum = 0x84,
|
||||
TypedConstant = 0x85,
|
||||
|
||||
ResourceLink = 0xC0,
|
||||
|
||||
Same = 0xFF
|
||||
}
|
||||
|
||||
public enum TransmissionTypeClass
|
||||
{
|
||||
Fixed = 0,
|
||||
Dynamic = 1,
|
||||
Typed = 2
|
||||
}
|
||||
|
||||
public struct TransmissionType
|
||||
{
|
||||
public TransmissionTypeIdentifier Identifier;
|
||||
public int Index;
|
||||
public TransmissionTypeClass Class;
|
||||
public uint Offset;
|
||||
public ulong ContentLength;
|
||||
public byte Exponent;
|
||||
|
||||
|
||||
public TransmissionType(TransmissionTypeIdentifier identifier, TransmissionTypeClass cls, int index, uint offset, ulong contentLength, byte exponent = 0)
|
||||
{
|
||||
Identifier = identifier;
|
||||
Index = index;
|
||||
Class = cls;
|
||||
Offset=offset;
|
||||
ContentLength = contentLength;
|
||||
Exponent = exponent;
|
||||
}
|
||||
|
||||
public static byte[] Compose(TransmissionTypeIdentifier identifier, byte[] data)
|
||||
{
|
||||
|
||||
if (data == null || data.Length == 0)
|
||||
return new byte[] { (byte)identifier };
|
||||
|
||||
var cls = (TransmissionTypeClass)((int)identifier >> 6);
|
||||
if (cls == TransmissionTypeClass.Fixed)
|
||||
{
|
||||
return DC.Combine(new byte[] { (byte)identifier }, 0, 1, data, 0, (uint)data.Length);
|
||||
}
|
||||
else
|
||||
{
|
||||
var len = (ulong)data.LongLength;
|
||||
|
||||
if (len == 0)
|
||||
{
|
||||
return new byte[1] { (byte) identifier };
|
||||
}
|
||||
else if (len <= 0xFF)
|
||||
{
|
||||
var rt = new byte[2 + len];
|
||||
rt[0] = (byte)((byte)identifier | 0x8);
|
||||
rt[1] = (byte)len;
|
||||
Buffer.BlockCopy(data, 0, rt, 2, (int)len);
|
||||
return rt;
|
||||
}
|
||||
else if (len <= 0xFF_FF)
|
||||
{
|
||||
var rt = new byte[3 + len];
|
||||
rt[0] = (byte)((byte)identifier | 0x10);
|
||||
rt[1] = (byte)((len >> 8) & 0xFF);
|
||||
rt[2] = (byte)(len & 0xFF);
|
||||
Buffer.BlockCopy(data, 0, rt, 3, (int)len);
|
||||
return rt;
|
||||
}
|
||||
else if (len <= 0xFF_FF_FF)
|
||||
{
|
||||
var rt = new byte[4 + len];
|
||||
rt[0] = (byte)((byte)identifier | 0x18);
|
||||
rt[1] = (byte)((len >> 16) & 0xFF);
|
||||
rt[2] = (byte)((len >> 8) & 0xFF);
|
||||
rt[3] = (byte)(len & 0xFF);
|
||||
Buffer.BlockCopy(data, 0, rt, 4, (int)len);
|
||||
return rt;
|
||||
}
|
||||
else if (len <= 0xFF_FF_FF_FF)
|
||||
{
|
||||
var rt = new byte[5 + len];
|
||||
rt[0] = (byte)((byte)identifier | 0x20);
|
||||
rt[1] = (byte)((len >> 24) & 0xFF);
|
||||
rt[2] = (byte)((len >> 16) & 0xFF);
|
||||
rt[3] = (byte)((len >> 8) & 0xFF);
|
||||
rt[4] = (byte)(len & 0xFF);
|
||||
Buffer.BlockCopy(data, 0, rt, 5, (int)len);
|
||||
return rt;
|
||||
}
|
||||
else if (len <= 0xFF_FF_FF_FF_FF)
|
||||
{
|
||||
var rt = new byte[6 + len];
|
||||
rt[0] = (byte)((byte)identifier | 0x28);
|
||||
rt[1] = (byte)((len >> 32) & 0xFF);
|
||||
rt[2] = (byte)((len >> 24) & 0xFF);
|
||||
rt[3] = (byte)((len >> 16) & 0xFF);
|
||||
rt[4] = (byte)((len >> 8) & 0xFF);
|
||||
rt[5] = (byte)(len & 0xFF);
|
||||
Buffer.BlockCopy(data, 0, rt, 6, (int)len);
|
||||
return rt;
|
||||
}
|
||||
else if (len <= 0xFF_FF_FF_FF_FF_FF)
|
||||
{
|
||||
var rt = new byte[7 + len];
|
||||
rt[0] = (byte)((byte)identifier | 0x30);
|
||||
rt[1] = (byte)((len >> 40) & 0xFF);
|
||||
rt[2] = (byte)((len >> 32) & 0xFF);
|
||||
rt[3] = (byte)((len >> 24) & 0xFF);
|
||||
rt[4] = (byte)((len >> 16) & 0xFF);
|
||||
rt[5] = (byte)((len >> 8) & 0xFF);
|
||||
rt[6] = (byte)(len & 0xFF);
|
||||
Buffer.BlockCopy(data, 0, rt, 7, (int)len);
|
||||
return rt;
|
||||
}
|
||||
else //if (len <= 0xFF_FF_FF_FF_FF_FF_FF)
|
||||
{
|
||||
var rt = new byte[8 + len];
|
||||
rt[0] = (byte)((byte)identifier | 0x38);
|
||||
rt[1] = (byte)((len >> 48) & 0xFF);
|
||||
rt[2] = (byte)((len >> 40) & 0xFF);
|
||||
rt[3] = (byte)((len >> 32) & 0xFF);
|
||||
rt[4] = (byte)((len >> 24) & 0xFF);
|
||||
rt[5] = (byte)((len >> 16) & 0xFF);
|
||||
rt[6] = (byte)((len >> 8) & 0xFF);
|
||||
rt[7] = (byte)(len & 0xFF);
|
||||
Buffer.BlockCopy(data, 0, rt, 8, (int)len);
|
||||
return rt;
|
||||
}
|
||||
//else // if (len <= 0xFF_FF_FF_FF_FF_FF_FF_FF)
|
||||
//{
|
||||
// var rt = new byte[9 + len];
|
||||
// rt[0] = (byte)((byte)identifier | 0x8);
|
||||
// rt[1] = (byte)((len >> 56) & 0xFF);
|
||||
// rt[2] = (byte)((len >> 48) & 0xFF);
|
||||
// rt[3] = (byte)((len >> 40) & 0xFF);
|
||||
// rt[4] = (byte)((len >> 32) & 0xFF);
|
||||
// rt[5] = (byte)((len >> 24) & 0xFF);
|
||||
// rt[6] = (byte)((len >> 16) & 0xFF);
|
||||
// rt[7] = (byte)((len >> 8) & 0xFF);
|
||||
// rt[8] = (byte)(len & 0xFF);
|
||||
// Buffer.BlockCopy(data, 0, rt, 9, (int)len);
|
||||
// return rt;
|
||||
//}
|
||||
|
||||
|
||||
// // add length
|
||||
// int bytes = 1;
|
||||
//for (var i = 56; i > 0; i -= 8, bytes++)
|
||||
// if (len <= (0xFF_FF_FF_FF_FF_FF_FF_FF >> i))
|
||||
// break;
|
||||
|
||||
//var rt = new byte[1 + bytes + data.Length];
|
||||
//rt[0] = (byte)((byte)identifier | (bytes << 3));
|
||||
|
||||
//for (var i = 1; i <= bytes; i++)
|
||||
// rt[i] = data.LongLength >> i * 8;
|
||||
|
||||
//Buffer.BlockCopy(data, 0, rt, 1 + bytes, data.Length);
|
||||
}
|
||||
}
|
||||
|
||||
public static (ulong, TransmissionType?) Parse(byte[] data, uint offset, uint ends)
|
||||
{
|
||||
var h = data[offset++];
|
||||
|
||||
var cls = (TransmissionTypeClass)(h >> 6);
|
||||
|
||||
if (cls == TransmissionTypeClass.Fixed)
|
||||
{
|
||||
var exp = (h & 0x38) >> 3;
|
||||
|
||||
if (exp == 0)
|
||||
return (1, new TransmissionType((TransmissionTypeIdentifier)h, cls, h & 0x7, 0, (byte)exp));
|
||||
|
||||
ulong cl = (ulong)(1 << (exp -1));
|
||||
|
||||
if (ends - offset < cl)
|
||||
return (cl - (ends - offset), null);
|
||||
|
||||
//offset += (uint)cl;
|
||||
|
||||
return (1 + cl, new TransmissionType((TransmissionTypeIdentifier)h, cls, h & 0x7, offset, cl, (byte)exp));
|
||||
}
|
||||
else
|
||||
{
|
||||
ulong cll = (ulong)(h >> 3) & 0x7;
|
||||
|
||||
if (ends - offset < cll)
|
||||
return (cll - (ends - offset), null);
|
||||
|
||||
ulong cl = 0;
|
||||
|
||||
for (uint i = 0; i < cll; i++)
|
||||
cl = cl << 8 | data[offset++];
|
||||
|
||||
if (ends - offset < cl)
|
||||
return (cl - (ends - offset), null);
|
||||
|
||||
return (1 + cl + cll, new TransmissionType((TransmissionTypeIdentifier)(h & 0xC7), cls, h & 0x7, offset, cl));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user