/// <summary> /// Attempts to deserialize a binary message from the provided stream /// </summary> /// <param name="data">The byte array which contains the the binary message data to be deserialized</param> /// <param name="message">When this method returns contains the binary message contained in the byte array if the serialization succeeded, or null if the serialization failed</param> /// <param name="ex">When this method returns contains the exception to be thrown if the serialization failed, or null if the serialization succeeded</param> /// <returns>true if the binary message was successfully deserialized from the byte array, false otherwise</returns> public static bool TryDeserialize(byte[] data, out BinaryMessage message, out Exception ex) { using (MemoryStream ms = new MemoryStream(data)) { return(TryDeserialize(ms, out message, out ex)); } }
/// <summary> /// Writes the packet size sub-chunk in the serialization stream /// </summary> /// <param name="stream">The stream where the data will be written</param> /// <param name="message">The message from where the source module will be read</param> /// <param name="packetSizePosition">The position in the stream where to write the packet size</param> /// <param name="packetStartPosition">The position in the stream where the packet starts</param> private static void SerializePacketSize(Stream stream, BinaryMessage message, long packetStartPosition, long packetSizePosition) { byte[] buffer; long packetChecksumPosition; uint packetSize; packetSize = (uint)(stream.Position - packetStartPosition); buffer = BitConverter.GetBytes((uint)packetSize); packetChecksumPosition = stream.Position; stream.Position = packetSizePosition; stream.Write(buffer, 0, buffer.Length); stream.Position = packetChecksumPosition; }
/// <summary> /// Attempts to deserialize a binary message from the provided stream /// </summary> /// <param name="stream">The stream which contains the the binary message data to be deserialized</param> /// <param name="message">When this method returns contains the binary message contained in the stream if the serialization succeeded, or null if the serialization failed</param> /// <param name="ex">When this method returns contains the exception to be thrown if the serialization failed, or null if the serialization succeeded</param> /// <returns>true if the binary message was successfully deserialized from the stream, false otherwise</returns> public static bool TryDeserialize(Stream stream, out BinaryMessage message, out Exception ex) { uint packetSize; message = null; ex = null; if (!ReadHeader(stream, out ex)) { return(false); } if (!ReadPacketSize(stream, out packetSize, out ex)) { return(false); } return(true); }
/// <summary> /// Attempts to serialize a binary message into the provided stream /// </summary> /// <param name="stream">The stream where to serialize the binary message</param> /// <param name="message">The binary message to serialize</param> /// <param name="ex">When this method returns contains the exception to be thrown if the serialization failed, or null if the serialization succeeded</param> /// <returns>true if the binary message was successfully serialized to the stream, false otherwise</returns> public static bool TrySerialize(Stream stream, BinaryMessage message, out Exception ex) { byte[] buffer; long packetStartPosition; long packetSizePosition; uint checksum; ex = null; if (stream == null) { ex = new ArgumentNullException("The stream can not be null"); return(false); } if (message == null) { ex = new ArgumentNullException("The message can not be null"); return(false); } // Write header packetStartPosition = stream.Position; buffer = BitConverter.GetBytes((ushort)BinaryMessage.PacketStart); stream.Write(buffer, 0, buffer.Length); // Reserve space for packet length packetSizePosition = stream.Position; buffer = BitConverter.GetBytes((uint)0x00000000); stream.Write(buffer, 0, buffer.Length); // Write message type stream.WriteByte((byte)message.MessageType); // Write the Timestamp buffer = BitConverter.GetBytes((uint)0x00000000); stream.Write(buffer, 0, buffer.Length); // Write the source module name SerializeSourceModule(stream, message); // Write the destination module name SerializeDestinationModule(stream, message); // Write the message data if (!message.TrySerializeData(stream, out ex)) { return(false); } // Write the packet size (it is necesary for calculating the checksum) SerializePacketSize(stream, message, packetStartPosition, packetSizePosition); // Calculate and write checksum checksum = CalculateChecksum(stream, packetStartPosition); buffer = BitConverter.GetBytes((uint)0x00000000); stream.Write(buffer, 0, buffer.Length); stream.Flush(); return(true); }
/// <summary> /// Writes the source module sub-chunk in the serialization stream /// </summary> /// <param name="stream">The stream where the data will be written</param> /// <param name="message">The message from where the source module will be read</param> private static void SerializeSourceModule(Stream stream, BinaryMessage message) { SerializeToStream(stream, message.SourceModuleName); }
/// <summary> /// Writes the destination module sub-chunk in the serialization stream /// </summary> /// <param name="stream">The stream where the data will be written</param> /// <param name="message">The message from where the destination module will be read</param> private static void SerializeDestinationModule(Stream stream, BinaryMessage message) { SerializeToStream(stream, message.DestinationModuleName); }