internal void JsonSerialize <valueType>(ref valueType value) where valueType : struct { if (OutputJsonSerializer == null) { OutputJsonSerializer = Json.Serializer.YieldPool.Default.Pop() ?? new Json.Serializer(); OutputJsonSerializer.SetTcpServer(); } OutputJsonSerializer.SerializeTcpServer(ref value, OutputSerializer.Stream); }
protected void freeSerializer() { if (OutputSerializer != null) { OutputSerializer.Free(); OutputSerializer = null; if (OutputJsonSerializer != null) { OutputJsonSerializer.Free(); OutputJsonSerializer = null; } } }
internal void FreeSerializer() { OutputStream = null; if (ReceiveDeSerializer != null) { ReceiveDeSerializer.Free(); ReceiveDeSerializer = null; } if (ReceiveJsonParser != null) { ReceiveJsonParser.Free(); ReceiveJsonParser = null; } if (OutputSerializer != null) { OutputSerializer.Free(); OutputSerializer = null; } if (OutputJsonSerializer != null) { OutputJsonSerializer.Free(); OutputJsonSerializer = null; } }
/// <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.ByteSize = 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 = Json.Serializer.YieldPool.Default.Pop() ?? new Json.Serializer(); outputJsonSerializer.SetTcpServer(); } outputJsonSerializer.SerializeTcpServer(ref inputParameter, outputSerializer.Stream); } int dataLength = outputStream.ByteSize - (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.ByteSize <= Buffer.Length) { if (start != write) { Memory.CopyNotNull(write, start, outputStream.ByteSize); } clientBuffer.Data.Set(Buffer.Buffer, Buffer.StartIndex, outputStream.ByteSize); } else { outputStream.GetSubBuffer(ref clientBuffer.CopyBuffer); clientBuffer.SetSendDataCopyBuffer(outputStream.ByteSize); 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); } } } }