54 lines
1.7 KiB
C#
54 lines
1.7 KiB
C#
#nullable enable
|
|
namespace Passer.Control.Core {
|
|
|
|
public class CustomMsg : IMessage {
|
|
public const byte Id = 0xB1;
|
|
public byte networkId;
|
|
public byte thingId;
|
|
public byte[] bytes;
|
|
|
|
public CustomMsg(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++];
|
|
}
|
|
public CustomMsg(byte networkId, byte thingId, byte[] bytes) : base() {
|
|
this.networkId = networkId;
|
|
this.thingId = thingId;
|
|
this.bytes = bytes;
|
|
}
|
|
public CustomMsg(byte networkId, Thing thing) : base() {
|
|
this.networkId = networkId;
|
|
this.thingId = thing.id;
|
|
this.bytes = [];
|
|
}
|
|
|
|
public override byte Serialize(ref byte[] buffer) {
|
|
if (bytes.Length == 0)
|
|
return 0;
|
|
|
|
byte ix = 0;
|
|
buffer[ix++] = CustomMsg.Id;
|
|
buffer[ix++] = this.networkId;
|
|
buffer[ix++] = this.thingId;
|
|
//buffer[ix++] = (byte)bytes.Length;
|
|
foreach (byte b in bytes)
|
|
buffer[ix++] = b;
|
|
|
|
return ix;
|
|
}
|
|
|
|
//public static async Task<bool> Receive(Stream dataStream, Participant client, byte packetSize) {
|
|
// byte[] buffer = await Receive(dataStream, packetSize);
|
|
|
|
// CustomMsg msg = new(buffer);
|
|
// client.messageQueue.Enqueue(msg);
|
|
// return true;
|
|
//}
|
|
}
|
|
|
|
} |