How do I capture connection refused/error events

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

Moderators: Lapo, Bax

bjsion
Posts: 16
Joined: 02 May 2011, 12:39
Contact:

How do I capture connection refused/error events

Postby bjsion » 21 Aug 2013, 11:17

Hi Devs,

Version Details
SmartFoxServer: 2.6.0
SmartFox Client: 1.3.0

When I start my game client I attempt to connect to the server. Lets say that for some reason, my server is down or the client cannot establish a connection before I even attempt to send any other requests. I would like to get a notification that the connection attempt was not successful and then handle the failure appropriately. However all I get is messages in the log and no event is fired. What event should I listen for in this scenario or how can my client be notified that the connection attempt failed terminally?

The log messages are:

Code: Select all

SFS - WARN] You are not connected. Request cannot be sent: Sfs2X.Requests.ExtensionRequest
waiting for response...
[SFS - ERROR] TCPSocketLayer: Connection error: Connection refused   at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x00000] in <filename unknown>:0
  at System.Net.Sockets.Socket.Connect (System.Net.IPAddress address, Int32 port) [0x00000] in <filename unknown>:0
  at Sfs2X.Core.Sockets.TCPSocketLayer.ConnectThread () [0x00000] in <filename unknown>:0
[SFS - DEBUG] [ BB-Connect ]: http://127.0.0.1:8080/BlueBox/BlueBox.do
[SFS - DEBUG] [ BB-Send ]: null|connect|null
[SFS - ERROR] ## BlueBox Error: Http error creating http connection: System.Net.Sockets.SocketException: Connection refused
  at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x00000] in <filename unknown>:0
  at System.Net.Sockets.Socket.Connect (System.Net.IPAddress address, Int32 port) [0x00000] in <filename unknown>:0
  at Sfs2X.Http.SFSWebClient.UploadValuesAsync (System.Uri uri, System.String paramName, System.String encodedData) [0x00000] in <filename unknown>:0
[SFS - DEBUG] [ BB-Connect ]: http://127.0.0.1:8080/BlueBox/BlueBox.do
[SFS - DEBUG] [ BB-Send ]: null|connect|null
[SFS - ERROR] ## BlueBox Error: Http error creating http connection: System.Net.Sockets.SocketException: Connection refused
  at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x00000] in <filename unknown>:0
  at System.Net.Sockets.Socket.Connect (System.Net.IPAddress address, Int32 port) [0x00000] in <filename unknown>:0
  at Sfs2X.Http.SFSWebClient.UploadValuesAsync (System.Uri uri, System.String paramName, System.String encodedData) [0x00000] in <filename unknown>:0
[SFS - DEBUG] [ BB-Connect ]: http://127.0.0.1:8080/BlueBox/BlueBox.do
[SFS - DEBUG] [ BB-Send ]: null|connect|null
[SFS - ERROR] ## BlueBox Error: Http error creating http connection: System.Net.Sockets.SocketException: Connection refused
  at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x00000] in <filename unknown>:0
  at System.Net.Sockets.Socket.Connect (System.Net.IPAddress address, Int32 port) [0x00000] in <filename unknown>:0
  at Sfs2X.Http.SFSWebClient.UploadValuesAsync (System.Uri uri, System.String paramName, System.String encodedData) [0x00000] in <filename unknown>:0


Below is the code I am using:

Code: Select all

MakeConnection()
{
            SmartFox smartFox = new SmartFox(false);

            // Register callback delegates
            smartFox.AddEventListener(SFSEvent.CONNECTION, OnTest2);
            smartFox.AddEventListener(SFSEvent.CONNECTION_LOST, OnTest2);
            smartFox.AddEventListener(SFSEvent.LOGIN, OnTest2);
            smartFox.AddEventListener(SFSEvent.LOGIN_ERROR, OnTest2);
            smartFox.AddEventListener(SFSEvent.EXTENSION_RESPONSE, OnTest2);
            smartFox.AddEventListener(SFSEvent.LOGOUT, OnTest2);
            smartFox.AddEventListener(SFSEvent.SOCKET_ERROR, OnTest2);
            smartFox.AddEventListener(SFSEvent.CONNECTION_RETRY, OnTest2);
            smartFox.AddEventListener(SFSEvent.CONNECTION_RESUME, OnTest2);

            smartFox.Connect("127.0.0.1", 9933);

            // For Debugging purposes only
            Console.WriteLine("waiting for response...");
            Thread.Sleep(3000);
}
 
