Search Unity

async socket problem

Discussion in 'Multiplayer' started by sinisa, Nov 2, 2009.

  1. sinisa

    sinisa

    Joined:
    Oct 30, 2009
    Posts:
    18
    anyone saw this error:

    m_ThreadCheck !Thread::EqualsCurrentThreadID(m_ThreadID)

    im trying to send and receive via async socket, and its functional but Ive got this error and off course it dies after some time.

    any help
     
  2. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Unity is not thread safe. This message says that a different thread is calling Unity functions, and it's not supposed to.

    Do you know what thread is calling Unity and making this happen? Do you know if your code is multi-threaded?

    HTH,
    Graham
     
  3. sinisa

    sinisa

    Joined:
    Oct 30, 2009
    Posts:
    18
    it begin to shows up when I try this:

    send to socket position of my FPcontroller graphic,
    receive that position and move another object with that parameters.

    in a non async it works.. but now not.

    tosocket(position)- fromsocket(position)-move Gameobject.

    cant figure out why its not working.
     
  4. Dannnnnnn

    Dannnnnnn

    Joined:
    Jun 26, 2009
    Posts:
    7
    is there any resolution to this issue? or is there a workaround?
     
  5. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Guys,

    I thought I had explained...

    If you are using async sockets, then I assume you are using callbacks that get called when the data has been received by the socket. These callbacks run in a different thread. The code that you write in these callback functions *must*not* call Unity API functions. If you call Unity API functions then you risk memory corruption. (Well, put another way, strange and impossible things to debug will happen, sometimes.) To help customers figure out that bad stuff may happen we have put some tests into the editor code to determine when a callback thread is calling into Unity. If you get this assert it's telling you that you need to change your code.

    One way to change your code is to store the data that is returned in the socket and flag to the original code that created the socket that data is available to be used. Yes this complicates the networking code. Yes it is needed.

    Thanks,
    Graham
     
  6. Dannnnnnn

    Dannnnnnn

    Joined:
    Jun 26, 2009
    Posts:
    7
    Where can we find documentation on this topic?

    Is there an example/tutorial, or reference in regard to instantiating/passing messages to objects that are in the Unity API (edit) from another thread?
     
  7. joew

    joew

    Joined:
    Apr 8, 2008
    Posts:
    96
    Dan see this thread: http://forum.unity3d.com/viewtopic.php?t=36014

    I explained how proper async networking functionality should work not only in Unity but in any networked game or application. You should either be flagging that there is data on the socket as mentioned above, or processing and queueing the data as I mentioned in the other post.
     
  8. Dannnnnnn

    Dannnnnnn

    Joined:
    Jun 26, 2009
    Posts:
    7
    after I posted a reply here, I found your code...

    and. . your example, although a nice example of async sockets, didnt' truly solve the problem. it's when you attempt to process the results of the message, creating or moving game objects for example, that is the problem...

    The only way I found to correct this is message queing.. take the results of all data received by the connection, and write it into a que of messages that can be handed off to the monobehavior class, and handled at update() as needed.
     
  9. joew

    joew

    Joined:
    Apr 8, 2008
    Posts:
    96
    Actually that is not my code but an implementation by somebody else :) They just posted after I did.

    This is exactly how I handle it and it's pretty much the main way to work with anything threaded not only networking. Right now I use a queue and lock on it but it is also possible to change this to a lockless queue if you notice it showing up higher on a profiler.