Disconnection [SFS - ERROR] [TCPSocketLayer]

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

Moderators: Lapo, Bax

Ardito
Posts: 128
Joined: 12 Sep 2016, 11:26
Location: Italy

Disconnection [SFS - ERROR] [TCPSocketLayer]

Postby Ardito » 08 Apr 2022, 20:11

Hi,
When the user is disconnected from the server for any reason (Inactivity or Logout), and then decides to log in again, at the first request that the client makes to the server I get this error:

Code: Select all

[SFS - ERROR] [TCPSocketLayer] General error reading data from socket: Unable to read data from the transport connection: Operazione di blocco interrotta da una chiamata a WSACancelBlockingCall.

UnityEngine.Debug:Log (object)
Sfs2X.Logging.Logger:Log (Sfs2X.Logging.LogLevel,string)
Sfs2X.Logging.Logger:Error (string[])
Sfs2X.Core.Sockets.TCPSocketLayer:LogError (string)
Sfs2X.Core.Sockets.TCPSocketLayer:HandleErrorCallback (object)
Sfs2X.Core.ThreadManager:ProcessItem (System.Collections.Hashtable)
Sfs2X.Core.ThreadManager:InThread ()
System.Threading.ThreadHelper:ThreadStart ()

I call sfs.Disconnect () when the user wants to log out, the user is inactive, the user closes the application.
Example of my code:

Code: Select all

    public static void Disconnect()
    {
        if (sfsConnection != null && sfsConnection.IsConnected)
            sfsConnection.Disconnect();
    }
   
     public void OnApplicationQuit()
    {
        Debug.Log("OnApplicationQuit");
        Networking.Disconnect();
    }
   

Unity 2020.3.32f1
Sfs2x 2.17.3

Edit:
This error appears both at the User Login and at the User Logout. And it appears in the client.


Best regards
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Disconnection [SFS - ERROR] [TCPSocketLayer]

Postby Lapo » 11 Apr 2022, 07:00

Hi,
when a User disconnects you should destroy the current SmartFox object (and remove the relative event listeners) and then create a new one if the User wants to start a new connection.

Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
Ardito
Posts: 128
Joined: 12 Sep 2016, 11:26
Location: Italy

Re: Disconnection [SFS - ERROR] [TCPSocketLayer]

Postby Ardito » 13 Apr 2022, 13:44

When I try to call "Destroy(sfsConnection);" I on Visual Studio get an error:

Code example:

Code: Select all

public static SmartFox sfsConnection;

    public static void Disconnect()
    {
        if (sfsConnection != null && sfsConnection.IsConnected)
            sfsConnection.Disconnect();
           
             Destroy(Networking.sfsConnection);//Error: It is not possible to convert from "Sfs2x.SmartFox" to "UnityEngine.Object"
    }
   

So maybe I have to destroy the object where the script is attached?
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Disconnection [SFS - ERROR] [TCPSocketLayer]

Postby Lapo » 13 Apr 2022, 14:52

I don't know where the Destroy() method call is coming from.
In order to "destroy" an object you just need to release all of its references. Normally, if you have just one reference, you can set that to null or set it to a new instance, which will dereference the previous one.

In the case of the client API you also need to remove all the previous event listeners.
This is how I would suggest to do it:

Code: Select all

client.RemoveAllEventListeners();
client = null;

where client is your SmartFox instance.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
Ardito
Posts: 128
Joined: 12 Sep 2016, 11:26
Location: Italy

Re: Disconnection [SFS - ERROR] [TCPSocketLayer]

Postby Ardito » 16 Apr 2022, 13:17

The problem persists
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Disconnection [SFS - ERROR] [TCPSocketLayer]

Postby Lapo » 18 Apr 2022, 07:01

Can you show me how you're handling the disconnection on the client side and how you start a new one?

Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
Ardito
Posts: 128
Joined: 12 Sep 2016, 11:26
Location: Italy

