Alternative approach for time-sensitive socket listener

Hi all,

I’m implementing an interface for a control device to talk to Unity.

I have a .NET .dll that contains all my network code that’s dropped into the assets folder and accessed by Unity scripts.

The control device talks over a UDP socket and i originally used the System.Net.Sockets.Socket async methods (BeginReceive,EndReceive etc) to listen in Unity for data and dispatch events when messages were received on the socket.

I got the crashing issues others have experienced and learnt from this thread http://forum.unity3d.com/viewtopic.php?t=25364&highlight=unity+threads+socket that it’s all about threading in Unity - i.e. the async methods of the Socket class use threads ‘behind the scenes’ which are causing crashes in the Editor and the Web Player.

First of all i want to ask is that last paragraph correct? Is the threading issue what’s causing the crashes?

If so i have a second question. What’s the best workaround? The data i’m sending from the control device needs to get into my Unity game asap. If i set up a socket in non-blocking mode in a seperate, background, thread and set up a while(true) loop that checks the Available property of the socket and, when data is Available fires off the event there seems to be a much greater lag on the connection. Data seems to take longer and there’s much more ‘jitter’. It’s just not a viable solution for me as the data is time-sensitive - it needs to arrive as quickly as possible.

I’m just wondering if others have experienced these issues and if there’s a standard solution (i’ve searched the forums and found others with similar issues but no standard solutions.)

Thanks for reading this far. If i come up with a solid solution myself i’ll be posting it but would be good to hear from others in the meantime.

T

Hello,

I experienced a similar situation with my network code. I have not implemented it yet, but Dreamora suggested to me that you run your listener in a new thread. Then instead of using the async callback to access a function in Unity, create a get function instead to “get” the current message. That way sockets are not destroyed after the first frame they are run. Hope this helps!