Search Unity

.NET UdpClient and Coroutines

Discussion in 'Scripting' started by tsphillips, Feb 9, 2006.

  1. tsphillips

    tsphillips

    Joined:
    Jan 9, 2006
    Posts:
    359
    The existence of coroutines leads me to believe that all user code in Unity is single threaded. If that is the case, does anyone know how to get around the blocking UdpClient.Receive() method?

    If the code were multi-threaded, then I would have a separate thread reading UDP packets and sending update messages to a controller object in another thread. Barring that, I would probably set an alarm (like a signal) to timeout the Receive() method.

    Anyone have any ideas how to receive UDP packets in real time without interfering with all the object updates?
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
  3. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    You could also check out the non-blocking socket functions.

    I found it easier just to spawn new threads and use the blocking ones, though.

    One gotcha is that you must shut those threads down in the OnApplicationQuit method.

    Although it was really funny seeing the network updates still happen after I clicked stopped within the editor...

    -Jon
     
  4. tsphillips

    tsphillips

    Joined:
    Jan 9, 2006
    Posts:
    359
    Joachim -- Excellent; very good to see multithreading is supported.

    Jon -- I couldn't find a non-blocking UDP receive for .NET. (Microsoft's .NET docs aren't exactly the most user friendly...) Can you post a link for the docs to such a method?
     
  5. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
  6. tsphillips

    tsphillips

    Joined:
    Jan 9, 2006
    Posts:
    359
    Excellent! Thanks! :D