2

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

Elspin
  • 21
  • 4
  • Hi! Have you managed to solve this issue? It seems that i stuck whith same trouble. I wrote tons of code in unity for hololens and only then understood that i must use UWP) – Daniil Vlasenko Dec 10 '16 at 15:01
  • @DanielVlasenko It's a bummer for sure. You can get around the UWP requirement by writing code in DLLs (make sure they're UWP compatible ones written in VS2015!) but in general you're going to be frustrated either way. DatagramSocket does not play nice with Unity, in callbacks crashes will be caused if you try to modify unity objects like transforms sometimes, it's really quite a mess. – Elspin Dec 12 '16 at 19:30
  • Yep, I just ended with datagramsocket code, and seems i finally made it works. But I tired to switch between two IDE to work with the same project. In socket callbacks I just update different fields, but unity objects update in Update() methods using those fields. Of course with locks etc... Unity does not allow us to update its objects in different threads. And UWP... is strange thing as for me – Daniil Vlasenko Dec 14 '16 at 10:05
  • DatagramSocket doesn't work with Unity as it runs Mono and basically has support for really really old .NET. Anyway there is a way to get around it by using a preprocessor macro: NETFX_CORE details here: https://developer.microsoft.com/en-us/windows/mixed-reality/using_the_windows_namespace_with_unity_apps_for_hololens However, I'm still unable to see any UDP packets coming to the Hololens. Works fine in Windows 10 and I can see the correct packets on the network with Wireshark. @DanielVlasenko, can you share your code? – sebrock Jul 02 '17 at 18:50

0 Answers0