86 lines
3.1 KiB
C#
86 lines
3.1 KiB
C#
namespace Passer.Control.Core {
|
|
|
|
public class ThingMsg : IMessage {
|
|
public const byte length = 5;
|
|
public const byte id = 0x80;
|
|
public byte networkId;
|
|
public byte thingId;
|
|
public byte thingType;
|
|
public byte parentId;
|
|
|
|
public ThingMsg(byte networkId, Thing thing) {
|
|
this.networkId = networkId;
|
|
this.thingId = thing.id;
|
|
this.thingType = thing.type;
|
|
this.parentId = thing.parent.id;
|
|
}
|
|
public ThingMsg(byte networkId, byte thingId, byte thingType, byte parentId) {
|
|
this.networkId = networkId;
|
|
this.thingId = thingId;
|
|
this.thingType = thingType;
|
|
this.parentId = parentId;
|
|
}
|
|
public ThingMsg(byte[] buffer) : base(buffer) { }
|
|
|
|
public override byte Serialize(ref byte[] buffer) {
|
|
byte ix = 0;
|
|
buffer[ix++] = ThingMsg.id;
|
|
buffer[ix++] = this.networkId;
|
|
buffer[ix++] = this.thingId;
|
|
buffer[ix++] = this.thingType;
|
|
buffer[ix++] = this.parentId;
|
|
return ThingMsg.length;
|
|
}
|
|
public override void Deserialize(byte[] buffer) {
|
|
uint ix = 0;
|
|
this.networkId = buffer[ix++];
|
|
this.thingId = buffer[ix++];
|
|
this.thingType = buffer[ix++];
|
|
this.parentId = buffer[ix];
|
|
}
|
|
|
|
public static bool Send(Participant client, Thing thing) {
|
|
ThingMsg msg = new(thing.networkId, thing.id, thing.type, thing.parent.id);
|
|
return SendMsg(client, msg);
|
|
}
|
|
//public static bool Send(Client client, byte networkId, byte thingId, byte thingType, byte parentId)
|
|
//{
|
|
// ThingMsg msg = new(networkId, thingId, thingType, parentId);
|
|
// //UnityEngine.Debug.Log($"Send thing [{msg.networkId}/{msg.thingId}]");
|
|
// 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);
|
|
ThingMsg msg = new(buffer);
|
|
//UnityEngine.Debug.Log($"Receive thing [{msg.networkId}/{msg.thingId}]");
|
|
|
|
// Do no process poses with nwid == 0 (== local)
|
|
//if (msg.networkId == 0)
|
|
// return true;
|
|
|
|
client.messageQueue.Enqueue(msg);
|
|
return true;
|
|
}
|
|
|
|
public static bool SendTo(Participant participant, Thing thing) {
|
|
if (participant == null || thing == null)
|
|
return false;
|
|
|
|
byte ix = 0;
|
|
participant.buffer[ix++] = ThingMsg.id;
|
|
participant.buffer[ix++] = participant.networkId;
|
|
participant.buffer[ix++] = thing.id;
|
|
participant.buffer[ix++] = thing.type;
|
|
if (thing.parent != null)
|
|
participant.buffer[ix++] = thing.parent.id;
|
|
else
|
|
participant.buffer[ix++] = 0;
|
|
|
|
return participant.SendBuffer(ix);
|
|
}
|
|
}
|
|
|
|
} |