RoboidControl-csharp/NetworkIdMsg.cs
2025-01-01 09:58:15 +01:00

50 lines
1.5 KiB
C#

namespace Passer.Control.Core {
public class NetworkIdMsg : IMessage {
public const byte Id = 0xA1;
public const byte length = 2;
public byte networkId;
public NetworkIdMsg(byte networkId) {
this.networkId = networkId;
}
NetworkIdMsg(byte[] buffer) : base(buffer) { }
public override byte Serialize(ref byte[] buffer) {
buffer[0] = NetworkIdMsg.Id;
buffer[1] = this.networkId;
return NetworkIdMsg.length;
}
public override void Deserialize(byte[] buffer) {
uint ix = 1;
this.networkId = buffer[ix];
}
public static bool Send(Participant client, byte networkId) {
NetworkIdMsg msg = new(networkId);
return SendMsg(client, msg);
}
public static async Task<bool> Receive(Stream dataStream, Participant client, byte packetSize) {
if (packetSize != length)
return false;
byte[] buffer = await Receive(dataStream, packetSize);
NetworkIdMsg msg = new(buffer);
client.messageQueue.Enqueue(msg);
return true;
}
public static bool SendTo(Participant participant, byte networkId) {
if (participant == null)
return false;
byte ix = 0;
participant.buffer[ix++] = NetworkIdMsg.Id;
participant.buffer[ix++] = networkId;
return participant.SendBuffer(ix);
}
}
}