diff --git a/Esiur/Data/Codec.cs b/Esiur/Data/Codec.cs index 7788e1e..a70cbb3 100644 --- a/Esiur/Data/Codec.cs +++ b/Esiur/Data/Codec.cs @@ -180,13 +180,13 @@ public static class Codec /// DistributedConnection is required in case a structure in the array holds items at the other end. /// DataType, in case the data is not prepended with DataType /// Value - public static (uint, object) ParseAsync(byte[] data, uint offset, DistributedConnection connection, uint[] requestSequence, TransmissionDataUnit? dataType = null) + public static (uint, object) ParseAsync(byte[] data, uint offset, DistributedConnection connection, uint[] requestSequence, ParsedTDU? dataType = null) { uint len = 0; if (dataType == null) { - (var longLen, dataType) = TransmissionDataUnit.Parse(data, offset, (uint)data.Length); + (var longLen, dataType) = ParsedTDU.Parse(data, offset, (uint)data.Length); if (dataType == null) throw new NullReferenceException("DataType can't be parsed."); @@ -201,11 +201,11 @@ public static class Codec //Console.WriteLine("Parsing " + tt.Class + " " + tt.Identifier); - if (tt.Class == TransmissionDataUnitClass.Fixed) + if (tt.Class == TDUClass.Fixed) { return (len, FixedAsyncParsers[tt.Exponent][tt.Index](data, dataType.Value.Offset, (uint)tt.ContentLength, connection, requestSequence)); } - else if (tt.Class == TransmissionDataUnitClass.Dynamic) + else if (tt.Class == TDUClass.Dynamic) { return (len, DynamicAsyncParsers[tt.Index](data, dataType.Value.Offset, (uint)tt.ContentLength, connection, requestSequence)); } @@ -215,13 +215,13 @@ public static class Codec } } - public static (uint, object) ParseSync(byte[] data, uint offset, Warehouse warehouse, TransmissionDataUnit? dataType = null) + public static (uint, object) ParseSync(byte[] data, uint offset, Warehouse warehouse, ParsedTDU? dataType = null) { uint len = 0; if (dataType == null) { - (var longLen, dataType) = TransmissionDataUnit.Parse(data, offset, (uint)data.Length); + (var longLen, dataType) = ParsedTDU.Parse(data, offset, (uint)data.Length); if (dataType == null) throw new NullReferenceException("DataType can't be parsed."); @@ -234,11 +234,11 @@ public static class Codec var tt = dataType.Value; - if (tt.Class == TransmissionDataUnitClass.Fixed) + if (tt.Class == TDUClass.Fixed) { return (len, FixedParsers[tt.Exponent][tt.Index](data, dataType.Value.Offset, (uint)tt.ContentLength, warehouse)); } - else if (tt.Class == TransmissionDataUnitClass.Dynamic) + else if (tt.Class == TDUClass.Dynamic) { return (len, DynamicParsers[tt.Index](data, dataType.Value.Offset, (uint)tt.ContentLength, warehouse)); } @@ -265,7 +265,7 @@ public static class Codec return false; } - public delegate TransmissionDataUnit Composer(object value, Warehouse warehouse, DistributedConnection connection); + public delegate TDU Composer(object value, Warehouse warehouse, DistributedConnection connection); public static Dictionary Composers = new Dictionary() { @@ -338,11 +338,11 @@ public static class Codec }; - internal static TransmissionDataUnit + internal static TDU ComposeInternal(object valueOrSource, Warehouse warehouse, DistributedConnection connection) { if (valueOrSource == null) - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.Null, null, 0, 0); + return new TDU(TDUIdentifier.Null); var type = valueOrSource.GetType(); @@ -369,7 +369,7 @@ public static class Codec valueOrSource = ((IUserType)valueOrSource).Get(); if (valueOrSource == null) - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.Null, null, 0, 0); + return new TDU(TDUIdentifier.Null); type = valueOrSource.GetType(); @@ -445,7 +445,7 @@ public static class Codec } - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.Null, null, 0, 0); + return new TDU(TDUIdentifier.Null); } @@ -460,7 +460,7 @@ public static class Codec public static byte[] Compose(object valueOrSource, Warehouse warehouse, DistributedConnection connection)//, bool prependType = true) { var tdu = ComposeInternal(valueOrSource, warehouse, connection); - return tdu.Compose(); + return tdu.Composed; } public static bool IsAnonymous(Type type) diff --git a/Esiur/Data/DataDeserializer.cs b/Esiur/Data/DataDeserializer.cs index 7b19524..f7946f8 100644 --- a/Esiur/Data/DataDeserializer.cs +++ b/Esiur/Data/DataDeserializer.cs @@ -680,13 +680,13 @@ public static class DataDeserializer //TransmissionDataUnitIdentifier? previous = null; //byte[]? previousUUID = null; - TransmissionDataUnit? previous = null; + ParsedTDU? previous = null; while (length > 0) { - var (longLen, dataType) = TransmissionDataUnit.Parse(data, offset, (uint)data.Length); + var (longLen, dataType) = ParsedTDU.Parse(data, offset, (uint)data.Length); - if (dataType.Value.Identifier == TransmissionDataUnitIdentifier.Same) + if (dataType.Value.Identifier == TDUIdentifier.Same) { // Add UUID } diff --git a/Esiur/Data/DataSerializer.cs b/Esiur/Data/DataSerializer.cs index a5b7c52..a557df4 100644 --- a/Esiur/Data/DataSerializer.cs +++ b/Esiur/Data/DataSerializer.cs @@ -14,116 +14,116 @@ public static class DataSerializer { public delegate byte[] Serializer(object value); - public static unsafe TransmissionDataUnit Int32Composer(object value, Warehouse warehouse, DistributedConnection connection) + public static unsafe TDU Int32Composer(object value, Warehouse warehouse, DistributedConnection connection) { var v = (int)value; var rt = new byte[4]; fixed (byte* ptr = rt) *((int*)ptr) = v; - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.Int32, rt, 0, 4); + return new TDU(TDUIdentifier.Int32, rt, 4); } - public static unsafe TransmissionDataUnit UInt32Composer(object value, Warehouse warehouse, DistributedConnection connection) + public static unsafe TDU UInt32Composer(object value, Warehouse warehouse, DistributedConnection connection) { var v = (uint)value; var rt = new byte[4]; fixed (byte* ptr = rt) *((uint*)ptr) = v; - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.UInt32, rt, 0, 4); + return new TDU(TDUIdentifier.UInt32, rt, 4); } - public static unsafe TransmissionDataUnit Int16Composer(object value, Warehouse warehouse, DistributedConnection connection) + public static unsafe TDU Int16Composer(object value, Warehouse warehouse, DistributedConnection connection) { var v = (short)value; var rt = new byte[2]; fixed (byte* ptr = rt) *((short*)ptr) = v; - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.Int16, rt, 0, 2); + return new TDU(TDUIdentifier.Int16, rt, 2); } - public static unsafe TransmissionDataUnit UInt16Composer(object value, Warehouse warehouse, DistributedConnection connection) + public static unsafe TDU UInt16Composer(object value, Warehouse warehouse, DistributedConnection connection) { var v = (ushort)value; var rt = new byte[2]; fixed (byte* ptr = rt) *((ushort*)ptr) = v; - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.UInt16, rt, 0, 2); + return new TDU(TDUIdentifier.UInt16, rt, 2); } - public static unsafe TransmissionDataUnit Float32Composer(object value, Warehouse warehouse, DistributedConnection connection) + public static unsafe TDU Float32Composer(object value, Warehouse warehouse, DistributedConnection connection) { var v = (float)value; var rt = new byte[4]; fixed (byte* ptr = rt) *((float*)ptr) = v; - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.Float32, rt, 0, 4); + return new TDU(TDUIdentifier.Float32, rt, 4); } - public static unsafe TransmissionDataUnit Float64Composer(object value, Warehouse warehouse, DistributedConnection connection) + public static unsafe TDU Float64Composer(object value, Warehouse warehouse, DistributedConnection connection) { var v = (double)value; var rt = new byte[8]; fixed (byte* ptr = rt) *((double*)ptr) = v; - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.Float64, rt, 0, 8); + return new TDU(TDUIdentifier.Float64, rt, 8); } - public static unsafe TransmissionDataUnit Int64Composer(object value, Warehouse warehouse, DistributedConnection connection) + public static unsafe TDU Int64Composer(object value, Warehouse warehouse, DistributedConnection connection) { var v = (long)value; var rt = new byte[8]; fixed (byte* ptr = rt) *((long*)ptr) = v; - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.Int64, rt, 0, 8); + return new TDU(TDUIdentifier.Int64, rt, 8); } - public static unsafe TransmissionDataUnit UIn64Composer(object value, Warehouse warehouse, DistributedConnection connection) + public static unsafe TDU UIn64Composer(object value, Warehouse warehouse, DistributedConnection connection) { var v = (ulong)value; var rt = new byte[8]; fixed (byte* ptr = rt) *((ulong*)ptr) = v; - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.UInt64, rt, 0, 8); + return new TDU(TDUIdentifier.UInt64, rt, 8); } - public static unsafe TransmissionDataUnit DateTimeComposer(object value, Warehouse warehouse, DistributedConnection connection) + public static unsafe TDU 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 new TransmissionDataUnit(TransmissionDataUnitIdentifier.DateTime, rt, 0, 8); + return new TDU(TDUIdentifier.DateTime, rt, 8); } - public static unsafe TransmissionDataUnit Decimal128Composer(object value, Warehouse warehouse, DistributedConnection connection) + public static unsafe TDU Decimal128Composer(object value, Warehouse warehouse, DistributedConnection connection) { var v = (decimal)value; var rt = new byte[16]; fixed (byte* ptr = rt) *((decimal*)ptr) = v; - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.Decimal128, rt, 0, 16); + return new TDU(TDUIdentifier.Decimal128, rt, 16); } - public static TransmissionDataUnit StringComposer(object value, Warehouse warehouse, DistributedConnection connection) + public static TDU StringComposer(object value, Warehouse warehouse, DistributedConnection connection) { var b = Encoding.UTF8.GetBytes((string)value); - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.String, b, 0, (uint)b.Length); + return new TDU(TDUIdentifier.String, b, (uint)b.Length); } - public static TransmissionDataUnit EnumComposer(object value, Warehouse warehouse, DistributedConnection connection) + public static TDU EnumComposer(object value, Warehouse warehouse, DistributedConnection connection) { if (value == null) - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.Null, null, 0, 0); + return new TDU(TDUIdentifier.Null, null, 0); //var warehouse = connection?.Instance?.Warehouse ?? connection?.Server?.Instance?.Warehouse; //if (warehouse == null) @@ -136,73 +136,73 @@ public static class DataSerializer var ct = template.Constants.FirstOrDefault(x => x.Value.Equals(intVal)); if (ct == null) - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.Null, null, 0, 0); + return new TDU(TDUIdentifier.Null, null, 0); - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.TypedEnum, - new byte[] { ct.Index }, 0, 1, template.ClassId.Data); + return new TDU(TDUIdentifier.TypedEnum, + new byte[] { ct.Index }, 1, template.ClassId.Data); } - public static TransmissionDataUnit UInt8Composer(object value, Warehouse warehouse, DistributedConnection connection) + public static TDU UInt8Composer(object value, Warehouse warehouse, DistributedConnection connection) { - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.UInt8, - new byte[] { (byte)value }, 0, 1); + return new TDU(TDUIdentifier.UInt8, + new byte[] { (byte)value }, 1); } - public static TransmissionDataUnit Int8Composer(object value, Warehouse warehouse, DistributedConnection connection) + public static TDU Int8Composer(object value, Warehouse warehouse, DistributedConnection connection) { - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.Int8, - new byte[] { (byte)(sbyte)value }, 0, 1); + return new TDU(TDUIdentifier.Int8, + new byte[] { (byte)(sbyte)value }, 1); } - public static TransmissionDataUnit Char8Composer(object value, Warehouse warehouse, DistributedConnection connection) + public static TDU Char8Composer(object value, Warehouse warehouse, DistributedConnection connection) { - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.Int8, - new byte[] { (byte)(char)value }, 0, 1); + return new TDU(TDUIdentifier.Int8, + new byte[] { (byte)(char)value }, 1); } - public static unsafe TransmissionDataUnit Char16Composer(object value, Warehouse warehouse, DistributedConnection connection) + public static unsafe TDU Char16Composer(object value, Warehouse warehouse, DistributedConnection connection) { var v = (char)value; var rt = new byte[2]; fixed (byte* ptr = rt) *((char*)ptr) = v; - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.Char16, rt, 0, 2); + return new TDU(TDUIdentifier.Char16, rt, 2); } - public static TransmissionDataUnit BoolComposer(object value, Warehouse warehouse, DistributedConnection connection) + public static TDU BoolComposer(object value, Warehouse warehouse, DistributedConnection connection) { if ((bool)value) { - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.True, null, 0, 0); + return new TDU(TDUIdentifier.True); } else { - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.True, null, 0, 0); + return new TDU(TDUIdentifier.True); } } - public static TransmissionDataUnit NotModifiedComposer(object value, Warehouse warehouse, DistributedConnection connection) + public static TDU NotModifiedComposer(object value, Warehouse warehouse, DistributedConnection connection) { - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.NotModified, null, 0, 0); + return new TDU(TDUIdentifier.NotModified); } - public static TransmissionDataUnit RawDataComposerFromArray(object value, Warehouse warehouse, DistributedConnection connection) + public static TDU RawDataComposerFromArray(object value, Warehouse warehouse, DistributedConnection connection) { var b = (byte[])value; - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.RawData, b, 0, (uint)b.Length); + return new TDU(TDUIdentifier.RawData, b, (uint)b.Length); } - public static TransmissionDataUnit RawDataComposerFromList(dynamic value, Warehouse warehouse, DistributedConnection connection) + public static TDU RawDataComposerFromList(dynamic value, Warehouse warehouse, DistributedConnection connection) { var b = value as List; - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.RawData, b.ToArray(), 0, (uint)b.Count); + return new TDU(TDUIdentifier.RawData, b.ToArray(), (uint)b.Count); } - //public static (TransmissionDataUnitIdentifier, byte[]) ListComposerFromArray(dynamic value, DistributedConnection connection) + //public static (TDUIdentifier, byte[]) ListComposerFromArray(dynamic value, DistributedConnection connection) //{ // var rt = new List(); // var array = (object[])value; @@ -210,31 +210,31 @@ public static class DataSerializer // for (var i = 0; i < array.Length; i++) // rt.AddRange(Codec.Compose(array[i], connection)); - // return (TransmissionDataUnitIdentifier.List, rt.ToArray()); + // return (TDUIdentifier.List, rt.ToArray()); //} - public static TransmissionDataUnit ListComposer(object value, Warehouse warehouse, DistributedConnection connection) + public static TDU ListComposer(object value, Warehouse warehouse, DistributedConnection connection) { var rt = ArrayComposer((IEnumerable)value, warehouse, connection); if (rt == null) - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.Null, new byte[0], 0, 0); + return new TDU(TDUIdentifier.Null, new byte[0], 0); else - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.List, rt, 0, (uint)rt.Length); + return new TDU(TDUIdentifier.List, rt, (uint)rt.Length); } - public static TransmissionDataUnit TypedListComposer(IEnumerable value, Type type, Warehouse warehouse, DistributedConnection connection) + public static TDU TypedListComposer(IEnumerable value, Type type, Warehouse warehouse, DistributedConnection connection) { var composed = ArrayComposer((IEnumerable)value, warehouse, connection); if (composed == null) - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.Null, new byte[0], 0, 0); + return new TDU(TDUIdentifier.Null, new byte[0], 0); var metadata = RepresentationType.FromType(type).Compose(); - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.TypedList, composed, 0, + return new TDU(TDUIdentifier.TypedList, composed, (uint)composed.Length, metadata); } @@ -249,10 +249,10 @@ public static class DataSerializer // .ToArray(); //} - public static TransmissionDataUnit PropertyValueArrayComposer(object value, Warehouse warehouse, DistributedConnection connection) + public static TDU PropertyValueArrayComposer(object value, Warehouse warehouse, DistributedConnection connection) { if (value == null) - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.Null, new byte[0], 0, 0); + return new TDU(TDUIdentifier.Null, new byte[0], 0); var rt = new List(); var ar = value as PropertyValue[]; @@ -264,14 +264,14 @@ public static class DataSerializer rt.AddRange(Codec.Compose(pv.Value, warehouse, connection)); } - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.RawData, rt.ToArray(), 0, + return new TDU(TDUIdentifier.RawData, rt.ToArray(), (uint)rt.Count); } - public static TransmissionDataUnit TypedMapComposer(object value, Type keyType, Type valueType, Warehouse warehouse, DistributedConnection connection) + public static TDU TypedMapComposer(object value, Type keyType, Type valueType, Warehouse warehouse, DistributedConnection connection) { if (value == null) - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.Null, new byte[0], 0, 0); + return new TDU(TDUIdentifier.Null, new byte[0], 0); var kt = RepresentationType.FromType(keyType).Compose(); var vt = RepresentationType.FromType(valueType).Compose(); @@ -284,13 +284,13 @@ public static class DataSerializer rt.AddRange(Codec.Compose(el, warehouse, connection)); - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.TypedMap, rt.ToArray(), 0, (uint)rt.Count, + return new TDU(TDUIdentifier.TypedMap, rt.ToArray(), (uint)rt.Count, DC.Combine(kt, 0, (uint)kt.Length, vt, 0, (uint)vt.Length)); } - public static TransmissionDataUnit TypedDictionaryComposer(object value, Type keyType, Type valueType, Warehouse warehouse, DistributedConnection connection) + public static TDU TypedDictionaryComposer(object value, Type keyType, Type valueType, Warehouse warehouse, DistributedConnection connection) { if (value == null) - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.Null, null, 0, 0); + return new TDU(TDUIdentifier.Null, null, 0); var kt = RepresentationType.FromType(keyType).Compose(); var vt = RepresentationType.FromType(valueType).Compose(); @@ -313,7 +313,7 @@ public static class DataSerializer rt.AddRange(Codec.Compose(el, warehouse, connection)); - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.TypedMap, rt.ToArray(), 0, (uint)rt.Count, + return new TDU(TDUIdentifier.TypedMap, rt.ToArray(), (uint)rt.Count, DC.Combine(kt, 0, (uint)kt.Length, vt, 0, (uint)vt.Length)); } @@ -324,19 +324,18 @@ public static class DataSerializer var rt = new List(); - TransmissionDataUnit? previous = null; + TDU? previous = null; foreach (var i in value) { var tdu = Codec.ComposeInternal(i, warehouse, connection); - if (tdu.MatchType(previous.Value)) + if (previous != null && tdu.MatchType(previous.Value)) { - rt.AddRange(TransmissionDataUnit.Compose(TransmissionDataUnitIdentifier.NotModified, - tdu.Data, null)); + rt.Add((byte)TDUIdentifier.NotModified); } else { - rt.AddRange(tdu.Compose()); + rt.AddRange(tdu.Composed); } previous = tdu; @@ -345,36 +344,36 @@ public static class DataSerializer return rt.ToArray(); } - public static TransmissionDataUnit ResourceListComposer(object value, Warehouse warehouse, DistributedConnection connection) + public static TDU ResourceListComposer(object value, Warehouse warehouse, DistributedConnection connection) { if (value == null) - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.Null, new byte[0], 0, 0); + return new TDU(TDUIdentifier.Null, new byte[0], 0); var composed = ArrayComposer((IEnumerable)value, warehouse, connection); - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.ResourceList, composed, 0, + return new TDU(TDUIdentifier.ResourceList, composed, (uint)composed.Length); } - public static TransmissionDataUnit RecordListComposer(object value, Warehouse warehouse, DistributedConnection connection) + public static TDU RecordListComposer(object value, Warehouse warehouse, DistributedConnection connection) { if (value == null) - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.Null, new byte[0], 0, 0); + return new TDU(TDUIdentifier.Null, new byte[0], 0); var composed = ArrayComposer((IEnumerable)value, warehouse, connection); - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.RecordList, - composed, 0, (uint)composed.Length); + return new TDU(TDUIdentifier.RecordList, + composed, (uint)composed.Length); } - public static unsafe TransmissionDataUnit ResourceComposer(object value, Warehouse warehouse, DistributedConnection connection) + public static unsafe TDU ResourceComposer(object value, Warehouse warehouse, DistributedConnection connection) { var resource = (IResource)value; if (resource.Instance == null || resource.Instance.IsDestroyed) { - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.Null, null, 0, 0); + return new TDU(TDUIdentifier.Null, null, 0); } if (Codec.IsLocalResource(resource, connection)) @@ -382,21 +381,21 @@ public static class DataSerializer var rid = (resource as DistributedResource).DistributedResourceInstanceId; if (rid <= 0xFF) - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.LocalResource8, new byte[] { (byte)rid }, 0, 1); + return new TDU(TDUIdentifier.LocalResource8, new byte[] { (byte)rid }, 1); else if (rid <= 0xFFFF) { var rt = new byte[2]; fixed (byte* ptr = rt) *((ushort*)ptr) = (ushort)rid; - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.LocalResource16, rt, 0, 2); + return new TDU(TDUIdentifier.LocalResource16, rt, 2); } else { var rt = new byte[4]; fixed (byte* ptr = rt) *((uint*)ptr) = rid; - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.LocalResource32, rt, 0, 4); + return new TDU(TDUIdentifier.LocalResource32, rt, 4); } } else @@ -408,29 +407,29 @@ public static class DataSerializer var rid = resource.Instance.Id; if (rid <= 0xFF) - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.RemoteResource8, new byte[] { (byte)rid }, 0, 1); + return new TDU(TDUIdentifier.RemoteResource8, new byte[] { (byte)rid }, 1); else if (rid <= 0xFFFF) { var rt = new byte[2]; fixed (byte* ptr = rt) *((ushort*)ptr) = (ushort)rid; - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.RemoteResource16, rt, 0, 2); + return new TDU(TDUIdentifier.RemoteResource16, rt, 2); } else { var rt = new byte[4]; fixed (byte* ptr = rt) *((uint*)ptr) = rid; - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.RemoteResource32, rt, 0, 4); + return new TDU(TDUIdentifier.RemoteResource32, rt, 4); } } } - public static unsafe TransmissionDataUnit MapComposer(object value, Warehouse warehouse, DistributedConnection connection) + public static unsafe TDU MapComposer(object value, Warehouse warehouse, DistributedConnection connection) { if (value == null) - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.Null, new byte[0], 0, 1); + return new TDU(TDUIdentifier.Null, new byte[0], 1); var rt = new List(); var map = (IMap)value; @@ -438,16 +437,16 @@ public static class DataSerializer foreach (var el in map.Serialize()) rt.AddRange(Codec.Compose(el, warehouse, connection)); - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.Map, rt.ToArray(), 0, (uint)rt.Count); + return new TDU(TDUIdentifier.Map, rt.ToArray(), (uint)rt.Count); } - public static unsafe TransmissionDataUnit UUIDComposer(object value, Warehouse warehouse, DistributedConnection connection) + public static unsafe TDU UUIDComposer(object value, Warehouse warehouse, DistributedConnection connection) { - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.UUID, ((UUID)value).Data, 0, 16); + return new TDU(TDUIdentifier.UUID, ((UUID)value).Data, 16); } - public static unsafe TransmissionDataUnit RecordComposer(object value, Warehouse warehouse, DistributedConnection connection) + public static unsafe TDU RecordComposer(object value, Warehouse warehouse, DistributedConnection connection) { var rt = new List();// BinaryList(); var record = (IRecord)value; @@ -461,7 +460,7 @@ public static class DataSerializer rt.AddRange(Codec.Compose(propValue, warehouse, connection)); } - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.Record, rt.ToArray(), 0, + return new TDU(TDUIdentifier.Record, rt.ToArray(), (uint)rt.Count, template.ClassId.Data); } @@ -481,10 +480,10 @@ public static class DataSerializer return rt.ToArray(); } - public static TransmissionDataUnit TupleComposer(object value, Warehouse warehouse, DistributedConnection connection) + public static TDU TupleComposer(object value, Warehouse warehouse, DistributedConnection connection) { if (value == null) - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.Null, new byte[0], 0, 0); + return new TDU(TDUIdentifier.Null, new byte[0], 0); var fields = value.GetType().GetFields(); var list = fields.Select(x => x.GetValue(value)).ToArray(); @@ -498,10 +497,10 @@ public static class DataSerializer var composed = ArrayComposer(list, warehouse, connection); if (composed == null) - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.Null, new byte[0], 0, 0); + return new TDU(TDUIdentifier.Null, new byte[0], 0); else { - return new TransmissionDataUnit(TransmissionDataUnitIdentifier.TypedTuple, composed, 0, + return new TDU(TDUIdentifier.TypedTuple, composed, (uint)composed.Length, metadata.ToArray()); } } diff --git a/Esiur/Data/ParsedTDU.cs b/Esiur/Data/ParsedTDU.cs new file mode 100644 index 0000000..ffe580a --- /dev/null +++ b/Esiur/Data/ParsedTDU.cs @@ -0,0 +1,123 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Esiur.Data +{ + public struct ParsedTDU + { + public TDUIdentifier Identifier; + public int Index; + public TDUClass Class; + public uint Offset; + public ulong ContentLength; + public byte[] Data; + public byte Exponent; + public ulong TotalLength; + public byte[] Metadata; + + public static (ulong, ParsedTDU?) Parse(byte[] data, uint offset, uint ends) + { + var h = data[offset++]; + + var cls = (TDUClass)(h >> 6); + + if (cls == TDUClass.Fixed) + { + var exp = (h & 0x38) >> 3; + + if (exp == 0) + return (1, new ParsedTDU() + { + Identifier = (TDUIdentifier)h, + Data = data, + Offset = offset, + Class = cls, + Exponent = (byte)exp, + Index = (byte)h & 0x7, + ContentLength = 0, + TotalLength = 1, + }); + + ulong cl = (ulong)(1 << (exp - 1)); + + if (ends - offset < cl) + return (cl - (ends - offset), null); + + //offset += (uint)cl; + + return (1 + cl, new ParsedTDU() + { + Identifier = (TDUIdentifier)h, + Data = data, + Offset = offset, + Class = cls, + ContentLength = cl, + TotalLength = 1 + cl, + Exponent = (byte)exp, + Index = (byte)h & 0x7, + }); + } + else if (cls == TDUClass.Typed) + { + 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); + + var metaData = DC.Clip(data, offset + 1, data[offset]); + offset += data[offset]; + + + return (1 + cl + cll, new ParsedTDU() + { + Identifier = (TDUIdentifier)(h & 0xC7), + Data = data, + Offset = offset, + Class = cls, + ContentLength = cl, + TotalLength = 1 + cl + cll, + Index = (byte)h & 0x7, + Metadata = metaData, + }); + } + 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 ParsedTDU() + { + Identifier = (TDUIdentifier)(h & 0xC7), + Data = data, + Offset = offset, + Class = cls, + ContentLength = cl, + TotalLength = 1 + cl + cll, + Index = (byte)h & 0x7 + }); + } + } + + } +} diff --git a/Esiur/Data/RepresentationType.cs b/Esiur/Data/RepresentationType.cs index 2146d17..c95be52 100644 --- a/Esiur/Data/RepresentationType.cs +++ b/Esiur/Data/RepresentationType.cs @@ -76,30 +76,30 @@ namespace Esiur.Data RepresentationTypeIdentifier.TypedResource }; - static Map typesMap = new Map() + static Map typesMap = new Map() { - [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, + [TDUIdentifier.UInt8] = RepresentationTypeIdentifier.UInt8, + [TDUIdentifier.Int8] = RepresentationTypeIdentifier.Int8, + [TDUIdentifier.UInt16] = RepresentationTypeIdentifier.UInt16, + [TDUIdentifier.Int16] = RepresentationTypeIdentifier.Int16, + [TDUIdentifier.UInt32] = RepresentationTypeIdentifier.UInt32, + [TDUIdentifier.Int32] = RepresentationTypeIdentifier.Int32, + [TDUIdentifier.UInt64] = RepresentationTypeIdentifier.UInt64, + [TDUIdentifier.Int64] = RepresentationTypeIdentifier.Int64, + [TDUIdentifier.UInt128] = RepresentationTypeIdentifier.UInt128, + [TDUIdentifier.Int128] = RepresentationTypeIdentifier.Int128, + [TDUIdentifier.Char8] = RepresentationTypeIdentifier.Char, + [TDUIdentifier.DateTime] = RepresentationTypeIdentifier.DateTime, + [TDUIdentifier.Float32] = RepresentationTypeIdentifier.Float32, + [TDUIdentifier.Float64] = RepresentationTypeIdentifier.Float64, + [TDUIdentifier.Decimal128] = RepresentationTypeIdentifier.Decimal, + [TDUIdentifier.False] = RepresentationTypeIdentifier.Bool, + [TDUIdentifier.True] = RepresentationTypeIdentifier.Bool, + [TDUIdentifier.Map] = RepresentationTypeIdentifier.Map, + [TDUIdentifier.List] = RepresentationTypeIdentifier.List, + [TDUIdentifier.RawData] = RepresentationTypeIdentifier.RawData, + [TDUIdentifier.Record] = RepresentationTypeIdentifier.Record, + [TDUIdentifier.String] = RepresentationTypeIdentifier.String, }; diff --git a/Esiur/Data/TDU.cs b/Esiur/Data/TDU.cs new file mode 100644 index 0000000..39798c1 --- /dev/null +++ b/Esiur/Data/TDU.cs @@ -0,0 +1,287 @@ +using Esiur.Net.IIP; +using System; +using System.Collections.Generic; +using System.Text; +using System.Xml.Linq; +using System.Xml.Schema; +using static Esiur.Data.Codec; + +namespace Esiur.Data; + +// Transmission Data Unit +public struct TDU +{ + public TDUIdentifier Identifier; + //public int Index; + public TDUClass Class; + //public ulong ContentLength; + + public byte[] Composed; + //public uint Offset; + + public byte[] Metadata; + + //public ulong Size + //{ + // get + // { + // if (TotalSize != ulong.MaxValue) + // return TotalSize; + // else + // { + // if (ContentLength <= 0xFF) + // return 2 + ContentLength; + // else if (ContentLength <= 0xFF_FF) + // return 3 + ContentLength; + // else if (ContentLength <= 0xFF_FF_FF) + // return 4 + ContentLength; + // else if (ContentLength <= 0xFF_FF_FF_FF) + // return 5 + ContentLength; + // else if (ContentLength <= 0xFF_FF_FF_FF_FF) + // return 6 + ContentLength; + // else if (ContentLength <= 0xFF_FF_FF_FF_FF_FF) + // return 7 + ContentLength; + // else if (ContentLength <= 0xFF_FF_FF_FF_FF_FF_FF) + // return 8 + ContentLength; + // else //if (ContentLength <= 0xFF_FF_FF_FF_FF_FF_FF_FF) + // return 9 + ContentLength; + // } + // } + //} + + //private ulong TotalSize; + + public TDU() + { + + } + + public TDU(TDUIdentifier identifier) + { + Identifier = identifier; + } + + public TDU(TDUIdentifier identifier, + byte[] data, ulong length, byte[] metadata = null) + { + Identifier = identifier; + //Index = (byte)identifier & 0x7; + Class = (TDUClass)((byte)identifier >> 6); + Metadata = metadata; + + + if (Class == TDUClass.Fixed) + { + Composed = DC.Combine(new byte[] { (byte)Identifier }, 0, 1, data, 0, (uint)length); + } + else if (Class == TDUClass.Dynamic + || Class == TDUClass.Extension) + { + + if (length == 0) + { + Composed = new byte[1] { (byte)Identifier }; + } + else if (length <= 0xFF) + { + Composed = new byte[2 + length]; + Composed[0] = (byte)((byte)Identifier | 0x8); + Composed[1] = (byte)length; + Buffer.BlockCopy(data, 0, Composed, 2, (int)length); + } + else if (length <= 0xFF_FF) + { + var Composed = new byte[3 + length]; + Composed[0] = (byte)((byte)Identifier | 0x10); + Composed[1] = (byte)((length >> 8) & 0xFF); + Composed[2] = (byte)(length & 0xFF); + Buffer.BlockCopy(data, 0, Composed, 3, (int)length); + } + else if (length <= 0xFF_FF_FF) + { + Composed = new byte[4 + length]; + Composed[0] = (byte)((byte)Identifier | 0x18); + Composed[1] = (byte)((length >> 16) & 0xFF); + Composed[2] = (byte)((length >> 8) & 0xFF); + Composed[3] = (byte)(length & 0xFF); + Buffer.BlockCopy(data, 0, Composed, 4, (int)length); + } + else if (length <= 0xFF_FF_FF_FF) + { + Composed = new byte[5 + length]; + Composed[0] = (byte)((byte)Identifier | 0x20); + Composed[1] = (byte)((length >> 24) & 0xFF); + Composed[2] = (byte)((length >> 16) & 0xFF); + Composed[3] = (byte)((length >> 8) & 0xFF); + Composed[4] = (byte)(length & 0xFF); + Buffer.BlockCopy(data, 0, Composed, 5, (int)length); + } + else if (length <= 0xFF_FF_FF_FF_FF) + { + Composed = new byte[6 + length]; + Composed[0] = (byte)((byte)Identifier | 0x28); + Composed[1] = (byte)((length >> 32) & 0xFF); + Composed[2] = (byte)((length >> 24) & 0xFF); + Composed[3] = (byte)((length >> 16) & 0xFF); + Composed[4] = (byte)((length >> 8) & 0xFF); + Composed[5] = (byte)(length & 0xFF); + Buffer.BlockCopy(data, 0, Composed, 6, (int)length); + } + else if (length <= 0xFF_FF_FF_FF_FF_FF) + { + Composed = new byte[7 + length]; + Composed[0] = (byte)((byte)Identifier | 0x30); + Composed[1] = (byte)((length >> 40) & 0xFF); + Composed[2] = (byte)((length >> 32) & 0xFF); + Composed[3] = (byte)((length >> 24) & 0xFF); + Composed[4] = (byte)((length >> 16) & 0xFF); + Composed[5] = (byte)((length >> 8) & 0xFF); + Composed[6] = (byte)(length & 0xFF); + Buffer.BlockCopy(data, 0, Composed, 7, (int)length); + } + else //if (len <= 0xFF_FF_FF_FF_FF_FF_FF) + { + Composed = new byte[8 + length]; + Composed[0] = (byte)((byte)Identifier | 0x38); + Composed[1] = (byte)((length >> 48) & 0xFF); + Composed[2] = (byte)((length >> 40) & 0xFF); + Composed[3] = (byte)((length >> 32) & 0xFF); + Composed[4] = (byte)((length >> 24) & 0xFF); + Composed[5] = (byte)((length >> 16) & 0xFF); + Composed[6] = (byte)((length >> 8) & 0xFF); + Composed[7] = (byte)(length & 0xFF); + Buffer.BlockCopy(data, 0, Composed, 8, (int)length); + } + } + else if (Class == TDUClass.Typed) + { + if (metadata == null) + throw new Exception("Metadata must be provided for types."); + + + if (metadata.Length > 0xFF) + throw new Exception("Metadata can't exceed 255 bytes in length."); + + var metaLen = (byte)metadata.Length; + + var len = 1 + (ulong)metaLen + length; + + + if (length == 0) + { + Composed = new byte[1] { (byte)Identifier }; + } + else if (metadata.Length > 0xFF) + { + throw new Exception("Metadata can't exceed 255 bytes in length."); + } + else if (length <= 0xFF) + { + Composed = new byte[2 + len]; + Composed[0] = (byte)((byte)Identifier | 0x8); + Composed[1] = (byte)len; + Composed[2] = metaLen; + + Buffer.BlockCopy(metadata, 0, Composed, 3, metaLen); + Buffer.BlockCopy(data, 0, Composed, 3 + metaLen, (int)length); + } + else if (len <= 0xFF_FF) + { + Composed = new byte[3 + len]; + Composed[0] = (byte)((byte)identifier | 0x10); + Composed[1] = (byte)((len >> 8) & 0xFF); + Composed[2] = (byte)(len & 0xFF); + Composed[3] = metaLen; + + Buffer.BlockCopy(metadata, 0, Composed, 4, metaLen); + Buffer.BlockCopy(data, 0, Composed, 4 + metaLen, (int)length); + } + else if (len <= 0xFF_FF_FF) + { + Composed = new byte[4 + len]; + Composed[0] = (byte)((byte)identifier | 0x18); + Composed[1] = (byte)((len >> 16) & 0xFF); + Composed[2] = (byte)((len >> 8) & 0xFF); + Composed[3] = (byte)(len & 0xFF); + Composed[4] = metaLen; + + Buffer.BlockCopy(metadata, 0, Composed, 5, metaLen); + Buffer.BlockCopy(data, 0, Composed, 5 + metaLen, (int)length); + + } + else if (len <= 0xFF_FF_FF_FF) + { + Composed = new byte[5 + len]; + Composed[0] = (byte)((byte)identifier | 0x20); + Composed[1] = (byte)((len >> 24) & 0xFF); + Composed[2] = (byte)((len >> 16) & 0xFF); + Composed[3] = (byte)((len >> 8) & 0xFF); + Composed[4] = (byte)(len & 0xFF); + Composed[5] = metaLen; + + Buffer.BlockCopy(metadata, 0, Composed, 6, metaLen); + Buffer.BlockCopy(data, 0, Composed, 6 + metaLen, (int)length); + } + else if (len <= 0xFF_FF_FF_FF_FF) + { + Composed = new byte[6 + len]; + Composed[0] = (byte)((byte)identifier | 0x28); + Composed[1] = (byte)((len >> 32) & 0xFF); + Composed[2] = (byte)((len >> 24) & 0xFF); + Composed[3] = (byte)((len >> 16) & 0xFF); + Composed[4] = (byte)((len >> 8) & 0xFF); + Composed[5] = (byte)(len & 0xFF); + Composed[6] = metaLen; + + Buffer.BlockCopy(metadata, 0, Composed, 7, metaLen); + Buffer.BlockCopy(data, 0, Composed, 7 + metaLen, (int)length); + } + else if (len <= 0xFF_FF_FF_FF_FF_FF) + { + Composed = new byte[7 + len]; + Composed[0] = (byte)((byte)identifier | 0x30); + Composed[1] = (byte)((len >> 40) & 0xFF); + Composed[2] = (byte)((len >> 32) & 0xFF); + Composed[3] = (byte)((len >> 24) & 0xFF); + Composed[4] = (byte)((len >> 16) & 0xFF); + Composed[5] = (byte)((len >> 8) & 0xFF); + Composed[6] = (byte)(len & 0xFF); + Composed[7] = metaLen; + + Buffer.BlockCopy(metadata, 0, Composed, 8, metaLen); + Buffer.BlockCopy(data, 0, Composed, 8 + metaLen, (int)length); + } + else //if (len <= 0xFF_FF_FF_FF_FF_FF_FF) + { + Composed = new byte[8 + len]; + Composed[0] = (byte)((byte)identifier | 0x38); + Composed[1] = (byte)((len >> 48) & 0xFF); + Composed[2] = (byte)((len >> 40) & 0xFF); + Composed[3] = (byte)((len >> 32) & 0xFF); + Composed[4] = (byte)((len >> 24) & 0xFF); + Composed[5] = (byte)((len >> 16) & 0xFF); + Composed[6] = (byte)((len >> 8) & 0xFF); + Composed[7] = (byte)(len & 0xFF); + Composed[8] = metaLen; + + Buffer.BlockCopy(metadata, 0, Composed, 9, metaLen); + Buffer.BlockCopy(data, 0, Composed, 9 + metaLen, (int)length); + } + } + + + } + + + public bool MatchType(TDU with) + { + if (Identifier != with.Identifier) + return false; + + if (Class == TDUClass.Typed) + if (!Metadata.SequenceEqual(with.Metadata)) + return false; + + return true; + } +} diff --git a/Esiur/Data/TransmissionDataUnitClass.cs b/Esiur/Data/TDUClass.cs similarity index 82% rename from Esiur/Data/TransmissionDataUnitClass.cs rename to Esiur/Data/TDUClass.cs index 67e5179..31a965c 100644 --- a/Esiur/Data/TransmissionDataUnitClass.cs +++ b/Esiur/Data/TDUClass.cs @@ -4,7 +4,7 @@ using System.Text; namespace Esiur.Data { - public enum TransmissionDataUnitClass + public enum TDUClass { Fixed = 0x0, Dynamic = 0x1, diff --git a/Esiur/Data/TransmissionDataUnitIdentifier.cs b/Esiur/Data/TDUIdentifier.cs similarity index 96% rename from Esiur/Data/TransmissionDataUnitIdentifier.cs rename to Esiur/Data/TDUIdentifier.cs index 87a482a..47177ee 100644 --- a/Esiur/Data/TransmissionDataUnitIdentifier.cs +++ b/Esiur/Data/TDUIdentifier.cs @@ -4,7 +4,7 @@ using System.Text; namespace Esiur.Data { - public enum TransmissionDataUnitIdentifier + public enum TDUIdentifier { Null = 0x0, False = 0x1, diff --git a/Esiur/Data/TransmissionDataUnit.cs b/Esiur/Data/TransmissionDataUnit.cs deleted file mode 100644 index 0f02009..0000000 --- a/Esiur/Data/TransmissionDataUnit.cs +++ /dev/null @@ -1,337 +0,0 @@ -using Esiur.Net.IIP; -using System; -using System.Collections.Generic; -using System.Text; -using System.Xml.Linq; - -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 byte[] Metadata; - - public ulong Size; - - public TransmissionDataUnit(TransmissionDataUnitIdentifier identifier, - byte[] data, uint offset, - ulong length, byte[] metadata = null, - byte exponent = 0) - { - Identifier = identifier; - Index = (byte)identifier & 0x7; - Class = (TransmissionDataUnitClass)((byte)identifier >> 6); - Offset = offset; - ContentLength = length; - Exponent = exponent; - Data = data; - Metadata = metadata; - - Size = Class == TransmissionDataUnitClass.Fixed ? 1 + length: 1 + length + - } - - 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 bool MatchType(TransmissionDataUnit with) - { - if (Identifier != with.Identifier) - return false; - - if (Class == TransmissionDataUnitClass.Typed) - if (!Metadata.SequenceEqual(with.Metadata)) - return false; - - return true; - } - - public byte[] Compose() - { - return null; - } - - public static byte[] Compose(TransmissionDataUnitIdentifier identifier, byte[] data, byte[] typeMetadata) - { - - 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 - || cls == TransmissionDataUnitClass.Extension) - { - 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(typeMetadata, 0, rt, 2, typeMetadata.Length); - Buffer.BlockCopy(data, 0, rt, 2 + typeMetadata.Length, data.Length); - 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(typeMetadata, 0, rt, 3, typeMetadata.Length); - Buffer.BlockCopy(data, 0, rt, 3 + typeMetadata.Length, data.Length); - 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(typeMetadata, 0, rt, 4, typeMetadata.Length); - Buffer.BlockCopy(data, 0, rt, 4 + typeMetadata.Length, data.Length); - - 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(typeMetadata, 0, rt, 5, typeMetadata.Length); - Buffer.BlockCopy(data, 0, rt, 5 + typeMetadata.Length, data.Length); - - 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(typeMetadata, 0, rt, 6, typeMetadata.Length); - Buffer.BlockCopy(data, 0, rt, 6 + typeMetadata.Length, data.Length); - - 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(typeMetadata, 0, rt, 7, typeMetadata.Length); - Buffer.BlockCopy(data, 0, rt, 7 + typeMetadata.Length, data.Length); - - 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(typeMetadata, 0, rt, 8, typeMetadata.Length); - Buffer.BlockCopy(data, 0, rt, 8 + typeMetadata.Length, data.Length); - - return rt; - } - } - - throw new Exception("Not supported class type."); - } - - 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((TransmissionDataUnitIdentifier)h, data, offset, 0)); - - ulong cl = (ulong)(1 << (exp - 1)); - - if (ends - offset < cl) - return (cl - (ends - offset), null); - - //offset += (uint)cl; - - return (1 + cl, new TransmissionDataUnit((TransmissionDataUnitIdentifier)h, data, offset, cl, null, (byte)exp)); - } - else if (cls == TransmissionDataUnitClass.Typed) - { - 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); - - var metaData = DC.Clip(data, offset + 1, data[offset]); - offset += data[offset]; - - return (1 + cl + cll, new TransmissionDataUnit((TransmissionDataUnitIdentifier)(h & 0xC7), data, offset, cl, metaData)); - } - 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((TransmissionDataUnitIdentifier)(h & 0xC7), data, offset, cl)); - - } - } - -} diff --git a/Esiur/Net/IIP/DistributedConnection.cs b/Esiur/Net/IIP/DistributedConnection.cs index cfcfcfb..6991c72 100644 --- a/Esiur/Net/IIP/DistributedConnection.cs +++ b/Esiur/Net/IIP/DistributedConnection.cs @@ -51,9 +51,9 @@ public partial class DistributedConnection : NetworkConnection, IStore { - public delegate void ProtocolGeneralHandler(DistributedConnection connection, TransmissionDataUnit dataType, byte[] data); + public delegate void ProtocolGeneralHandler(DistributedConnection connection, ParsedTDU dataType, byte[] data); - public delegate void ProtocolRequestReplyHandler(DistributedConnection connection, uint callbackId, TransmissionDataUnit dataType, byte[] data); + public delegate void ProtocolRequestReplyHandler(DistributedConnection connection, uint callbackId, ParsedTDU dataType, byte[] data); // Delegates public delegate void ReadyEvent(DistributedConnection sender); diff --git a/Esiur/Net/IIP/DistributedConnectionProtocol.cs b/Esiur/Net/IIP/DistributedConnectionProtocol.cs index e34c706..ed27cef 100644 --- a/Esiur/Net/IIP/DistributedConnectionProtocol.cs +++ b/Esiur/Net/IIP/DistributedConnectionProtocol.cs @@ -262,7 +262,7 @@ partial class DistributedConnection SendReply(IIPPacketReply.Chunk, callbackId, chunk); } - void IIPReplyCompleted(uint callbackId, TransmissionDataUnit dataType, byte[] data) + void IIPReplyCompleted(uint callbackId, ParsedTDU dataType, byte[] data) { var req = requests.Take(callbackId); @@ -286,12 +286,12 @@ partial class DistributedConnection } } - void IIPExtensionAction(byte actionId, TransmissionDataUnit? dataType, byte[] data) + void IIPExtensionAction(byte actionId, ParsedTDU? dataType, byte[] data) { // nothing is supported now } - void IIPReplyPropagated(uint callbackId, TransmissionDataUnit dataType, byte[] data) + void IIPReplyPropagated(uint callbackId, ParsedTDU dataType, byte[] data) { var req = requests[callbackId]; @@ -315,7 +315,7 @@ partial class DistributedConnection } } - void IIPReplyError(uint callbackId, TransmissionDataUnit dataType, byte[] data, ErrorType type) + void IIPReplyError(uint callbackId, ParsedTDU dataType, byte[] data, ErrorType type) { var req = requests.Take(callbackId); @@ -334,7 +334,7 @@ partial class DistributedConnection req.TriggerError(new AsyncException(type, errorCode, errorMsg)); } - void IIPReplyProgress(uint callbackId, TransmissionDataUnit dataType, byte[] data) + void IIPReplyProgress(uint callbackId, ParsedTDU dataType, byte[] data) { var req = requests[callbackId]; @@ -353,7 +353,7 @@ partial class DistributedConnection req.TriggerProgress(ProgressType.Execution, current, total); } - void IIPReplyWarning(uint callbackId, TransmissionDataUnit dataType, byte[] data) + void IIPReplyWarning(uint callbackId, ParsedTDU dataType, byte[] data) { var req = requests[callbackId]; @@ -374,7 +374,7 @@ partial class DistributedConnection - void IIPReplyChunk(uint callbackId, TransmissionDataUnit dataType, byte[] data) + void IIPReplyChunk(uint callbackId, ParsedTDU dataType, byte[] data) { var req = requests[callbackId]; @@ -389,16 +389,16 @@ partial class DistributedConnection req.TriggerChunk(parsed); } - void IIPNotificationResourceReassigned(TransmissionDataUnit dataType, byte[] data) + void IIPNotificationResourceReassigned(ParsedTDU dataType, byte[] data) { // uint resourceId, uint newResourceId } - void IIPNotificationResourceMoved(TransmissionDataUnit dataType, byte[] data) { } + void IIPNotificationResourceMoved(ParsedTDU dataType, byte[] data) { } - void IIPNotificationSystemFailure(TransmissionDataUnit dataType, byte[] data) { } + void IIPNotificationSystemFailure(ParsedTDU dataType, byte[] data) { } - void IIPNotificationResourceDestroyed(TransmissionDataUnit dataType, byte[] data) + void IIPNotificationResourceDestroyed(ParsedTDU dataType, byte[] data) { var (size, rt) = Codec.ParseSync(data, dataType.Offset, Instance.Warehouse, dataType); @@ -429,7 +429,7 @@ partial class DistributedConnection } - void IIPNotificationPropertyModified(TransmissionDataUnit dataType, byte[] data) + void IIPNotificationPropertyModified(ParsedTDU dataType, byte[] data) { // resourceId, index, value var (valueOffset, valueSize, args) = @@ -468,7 +468,7 @@ partial class DistributedConnection } - void IIPNotificationEventOccurred(TransmissionDataUnit dataType, byte[] data) + void IIPNotificationEventOccurred(ParsedTDU dataType, byte[] data) { // resourceId, index, value var (valueOffset, valueSize, args) = @@ -517,7 +517,7 @@ partial class DistributedConnection }); } - void IIPRequestAttachResource(uint callback, TransmissionDataUnit dataType, byte[] data) + void IIPRequestAttachResource(uint callback, ParsedTDU dataType, byte[] data) { var (_, value) = Codec.ParseSync(data, 0, Instance.Warehouse, dataType); @@ -572,7 +572,7 @@ partial class DistributedConnection }); } - void IIPRequestReattachResource(uint callback, TransmissionDataUnit dataType, byte[] data) + void IIPRequestReattachResource(uint callback, ParsedTDU dataType, byte[] data) { // resourceId, index, value var (valueOffset, valueSize, args) = @@ -630,7 +630,7 @@ partial class DistributedConnection }); } - void IIPRequestDetachResource(uint callback, TransmissionDataUnit dataType, byte[] data) + void IIPRequestDetachResource(uint callback, ParsedTDU dataType, byte[] data) { var (_, value) = Codec.ParseSync(data, 0, Instance.Warehouse, dataType); @@ -661,7 +661,7 @@ partial class DistributedConnection }); } - void IIPRequestCreateResource(uint callback, TransmissionDataUnit dataType, byte[] data) + void IIPRequestCreateResource(uint callback, ParsedTDU dataType, byte[] data) { var (_, parsed) = Codec.ParseAsync(data, 0, this, null, dataType); @@ -722,7 +722,7 @@ partial class DistributedConnection } - void IIPRequestDeleteResource(uint callback, TransmissionDataUnit dataType, byte[] data) + void IIPRequestDeleteResource(uint callback, ParsedTDU dataType, byte[] data) { var (_, value) = Codec.ParseSync(data, 0, Instance.Warehouse, dataType); @@ -751,7 +751,7 @@ partial class DistributedConnection }); } - void IIPRequestMoveResource(uint callback, TransmissionDataUnit dataType, byte[] data) + void IIPRequestMoveResource(uint callback, ParsedTDU dataType, byte[] data) { var (offset, length, args) = DataDeserializer.LimitedCountListParser(data, dataType.Offset, @@ -790,12 +790,12 @@ partial class DistributedConnection - void IIPRequestToken(uint callback, TransmissionDataUnit dataType, byte[] data) + void IIPRequestToken(uint callback, ParsedTDU dataType, byte[] data) { // @TODO: To be implemented } - void IIPRequestLinkTemplates(uint callback, TransmissionDataUnit dataType, byte[] data) + void IIPRequestLinkTemplates(uint callback, ParsedTDU dataType, byte[] data) { var (_, value) = Codec.ParseSync(data, 0, Instance.Warehouse, dataType); @@ -828,7 +828,7 @@ partial class DistributedConnection Instance.Warehouse.Query(resourceLink).Then(queryCallback); } - void IIPRequestTemplateFromClassName(uint callback, TransmissionDataUnit dataType, byte[] data) + void IIPRequestTemplateFromClassName(uint callback, ParsedTDU dataType, byte[] data) { var (_, value) = Codec.ParseSync(data, 0, Instance.Warehouse, dataType); @@ -847,7 +847,7 @@ partial class DistributedConnection } } - void IIPRequestTemplateFromClassId(uint callback, TransmissionDataUnit dataType, byte[] data) + void IIPRequestTemplateFromClassId(uint callback, ParsedTDU dataType, byte[] data) { var (_, value) = Codec.ParseSync(data, 0, Instance.Warehouse, dataType); @@ -869,7 +869,7 @@ partial class DistributedConnection - void IIPRequestTemplateFromResourceId(uint callback, TransmissionDataUnit dataType, byte[] data) + void IIPRequestTemplateFromResourceId(uint callback, ParsedTDU dataType, byte[] data) { var (_, value) = Codec.ParseSync(data, 0, Instance.Warehouse, dataType); @@ -892,7 +892,7 @@ partial class DistributedConnection - void IIPRequestGetResourceIdByLink(uint callback, TransmissionDataUnit dataType, byte[] data) + void IIPRequestGetResourceIdByLink(uint callback, ParsedTDU dataType, byte[] data) { var (_, parsed) = Codec.ParseSync(data, 0, Instance.Warehouse, dataType); var resourceLink = (string)parsed; @@ -920,7 +920,7 @@ partial class DistributedConnection } - void IIPRequestQueryResources(uint callback, TransmissionDataUnit dataType, byte[] data) + void IIPRequestQueryResources(uint callback, ParsedTDU dataType, byte[] data) { var (_, parsed) = Codec.ParseSync(data, 0, Instance.Warehouse, dataType); @@ -978,7 +978,7 @@ partial class DistributedConnection } - void IIPRequestProcedureCall(uint callback, TransmissionDataUnit dataType, byte[] data) + void IIPRequestProcedureCall(uint callback, ParsedTDU dataType, byte[] data) { var (offset, length, args) = DataDeserializer.LimitedCountListParser(data, dataType.Offset, dataType.ContentLength, Instance.Warehouse, 1); @@ -1038,7 +1038,7 @@ partial class DistributedConnection } } - void IIPRequestStaticCall(uint callback, TransmissionDataUnit dataType, byte[] data) + void IIPRequestStaticCall(uint callback, ParsedTDU dataType, byte[] data) { var (offset, length, args) = DataDeserializer.LimitedCountListParser(data, dataType.Offset, dataType.ContentLength, Instance.Warehouse, 2); @@ -1115,7 +1115,7 @@ partial class DistributedConnection } } - void IIPRequestInvokeFunction(uint callback, TransmissionDataUnit dataType, byte[] data) + void IIPRequestInvokeFunction(uint callback, ParsedTDU dataType, byte[] data) { var (offset, length, args) = DataDeserializer.LimitedCountListParser(data, dataType.Offset, dataType.ContentLength, Instance.Warehouse, 2); @@ -1371,7 +1371,7 @@ partial class DistributedConnection } } - void IIPRequestSubscribe(uint callback, TransmissionDataUnit dataType, byte[] data) + void IIPRequestSubscribe(uint callback, ParsedTDU dataType, byte[] data) { var (offset, length, args) = DataDeserializer.LimitedCountListParser(data, dataType.Offset, @@ -1430,7 +1430,7 @@ partial class DistributedConnection } - void IIPRequestUnsubscribe(uint callback, TransmissionDataUnit dataType, byte[] data) + void IIPRequestUnsubscribe(uint callback, ParsedTDU dataType, byte[] data) { var (offset, length, args) = DataDeserializer.LimitedCountListParser(data, dataType.Offset, @@ -1491,7 +1491,7 @@ partial class DistributedConnection - void IIPRequestSetProperty(uint callback, TransmissionDataUnit dataType, byte[] data) + void IIPRequestSetProperty(uint callback, ParsedTDU dataType, byte[] data) { var (offset, length, args) = DataDeserializer.LimitedCountListParser(data, dataType.Offset, @@ -2056,7 +2056,7 @@ partial class DistributedConnection - void IIPRequestKeepAlive(uint callback, TransmissionDataUnit dataType, byte[] data) + void IIPRequestKeepAlive(uint callback, ParsedTDU dataType, byte[] data) { var (offset, length, args) = DataDeserializer.LimitedCountListParser(data, dataType.Offset, diff --git a/Esiur/Net/Packets/IIPAuthPacket.cs b/Esiur/Net/Packets/IIPAuthPacket.cs index 577a205..0c9a6ea 100644 --- a/Esiur/Net/Packets/IIPAuthPacket.cs +++ b/Esiur/Net/Packets/IIPAuthPacket.cs @@ -136,7 +136,7 @@ public class IIPAuthPacket : Packet set; } - public TransmissionDataUnit? DataType + public ParsedTDU? DataType { get; set; @@ -188,7 +188,7 @@ public class IIPAuthPacket : Packet if (NotEnough(offset, ends, 1)) return -dataLengthNeeded; - (var size, DataType) = TransmissionDataUnit.Parse(data, offset, ends); + (var size, DataType) = ParsedTDU.Parse(data, offset, ends); if (DataType == null) return -(int)size; @@ -208,7 +208,7 @@ public class IIPAuthPacket : Packet if (NotEnough(offset, ends, 1)) return -dataLengthNeeded; - (var size, DataType) = TransmissionDataUnit.Parse(data, offset, ends); + (var size, DataType) = ParsedTDU.Parse(data, offset, ends); if (DataType == null) return -(int)size; @@ -280,7 +280,7 @@ public class IIPAuthPacket : Packet Reference = data.GetUInt32(offset, Endian.Little); offset += 4; - (var size, DataType) = TransmissionDataUnit.Parse(data, offset, ends); + (var size, DataType) = ParsedTDU.Parse(data, offset, ends); if (DataType == null) return -(int)size; @@ -445,7 +445,7 @@ public class IIPAuthPacket : Packet if (NotEnough(offset, ends, 1)) return -dataLengthNeeded; - (var size, DataType) = TransmissionDataUnit.Parse(data, offset, ends); + (var size, DataType) = ParsedTDU.Parse(data, offset, ends); if (DataType == null) return -(int)size; diff --git a/Esiur/Net/Packets/IIPPacket.cs b/Esiur/Net/Packets/IIPPacket.cs index bab05f5..f9c9860 100644 --- a/Esiur/Net/Packets/IIPPacket.cs +++ b/Esiur/Net/Packets/IIPPacket.cs @@ -44,7 +44,7 @@ class IIPPacket : Packet public byte Extension { get; set; } - public TransmissionDataUnit? DataType { get; set; } + public ParsedTDU? DataType { get; set; } private uint dataLengthNeeded; @@ -124,7 +124,7 @@ class IIPPacket : Packet if (NotEnough(offset, ends, 1)) return -dataLengthNeeded; - (var size, DataType) = TransmissionDataUnit.Parse(data, offset, ends); + (var size, DataType) = ParsedTDU.Parse(data, offset, ends); if (DataType == null) return -(int)size; diff --git a/Test/Program.cs b/Test/Program.cs index 065edd8..9f2b6da 100644 --- a/Test/Program.cs +++ b/Test/Program.cs @@ -59,10 +59,11 @@ namespace Test static void TestSerialization(object x, DistributedConnection connection = null) { - var y = Codec.Compose(x, connection); - var rr = DC.ToHex(y); + var d = Codec.Compose(x, Warehouse.Default, connection); + // var rr = DC.ToHex(y); - Console.WriteLine(x.GetType().Name + ": " + rr); + var y = Codec.ParseSync(d, 0, Warehouse.Default); + Console.WriteLine($"{x.GetType().Name}: {x} == {y}, {d.ToHex()}"); } @@ -81,7 +82,13 @@ namespace Test static async Task Main(string[] args) { - + TestSerialization("Hello"); + TestSerialization(10); + TestSerialization(10.1); + TestSerialization(10.1d); + TestSerialization((byte)1); + TestSerialization((byte)2); + TestSerialization(new int[] { 1, 2, 3, 4 }); //var x = LogLevel.Warning; //TestSerialization(LogLevel.Warning); @@ -93,7 +100,7 @@ namespace Test // ["JS"] = null //}); - + //TestSerialization(new StudentRecord() { Name = "Ali", Grade = 90 }); @@ -107,7 +114,7 @@ namespace Test //Console.WriteLine(g); - + var a = new ECDH(); var b = new ECDH(); @@ -175,7 +182,7 @@ namespace Test sender.Send("Hello"); }); - await wh.Open(); + await wh.Open(); // Start testing TestClient(service); @@ -261,7 +268,7 @@ namespace Test //Console.WriteLine(path); - + } static async void perodicTimerElapsed(object state)