Пример #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");
        }
Пример #2
0
        /// <summary>
        /// 反序列化
        /// <see cref="Serialize"/>
        /// </summary>
        public void Deserialize(ArrayPool <RpcValue> rpcValueArrayPool
                                , ObjectPool <RpcValue> rpcValuePool
                                , byte[] buffer
                                , ref int offset
                                , bool enableDeserializeValueType = true)
        {
            if (enableDeserializeValueType)
            {
                ValueType = (ValueType)buffer[offset];
                offset++;
            }

            switch (ValueType)
            {
            case ValueType.Byte:
                ByteValue = buffer[offset];
                offset++;
                break;

            case ValueType.Short:
                ShortValue = BitConverter.ToInt16(buffer, offset);
                offset    += sizeof(short);
                break;

            case ValueType.Int:
                IntValue = BitConverter.ToInt32(buffer, offset);
                offset  += sizeof(int);
                break;

            case ValueType.Long:
                LongValue = BitConverter.ToInt64(buffer, offset);
                offset   += sizeof(long);
                break;

            case ValueType.Float:
                FloatValue = BitConverter.ToSingle(buffer, offset);
                offset    += sizeof(float);
                break;

            case ValueType.Double:
                DoubleValue = BitConverter.ToDouble(buffer, offset);
                offset     += sizeof(double);
                break;

            case ValueType.FixedValueTypeArray:
            {
                int elementCount = BitConverter.ToInt32(buffer, offset);
                offset += sizeof(int);

                ValueType elementValueType = (ValueType)buffer[offset];
                offset++;
                ArrayValue = rpcValueArrayPool.AllocBuffer(elementCount);
                int        endPoint = ArrayValue.GetOffset() + ArrayValue.GetSize();
                RpcValue[] elements = ArrayValue.GetBuffer();
                for (int iElement = ArrayValue.GetOffset(); iElement < endPoint; iElement++)
                {
                    RpcValue iterElement = rpcValuePool.Alloc();
                    elements[iElement] = iterElement;

                    iterElement.ValueType = elementValueType;
                    iterElement.Deserialize(rpcValueArrayPool
                                            , rpcValuePool
                                            , buffer
                                            , ref offset
                                            , false);
                }
            }
            break;

            case ValueType.VariableValueTypeArray:
            {
                int elementCount = BitConverter.ToInt32(buffer, offset);
                offset += sizeof(int);

                ArrayValue = rpcValueArrayPool.AllocBuffer(elementCount);
                int        endPoint = ArrayValue.GetOffset() + ArrayValue.GetSize();
                RpcValue[] elements = ArrayValue.GetBuffer();
                for (int iElement = ArrayValue.GetOffset(); iElement < endPoint; iElement++)
                {
                    RpcValue iterElement = rpcValuePool.Alloc();
                    elements[iElement] = iterElement;

                    iterElement.Deserialize(rpcValueArrayPool
                                            , rpcValuePool
                                            , buffer
                                            , ref offset);
                }
            }
            break;

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