How to make the login part in QuickFIX

2019-01-20 12:46发布

How can I make the login part in QuickFIX in c++? I found tons of tutorials and articles on how to do this on c# or java, but nothing on c++.

I have a server (acceptor), and a client (initiator). The username and password of the client are stored in the settings file, and are hardcoded in the server program.

From what I've read in the client I set the username and password in fromAdmin() and read and check the in the server in the toAdmin(), but how do I do that?

Here's what I've tried so far:

  • cast the message to a FIX44::Logon& object using:

    FIX44::Logon& logon_message = dynamic_cast<FIX44::Logon&>(message);
    
  • Set the Username and password to the logon object like this:

    if(session_settings.has("Username"))
    {
        FIX::Username username = session_settings.getString("Username");
        logon_message.set(username);
    }
    
  • And send the message like this:

    FIX::Message messageToSend = logon_message;
    FIX::Session::sendToTarget(messageToSend);
    

But I get this error on the cast:

cannot dynamic_cast 'message' (of type 'class FIX::Message') to type 'struct FIX44::Logon&' (target is not pointer or reference to complete type)

What I've tried I got inspired from http://niki.code-karma.com/2011/01/quickfix-logon-support-for-username-password/comment-page-1/.

I'm still not clear on how to make the client and the server.

Can anyone help me?

1条回答
不美不萌又怎样
2楼-- · 2019-01-20 13:47

Possible mistakes:

1) I think you have fromAdmin()/toAdmin() backward. toAdmin() is called on outgoing admin messages, fromAdmin() is called on incoming. For the Initiator, you must set the fields within the toAdmin() callback. Your Acceptor will check the user/pass in fromAdmin().

2) Are you trying to dynamic_cast without first checking to see if it was a Logon message? The toAdmin() callback handles all admin messages; the message could be a Heartbeat, Logon, Logout, etc. That might explain your cast error.

As to what the code should look like, my C++ is rusty, but the basic pattern is this:

void YourMessageCracker::toAdmin( FIX::Message& message, const FIX::SessionID& sessionID)
{
    if (FIX::MsgType_Logon == message.getHeader().getField(FIX::FIELD::MsgType))
    {
        FIX44::Logon& logon_message = dynamic_cast<FIX44::Logon&>(message);
        logon_message.setField(FIX::Username("my_username"));
        logon_message.setField(FIX::Password("my_password"));
    }
}

From there, I think you can see how you'd write a similar fromAdmin() where you'd get the fields instead of setting them.

The above uses hard-coded user/pass, but you probably want to pull it from the config file. I think your calls to session_settings.getString(str) are correct for that.

(Please forgive any coding errors. I'm much more fluent in the Java/C# versions of the QF engine, though the basic principles are the same.)

I see that your first web reference uses the FIELD_GET_REF macro. It may be better than message.getHeader().getField(), but I'm not familiar with it.

EDIT: I hope you didn't start reading this until I finished my 45 edits of it. I think it's good now.

查看更多
登录 后发表回答