/// <summary> /// Writes X Protocol frames to the X Plugin. /// </summary> /// <param name="id">The integer representation of the client message identifier used for the message.</param> /// <param name="message">The message to include in the X Protocol frame.</param> public void Write(int id, IMessage message) { var messageSize = message.CalculateSize(); if (CompressionController != null && CompressionController.IsCompressionEnabled && messageSize > XCompressionController.COMPRESSION_THRESHOLD && CompressionController.ClientSupportedCompressedMessages.Contains((ClientMessageId)id) ) { // Build the compression protobuf message. var messageHeader = new byte[5]; var messageBytes = message.ToByteArray(); byte[] payload = new byte[messageHeader.Length + messageBytes.Length]; var sizeArray = BitConverter.GetBytes(messageSize + 1); Buffer.BlockCopy(sizeArray, 0, messageHeader, 0, sizeArray.Length); messageHeader[4] = (byte)id; Buffer.BlockCopy(messageHeader, 0, payload, 0, messageHeader.Length); Buffer.BlockCopy(messageBytes, 0, payload, messageHeader.Length, messageBytes.Length); var compression = new Compression(); compression.UncompressedSize = (ulong)(messageSize + messageHeader.Length); compression.ClientMessages = (ClientMessages.Types.Type)id; compression.Payload = ByteString.CopyFrom(CompressionController.Compress(payload)); // Build the X Protocol frame. _stream.Write(BitConverter.GetBytes(compression.CalculateSize() + 1), 0, 4); _stream.WriteByte((byte)(ClientMessageId.COMPRESSION)); if (messageSize > 0) { compression.WriteTo(_stream); } } else { _stream.Write(BitConverter.GetBytes(messageSize + 1), 0, 4); _stream.WriteByte((byte)id); if (messageSize > 0) { message.WriteTo(_stream); } } _stream.Flush(); }