Auto reconnection mechanism problem

Post here your questions about the Unity / .Net / Mono / Windows 8 / Windows Phone 8 API for SFS2X

Moderators: Lapo, Bax

COB
Posts: 68
Joined: 28 Dec 2010, 08:54

Auto reconnection mechanism problem

Postby COB » 28 Jul 2011, 05:10

I'm trying to implement reconnection mechanism and in theory it looks to be very simple. I set User Reconnection Timeframe in admin panel to 5s. In my code I have:

Code: Select all

smartFoxClient.AddEventListener(SFSEvent.CONNECTION_RETRY, OnConnectionRetry);
      smartFoxClient.AddEventListener(SFSEvent.CONNECTION_RESUME, OnConnectionResume);

and

Code: Select all

        void OnConnectionRetry(BaseEvent evt) {
        Debug.Log("Connection retry");
            // Freeze your GUI and provide some feedback to the Player
        }

        void OnConnectionResume(BaseEvent evt) {
        Debug.Log("Connection resume");
            // Unfreeze the GUI and let the player continue with the game...
        }   

Then I execute smartFoxClient.KillConnection();. After that I immediately see "Connection retry" and "Connection resume" in console. However, there are also many errors concerning sending data to SFS (saying that I'm trying to use smartFoxClient set to null) after these events. In fact, in admin panel I see that user disappears, so connection was not really resumed properly. My question is why? What am I doing wrong? Do I have to login manually when connection resume is fired? I will appreciate your quick response because it's very important for me to implement.
appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Postby appels » 28 Jul 2011, 18:32

Nope, you don't have to do anything, it should reconnect automaticly. It works fine on my box. Not sure if you set the SFS variable to Null is a good test. I tested it between 2 servers by unplugging the network cable briefly and that worked fine.
COB
Posts: 68
Joined: 28 Dec 2010, 08:54

Postby COB » 28 Jul 2011, 18:52

I test reconnection with smartFoxClient.KillConnection(); method. I don't set anything to null. When I kill the connection, I see connection retry and resume event. However, after that I see errors in console which say that smartFoxClient is set to null, so in fact connection was not reestablished properly. Also in admin panel user disappears.
I also tried to make such mechanism manually. In this scenario I periodically try to connect to server. When it is not available I receive connect event with "failed" status and this is ok, but when I eventually manage to connect, connect event is not fired at all...
COB
Posts: 68
Joined: 28 Dec 2010, 08:54

Postby COB » 29 Jul 2011, 04:45

I have for example such method:

Code: Select all

      // Send transform to all other users
      public void DoSend() {
         SmartFox client = NetworkController.GetClient();
         Debug.Log(client.IsConnected);
         ISFSObject data = new SFSObject();
         if (!isRobot) data.PutUtfString("_cmd", "t");  //We put _cmd = "t" here to know that this object contains player transform sync data.
         else {
            data.PutUtfString("_cmd", "rt"); //We put _cmd = "rt" here to know that this object contains robot transform sync data.
            data.PutInt("robot_id", robotId);
         }
         data.PutFloat("x", this.position.x);
         data.PutFloat("y", this.position.y);
         data.PutFloat("z", this.position.z);
         
         data.PutFloat("rx", this.rotation.x);
         data.PutFloat("ry", this.rotation.y);
         data.PutFloat("rz", this.rotation.z);
         data.PutFloat("w", this.rotation.w);
         
         // We send data using SendObject method here. To optimize this you can use SendXtMessage method with custum formatted method
         // Also an extension on the server side could decide which users really need to receive the transform
         client.Send(new ObjectMessageRequest(data));
      }

In console I see:

For Debug.Log(client.IsConnected); line:
True
UnityEngine.Debug:Log(Object)
NetworkTransform:DoSend() (at Assets/Custom Scripts/network/NetworkTransform.cs:45)
NetworkTransformSender:SendTransform() (at Assets/Custom Scripts/network/NetworkTransformSender.cs:31)
NetworkTransformSender:FixedUpdate() (at Assets/Custom Scripts/network/NetworkTransformSender.cs:24)


and then for client.Send(new ObjectMessageRequest(data)); line:
NullReferenceException: Object reference not set to an instance of an object
Sfs2X.Requests.GenericMessageRequest.ExecuteObjectMessage (Sfs2X.SmartFox sfs)
Sfs2X.Requests.GenericMessageRequest.Execute (Sfs2X.SmartFox sfs)
Sfs2X.SmartFox.Send (IRequest request)
NetworkTransform.DoSend () (at Assets/Custom Scripts/network/NetworkTransform.cs:63)
NetworkTransformSender.SendTransform () (at Assets/Custom Scripts/network/NetworkTransformSender.cs:31)
NetworkTransformSender.FixedUpdate () (at Assets/Custom Scripts/network/NetworkTransformSender.cs:24)

Method DoSend is executed in loop, so I see this log many times each time after the reconnection. I don't understand this, because client.IsConnected returns true.
COB
Posts: 68
Joined: 28 Dec 2010, 08:54

Postby COB » 02 Aug 2011, 07:14

Here are some logs from my console.
As you can see I put some logs before I am sending any data to server. I also display SFS client property IsConnected each time. Then I use KillConnection() method, and after that you can see OnConnectionRetry and OnConnectionResume. For a while SFS is not connected, but then it is connected again. However, as you can see, I get this null exception when I am sending data to server after reconnection even when IsConnected says that connection is reestablished. Do you have any idea what is wrong?

Image

Uploaded with ImageShack.us
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 05 Aug 2011, 07:05

KillConnection is not meant for public usage, and does not properly return the API into a sane state. It simply kills the socket and thats it.

Code: Select all

      /// <summary>
      /// Simulate an abrupt disconnection
      /// For testing/simulations only!!!
      /// </summary>
      public void KillConnection()    {
         bitSwarm.KillConnection();
      }
Full Control - maker of Unity/C# and Java SFS API and indie games
Follow on twitter: http://twitter.com/thomas_h_lund
COB
Posts: 68
Joined: 28 Dec 2010, 08:54

Postby COB » 05 Aug 2011, 09:38

I use it only for tests. I thought that this function shall be used for the SFS reconnection mechanism tests. As you can see it kills the connection and it is even reestablished, but then API does not behave properly.
So, how should I test this mechanism? My second question is how many times SFS attempts to reconnect? Only once after connection is lost and then if reconnection is not successful I should implement standard connection/login mechanism to reeastablish it?
appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Postby appels » 05 Aug 2011, 12:28

COB wrote:So, how should I test this mechanism?

Unplug network cable or block with a firewall.
COB
Posts: 68
Joined: 28 Dec 2010, 08:54

Postby COB » 08 Aug 2011, 06:19

Ok, I will try this way. However, what does exactly "User reconnection timeframe" mean in zone configuration? How this setting works?

I'm also trying to make manual reconnection mechanism and try to reestablish manually the connection, but after I'm disconnected, calling
smartFoxClient.Connect(IP, PORT);
does not make any change. I'm still not connected even when it is called after network connection is reestablished.
appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Postby appels » 08 Aug 2011, 10:00

reconnection timeframe = the time between the client connection lost on the server and the time the server should stop and try to reconnect the lost client
Just a wild guess but i don't think i'm far off.
ericheimburg
Posts: 26
Joined: 18 Mar 2010, 07:25

Postby ericheimburg » 09 Oct 2011, 09:41

Okay so KillConnection() basically ruins the client instance forever and should never be called. Got it.

In that case, can you add to your TODO list to make KillConnection work? Ideally with a timeout so we can emulate our apps being disconnected for a fixed amount of time?

It can be very difficult to code the pause/unpause system in some multiplayer games. And it's friggin TORTURE to code it if the only way to test is to constantly get up, yank the wire from the wall, hurry back to see what the app is doing, and the run back to plug it in again. Over and over and over.
appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Postby appels » 09 Oct 2011, 09:53

use a firewall
User avatar
copet80
Posts: 42
Joined: 27 Apr 2010, 22:23
Location: Australia
Contact:

Re: Auto reconnection mechanism problem

Postby copet80 » 28 Sep 2012, 04:51

I have a question though, should we always freeze the GUI while reconnecting? Say, if whatever the client does during CONNECTION_RETRY is not detrimental to decision making (i.e. submitting chat messages that can pretty much be ignored without any effect to the gameplay), is it okay to just let the user think that nothing happens while the reconnecting is going on in the background?

Also just one quick question:
- When CONNECTION_RETRY is fired, is CONNECTION_LOST also fired?
User avatar
rjgtav
Posts: 2813
Joined: 19 Apr 2009, 11:31
Location: Lisbon, Portugal

Re: Auto reconnection mechanism problem

Postby rjgtav » 28 Sep 2012, 13:30

Hi.
Maybe you could do something like skype does. When you send a chat message while you were recoonecting, you would add a small icon telling that the message hasn't arrived yet. Then when you reconnected, you could send all the messages that were on the queue again, at a specified rate (not on a for loop).
- When CONNECTION_RETRY is fired, is CONNECTION_LOST also fired?

When you have the HRC system is activated and the client loses the connection to the server, it firstly fires a CONNECTION_RETRY event.
Then, if it managed ti successfully reconnect, you get a CONNECTION_RESUME event.
If the client wasn't able to reconnect, you get a CONNECTION_LOST event.
Skills: SFS Pro, SFS2X, AS2.0/AS3.0, Java, HTML5/CSS3/JS, C#
Portfolio: https://rjgtav.wordpress.com/
SFS Tutorials: http://sfs-tutor.blogspot.com/ - Discontinued. Some examples may be bugged.
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Re: Auto reconnection mechanism problem

Postby ThomasLund » 30 Sep 2012, 12:29

yeah - I think its down to your client and what specific part of it is trying to send to the server.

If its the game itself where you are running around (e.g. arena game) - I'd freeze the game and show a "reconnecting" message. For chat application, do like Skype. For a turn based game, can keep letting user add more commands into the queue.

/T
Full Control - maker of Unity/C# and Java SFS API and indie games

Follow on twitter: http://twitter.com/thomas_h_lund

Return to “SFS2X C# API”

Who is online

Users browsing this forum: No registered users and 22 guests