Пример #1
0
 internal void JsonSerialize <valueType>(ref valueType value)
     where valueType : struct
 {
     if (OutputJsonSerializer == null)
     {
         OutputJsonSerializer = AutoCSer.JsonSerializer.YieldPool.Default.Pop() ?? new AutoCSer.JsonSerializer();
         OutputJsonSerializer.SetTcpServer();
     }
     OutputJsonSerializer.SerializeTcpServer(ref value, OutputSerializer.Stream);
 }
Пример #2
0
        /// <summary>
        /// 创建命令输入数据
        /// </summary>
        /// <typeparam name="inputParameterType"></typeparam>
        /// <param name="commandInfo"></param>
        /// <param name="inputParameter"></param>
        /// <param name="clientBuffer"></param>
        private unsafe void build <inputParameterType>(TcpServer.CommandInfoBase commandInfo, ref inputParameterType inputParameter, ref ClientBuffer clientBuffer)
            where inputParameterType : struct
        {
            byte *start = outputStream.Data.Byte;

            outputStream.Data.CurrentIndex = sizeof(uint) + sizeof(int);
            if ((commandInfo.CommandFlags & TcpServer.CommandFlags.JsonSerialize) == 0)
            {
                if (commandInfo.SimpleSerializeInputParamter == 0)
                {
                    int parameterIndex = commandInfo.InputParameterIndex;
                    if (serializeParameterIndex == parameterIndex)
                    {
                        outputSerializer.SerializeTcpServerNext(ref inputParameter);
                    }
                    else
                    {
                        outputSerializer.SerializeTcpServer(ref inputParameter);
                        serializeParameterIndex = parameterIndex;
                    }
                }
                else
                {
                    SimpleSerialize.TypeSerializer <inputParameterType> .Serializer(outputStream, ref inputParameter);
                }
            }
            else
            {
                if (outputJsonSerializer == null)
                {
                    outputJsonSerializer = AutoCSer.JsonSerializer.YieldPool.Default.Pop() ?? new AutoCSer.JsonSerializer();
                    outputJsonSerializer.SetTcpServer();
                }
                outputJsonSerializer.SerializeTcpServer(ref inputParameter, outputSerializer.Stream);
            }
            int dataLength = outputStream.Data.CurrentIndex - (sizeof(uint) + sizeof(int));

            if (dataLength <= maxInputSize)
            {
                byte *write = outputStream.Data.Byte;
                *(uint *)write = (uint)commandInfo.Command | (uint)commandInfo.CommandFlags;
                *(int *)(write + sizeof(uint)) = dataLength;

                if (outputStream.Data.CurrentIndex <= Buffer.Length)
                {
                    if (start != write)
                    {
                        AutoCSer.Memory.Common.CopyNotNull(write, start, outputStream.Data.CurrentIndex);
                    }
                    clientBuffer.Data.Set(Buffer.Buffer, Buffer.StartIndex, outputStream.Data.CurrentIndex);
                }
                else
                {
                    outputStream.Data.GetSubBuffer(ref clientBuffer.CopyBuffer);
                    clientBuffer.SetSendDataCopyBuffer(outputStream.Data.CurrentIndex);
                    if (clientBuffer.CopyBuffer.Length <= SendBufferMaxSize)
                    {
                        Buffer.Free();
                        clientBuffer.CopyBuffer.CopyToClear(ref Buffer);
                    }
                }
                if (dataLength < MinCompressSize || !clientBuffer.CompressSendData(dataLength, SendMarkData))
                {
                    if (SendMarkData == 0)
                    {
                        clientBuffer.IsError = true;
                    }
                    else
                    {
                        clientBuffer.MarkSendData(dataLength, SendMarkData);
                    }
                }
            }
        }