Ejemplo n.º 1
0
        public void Test(RpcValue paramater)
        {
            ArrayPool <byte> bufferPool = TcpClient.GetBufferPool();

            ArrayPool <byte> .Node buffer;
            lock (bufferPool)
            {
                buffer = bufferPool.AllocBuffer(DEFAULT_BUFFER_SIZE);
            }

            int serializePoint = buffer.GetOffset();

            paramater.Serialize(buffer.GetBuffer(), ref serializePoint);

            int dserializePoint = buffer.GetOffset();

            paramater.Deserialize(RpcValueArrayPool
                                  , RpcValuePool
                                  , buffer.GetBuffer()
                                  , ref dserializePoint);

            lock (bufferPool)
            {
                bufferPool.ReleaseBuffer(buffer);
            }

            MDebug.Assert(serializePoint == dserializePoint
                          , "Rpc"
                          , "serializePoint == dserializePoint");
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="buffer">目标Buffer</param>
        /// <param name="offset">写入位置</param>
        /// <param name="enableSerializeValueType">
        /// 是否序列化<see cref="ValueType"/>
        /// 需要这个开关是因为序列化<see cref="ValueType.FixedValueTypeArray"/>时,不需要序列化数组中每个元素的<see cref="ValueType"/>
        /// </param>
        public void Serialize(byte[] buffer
                              , ref int offset
                              , bool enableSerializeValueType = true)
        {
            if (enableSerializeValueType)
            {
                MBitConverter.WriteTo(buffer, ref offset, (byte)ValueType);
            }

            switch (ValueType)
            {
            case ValueType.Byte:
                MBitConverter.WriteTo(buffer, ref offset, ByteValue);
                break;

            case ValueType.Short:
                MBitConverter.WriteTo(buffer, ref offset, ShortValue);
                break;

            case ValueType.Int:
                MBitConverter.WriteTo(buffer, ref offset, IntValue);
                break;

            case ValueType.Long:
                MBitConverter.WriteTo(buffer, ref offset, LongValue);
                break;

            case ValueType.Float:
                MBitConverter.WriteTo(buffer, ref offset, FloatValue);
                break;

            case ValueType.Double:
                MBitConverter.WriteTo(buffer, ref offset, DoubleValue);
                break;

            case ValueType.FixedValueTypeArray:
            {
                int elementCount = ArrayValue.GetSize();
                MBitConverter.WriteTo(buffer, ref offset, elementCount);
                MDebug.Assert(elementCount > 0
                              , "Rpc"
                              , "elementCount > 0");

                int        startPoint       = ArrayValue.GetOffset();
                int        endPoint         = startPoint + elementCount;
                RpcValue[] elements         = ArrayValue.GetBuffer();
                ValueType  elementValueType = elements[startPoint].ValueType;
                MBitConverter.WriteTo(buffer, ref offset, (byte)elementValueType);
                for (int iElement = startPoint; iElement < endPoint; iElement++)
                {
                    RpcValue iterElement = elements[iElement];
#if GF_DEBUG
                    MDebug.Assert(iterElement.ValueType == elementValueType
                                  , "Rpc"
                                  , "iterElement.ValueType == elementValueType");
#endif
                    iterElement.Serialize(buffer, ref offset, false);
                }
            }
            break;

            case ValueType.VariableValueTypeArray:
            {
                int elementCount = ArrayValue.GetSize();
                MBitConverter.WriteTo(buffer, ref offset, elementCount);
                MDebug.Assert(elementCount > 0
                              , "Rpc"
                              , "elementCount > 0");

                int        startPoint = ArrayValue.GetOffset();
                int        endPoint   = startPoint + elementCount;
                RpcValue[] elements   = ArrayValue.GetBuffer();
                for (int iElement = startPoint; iElement < endPoint; iElement++)
                {
                    RpcValue iterElement = elements[iElement];
                    iterElement.Serialize(buffer, ref offset);
                }
            }
            break;

            default:
                throw new Exception("Not support ValueType: " + ValueType);
            }
        }