public ManualResetEventSlim Send(CommandObject command) { if (!command.Schema.ProcessRuntimeID.HasValue) { return(Send(PacketMethods.CreatePacket(PacketContents.CommandSchemaWithData, 0, command.ToCommand().ToArray()))); } else { int schemeRuntimeID; lock (m_knownSchemas) { if (!m_knownSchemas.TryGetValue(command.Schema.ProcessRuntimeID.Value, out schemeRuntimeID)) { if (command.Schema.ProcessRuntimeID == -1) { throw new Exception("Cannot serialize a schema that has not been defined in this process."); } schemeRuntimeID = m_knownSchemas.Count; m_knownSchemas.Add(command.Schema.ProcessRuntimeID.Value, schemeRuntimeID); Send(command.Schema.ToCommand(schemeRuntimeID)); } } return(Send(command.ToDataCommandPacket(schemeRuntimeID))); } }
public int ReadFromBuffer(out CtpCommand packet, byte[] buffer, int current, int length) { //Note: The code in these two modules are identical: packet = null; if (m_disposed) { return(0); } if (length < 2) { return(0); } if (!PacketMethods.TryReadPacket(buffer, current, length, MaximumPacketSize, out PacketContents payloadType, out int payloadFlags, out byte[] payloadBuffer, out int consumedLength)) { return(0); } switch (payloadType) { case PacketContents.CommandSchema: var scheme = new CtpCommandSchema(payloadBuffer); m_inboundSchemes[payloadFlags] = scheme; if (m_inboundSchemes.Count > MaximumSchemeCount) { throw new Exception("Too many schemes have been defined."); } break; case PacketContents.CommandSchemaWithData: packet = new CtpCommand(payloadBuffer); break; case PacketContents.CommandData: if (m_inboundSchemes.TryGetValue(payloadFlags, out var commandSchema)) { packet = new CtpCommand(commandSchema, payloadBuffer); } break; case PacketContents.CompressedDeflate: case PacketContents.CompressedZlib: packet = Inflate(payloadType, payloadFlags, payloadBuffer); break; default: throw new ArgumentOutOfRangeException(); } return(consumedLength); }
private ManualResetEventSlim Send(PooledBuffer packet) { if (packet.Length > MaximumPacketSize) { throw new Exception("This packet is too large to send, if this is a legitimate size, increase the MaxPacketSize."); } switch (m_compressionMode) { case CtpCompressionMode.None: return(m_send(packet)); break; case CtpCompressionMode.Deflate: using (var ms = new MemoryStream()) { using (var comp = new DeflateStream(ms, CompressionMode.Compress, true)) { packet.CopyTo(comp); } return(m_send(PacketMethods.CreatePacket(PacketContents.CompressedDeflate, packet.Length, ms.ToArray()))); } break; case CtpCompressionMode.Zlib: if (m_stream == null) { m_stream = new MemoryStream(); m_deflate = new Ionic.Zlib.DeflateStream(m_stream, Ionic.Zlib.CompressionMode.Compress); m_deflate.FlushMode = FlushType.Sync; } m_stream.Write(BigEndian.GetBytes(packet.Length), 0, 4); packet.CopyTo(m_deflate); m_deflate.Flush(); byte[] rv = m_stream.ToArray(); m_stream.SetLength(0); return(m_send(PacketMethods.CreatePacket(PacketContents.CompressedZlib, packet.Length, rv))); break; default: throw new ArgumentOutOfRangeException(); } }