// Method just for testing, not a real implementation
public void OnTest2(BaseEvent evt)
{
            Console.WriteLine("response = " + evt);
            Console.WriteLine("target = " + evt.Target);
            Console.WriteLine("params = " + evt.Params);
            foreach (DictionaryEntry entry in evt.Params)
                Console.WriteLine(entry.Key + " = " + entry.Value);
            Console.WriteLine("type = " + evt.Type);
}



Cheers,
Ben
casperjeff
Posts: 19
Joined: 12 Aug 2013, 22:21

Re: How do I capture connection refused/error events

Postby casperjeff » 21 Aug 2013, 19:14

I'm assuming you need to do this:

void FixedUpdate ()
{
//Easy to miss this in the docs - without it , we don't capture events. :(
smartFox.ProcessEvents ();
}
bjsion
Posts: 16
Joined: 02 May 2011, 12:39
Contact:

Re: How do I capture connection refused/error events

Postby bjsion » 22 Aug 2013, 05:11

Hi casperjeff,

I was calling the ProcessEvents method in my real code after I had connected. In my test case I did not call this method at all. However, even adding this method call to my test case does not seem to help.

Do I need to poll while the smartfox client is connecting and once finished check if the connection is valid, i.e. not use an event?

Below is full test case. When I run the code below, I do not get any events triggered.

Code: Select all

using System;
using System.Collections;
using System.Threading;
using NUnit.Framework;
using Sfs2X.Core;

namespace Sfs2X
{
    [TestFixture()]
    public class SmartFoxTest
    {

        [Test]
        public void TestConnection()
        {
            SmartFox smartFox = new SmartFox(false);

            // Register callback delegates
            smartFox.AddEventListener(SFSEvent.CONNECTION, OnEvent);
            smartFox.AddEventListener(SFSEvent.CONNECTION_LOST, OnEvent);
            smartFox.AddEventListener(SFSEvent.LOGIN, OnEvent);
            smartFox.AddEventListener(SFSEvent.LOGIN_ERROR, OnEvent);
            smartFox.AddEventListener(SFSEvent.EXTENSION_RESPONSE, OnEvent);
            smartFox.AddEventListener(SFSEvent.LOGOUT, OnEvent);
            smartFox.AddEventListener(SFSEvent.SOCKET_ERROR, OnEvent);
            smartFox.AddEventListener(SFSEvent.CONNECTION_RETRY, OnEvent);
            smartFox.AddEventListener(SFSEvent.CONNECTION_RESUME, OnEvent);

            smartFox.ProcessEvents();

            smartFox.Connect("127.0.0.1", 9933);
            Assert.IsFalse(smartFox.IsConnected);
            while (smartFox.IsConnecting)
            {
                Console.WriteLine("waiting for response...");
                Thread.Sleep(3000);
            }

            Assert.IsFalse(smartFox.IsConnected);
        }

        private void OnEvent(BaseEvent evt)
        {
            // TODO: write some asserts.
            Console.WriteLine("Got event");
            Console.WriteLine("response = " + evt);
            Console.WriteLine("target = " + evt.Target);
            Console.WriteLine("params = " + evt.Params);
            foreach (DictionaryEntry entry in evt.Params)
                Console.WriteLine(entry.Key + " = " + entry.Value);
            Console.WriteLine("type = " + evt.Type);
        }
    }
}



Cheers,
Ben
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: How do I capture connection refused/error events

Postby Lapo » 23 Aug 2013, 14:50

