Christopher Andrade
Christopher Andrade

Reputation: 53

Quickfix/n - No event for connection timeout or host not found?

So I have a project where I need to create failover between two FIX quote hosts in case of failure.

The FixApplication (IApplication) OnLogout() is nice to hook an event to when a socket is dropped or you are logged out. This is simple. But this only works if the socket connection was successful to start off with.

If you start up the application and the host is down, no method is actually called for this anywhere. Not in IApplication, nor in the socket IInitiator. No error is thrown anywhere either, the QuickFix initiator will simply just silently retry.

I am using a Timer with a callback right now to manually poll every so often and check if Initiator IsLoggedOn() is false. Then triggering a failover.

But this is really clunky and not elegant.

Is there any other event or method I can hook into to receive the notice for socket network failures BEFORE a network connection and session is established successfully?

Thanks!

Timer t = new Timer(5000) 
t.Elapsed += CheckSocketConnected;
private void CheckSocketConnected(object source, ElapsedEventArgs e)
{
     var connected = socketInitiator.IsLoggedOn;
     if (!connected)
     {
         SwitchToAlternateProvider();
     }
}

Upvotes: 1

Views: 1276

Answers (1)

Christopher Andrade
Christopher Andrade

Reputation: 53

Well, after realising the limitation of the QuickFix/N component, I would never receive any feedback from this library if the host was down.

My resolution was to just simply use the following piece of code to check if socket was open before starting the connection in QuickFix/n

bool IsPortOpen(string host, int port, TimeSpan timeout)
{
    try
    {
        using(var client = new TcpClient())
        {
            var result = client.BeginConnect(host, port, null, null);
            var success = result.AsyncWaitHandle.WaitOne(timeout);
            if (!success)
            {
                return false;
            }

            client.EndConnect(result);
        }

    }
    catch
    {
        return false;
    }
    return true;
}

Upvotes: 2

Related Questions