public void WriteCommands() { if (this.IsConnected && this.socket.CanWrite) { while (true) { MemoryStreamData data; while (true) { if (this.packetQueue.Count != 0) { CommandPacket packet = this.packetQueue.Dequeue(); try { data = this.protocolAdapter.Encode(packet); break; } catch (Exception exception) { this.log.DebugFormat("OnSocketProblem {0}", exception.Message); this.OnSocketProblem(ProblemStatus.EncodeError, exception); } finally { ClientNetworkInstancesCache.ReleaseCommandPacketWithCommandsCollection(packet); } return; } else { return; } break; } try { if (this.infoEnabled) { this.log.DebugFormat("WriteCommands {0}", data.Length); } this.socket.Write(data.GetBuffer(), 0, (int)data.Length); } catch (IOException exception2) { this.OnSocketProblem(ProblemStatus.SendError, exception2); } finally { ClientProtocolInstancesCache.ReleaseMemoryStreamData(data); } } } }
public MemoryStreamData Encode(CommandPacket commandPacket) { MemoryStreamData memoryStreamDataInstance = ClientProtocolInstancesCache.GetMemoryStreamDataInstance(); ProtocolBuffer protocolBufferInstance = ClientProtocolInstancesCache.GetProtocolBufferInstance(); List <Command> commands = commandPacket.Commands; int count = commands.Count; for (int i = 0; i < count; i++) { this.commandsCodec.Encode(protocolBufferInstance, commands[i]); } this.protocol.WrapPacket(protocolBufferInstance, memoryStreamDataInstance); ClientProtocolInstancesCache.ReleaseProtocolBufferInstance(protocolBufferInstance); return(memoryStreamDataInstance); }
private void KeepPartialReceivedData() { if (this.IsPartialDataEmpty()) { this.partialReceivedData.Position = 0L; this.partialReceivedData.SetLength(0L); } else { long position = this.partialReceivedData.Position; long length = this.partialReceivedData.Length; MemoryStreamData partialReceivedData = this.partialReceivedData; this.partialReceivedData = ClientProtocolInstancesCache.GetMemoryStreamDataInstance(); this.partialReceivedData.Write(partialReceivedData.GetBuffer(), (int)position, (int)(length - position)); ClientProtocolInstancesCache.ReleaseMemoryStreamData(partialReceivedData); } }
public bool TryDecode(out CommandPacket commandPacket) { commandPacket = null; if (this.partialReceivedData.Length == 0L) { return(false); } ProtocolBuffer protocolBufferInstance = ClientProtocolInstancesCache.GetProtocolBufferInstance(); bool flag = this.TryUnwrapPacket(protocolBufferInstance); if (flag) { this.KeepPartialReceivedData(); commandPacket = this.DecodePacket(protocolBufferInstance); } ClientProtocolInstancesCache.ReleaseProtocolBufferInstance(protocolBufferInstance); return(flag); }
public ProtocolAdapterImpl(Protocol protocol, CommandsCodec commandsCodec) { this.protocol = protocol; this.commandsCodec = commandsCodec; this.partialReceivedData = ClientProtocolInstancesCache.GetMemoryStreamDataInstance(); }
public void FreeProtocolBuffer(ProtocolBuffer protocolBuffer) { ClientProtocolInstancesCache.ReleaseProtocolBufferInstance(protocolBuffer); }
public ProtocolBuffer NewProtocolBuffer() => ClientProtocolInstancesCache.GetProtocolBufferInstance();