I'm mostly a C++ programmer looking to port some code over to C# but unfortunately developing for the hololens has forced me to use UWP. The following code has been working just fine for receiving some very fast UDP broadcasts (~250-500 per second)
while ( listener.Available > 0 )
{
byte[] bytes = listener.Receive(ref groupEP);
position = BitConverter.ToSingle(bytes, 0);
}
and as far as the "getting it to work" stage it's done. But moving over to UWP I'm apparently no longer allowed to use UdpClient and now forced to use "DatagramSocket" which has an async callback rather than giving you control of when you check for/process your data. I'm not generally against a callback vs playing catchup each update loop but the DatagramSocket doing roughly the same thing is causing flickering in rendering and generally just not working at all. I've tried about a thousand different ways of re-organizing my callback, but currently it looks like this:
async void MessageReceived(DatagramSocket socket, DatagramSocketMessageReceivedEventArgs eventArguments)
{
try
{
lock (this)
{
DataReader broadcastReader = eventArguments.GetDataReader();
asyncPosition = broadcastReader.ReadSingle();
}
}
catch (Exception exception)
{
}
}
I then in my update loop have this at the end, and position is used in my render routine
lock (this)
{
position = asyncPosition;
}
No matter what I put anywhere in the program or how I try and organize the callback and the variables used I can't seem to find any way to get this to work properly, and I'd really appreciate a primer on how to properly handle a DatagramSocket