using System; using System.Collections.Generic; using System.Collections.Concurrent; using System.Net; using System.Net.Sockets; namespace Passer.Control.Core { public class Participant : RemoteParticipant { public byte[] buffer = new byte[1024]; public ulong publishInterval = 3000; // = 3 seconds //public byte networkId = 0; public string name = "Participant"; public IPEndPoint endPoint = null; public UdpClient udpClient = null; public string broadcastIpAddress = "255.255.255.255"; public readonly ConcurrentQueue messageQueue = new(); #region Init /// /// Create a porticiapnt /// public Participant() { others.Add(this); } /// /// Create a participant with the give UDP port /// /// The port number on which to communicate public Participant(int port) : this() { this.port = port; } /// /// Create a new participant for a site at the given address and port /// /// The ip address of the site server /// The port number of the site server public Participant(string ipAddress = "0.0.0.0", int port = 7681) : this() { this.ipAddress = ipAddress; this.port = port; this.endPoint = new IPEndPoint(IPAddress.Parse(ipAddress), port); // for sending this.udpClient = new UdpClient(port); // for receiving this.udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, port)); this.udpClient.BeginReceive(new AsyncCallback(result => ReceiveUDP(result)), null); } /// /// Create a participant using the given udp client /// /// UDP client to use for communication /// The port number on which to communicate public Participant(UdpClient udpClient, int port) : this() { this.udpClient = udpClient; this.port = port; } public List others = new List(); public RemoteParticipant GetParticipant(string ipAddress, int port) { foreach (RemoteParticipant c in others) { if (c.ipAddress == ipAddress && c.port == port) return c; } return null; } public RemoteParticipant AddParticipant(string ipAddress, int port) { RemoteParticipant participant = new(ipAddress, port); participant.networkId = (byte)this.others.Count; //others.Add(); return participant; } protected readonly Dictionary> thingMsgProcessors = new(); public delegate Thing ThingConstructor(byte networkId, byte thingId); public void Register(byte thingType, ThingConstructor constr) { thingMsgProcessors[thingType] = new Func(constr); } public void Register(Thing.Type thingType) where ThingClass : Thing { Register((byte)thingType); } public void Register(byte thingType) where ThingClass : Thing { thingMsgProcessors[thingType] = (byte networkId, byte thingId) => Activator.CreateInstance(typeof(ThingClass), networkId, thingId) as ThingClass; Console.WriteLine($"Registering {typeof(ThingClass)} for thing type {thingType}"); } private List things; public static event Thing.ThingHandler OnNewThing; public Thing Get(byte networkId, byte thingId) { Thing thing = things.Find(aThing => Thing.IsThing(aThing, networkId, thingId)); return thing; } public void Add(Thing thing, bool invokeEvent = true) { Console.WriteLine("added thing"); Thing foundThing = Get(thing.networkId, thing.id); if (foundThing == null) { if (thing.id == 0) thing.id = (byte)(things.Count + 1); things.Add(thing); if (invokeEvent) OnNewThing?.Invoke(thing); Console.Write($"Add thing [{thing.networkId}/{thing.id}] {thing.name}"); } } #endregion Init #region Update protected void ReceiveUDP(IAsyncResult result) { if (udpClient == null || this.endPoint == null) return; byte[] data = udpClient.EndReceive(result, ref this.endPoint); // This does not yet take multi-packet messages into account! RemoteParticipant remoteParticipant = this.GetParticipant(endPoint.Address.ToString(), endPoint.Port); if (remoteParticipant == null) remoteParticipant = this.AddParticipant(endPoint.Address.ToString(), endPoint.Port); ReceiveData(data, remoteParticipant); udpClient.BeginReceive(new AsyncCallback(result => ReceiveUDP(result)), null); } private ulong nextPublishMe = 0; public virtual void Update(ulong currentTimeMs = 0) { if (currentTimeMs == 0) { #if UNITY_5_3_OR_NEWER currentTimeMs = (ulong)(UnityEngine.Time.time * 1000); #endif } if (this.publishInterval > 0 && currentTimeMs > this.nextPublishMe) { this.Publish(new ClientMsg(this.networkId)); // Console.WriteLine($"{this.name} Sent ClientMsg {this.networkId}"); this.nextPublishMe = currentTimeMs + this.publishInterval; } // for (int ix = 0; ix < this.others.Count; ix++) { // Participant client = this.others[ix]; // if (client == null) // continue; // this.ProcessMessages(client); // } Thing.UpdateAll(currentTimeMs); } #endregion Update #region Send public void SendThingInfo(RemoteParticipant remoteParticipant, Thing thing) { Console.WriteLine("Send thing info"); this.Send(remoteParticipant, new ThingMsg(this.networkId, thing)); this.Send(remoteParticipant, new NameMsg(this.networkId, thing)); this.Send(remoteParticipant, new ModelUrlMsg(this.networkId, thing)); } public bool Send(IMessage msg) { int bufferSize = msg.Serialize(ref this.buffer); if (bufferSize <= 0) return true; // Console.WriteLine($"msg to {endPoint.Address.ToString()} {endPoint.Port}"); this.udpClient?.Send(this.buffer, bufferSize, this.endPoint); return true; } public bool Send(RemoteParticipant remoteParticipant, IMessage msg) { int bufferSize = msg.Serialize(ref this.buffer); if (bufferSize <= 0) return true; IPEndPoint participantEndpoint = new(IPAddress.Parse(remoteParticipant.ipAddress), remoteParticipant.port); Console.WriteLine($"msg to {participantEndpoint.Address.ToString()} {participantEndpoint.Port}"); this.udpClient?.Send(this.buffer, bufferSize, participantEndpoint); return true; } public bool Publish(IMessage msg) { int bufferSize = msg.Serialize(ref this.buffer); if (bufferSize <= 0) return true; // Console.WriteLine($"publish to {broadcastIpAddress.ToString()} {this.port}"); this.udpClient?.Send(this.buffer, bufferSize, this.broadcastIpAddress, this.port); return true; } public bool SendBuffer(int bufferSize) { //if (this.ipAddress == null) // return false; // UnityEngine.Debug.Log($"Send msg {buffer[0]} to {ipAddress}"); //this.udpClient.Send(this.buffer, bufferSize, this.ipAddress, this.port); this.udpClient?.Send(this.buffer, bufferSize, this.endPoint); return true; } public bool PublishBuffer(int bufferSize) { if (this.broadcastIpAddress == null) return false; this.udpClient?.Send(this.buffer, bufferSize, this.broadcastIpAddress, this.port); return true; } #endregion #region Receive public void ReceiveData(byte[] data, RemoteParticipant remoteParticipant) { byte msgId = data[0]; Console.Write($"received msg {msgId}"); if (msgId == 0xFF) { // Timeout return; } switch (msgId) { case ClientMsg.Id: // 0xA0 / 160 this.Process(remoteParticipant, new ClientMsg(data)); break; case NetworkIdMsg.Id: // 0xA1 / 161 this.Process(remoteParticipant, new NetworkIdMsg(data)); break; case InvestigateMsg.Id: // 0x81 // result = await InvestigateMsg.Receive(dataStream, client, packetSize); break; case ThingMsg.id: // 0x80 / 128 this.Process(new ThingMsg(data)); break; case NameMsg.Id: // 0x91 / 145 this.Process(new NameMsg(data)); break; case ModelUrlMsg.Id: // 0x90 / 144 this.Process(new ModelUrlMsg(data)); break; case PoseMsg.Id: // 0x10 / 16 // result = await PoseMsg.Receive(dataStream, client, packetSize); break; case CustomMsg.Id: // 0xB1 / 177 this.Process(new CustomMsg(data)); 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; default: break; } } #endregion #region Process protected virtual void Process(RemoteParticipant sender, ClientMsg msg) { } protected virtual void Process(RemoteParticipant sender, NetworkIdMsg msg) { Console.WriteLine($"{this.name} receive network id {this.networkId} {msg.networkId}"); if (this.networkId != msg.networkId) { this.networkId = msg.networkId; foreach (Thing thing in Thing.GetAllThings()) this.SendThingInfo(sender, thing); } } protected virtual void Process(InvestigateMsg msg) { } protected virtual void Process(ThingMsg msg) { Console.WriteLine($"received thing {msg.thingId}"); } protected virtual void Process(NameMsg msg) { Console.WriteLine($"received name {msg.name}"); Thing thing = Thing.Get(msg.networkId, msg.thingId); if (thing != null) thing.name = msg.name; } protected virtual void Process(ModelUrlMsg msg) { Console.WriteLine($"received name {msg.url}"); } protected virtual void Process(PoseMsg msg) { } protected virtual void Process(CustomMsg msg) { Console.WriteLine($"receive custom msg"); Thing thing = Thing.Get(msg.networkId, msg.thingId); thing?.ProcessBinary(msg.bytes); } protected virtual void Process(TextMsg temsgxt) { } protected virtual void Process(DestroyMsg msg) { } private void ForwardMessage(IMessage msg) { foreach (Participant client in others) { if (client == this) continue; //UnityEngine.Debug.Log($"---> {client.ipAddress}"); IMessage.SendMsg(client, msg); } } #endregion } }