[Unity] Those static SFSEvent delegates are a problem

Post here all your questions related with SmartFoxServer .Net/Unity3D API

Moderators: Lapo, Bax

peta
Posts: 10
Joined: 30 Sep 2008, 16:02

[Unity] Those static SFSEvent delegates are a problem

Postby peta » 22 Dec 2008, 20:11

Hi Thomas,

Thanks for the great work you've done porting the SmartFox API to Unity, and I hope I can contribute to the effort a little. I'm using the just released 1.0 version.

My NUnit unit tests have exposed a problem with the event handling architecture. All is OK until I try to open 2 SFS client connections. This is something I do regularly with actionscript (I use fluint there), for example to make sure the system responds correctly to an attempt to login with the same username, or to run a turn based game extension through its paces.

Of course the problem is that all the handlers added to a static delegate in SFSEvent get called whenever an event occurs on either connection and there is no way to infer which client is responsible for the event.

Would it not be better to make the event delegates public non-static members of SmartFoxClient so you could...

Code: Select all

    SmartFoxClient clientA = new SmartFoxClient();
    clientA.OnWhatever += OnWhateverA;
    SmartFoxClient clientB = new SmartFoxClient();
    clientB.OnWhatever += OnWhateverB;

basically the same way its done in Actionscript.

I guess the current way is OK if you only ever open a single client connection, but could it also cause problems if you open 2 unity windows in the same browser. I know very little about how the Unity plugin and the mono virtual engine work, but is it possible that the two plugins share the same virtual engine, memory, and that there is only one instance of each static delegate per browser?
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Postby Lapo » 23 Dec 2008, 07:23

Hi there,
yep you have a good point here. I think that part will need some redesign sooner or later :(

All is OK until I try to open 2 SFS client connections.

Yes, usually this is not done very frequently.

This is something I do regularly with actionscript (I use fluint there), for example to make sure the system responds correctly to an attempt to login with the same username, or to run a turn based game extension through its paces.

Could you better explain how 2 connections help in those cases?

I know very little about how the Unity plugin and the mono virtual engine work, but is it possible that the two plugins share the same virtual engine, memory, and that there is only one instance of each static delegate per browser?

I am no Unity expert so I'll leave the last word to Thomas but this sounds very unlikely. Each plugin is a new instance running in its own memory space. No sharing going on.
Lapo
--
gotoAndPlay()
...addicted to flash games
peta
Posts: 10
Joined: 30 Sep 2008, 16:02

Postby peta » 23 Dec 2008, 13:27

Hi Lapo,
Lapo wrote:
This is something I do regularly with actionscript (I use fluint there), for example to make sure the system responds correctly to an attempt to login with the same username, or to run a turn based game extension through its paces.

Could you better explain how 2 connections help in those cases?

In the first case I am testing a custom login extension. I open a connection on one SFSClient and login in on it. I leave the connection logged in. I then open another connection to the same host on a different SFSclient and login using the same username again. The unit test asserts if I do not get the correct error message.

In the second case I am testing a turn based game extension, e.g. sfstris. I open two or more SFSClient connections and login on each of them with different user names. One connection is for player1 and the other for player2 (and perhaps others for observers). The various unit tests SendXtMessages to the extension and check that the appropriate responses are received, e.g. 'win', 'draw' etc. Basically I play the game using the unit test framework.

Lapo wrote:
I know very little about how the Unity plugin and the mono virtual engine work, but is it possible that the two plugins share the same virtual engine, memory, and that there is only one instance of each static delegate per browser?

I am no Unity expert so I'll leave the last word to Thomas but this sounds very unlikely. Each plugin is a new instance running in its own memory space. No sharing going on.

I thought so too, but it would be nice to know for sure.
peta
Posts: 10
Joined: 30 Sep 2008, 16:02

Delegates are being executed after Dispose

Postby peta » 23 Dec 2008, 14:52

I've found another problem with the static delegates in the unit testing scenario. In the teardown I remove all my event handlers and Dispose() of the SFSClient used to run the test. However the instance is sometimes still hanging out in memory waiting to be GCed, and for some reason (apologies: I havent had time to dig into the exact cause of this too deeply) sometimes disconnects, after the Dispose, with this error message

Code: Select all

Disconnect due to: Unable to read data from the transport connection: A request to send or receive data was disallowed because the socket had already been shut down in that direction with a previous shutdown call.

In the meantime NUnit has begun another unit test with a new SFSClient and has setup event handlers including one for OnConnectionLost. Its a race condition, and sometimes the new event handler is erroneously triggered by the old SFS client.

SmartFoxClient.DispatchEvent() shouldn't be executing delegates when IsDisposed is true.
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 03 Jan 2009, 16:08

Hi!

Just returned from vacation, and cleaning out mailbox + replying to posts. So sorry for the delay.

In general some very specific unit test scenarios are the only place there is a problem with the static nature of the SFSEvent. While it is pretty simple to change this into an instance variable that is public instead, it has a big impact on all existing clients if this is changed at this point.

There is no issue running multiple instances of your client - only inside the same instance.

Would really have loved to have that feedback a month ago :-)

So now its a matter of either living with the limitation in terms of unit testing or breaking all existing code in an upcoming release with the more general approach of a non-static SFSEvent.

So for now I dont see it as an error, but as an improvement that has big consequences. Just like some of the other improvements I would like to do to the API to make it easier to use + more C# like.

But please discuss!!! Also other people. If there are lots of users who want it, then we definitely should break the existing codes and improve the code. For now I would certainly say "you got the sources" and encourage you to build your own version with non-static SFSEvent so you can continue your development.

Best
Thomas
peta
Posts: 10
Joined: 30 Sep 2008, 16:02

Postby peta » 06 Jan 2009, 16:40

Hi Thomas,

Just back from vacation myself. Thanks for the info. I really wish I could have given you this feedback a month ago, but I only just started getting into it after you released the 1.0 version.

So now its a matter of either living with the limitation in terms of unit testing or breaking all existing code in an upcoming release with the more general approach of a non-static SFSEvent.

The two approaches could co-exist. Place non-static delegates in SFClient, and provide the means to switch DispatchEvent between the SFSEvent static delegates and the the SFClient non-static ones. Deprecate the static delegates over time.
For now I would certainly say "you got the sources" and encourage you to build your own version with non-static SFSEvent so you can continue your development.

Unit testing is central to our development process so I have no choice in the matter. It would help me avoid forking your code if DispatchEvent was protected virtual.

cheers, peta.

Return to “.Net / Unity3D API”

Who is online

Users browsing this forum: No registered users and 5 guests