78 lines
2.6 KiB
C#
78 lines
2.6 KiB
C#
namespace RoboidControl {
|
|
|
|
/// <summary>
|
|
/// A message containing binary data for custom communication
|
|
/// </summary>
|
|
public class BinaryMsg : IMessage {
|
|
/// <summary>
|
|
/// The message ID
|
|
/// </summary>
|
|
public const byte Id = 0xB1;
|
|
/// <summary>
|
|
/// The length of the message in bytes, excluding the binary data
|
|
/// </summary>
|
|
/// For the total size of the message this.bytes.Length should be added to this value.
|
|
public const byte length = 4;
|
|
/// <summary>
|
|
/// The network ID identifying the thing
|
|
/// </summary>
|
|
public byte networkId;
|
|
/// <summary>
|
|
/// The ID of the thing
|
|
/// </summary>
|
|
public byte thingId;
|
|
|
|
public Thing thing;
|
|
/// <summary>
|
|
/// The length of the data
|
|
/// </summary>
|
|
public byte dataLength;
|
|
/// <summary>
|
|
/// The binary data
|
|
/// </summary>
|
|
public byte[] data;
|
|
|
|
/// <summary>
|
|
/// Create an empty message for sending
|
|
/// </summary>
|
|
/// <param name="networkId">The netowork ID of the thing</param>
|
|
/// <param name="thingId">The ID of the thing</param>
|
|
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;
|
|
}
|
|
}
|
|
|
|
} |