/*
Copyright (c) 2017 Ahmed Kh. Zamil
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.Text;
using Esiur.Misc;
using System.ComponentModel;
using Esiur.Data;
using Esiur.Core;
using Esiur.Net.IIP;
using Esiur.Resource;
using System.Linq;
using System.Reflection;
using Esiur.Resource.Template;
using System.Runtime.CompilerServices;
using System.Collections;
using System.Dynamic;
namespace Esiur.Data;
public static class Codec
{
delegate AsyncReply Parser(byte[] data, uint offset, uint length, DistributedConnection connection, uint[] requestSequence);
static Parser[][] FixedParsers = new Parser[][]
{
new Parser[]{
DataDeserializer.NullParser,
DataDeserializer.BooleanFalseParser,
DataDeserializer.BooleanTrueParser,
DataDeserializer.NotModifiedParser,
},
new Parser[]{
DataDeserializer.ByteParser,
DataDeserializer.SByteParser,
DataDeserializer.Char8Parser,
},
new Parser[]{
DataDeserializer.Int16Parser,
DataDeserializer.UInt16Parser,
DataDeserializer.Char16Parser,
},
new Parser[]{
DataDeserializer.Int32Parser,
DataDeserializer.UInt32Parser,
DataDeserializer.Float32Parser,
DataDeserializer.ResourceParser,
DataDeserializer.LocalResourceParser,
},
new Parser[]{
DataDeserializer.Int64Parser,
DataDeserializer.UInt64Parser,
DataDeserializer.Float64Parser,
DataDeserializer.DateTimeParser,
},
new Parser[]
{
DataDeserializer.Int128Parser, // int 128
DataDeserializer.UInt128Parser, // uint 128
DataDeserializer.Float128Parser,
}
};
static Parser[] DynamicParsers = new Parser[]
{
DataDeserializer.RawDataParser,
DataDeserializer.StringParser,
DataDeserializer.ListParser,
DataDeserializer.ResourceListParser,
DataDeserializer.RecordListParser,
};
static Parser[] TypedParsers = new Parser[]
{
DataDeserializer.RecordParser,
DataDeserializer.TypedListParser,
DataDeserializer.TypedMapParser,
DataDeserializer.TupleParser,
DataDeserializer.EnumParser,
DataDeserializer.ConstantParser,
};
///
/// Parse a value
///
/// Bytes array
/// Zero-indexed offset.
/// Output the number of bytes parsed
/// 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, AsyncReply) Parse(byte[] data, uint offset, DistributedConnection connection, uint[] requestSequence, TransmissionType? dataType = null)
{
uint len = 0;
if (dataType == null)
{
(var longLen, dataType) = TransmissionType.Parse(data, offset, (uint)data.Length);
len = (uint)longLen;
offset = dataType.Value.Offset;
}
else
len = (uint)dataType.Value.ContentLength;
var tt = dataType.Value;
if (tt.Class == TransmissionTypeClass.Fixed)
{
return (len, FixedParsers[tt.Exponent][tt.Index](data, dataType.Value.Offset, (uint)tt.ContentLength, connection, requestSequence));
}
else if (tt.Class == TransmissionTypeClass.Dynamic)
{
return (len, DynamicParsers[tt.Index](data, dataType.Value.Offset, (uint)tt.ContentLength, connection, requestSequence));
}
else //if (tt.Class == TransmissionTypeClass.Typed)
{
return (len, TypedParsers[tt.Index](data, dataType.Value.Offset, (uint)tt.ContentLength, connection, requestSequence));
}
}
///
/// Check if a resource is local to a given connection.
///
/// Resource to check.
/// DistributedConnection to check if the resource is local to it.
/// True, if the resource owner is the given connection, otherwise False.
public static bool IsLocalResource(IResource resource, DistributedConnection connection)
{
if (resource is DistributedResource)
if ((resource as DistributedResource).DistributedResourceConnection == connection)
return true;
return false;
}
public delegate (TransmissionTypeIdentifier, byte[]) Composer(object value, DistributedConnection connection);
public static Dictionary Composers = new Dictionary()
{
// Fixed
[typeof(bool)] = DataSerializer.BoolComposer,
[typeof(bool?)] = DataSerializer.BoolComposer,
[typeof(NotModified)] = DataSerializer.NotModifiedComposer,
[typeof(byte)] = DataSerializer.UInt8Composer,
[typeof(byte?)] = DataSerializer.UInt8Composer,
[typeof(sbyte)] = DataSerializer.Int8Composer,
[typeof(sbyte?)] = DataSerializer.Int8Composer,
[typeof(char)] = DataSerializer.Char16Composer,
[typeof(char?)] = DataSerializer.Char16Composer,
[typeof(short)] = DataSerializer.Int16Composer,
[typeof(short?)] = DataSerializer.Int16Composer,
[typeof(ushort)] = DataSerializer.UInt16Composer,
[typeof(ushort?)] = DataSerializer.UInt16Composer,
[typeof(int)] = DataSerializer.Int32Composer,
[typeof(int?)] = DataSerializer.Int32Composer,
[typeof(uint)] = DataSerializer.UInt32Composer,
[typeof(uint?)] = DataSerializer.UInt32Composer,
[typeof(float)] = DataSerializer.Float32Composer,
[typeof(float?)] = DataSerializer.Float32Composer,
[typeof(long)] = DataSerializer.Int64Composer,
[typeof(long?)] = DataSerializer.Int64Composer,
[typeof(ulong)] = DataSerializer.UIn64Composer,
[typeof(ulong?)] = DataSerializer.UIn64Composer,
[typeof(double)] = DataSerializer.Float64Composer,
[typeof(double?)] = DataSerializer.Float64Composer,
[typeof(DateTime)] = DataSerializer.DateTimeComposer,
[typeof(DateTime?)] = DataSerializer.DateTimeComposer,
[typeof(decimal)] = DataSerializer.Float128Composer,
[typeof(decimal?)] = DataSerializer.Float128Composer,
[typeof(byte[])] = DataSerializer.RawDataComposerFromArray,
//[typeof(byte?[])] = DataSerializer.RawDataComposerFromArray,
[typeof(List)] = DataSerializer.RawDataComposerFromList,
//[typeof(List)] = DataSerializer.RawDataComposerFromList,
[typeof(string)] = DataSerializer.StringComposer,
// Special
[typeof(object[])] = DataSerializer.ListComposer,// DataSerializer.ListComposerFromArray,
[typeof(List