namespace RoboidControl { /// /// A message containing binary data for custom communication /// public class BinaryMsg : IMessage { /// /// The message ID /// public const byte Id = 0xB1; /// /// The length of the message, excluding the binary data /// /// For the total size of the message this.bytes.Length should be added to this value. public const byte length = 2; /// /// The network ID identifying the thing /// public byte networkId; /// /// The ID of the thing /// public byte thingId; public Thing thing; /// /// The binary data /// public byte[] bytes; /// /// Create a new message for sending /// /// The netowork ID of the thing /// The ID of the thing /// The binary data for the thing public BinaryMsg(byte networkId, byte thingId, byte[] bytes) : base() { this.networkId = networkId; this.thingId = thingId; this.bytes = bytes; } /// /// Create an empty message for sending /// /// The netowork ID of the thing /// The ID of the thing public BinaryMsg(byte networkId, Thing thing) : base() { this.networkId = networkId; this.thingId = thing.id; this.thing = thing; this.bytes = this.thing.GenerateBinary(); // if (this.bytes.Length > 0) // System.Console.Write($"Binary message for [{networkId}/{thing.id}]"); } /// @copydoc Passer::RoboidControl::IMessage::IMessage(byte[] buffer) public BinaryMsg(byte[] buffer) { byte ix = 1; this.networkId = buffer[ix++]; this.thingId = buffer[ix++]; byte length = (byte)(buffer.Length - ix); this.bytes = new byte[length]; for (uint bytesIx = 0; bytesIx < length; bytesIx++) this.bytes[bytesIx] = buffer[ix++]; } /// @copydoc Passer::RoboidControl::IMessage::Serialize public override byte Serialize(ref byte[] buffer) { if (buffer.Length < BinaryMsg.length + this.bytes.Length || this.bytes.Length == 0) return 0; byte ix = 0; buffer[ix++] = BinaryMsg.Id; buffer[ix++] = this.networkId; buffer[ix++] = this.thingId; foreach (byte b in bytes) buffer[ix++] = b; return ix; } } }