Disconnecting properly in Unity

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

berry
Posts: 11
Joined: 06 Sep 2010, 09:53

Disconnecting properly in Unity

Postby berry » 28 Sep 2010, 11:46

Hi,
I'm working on a test project in Unity 2.6 with SFS2Xb14. So far I've basicsly just setup an example-application to figure out how everything works, since I'm still new to both Unity and SmartFox (, not to programming though). Currently I've got a simple gameobject for each client to move around while updating this movement on the other clients in the same room.
Though everything seems to be working like it should in-game, the Unity editor will crash a while after closing the application. After stopping it I can see Unity starts building up memory consumption in the windows taskmangager and freezes or crashes after a while. When running a webbuild of this app, Firefix also crashes a lot after closing the tab the application was running in.

Currently I'm using the follow statements to quit:

OnApplicationQuit() {
smartFox.RemoveAllEventListeners();
if ( smartFox.IsConnected )
smartFox.Disconnect();
}

With 'smartFox' being an instance of SmartFoxClient.

Does anyone have the same issue or know how to fix this? I'm really running out of ideas here, so any help or new insights on the situation would be appreciated.

Thanks...
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 28 Sep 2010, 12:39

Hey,

Yeah - basically the message from Unity support is "dont use sockets with 2.6 as closing them can cause hangs and crashes".

There is no workaround and no fix - except to upgrade to Unity 3 (released yesterday)

Sorry

/Thomas
berry
Posts: 11
Joined: 06 Sep 2010, 09:53

Postby berry » 28 Sep 2010, 12:46

Hmm.. I see..

Thnx for the reply. I'll look into switching to Unity3 then :D .

EDIT:
By the way.. I believe I heared something about security issues with Unity 3's webplayer and SmartFox, making it impossible to connect to the server. Is this true? Cause I'm working on a browser based application and will need to build webplayers.
I could be wrong about this, but it would be nice to know stuff like this in advance.

EDIT2:
Never mind.. I've found your post about this Thomas.. so I'll make sure to include the policies stuff.
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 28 Sep 2010, 15:54

Super :-)
berry
Posts: 11
Joined: 06 Sep 2010, 09:53

Postby berry » 29 Sep 2010, 08:41

Hi again,

In the meantime I switched to Unity 3, which wasn't a big issue since I'm only at the start of a rather small project. I already managed to connect to the SFS2X server via a Webplayer build with the Security.PrefetchSocketPolicy() method, so there's no issues with that so far.

But...
Though I can run my testapplication in the editor at least once, it will freeze after stopping it and trying to run it for a second time. So basicly it's still the same problem as I had in Unity 2.6 as described above.
It might be worth noting that I'm accessing the SmartFoxClient object through an abstraction layer. This is because I'm also testing it with a Photon implementation, without constantly having to rewrite my code. Photon seems to cause no problems at all, and I can run my app as many times as I want without freezing. This is why I'm quite confident it has to be something related to SmartFox (as opposed to Unity).
I hope you can make any sense out of all this, cause I'd really like to get things to work properly with SmartFox as well.

Once again, any comments would be greatly appreciated :)
stevets
Posts: 60
Joined: 11 Jul 2010, 12:22
Location: USA

Postby stevets » 29 Sep 2010, 09:36

Hi Berry,
I am having the same problem with the Unity examples.
Take the Lobby as an example. It works fine when you log in to the Lobby. I can log out and back in without a problem.
Everything is working as expected.

When I log out of the Lobby and turn off the Unity editor "play" arrow to go back into scene mode Unity still functions as expected. However, the smartfoxserver takes time to time out the session. If I press the Unity "play" button before the session times out, then Unity crashes.
If i wait for the session to rime out, then Unity plays again as expected.

I would like to know what is the best way to deal with this issue. Is there an additional command that could be sent to terminate the session?
I have tried the smartFox.disconnect(), but it does not seem to address this. The only thing that seemed to force the session to terminate was the KillConnection()---but I am not supposed to use it:).

I think that the webplayer crashes for the same reason.

I have not heard anyone else mention this, so I think that I must be missing something very simple.

Does this fit with your experience Berry?

Steve
berry
Posts: 11
Joined: 06 Sep 2010, 09:53

