Improved reloading support

This commit is contained in:
Pascal Serrarens 2024-12-11 11:29:01 +01:00
parent cdd1a7a53f
commit 0f0fcfdfbf
3 changed files with 22 additions and 11 deletions

View File

@ -47,6 +47,7 @@ namespace Passer.Control {
ProcessNetworkId(networkId);
break;
case InvestigateMsg investigate:
UnityEngine.Debug.Log($"investigate [{investigate.networkId}/{investigate.thingId}]");
ProcessInvestigate(investigate);
break;
case ThingMsg thing:
@ -96,11 +97,12 @@ namespace Passer.Control {
protected virtual void ProcessDestroy(DestroyMsg destroy) { }
private void ForwardMessage(IMessage thing) {
private void ForwardMessage(IMessage msg) {
foreach (Client client in Client.clients) {
if (client == this)
continue;
IMessage.SendMsg(client, thing);
UnityEngine.Debug.Log($"---> {client.ipAddress}");
IMessage.SendMsg(client, msg);
}
}
}

View File

@ -19,6 +19,7 @@ namespace Passer.Control {
if (client == null || client.ipAddress == null)
return false;
//UnityEngine.Debug.Log($"Send msg {buffer[0]} to {client.ipAddress}");
client.udpClient.Send(buffer, buffer.Length, client.ipAddress, client.port);
return true;
}
@ -168,8 +169,8 @@ namespace Passer.Control {
this.thingId = buffer[ix++];
}
public static bool Send(Client client, byte thingId) {
InvestigateMsg msg = new(client.networkId, thingId);
public static bool Send(Client client, byte networkId, byte thingId) {
InvestigateMsg msg = new(networkId, thingId);
return SendMsg(client, msg);
}
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize) {
@ -232,6 +233,10 @@ namespace Passer.Control {
byte[] buffer = await Receive(dataStream, packetSize);
ThingMsg msg = new(buffer);
// Do no process poses with nwid == 0 (== local)
if (msg.networkId == 0)
return true;
client.messageQueue.Enqueue(msg);
return true;
}
@ -406,13 +411,16 @@ namespace Passer.Control {
return SendMsg(client, msg);
}
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize) {
UnityEngine.Debug.Log("Receive pose");
if (packetSize != length)
return false;
byte[] buffer = await Receive(dataStream, packetSize);
PoseMsg msg = new(buffer);
// Do no process poses with nwid == 0 (== local)
if (msg.networkId == 0)
return true;
client.messageQueue.Enqueue(msg);
return true;
}
@ -436,25 +444,25 @@ namespace Passer.Control {
}
public override byte[] Serialize() {
byte[] buffer = new byte[4 + this.bytes.Length];
byte[] buffer = new byte[3 + this.bytes.Length];
byte ix = 0;
buffer[ix++] = CustomMsg.Id;
buffer[ix++] = this.networkId;
buffer[ix++] = this.thingId;
buffer[ix++] = (byte)bytes.Length;
//buffer[ix++] = (byte)bytes.Length;
foreach (byte b in bytes)
buffer[ix++] = b;
return buffer;
}
public override void Deserialize(byte[] buffer) {
uint ix = 0;
byte ix = 0;
this.networkId = buffer[ix++];
this.thingId = buffer[ix++];
byte length = buffer[ix++];
byte length = (byte)(buffer.Length - ix);//buffer[ix++];
this.bytes = new byte[length];
for (uint bytesIx = 0; ix < length; ix++, bytesIx++)
this.bytes[bytesIx] = buffer[ix];
for (uint bytesIx = 0; bytesIx < length; bytesIx++)
this.bytes[bytesIx] = buffer[ix++];
}
public static void Send(Client client, byte thingId, byte[] bytes) {

View File

@ -21,6 +21,7 @@ namespace Passer.Control {
return;
}
//UnityEngine.Debug.Log($"R {msgId} from {client.ipAddress}");
bool result = false;
switch (msgId) {
case ClientMsg.Id: // 0xA0 / 160