Merge commit 'a8983f5a7a1bff05017b592a6e8b23208f2aac3c'

This commit is contained in:
Pascal Serrarens 2025-01-14 12:06:50 +01:00
commit b28a6bd3d9
24 changed files with 82 additions and 86 deletions

View File

@ -1,2 +0,0 @@
fileFormatVersion: 2
guid: f73ac1131d4d7bd4a8f5b62500069867

View File

@ -1,3 +1,4 @@
/*
namespace Passer.Control.Core {
public class CustomMsg : IMessage {
@ -42,4 +43,5 @@ namespace Passer.Control.Core {
//}
}
}
}
*/

View File

@ -1,3 +1,4 @@
/*
using System;
using System.IO;
using System.Threading.Tasks;
@ -170,4 +171,5 @@ public class EchoStream : Stream {
public override void SetLength(long value) {
throw new NotImplementedException();
}
}
}
*/

View File

@ -1,2 +0,0 @@
fileFormatVersion: 2
guid: 422516a56cbf14d46aaa0b1bc09115bf

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 028f10636fdaa594a9a9969924e5ff18
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 157cf85b523c1f648adc007539f8b736
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: d3f26f22a5422a44997b39a1844ab2ad
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

45
Messages/CustomMsg.cs Normal file
View File

@ -0,0 +1,45 @@
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 override byte Serialize(ref byte[] buffer) {
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;
//}
}
}

View File

@ -1,2 +0,0 @@
fileFormatVersion: 2
guid: 462c645008baabe46b81f5d0ab83e26e

View File

@ -1,2 +0,0 @@
fileFormatVersion: 2
guid: c51dae79b8c6f5a428349e1f4d34982c

View File

@ -234,16 +234,16 @@ namespace Passer.Control.Core {
Console.WriteLine($"received name {msg.url}");
}
protected virtual void ProcessPoseMsg(PoseMsg msg) { }
protected virtual void Process(PoseMsg msg) { }
protected virtual void Process(CustomMsg msg) {
Thing thing = Thing.Get(msg.networkId, msg.thingId);
thing?.ProcessBytes(msg.bytes);
}
protected virtual void ProcessTextMsg(TextMsg temsgxt) { }
protected virtual void Process(TextMsg temsgxt) { }
protected virtual void ProcessDestroyMsg(DestroyMsg msg) { }
protected virtual void Process(DestroyMsg msg) { }
private void ForwardMessage(IMessage msg) {
foreach (Participant client in others) {

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 302ed9b89a108a4429ea274044424a03
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,7 +0,0 @@
fileFormatVersion: 2
guid: 3b79b5b373e9ced4abe72eb4d2f83c6a
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,3 +1,5 @@
using System;
namespace Passer.Control.Core {
public class TemperatureSensor : Thing {
@ -7,7 +9,7 @@ namespace Passer.Control.Core {
public override void ProcessBytes(byte[] bytes) {
byte ix = 0;
float temp = LowLevelMessages.ReceiveFloat16(bytes, ref ix);
UnityEngine.Debug.Log($"temperature {this.name} = {temp} C");
Console.WriteLine($"temperature {this.name} = {temp} C");
}
}

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
@ -14,9 +15,11 @@ namespace Passer.Control.Core {
this.udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, port));
this.udpClient.BeginReceive(new AsyncCallback(result => ReceiveUDP(result)), null);
this.name = "Site Server";
Register<TemperatureSensor>((byte)Thing.Type.TemperatureSensor);
}
public override void Update(long currentTimeMs) {
public override void Update(long currentTimeMs) {
Thing.UpdateAll(currentTimeMs);
}
@ -30,17 +33,28 @@ namespace Passer.Control.Core {
protected override void Process(Participant sender, NetworkIdMsg msg) {
}
delegate Thing ThingConstructor(byte networkId, byte thingId);
readonly Dictionary<byte, ThingConstructor> thingMsgProcessors = new();
public void Register<ThingClass>(byte thingType) where ThingClass : Thing {
thingMsgProcessors[thingType] = (byte networkId, byte thingId) => {
if (Activator.CreateInstance(typeof(Thing), networkId, thingId) is not ThingClass instance)
throw new InvalidOperationException($"Could not created an instance of {(typeof(ThingClass))}.");
return instance;
};
Console.WriteLine($"Registering {typeof(ThingClass)} for thing type {thingType}");
}
protected override void Process(ThingMsg msg) {
Thing thing = Thing.Get(msg.networkId, msg.thingId);
if (thing == null) {
switch ((Thing.Type) msg.thingType) {
case Thing.Type.TemperatureSensor:
new TemperatureSensor(msg.networkId, msg.thingId);
break;
default:
new Thing(this, msg.networkId, msg.thingId, msg.thingType);
break;
}
if (thingMsgProcessors.TryGetValue(msg.thingType, out ThingConstructor thingConstructor)) {
thingConstructor(msg.networkId, msg.thingId);
}
else {
new Thing(this, msg.networkId, msg.thingId, msg.thingType);
}
}
}

View File

@ -1,2 +0,0 @@
fileFormatVersion: 2
guid: 53345abb9310d344baa67c19a8d8249f

View File

@ -193,7 +193,7 @@ namespace Passer.Control.Core {
allThings.Add(thing);
if (invokeEvent)
OnNewThing?.Invoke(thing);
UnityEngine.Debug.Log($"Add thing [{thing.networkId}/{thing.id}] {thing.name}");
//UnityEngine.Debug.Log($"Add thing [{thing.networkId}/{thing.id}] {thing.name}");
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 7429282ee0e367445bd1e2111631b27d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,4 +1,4 @@
using System;
using System;
using System.Threading;
using NUnit.Framework;