soundvision/UnityProject/Assets/Scripts/PdConnection/PdReceiver.cs

35 lines
799 B
C#
Raw Normal View History

2019-10-05 12:18:52 +00:00
using System;
using System.Net;
using System.Net.Sockets;
namespace cylvester
{
public interface IPdReceiver
{
event Action<byte[]> DataReceived;
void Update();
}
public class PdReceiver : IPdReceiver
{
private readonly UdpClient udpClient_;
private IPEndPoint remote_;
public PdReceiver(int port)
{
udpClient_ = new UdpClient(port);
remote_ = new IPEndPoint(IPAddress.Any, port);
}
public void Update()
{
while (udpClient_.Available > 0)
{
var receivedData = udpClient_.Receive(ref remote_);
DataReceived?.Invoke(receivedData);
}
}
public event Action<byte[]> DataReceived;
}
}