141 lines
4.3 KiB
C#
141 lines
4.3 KiB
C#
using System.Collections.Generic;
|
|
using System.Collections.Concurrent;
|
|
using System.Net.Sockets;
|
|
using System.IO;
|
|
|
|
namespace Passer.Control
|
|
{
|
|
|
|
public class Participant
|
|
{
|
|
//public ConnectionMethod connection;
|
|
public UdpClient udpClient;
|
|
public string ipAddress;
|
|
public string broadcastIpAddress = "255.255.255.255";
|
|
public int port;
|
|
public Stream dataStream;
|
|
|
|
public byte[] buffer = new byte[256];
|
|
|
|
public byte networkId = 0;
|
|
|
|
public readonly ConcurrentQueue<IMessage> messageQueue = new();
|
|
|
|
public static Participant GetClient(string ipAddress, int port)
|
|
{
|
|
foreach (Participant c in clients)
|
|
{
|
|
if (c.ipAddress == ipAddress && c.port == port)
|
|
return c;
|
|
}
|
|
return null;
|
|
}
|
|
static public List<Participant> clients = new List<Participant>();
|
|
|
|
public Participant(UdpClient udpClient, int port)
|
|
{
|
|
this.udpClient = udpClient;
|
|
this.ipAddress = null;
|
|
this.port = port;
|
|
this.dataStream = new EchoStream();
|
|
clients.Add(this);
|
|
}
|
|
|
|
public bool SendBuffer()
|
|
{
|
|
if (this.ipAddress == null)
|
|
return false;
|
|
|
|
//UnityEngine.Debug.Log($"Send msg {buffer[0]} to {ipAddress}");
|
|
this.udpClient.Send(this.buffer, this.buffer.Length, this.ipAddress, this.port);
|
|
return true;
|
|
}
|
|
|
|
public bool PublishBuffer()
|
|
{
|
|
if (this.broadcastIpAddress == null)
|
|
return false;
|
|
|
|
this.udpClient.Send(this.buffer, this.buffer.Length, this.broadcastIpAddress, this.port);
|
|
return true;
|
|
}
|
|
|
|
public virtual void ProcessMessages()
|
|
{
|
|
while (this.messageQueue.TryDequeue(out IMessage msg))
|
|
ProcessMessage(msg);
|
|
}
|
|
|
|
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:
|
|
//UnityEngine.Debug.Log($"Name [{name.networkId}/{name.thingId}] {name.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;
|
|
default:
|
|
return;
|
|
}
|
|
ForwardMessage(msg);
|
|
}
|
|
|
|
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) { }
|
|
|
|
private void ForwardMessage(IMessage msg)
|
|
{
|
|
foreach (Participant client in Participant.clients)
|
|
{
|
|
if (client == this)
|
|
continue;
|
|
//UnityEngine.Debug.Log($"---> {client.ipAddress}");
|
|
IMessage.SendMsg(client, msg);
|
|
}
|
|
}
|
|
}
|
|
} |