Hello,
It's not a good idea to point all different events to a single handler method.
In any case if the connection fails, you should get a CONNECT event with the success parameter turned to false.

Tested with API version 1.3.0

For reference you can take a look the Lobby Example provided with the Unity example pack:
http://www.smartfoxserver.com/download/sfs2x#p=examples

Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
bjsion
Posts: 16
Joined: 02 May 2011, 12:39
Contact:

Re: How do I capture connection refused/error events

Postby bjsion » 27 Aug 2013, 12:23

Hi Lapo,

Thanks for the advice. Yep I realise I should not point all events to one handler method. It was just for simplicity's sake of the test.

Anyway I finally managed to get it to work with my test. The trick was that I had to wait till the connection failed and the event was fired. For the purposes of my test I did the following:

Code: Select all

            smartFox.Connect("127.0.0.1", 9933);
            Assert.IsFalse(smartFox.IsConnected);
            while (smartFox.IsConnecting)
            {
                Console.WriteLine("waiting for response...");
                Thread.Sleep(20);
            }
            smartFox.ProcessEvents();


This next question is purely for my own knowledge. I realise in Unity I can use an Update or FixedUpdate method to poll for the process events. However if I am just using C# is there a recommended approach? Is it a good idea to check the IsConnecting attribute?

For the record the full working test to check if the connection failed is:

Code: Select all

using System;
using System.Collections;
using System.Threading;
using NUnit.Framework;
using Sfs2X.Core;

namespace Sfs2X
{
    [TestFixture()]
    public class SmartFoxTest
    {
        private bool isExpectedEventReceived;

        [SetUp]
        public void SetUp()
        {
            this.isExpectedEventReceived = false;
        }

        [Test]
        public void TestConnection()
        {
            SmartFox smartFox = new SmartFox(false);

            // Register callback delegates
            smartFox.AddEventListener(SFSEvent.CONNECTION, OnConnection);
            smartFox.AddEventListener(SFSEvent.CONNECTION_LOST, OnUnexpectedEvent);
            smartFox.AddEventListener(SFSEvent.LOGIN, OnUnexpectedEvent);
            smartFox.AddEventListener(SFSEvent.LOGIN_ERROR, OnUnexpectedEvent);
            smartFox.AddEventListener(SFSEvent.EXTENSION_RESPONSE, OnUnexpectedEvent);
            smartFox.AddEventListener(SFSEvent.LOGOUT, OnUnexpectedEvent);
            smartFox.AddEventListener(SFSEvent.SOCKET_ERROR, OnUnexpectedEvent);
            smartFox.AddEventListener(SFSEvent.CONNECTION_RETRY, OnUnexpectedEvent);
            smartFox.AddEventListener(SFSEvent.CONNECTION_RESUME, OnUnexpectedEvent);

            smartFox.Connect("127.0.0.1", 9933);
            Assert.IsFalse(smartFox.IsConnected);
            while (smartFox.IsConnecting)
            {
                Console.WriteLine("waiting for response...");
                Thread.Sleep(20);
            }
            smartFox.ProcessEvents();

            Assert.IsFalse(smartFox.IsConnected);
            Assert.IsTrue(isExpectedEventReceived);
        }

        private void OnConnection(BaseEvent evt)
        {
            this.isExpectedEventReceived = true;
            Assert.IsFalse((bool) evt.Params["success"]);
        }

        private void OnUnexpectedEvent(BaseEvent evt)
        {
            Assert.Fail("unexpected event + " + evt);
        }
    }
}
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: How do I capture connection refused/error events

Postby Lapo » 27 Aug 2013, 15:11

In pure C# you don't need to use the polling technique.

You can use this code to initialize the API:

Code: Select all

SmartFox sfs = new SmartFox();
sfs.ThreadSafeMode = false;


Turning off the ThreadSafeMode will make the API work "normally" as expected in any other multi-threaded language.
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X C# API”

Who is online

Users browsing this forum: Ronaldniff and 27 guests