Sometimes there is a requirement to accept an incoming FIX session 'on the fly', without the prior creation of the OnixS::FIX::Session object. This often happens when an unknown session-initiator is trying to connect to the Engine. For this reason, OnixS C++ FIX Engine exposes an ability to accept such a connection by creating a temporary session-acceptor and to respond to the counterparty. To take advantage of this feature it is necessary to inherit from the OnixS::FIX::IEngineListener class, to overwrite the OnixS::FIX::IEngineListener::onUnknownIncomingConnection method and register isntance of inherited listener with the OnixS::FIX::Engine instance using the OnixS::FIX::Engine::registerListener method.
An automatically created OnixS::FIX::Session object that corresponds to the incoming FIX connection is available as a parameter in overridden OnixS::FIX::IEngineListener::onUnknownIncomingConnection member.
To accept the incoming connection, update value of acceptConnection
parameter to be true. Otherwise the incoming connection will be rejected and the created OnixS::FIX::Session object will be disposed of by the Engine.
#include <OnixS/FIXEngine.h> using namespace OnixS::FIX; class UnknownSessionAcceptor : public IEngineListener, public ISessionListener { public: UnknownSessionAcceptor( bool acceptConnection, const string& rejectReason) : acceptConnection_(acceptConnection), rejectReason_(rejectReason), session_(NULL) { } virtual ~UnknownSessionAcceptor() { delete session_; } virtual void onUnknownIncomingConnection( const Message& incomingLogon, Session* session, bool* acceptConnection, std::string* rejectReason) { *acceptConnection = acceptConnection_; if(acceptConnection_) { session_ = session; // Session is already created, but we would like somehow listen to // session events. Following statement exactly does what do we want. session_->registerListener(this); } else { // Once session will be rejected, Engine will send logout message. // Engine will use reject reason as text in logout message. *rejectReason = rejectReason_; } } virtual void onInboundApplicationMsg(const Message& message, Session*) { // Process incoming messages in same way as working with usual sessions. } private: bool acceptConnection_; std::string rejectReason_; OnixS::FIX::Session* session_; }; // ... UnknownSessionAcceptor unknownSessionAcceptor(true, ""); Engine::instance()->registerListener(&unknownSessionAcceptor); // ...