Example #1
0
 internal SerializedRequest(byte[] rentedArray, int realLength, SerializedRequest linkedSerializedRequest = null)
 {
     RentedArray             = rentedArray;
     RealLength              = realLength;
     LinkedSerializedRequest = linkedSerializedRequest;
     Request = new ReadOnlyMemory <byte>(rentedArray, 0, realLength);
 }
Example #2
0
        public SerializedRequest Serialize(TData data)
        {
            int realLength;

            byte[]            serializedBody           = null;
            SerializedRequest composeSerializedRequest = null;
            var examined = 0;

            if (_reflection.BodyProperty != null)
            {
                var bodyValue = _reflection.BodyProperty.Get(data);

                if (bodyValue == null)
                {
                    throw TcpException.SerializerBodyPropertyIsNull();
                }

                serializedBody = _bitConverter.ConvertToBytes(bodyValue, _reflection.BodyProperty.PropertyType, _reflection.BodyProperty.Attribute.Reverse);
                realLength     = CalculateRealLength(_reflection.LengthProperty, ref data, _reflection.MetaLength, serializedBody.Length);
            }
            else if (_reflection.ComposeProperty != null)
            {
                var composeValue = _reflection.ComposeProperty.Get(data);

                if (composeValue == null)
                {
                    throw TcpException.SerializerComposePropertyIsNull();
                }

                if (_reflection.ComposeProperty.PropertyType.IsPrimitive)
                {
                    serializedBody = _bitConverter.ConvertToBytes(composeValue, _reflection.ComposeProperty.PropertyType, _reflection.ComposeProperty.Attribute.Reverse);
                    realLength     = CalculateRealLength(_reflection.LengthProperty, ref data, _reflection.MetaLength, serializedBody.Length);
                }
                else
                {
                    composeSerializedRequest = _reflection.ComposeProperty.Composition.Serialize(composeValue);
                    realLength = CalculateRealLength(_reflection.LengthProperty, ref data, _reflection.MetaLength, composeSerializedRequest.RealLength);
                }
            }
            else
            {
                realLength = _reflection.MetaLength;
            }

            var rentedArray = _byteArrayFactory(realLength);

            foreach (var property in _reflection.Properties)
            {
                if (!property.PropertyType.IsPrimitive && property.Attribute.TcpDataType == TcpDataType.Compose && composeSerializedRequest != null)
                {
                    Array.Copy(
                        composeSerializedRequest.RentedArray,
                        0,
                        rentedArray,
                        property.Attribute.Index,
                        composeSerializedRequest.RealLength
                        );
                }
                else
                {
                    var value = property.Attribute.TcpDataType == TcpDataType.Body || property.Attribute.TcpDataType == TcpDataType.Compose
                        ? serializedBody ?? throw TcpException.SerializerBodyPropertyIsNull()
                        : _bitConverter.ConvertToBytes(property.Get(data), property.PropertyType, property.Attribute.Reverse);

                    var valueLength = value.Length;

                    if (property.Attribute.TcpDataType != TcpDataType.Body && property.Attribute.TcpDataType != TcpDataType.Compose && valueLength > property.Attribute.Length)
                    {
                        throw TcpException.SerializerLengthOutOfRange(property.PropertyType.ToString(), valueLength.ToString(), property.Attribute.Length.ToString());
                    }

                    value.CopyTo(rentedArray, property.Attribute.Index);
                }

                if (++examined == _reflection.Properties.Count)
                {
                    break;
                }
            }

            return(new SerializedRequest(rentedArray, realLength, composeSerializedRequest));