2 using System.Collections.Generic;
16 private UdpClient udpClient;
18 private Thread udpThread;
20 private string ControlIP;
21 private List<uint> allowedStreams;
22 private List<ReceiverStreamParams> receiverStreams;
32 ref List<uint> allowedStreams, ref List<ReceiverStreamParams> receiverStreams)
34 this.allowedStreams = allowedStreams;
35 this.receiverStreams = receiverStreams;
37 this.ControlIP = ControlIP;
38 this.udpPort = udpPort;
39 udpClient =
new UdpClient(ControlIP, udpPort);
54 udpThread =
new Thread(listenForUDPData);
63 void listenForUDPData()
65 IPEndPoint ep =
new IPEndPoint(IPAddress.Parse(ControlIP), udpPort);
66 Debug.Log(
"Opening UDP Connection" + ep);
70 udpClient.BeginReceive(
new AsyncCallback(UDPReceiveCallback), state);
77 Debug.Log(
"Closing UDP Connection");
85 void UDPReceiveCallback(IAsyncResult ar)
91 UdpClient c = ((
UdpState)(ar.AsyncState)).c;
92 IPEndPoint e = ((
UdpState)(ar.AsyncState)).e;
93 byte[] buf = udpClient.EndReceive(ar, ref e);
97 udpClient.BeginReceive(
new AsyncCallback(UDPReceiveCallback), state);
106 void parseUDPMessage(byte[] udpBuf)
108 int headerSize = BitConverter.ToUInt16(udpBuf, 0);
109 int dataSize = (int)BitConverter.ToUInt16(udpBuf, 2);
110 uint streamID = (uint)BitConverter.ToUInt16(udpBuf, 4);
111 int federationID = (int)BitConverter.ToUInt16(udpBuf, 6);
115 byte[] header =
new byte[headerSize];
118 Buffer.BlockCopy(udpBuf, 8, header, 0, headerSize);
120 byte[] msg =
new byte[dataSize];
121 Buffer.BlockCopy(udpBuf, 8 + headerSize, msg, 0, dataSize);
124 if (allowedStreams.Contains(streamID))
126 for (
int i = 0; i < receiverStreams.Count; i++)
128 if (receiverStreams[i].streamIDs.Contains(streamID))
130 receiverStreams[i].ReceiverStream.OnMessage(streamID, header, msg);
144 byte[] numByte = BitConverter.GetBytes(num);
145 byte[] UDPInitMsg = constructUDPMsg(streamID,
new byte[0], numByte);
146 udpClient.Send(UDPInitMsg, UDPInitMsg.Length);
148 Debug.Log(
"Sent empty receiver packet");
156 public void send(uint streamID, byte[] header, byte[] data)
164 if (udpClient == null)
throw new Exception(
"udpClient undefined");
168 byte[] UDPInitMsg = constructUDPMsg(streamID, header, data);
169 udpClient.Send(UDPInitMsg, UDPInitMsg.Length);
171 catch (SocketException socketException)
173 Debug.LogException(socketException);
174 throw new Exception(
"Socket exception: " + socketException);
185 byte[] constructUDPMsg(uint streamID, byte[] header, byte[] data)
187 byte[] dataPacket =
new byte[8 + header.Length + data.Length];
189 byte[] headerLengthBytes = BitConverter.GetBytes((ushort)header.Length);
191 dataPacket[0] = headerLengthBytes[0];
192 dataPacket[1] = headerLengthBytes[1];
194 byte[] dataLengthBytes = BitConverter.GetBytes((ushort)data.Length);
195 dataPacket[2] = dataLengthBytes[0];
196 dataPacket[3] = dataLengthBytes[1];
199 byte[] streamIDBytes = BitConverter.GetBytes((ushort)streamID);
200 dataPacket[4] = streamIDBytes[0];
201 dataPacket[5] = streamIDBytes[1];
207 if (header.Length > 0)
209 string headerStr = JsonUtility.ToJson(header);
210 byte[] headerByte = Encoding.ASCII.GetBytes(headerStr);
211 Buffer.BlockCopy(headerByte, 0, dataPacket, 8, headerByte.Length);
214 Buffer.BlockCopy(data, 0, dataPacket, 8 + header.Length, data.Length);
Client to send and receive data to/from CoreLink via UDP
CorelinkUDP(string ControlIP, int udpPort, ref List< uint > allowedStreams, ref List< ReceiverStreamParams > receiverStreams)
Constructor that begins the UDP Connection
void startUDPListenThread()
RECEIVER FUNCTIONS.
void send(uint streamID, byte[] header, byte[] data)
SENDER FUNCTIONS.
Utility class for asynchronous data receiving
void stopConnection()
CLEANUP FUNCTIONS.
void receiverPing(uint streamID)
Send an empty packet to the CoreLink server to assign this receiver stream id to a port behind a fire...