Re: Disconnection [SFS - ERROR] [TCPSocketLayer]

Postby Ardito » 19 Apr 2022, 02:06

This is quite complex, but I'll try.
I have a static class called "Networking", inside I have various classes for all requests.

When opening the app I do the version check as #Guest_N to # Extension_1, and after the response from the server I disconnect it.
The user finds himself in a screen with:
- Registration
- Login
- Forgot password

For each request that the user makes:
1) First I do another version check like #Guest_N all #Extension_1, after the response from the server I disconnect it.
2) I make the request of #Guest_N to # Extension_1 and then I disconnect it.

At login the user from #Guest_N becomes #User in the # Extension_2.

For the Logout instead I have various points in the code where a user can logout:
- OnApplicationQuit()
- User inactivity
- Logout in the settings panel
- Loss of connection

I each of these requests when I unlink #Guest_N I call this method:

Code: Select all

    public static void Disconnect()
    {
        if (sfsConnection != null && sfsConnection.IsConnected)
        {
            sfsConnection.RemoveAllEventListeners();
            sfsConnection.Disconnect();
            sfsConnection = null;
        }
    }

While when the #Guest_N make the requests I log in like this:

Code: Select all

    public class Control_Version
    {
        public static void controlVersion() //It starts from here
        {
            Debug.Log("Controllo versione " + Application.version);
            sfsConnection = new SmartFox();
            sfsConnection.ThreadSafeMode = true;
            sfsConnection.AddEventListener(SFSEvent.CONNECTION, OnVersionConnection);
            sfsConnection.AddEventListener(SFSEvent.CRYPTO_INIT, OnCryptoInit);
            sfsConnection.Connect(ServerIP, ServerPort);
        }

        public static void OnCryptoInit(BaseEvent evt)
        {
            if ((bool)evt.Params["success"])
            {
                Debug.Log("Control_Version OnCryptoInit TRUE");
                // Send a login request
                DoLogin();
            }
            else Debug.Log("Control_Version OnCryptoInit FALSE");
        }

        public static void DoLogin()
        {
            Debug.Log("Control_Version DoLogin");
            sfsConnection.AddEventListener(SFSEvent.LOGIN, OnLoginVersion);
            sfsConnection.AddEventListener(SFSEvent.LOGIN_ERROR, OnLoginVersionError);
            sfsConnection.Send(new LoginRequest("", null, "Extension_1"));
        }

        private static void OnVersionConnection(BaseEvent evt)
        {
            if ((bool)evt.Params["success"])
            {
                Debug.Log("OnVersionConnection");
                if (useEncryption) sfsConnection.InitCrypto();
                else DoLogin();
            }
            else
            {
                ResetListeners();

                //Show panel Warning Error Connection
      //........
      //........
            }
        }

        private static void OnLoginVersion(BaseEvent evt)
        {
            Debug.Log("OnLoginVersion");
            sfsConnection.AddEventListener(SFSEvent.EXTENSION_RESPONSE, OnExtensionResponse);
            ISFSObject objOut = new SFSObject();
            objOut.PutUtfString("version", Application.version);
            sfsConnection.Send(new ExtensionRequest("version", objOut));
        }

        private static void OnLoginVersionError(BaseEvent evt)
        {
            ResetListeners();

                 //Show panel Warning Error Connection
      //........
      //........
        }

        public static void OnExtensionResponse(BaseEvent evt)
        {
            ResetListeners();
            if ((string)evt.Params["cmd"] == "version")
            {           
                ISFSObject myData = (SFSObject)evt.Params["params"];
                Debug.Log(myData.GetDump());
                switch (myData.GetByte("type"))
                {
                    case 1: //Maintenance in progress
                        //My code
                        break;

                    case 2:         //Control Version
                        //My code
                        break;
                }
            }
        }

        private static void ResetListeners()
        {
            Debug.Log("ResetListeners");         
            sfsConnection.RemoveEventListener(SFSEvent.CONNECTION, OnVersionConnection);
            sfsConnection.RemoveEventListener(SFSEvent.CRYPTO_INIT, OnCryptoInit);
            sfsConnection.RemoveEventListener(SFSEvent.LOGIN, OnLoginVersion);
            sfsConnection.RemoveEventListener(SFSEvent.LOGIN_ERROR, OnLoginVersionError);
            sfsConnection.RemoveEventListener(SFSEvent.EXTENSION_RESPONSE, OnExtensionResponse);
            Disconnect();
        }
    }
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Disconnection [SFS - ERROR] [TCPSocketLayer]

