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 in bytes, excluding the binary data /// /// For the total size of the message this.bytes.Length should be added to this value. public const byte length = 4; /// /// The network ID identifying the thing /// public byte networkId; /// /// The ID of the thing /// public byte thingId; public Thing thing; /// /// The length of the data /// public byte dataLength; /// /// The binary data /// public byte[] data; /// /// 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.data = this.thing.GenerateBinary(); this.dataLength = (byte)this.data.Length; } /// @copydoc Passer::RoboidControl::IMessage::IMessage(byte[] buffer) public BinaryMsg(byte[] buffer) { byte ix = 1; this.networkId = buffer[ix++]; this.thingId = buffer[ix++]; this.dataLength = buffer[ix++]; this.data = new byte[this.dataLength]; for (uint dataIx = 0; dataIx < this.dataLength; dataIx++) this.data[dataIx] = buffer[ix++]; } /// @copydoc Passer::RoboidControl::IMessage::Serialize public override byte Serialize(ref byte[] buffer) { if (buffer.Length < BinaryMsg.length + this.data.Length || this.data.Length == 0) return 0; #if DEBUG System.Console.WriteLine($"Send BinaryMsg [{this.networkId}/{this.thingId}] {this.dataLength}"); #endif byte ix = 0; buffer[ix++] = BinaryMsg.Id; buffer[ix++] = this.networkId; buffer[ix++] = this.thingId; buffer[ix++] = this.dataLength; foreach (byte b in data) buffer[ix++] = b; return ix; } } }