Used Client override for processing messages

This commit is contained in:
Pascal Serrarens 2024-12-09 10:02:19 +01:00
parent 355dd5c1c5
commit fbeed8e809
4 changed files with 548 additions and 434 deletions

97
Client.cs Normal file
View File

@ -0,0 +1,97 @@
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Net.Sockets;
namespace Passer.Control {
public class Client {
//public ConnectionMethod connection;
public UdpClient udpClient;
public string ipAddress;
public int port;
public byte networkId;
public readonly ConcurrentQueue<IMessage> messageQueue = new();
public static Client GetClient(string ipAddress, int port) {
foreach (Client c in clients) {
if (c.ipAddress == ipAddress && c.port == port)
return c;
}
return null;
}
static public List<Client> clients = new List<Client>();
public static Client NewClient() {
Client client = new();
clients.Add(client);
client.networkId = 0;
return client;
}
public static Client NewUDPClient(UdpClient udpClient, string ipAddress, int port) {
Client client = NewClient();
client.ipAddress = null;
client.port = port;
client.udpClient = udpClient;
return client;
}
public void ProcessMessage(IMessage msg) {
switch (msg) {
case ClientMsg clientMsg:
ProcessClient(clientMsg);
break;
case NetworkIdMsg networkId:
ProcessNetworkId(networkId);
break;
case InvestigateMsg investigate:
ProcessInvestigate(investigate);
break;
case ThingMsg thing:
ProcessThing(thing);
break;
case NameMsg name:
ProcessName(name);
break;
case ModelUrlMsg modelUrl:
ProcessModelUrl(modelUrl);
break;
case PoseMsg pose:
ProcessPose(pose);
break;
case CustomMsg custom:
ProcessCustom(custom);
break;
case TextMsg text:
ProcessText(text);
break;
case DestroyMsg destroy:
ProcessDestroy(destroy);
break;
}
}
protected virtual void ProcessClient(ClientMsg client) { }
protected virtual void ProcessNetworkId(NetworkIdMsg networkId) { }
protected virtual void ProcessInvestigate(InvestigateMsg investigate) { }
protected virtual void ProcessThing(ThingMsg thing) { }
protected virtual void ProcessName(NameMsg name) { }
protected virtual void ProcessModelUrl(ModelUrlMsg modelUrl) { }
protected virtual void ProcessPose(PoseMsg pose) { }
protected virtual void ProcessCustom(CustomMsg custom) { }
protected virtual void ProcessText(TextMsg text) { }
protected virtual void ProcessDestroy(DestroyMsg destroy) { }
}
}

2
Client.cs.meta Normal file
View File

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

View File