Postby Lapo » 19 Apr 2022, 07:32

Hi,
in your opening post you mentioned the user being disconnected from the server for any reason (Inactivity or Logout).
However in the code you have posted there is no trace of a Disconnection handler. How do you handle it?

The OnVersionConnection handles the LOGIN_ERROR event, but the the event doesn't send a "success" parameter, it only sends an error code and message. You should take a look at the client documentation:
http://docs2x.smartfoxserver.com/api-do ... 432b93.htm

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
Ardito
Posts: 128
Joined: 12 Sep 2016, 11:26
Location: Italy

Re: Disconnection [SFS - ERROR] [TCPSocketLayer]

Postby Ardito » 19 Apr 2022, 13:08

Yes, he sends it, I receive it

My test:
Code:

Code: Select all

        private static void OnVersionConnection(BaseEvent evt)
        {
            if ((bool)evt.Params["success"])
            {
                Debug.Log("OnVersionConnection " + evt.Params["success"]);
                if (useEncryption) sfsConnection.InitCrypto();
                else DoLogin();
            }
            else
            {
                ResetListeners();

            }
        }

Result:

Code: Select all

OnVersionConnection True
UnityEngine.Debug:Log (object)
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Disconnection [SFS - ERROR] [TCPSocketLayer]

Postby Lapo » 21 Apr 2022, 08:15

Sorry, it looks like I've read that incorrectly.
However the code is a bit confusing. I don't understand what is the purpose of the ResetListeners() method.
It seems redundant. You just need to call client.RemoveAllEventListeners(); when there is a disconnection event.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
Ardito
Posts: 128
Joined: 12 Sep 2016, 11:26
Location: Italy

Re: Disconnection [SFS - ERROR] [TCPSocketLayer]

Postby Ardito » 21 Apr 2022, 15:57

Yes the code is very redundant, it is quite old code, I will try to improve it ..

Greetings
Ardito
Posts: 128
Joined: 12 Sep 2016, 11:26
Location: Italy

Re: Disconnection [SFS - ERROR] [TCPSocketLayer]

Postby Ardito » 23 Apr 2022, 13:25

I tried in a new project, and the error doesn't show up, so something is wrong with my programming
Ardito
Posts: 128
Joined: 12 Sep 2016, 11:26
Location: Italy

Re: Disconnection [SFS - ERROR] [TCPSocketLayer]

Postby Ardito » 23 Apr 2022, 15:57

I don't understand why in my code I get this error. I have tried everything, is there an integrated trace?
Ardito
Posts: 128
Joined: 12 Sep 2016, 11:26
Location: Italy

Re: Disconnection [SFS - ERROR] [TCPSocketLayer]

Postby Ardito » 23 Apr 2022, 17:19

This error always pops up after OnConnect, but I first do sfs.Disconnect ();
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Disconnection [SFS - ERROR] [TCPSocketLayer]

Postby Lapo » 23 Apr 2022, 18:47

Ardito wrote:I don't understand why in my code I get this error. I have tried everything, is there an integrated trace?

There is the debug function that shows the detail of the messages sent and received but that's is unlikely to help for this problem. You can activate if by setting ConfigData.Debug = True

If your separate test project works as expected try to replicate the same disconnect logic in the original project.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 52 guests