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 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.
To accept the incoming connection, create the new OnixS::FIX::Session object, and call the OnixS::FIX::Session::logonAsAcceptor method to establish the connection. The required OnixS::FIX::Session constructor parameters can be filled from the incomingLogon
message.
To decline the incoming connection, no action is needed. One thing, that you can do, is to set the rejectReason
parameter and Engine will use it as a text in the logout message.
Example
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, const int listenPort, const int counterpartyPort, const std::string& counterpartyIpAddress, std::string * rejectReason)
{
if(acceptConnection_)
{
session_ = new Session(incomingLogon.get(Tags::TargetCompID), incomingLogon.get(Tags::SenderCompID), incomingLogon.dictionary(), this);
session_->logonAsAcceptor();
}
else
{
if(rejectReason_ != "")
{
*rejectReason = rejectReason_;
}
}
}
virtual void onInboundApplicationMsg(Message& message, Session*)
{
}
virtual void onError (EngineErrorReason::Enum reason, const std::string& description)
{
clog << "ERROR: " << description << endl;
}
virtual void onWarning (EngineWarningReason::Enum reason, const std::string& description)
{
clog << "WARNING: " << description << endl;
}
private:
bool acceptConnection_;
std::string rejectReason_;
};
UnknownSessionAcceptor unknownSessionAcceptor(true, "");
Engine::instance()->registerListener(&unknownSessionAcceptor);