@ -1,48 +1,8 @@
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.IO; using System.IO;
using System.Net.Sockets;
using System.Threading.Tasks; using System.Threading.Tasks;
using System;
namespace Passer.Control { namespace Passer.Control {
public class Client {
//public ConnectionMethod connection;
public UdpClient udpClient;
public string ipAddress;
public int port;
public byte networkId;
public readonly ConcurrentQueue<IMessage> messageQueue = new();
public static Client GetClient(string ipAddress, int port) {
foreach (Client c in clients) {
if (c.ipAddress == ipAddress && c.port == port)
return c;
}
return null;
}
static public List<Client> clients = new List<Client>();
public static Client NewClient() {
Client client = new();
clients.Add(client);
client.networkId = 0;
return client;
}
public static Client NewUDPClient(UdpClient udpClient, string ipAddress, int port) {
Client client = NewClient();
client.ipAddress = null;
client.port = port;
client.udpClient = udpClient;
return client;
}
}
public class IMessage { public class IMessage {
public IMessage() { } public IMessage() { }
public IMessage(byte[] data) { public IMessage(byte[] data) {
@ -189,7 +149,7 @@ namespace Passer.Control {
#region Investigate #region Investigate
class InvestigateMsg : IMessage { public class InvestigateMsg : IMessage {
public const byte Id = 0x81; public const byte Id = 0x81;
public const byte length = 3; public const byte length = 3;
public byte networkId; public byte networkId;
@ -455,24 +415,25 @@ namespace Passer.Control {
#endregion Pose #endregion Pose
#region Bytes #region Custom
public class BytesMsg : IMessage { public class CustomMsg : IMessage {
public const byte Id = 0xB1; public const byte Id = 0xB1;
public byte networkId; public byte networkId;
public byte thingId; public byte thingId;
public byte[] bytes; public byte[] bytes;
public BytesMsg(byte[] data) : base(data) { } public CustomMsg(byte[] data) : base(data) { }
public BytesMsg(byte networkId, byte thingId, byte[] bytes) : base() { public CustomMsg(byte networkId, byte thingId, byte[] bytes) : base() {
this.networkId = networkId; this.networkId = networkId;
this.thingId = thingId; this.thingId = thingId;
this.bytes = bytes; this.bytes = bytes;
} }
public override byte[] Serialize() { public override byte[] Serialize() {
byte[] buffer = new byte[4 + this.bytes.Length]; byte[] buffer = new byte[4 + this.bytes.Length];
int ix = 0; int ix = 0;
buffer[ix++] = BytesMsg.Id; buffer[ix++] = CustomMsg.Id;
buffer[ix++] = this.networkId; buffer[ix++] = this.networkId;
buffer[ix++] = this.thingId; buffer[ix++] = this.thingId;
buffer[ix++] = (byte)bytes.Length; buffer[ix++] = (byte)bytes.Length;
@ -482,7 +443,6 @@ namespace Passer.Control {
return buffer; return buffer;
} }
public override void Deserialize(byte[] data) { public override void Deserialize(byte[] data) {
//this.bytes = data;
uint ix = 0; uint ix = 0;
this.thingId = data[ix++]; this.thingId = data[ix++];
this.bytes = new byte[data.Length - ix]; this.bytes = new byte[data.Length - ix];
@ -492,20 +452,42 @@ namespace Passer.Control {
public static void Send(Client client, byte thingId, byte[] bytes) { public static void Send(Client client, byte thingId, byte[] bytes) {
BytesMsg msg = new(client.networkId, thingId, bytes); CustomMsg msg = new(client.networkId, thingId, bytes);
SendMsg(client, msg); SendMsg(client, msg);
} }
// received bytes
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize) { public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize) {
byte[] buffer = await Receive(dataStream, packetSize); byte[] buffer = await Receive(dataStream, packetSize);
BytesMsg msg = new(buffer); CustomMsg msg = new(buffer);
client.messageQueue.Enqueue(msg); client.messageQueue.Enqueue(msg);
return true; return true;
} }
} }
#endregion Bytes #endregion Custom
#region Text
public class TextMsg : IMessage {
public const byte Id = 0xB0;
public string text;
public TextMsg(byte[] data) : base(data) { }
public override void Deserialize(byte[] data) {
uint ix = 0;
uint strlen = data[ix++];
this.text = System.Text.Encoding.UTF8.GetString(data, (int)ix, (int)strlen);
}
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize) {
byte[] buffer = await Receive(dataStream, packetSize);
TextMsg msg = new(buffer);
client.messageQueue.Enqueue(msg);
return true;
}
}
#endregion
#region Destroy #region Destroy

View File

@ -26,35 +26,35 @@ namespace Passer.Control {
bool result = false; bool result = false;
switch (msgId) { switch (msgId) {
case PoseMsg.Id: // Object Pose (16) case ClientMsg.Id: // 0xA0 / 160
result = await PoseMsg.Receive(dataStream, client, packetSize);
break;
case DestroyMsg.Id: // Destroy object (32)
result = await DestroyMsg.Receive(dataStream, client, packetSize);
break;
case ThingMsg.Id:
result = await ThingMsg.Receive(dataStream, client, packetSize);
break;
case InvestigateMsg.Id:
result = await InvestigateMsg.Receive(dataStream, client, packetSize);
break;
case ModelUrlMsg.Id: // Model URL (144)
result = await BytesMsg.Receive(dataStream, client, packetSize);
break;
case NameMsg.Id: // Object Name (145)
result = await NameMsg.Receive(dataStream, client, packetSize);
break;
case ClientMsg.Id:
result = await ClientMsg.Receive(dataStream, client, packetSize); result = await ClientMsg.Receive(dataStream, client, packetSize);
break; break;
case NetworkIdMsg.Id: case NetworkIdMsg.Id: // 0xA1 / 161
result = await NetworkIdMsg.Receive(dataStream, client, packetSize); result = await NetworkIdMsg.Receive(dataStream, client, packetSize);
break; break;
//case TextMsg.Id: // Text (176) case InvestigateMsg.Id: // 0x81
// result = await TextMsg.Receive(dataStream, client, packetSize); result = await InvestigateMsg.Receive(dataStream, client, packetSize);
// break; break;
case BytesMsg.Id: case ThingMsg.Id: // 0x80 / 128
result = await BytesMsg.Receive(dataStream, client, packetSize); result = await ThingMsg.Receive(dataStream, client, packetSize);
break;
case NameMsg.Id: // 0x91 / 145
result = await NameMsg.Receive(dataStream, client, packetSize);
break;
case ModelUrlMsg.Id: // 0x90 / 144
result = await ModelUrlMsg.Receive(dataStream, client, packetSize);
break;
case PoseMsg.Id: // 0x10 / 16
result = await PoseMsg.Receive(dataStream, client, packetSize);
break;
case CustomMsg.Id: // 0xB1 / 177
result = await CustomMsg.Receive(dataStream, client, packetSize);
break;
case TextMsg.Id: // 0xB0 / 176
result = await TextMsg.Receive(dataStream, client, packetSize);
break;
case DestroyMsg.Id: // 0x20 / 32
result = await DestroyMsg.Receive(dataStream, client, packetSize);
break; break;
default: default:
break; break;
@ -65,21 +65,54 @@ namespace Passer.Control {
} }
} }
public static void ProcessMessage(ISiteServer site, Client client, IMessage msg) { //public static void ProcessMessage(ISiteServer site, Client client, IMessage msg) {
switch (msg) { // client.ProcessMessage(site, client, msg);
case NetworkIdMsg networkId: // switch (msg) {
site.ProcessNetworkId(client, networkId); // case ClientMsg clientMsg:
break; // site.ProcessClient(client, clientMsg);
case ModelUrlMsg modelUrl: // break;
site.ProcessModelUrl(client, modelUrl); // case NetworkIdMsg networkId:
break; // site.ProcessNetworkId(client, networkId);
} // break;
} // case InvestigateMsg investigate:
// site.ProcessInvestigate(client, investigate);
// break;
// case ThingMsg thing:
// site.ProcessThing(client, thing);
// break;
// case NameMsg name:
// site.ProcessName(client, name);
// break;
// case ModelUrlMsg modelUrl:
// site.ProcessModelUrl(client, modelUrl);
// break;
// case PoseMsg pose:
// site.ProcessPose(client, pose);
// break;
// case CustomMsg custom:
// site.ProcessCustom(client, custom);
// break;
// case TextMsg text:
// site.ProcessText(client, text);
// break;
// case DestroyMsg destroy:
// site.ProcessDestroy(client, destroy);
// break;
// }
//}
} }
public interface ISiteServer { //public interface ISiteServer {
public void ProcessNetworkId(Client client, NetworkIdMsg networkId); // public void ProcessClient(Client client, ClientMsg clientMsg);
public void ProcessModelUrl(Client client, ModelUrlMsg modelUrl); // public void ProcessNetworkId(Client client, NetworkIdMsg networkId);
} // public void ProcessInvestigate(Client client, InvestigateMsg investigate);
// public void ProcessThing(Client client, ThingMsg thing);
// public void ProcessName(Client client, NameMsg name);
// public void ProcessModelUrl(Client client, ModelUrlMsg modelUrl);
// public void ProcessPose(Client client, PoseMsg pose);
// public void ProcessCustom(Client client, CustomMsg custom);
// public void ProcessText(Client client, TextMsg text);
// public void ProcessDestroy(Client client, DestroyMsg destroy);
//}
} }