Postby berry » 29 Sep 2010, 09:45

Hi Steve,

Yep, sounds like you're having the exact same issues I'm having.
I was quite surprised not being able to find anything about this stuff on the forums, so just like you said: might be something simple we're missing..
I've just been using the smartFox.Disconnect(), giving me the same results you just described. KillConcetion() might be some kind of workaround, but not one I'm eager to use..

Hope there's someone out there to point us in the right direction :wink:.
stevets
Posts: 60
Joined: 11 Jul 2010, 12:22
Location: USA

Postby stevets » 29 Sep 2010, 09:50

It appears that the smartfoxserver is doing what is expected and that maybe the logic on the Unity Client needs to handle this scenario. more gracefully.

What do you think?

I hope to get some help as well.

Steve
berry
Posts: 11
Joined: 06 Sep 2010, 09:53

Postby berry » 29 Sep 2010, 10:22

Well, kind of...

Still... Photon doesn't seem to have any problem in this area. Though Photon uses UDP and this far I've only been working with TCP on SmartFox. Might have something to do with that as well, since with UDP there's no real connection to break.
Have you tested SFS2X with UDP yet?.. maybe this would give different results. At least I'm gonna give it a try, because UDP would be the prefered way of transfering data for my project anyway.
stevets
Posts: 60
Joined: 11 Jul 2010, 12:22
Location: USA

Postby stevets » 29 Sep 2010, 10:47

I think you are right about UDP as the reason Photon does not exhibit the same behavior. I am only using TCP right now, so I can only speak to my limited knowledge of it.

I have also found that the Unity is still connected to the server even though the "play" button is turned off. So the socket is still open.
So I think that if Unity or webplayer crashes we need a way to close the socket connection.

Is there some way to use a try{}catch(SocketException) to resolve this.

Also, you can find some useful info about SFS2X UDP here
http://www.smartfoxserver.com/forums/viewtopic.php?t=8347
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 29 Sep 2010, 11:32

Hmmmm - lets track that one down then. Should definitely not happen in 3.0.

(In 2.6 there is definitely a Unity/mono bug thats unfixable - I talked personally to a few Unity engineers to check up on that)

One thing to note is that the editor never calls OnApplicationQuit if I recall. Thats usually why connections do not terminate there if you put code into that hook.

Can you post a few snippets on what you do in your game when shutting down?

/Thomas
berry
Posts: 11
Joined: 06 Sep 2010, 09:53

Postby berry » 29 Sep 2010, 11:48

Hi Thomas,

As I stated in my first post of this topic, I'm using the following code when I quit the application:

Code: Select all

OnApplicationQuit() {
smartFox.RemoveAllEventListeners();
if ( smartFox.IsConnected )
smartFox.Disconnect();
}


And I'm possitive it's being called in the editor as well, because of a simple Debug.Log() I've put in there just to make sure.
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 29 Sep 2010, 12:11

Oh - missed that part. Sorry.

Will try to look into this so we can get that fixed

/Thomas
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 06 Oct 2010, 09:09

Found the cause - and hopefully the fix too. The disconnection was handled in a timer thread. So if/when the main Unity thread closed before the socket disconnect thread happened = boom.

Give this dll a try and report back if it worked for you or not:

http://www.fullcontrol.dk/downloads/SFS2X/SmartFox2.dll

/Thomas
stevets
Posts: 60
Joined: 11 Jul 2010, 12:22
Location: USA

Postby stevets » 06 Oct 2010, 11:23

I still observe the following problems with the new API,

I am using the Lobby example that shipped with SFS2X. All I added was the InitUDP() and corresponding eventlistener.
In each case I log out of the room and am at the login screen before I do the following,

1. The Unity 3 editor (RC5) crashes when I stop Unity by deactivating the play button. I have to terminate the editor process to close the program.

2. The webplayer built from Unity 3 (RC5) runs fine. When I close the IE browser there are no lockups.

3. The standalone build from Unity 3 (RC5) runs fine. When I try to close the window it crashes temporarily before I can close the window completely.

Steve

Return to “SFS2X Questions”

Who is online

Users browsing this forum: Aldenea, DavinVicy, DerikFent, JerodSa, Kevengar and 33 guests