This sample connects to the pre-defined host and port (acts as a FIX Initiator). When the session is established, the SingleOrder - New FIX message is sent to the counterparty. This is done using the ExternalThread threading model.
{
public:
Listener() noexcept : finished_(false), failed_(false) {}
~Listener() override = default;
bool finished() const noexcept
{
return finished_;
}
bool failed() const noexcept
{
return failed_;
}
void onInboundApplicationMsg(OnixS::FIX::Message & msg, OnixS::FIX::Session * sn) final;
void onInboundSessionMsg(OnixS::FIX::Message & msg, OnixS::FIX::Session * sn) final;
private:
bool finished_;
bool failed_;
};
#include "Listener.h"
#include <iostream>
#include <string>
{
std::clog << "\nIncoming application-level message:\n" << msg << std::endl;
}
{
std::clog << "\nIncoming session-level message:\n" << msg << std::endl;
}
{
std::clog
<< "\nSession's state is changed, prevState="
<< ", newState="
<< std::endl;
finished_ = true;
}
{
std::cerr << "\nSession-level error:" << description << std::endl;
failed_ = true;
finished_ = true;
}
{
std::cerr << "\nSession-level warning:" << description << std::endl;
}
#include "Listener.h"
#include "../../Common/Helpers.h"
#include "../../Common/Settings.h"
using namespace Settings;
int main()
{
std::clog << "ExternalThread sample." << std::endl << std::endl;
try
{
manageLinuxSignals();
EngineShutdownGuard engineShutdownGuard(&stack);
Listener acceptorListener;
Session acceptor(&stack, TargetCompId, SenderCompId, FixProtocolVersion, &acceptorListener);
acceptor.logonAsAcceptor();
Listener initiatorListener;
Session initiator(&stack, SenderCompId, TargetCompId, FixProtocolVersion, &initiatorListener);
const bool SetResetSeqNumFlagOnLogon = true;
const int HeartBtInt = 30;
{
SharedFuture<void> logonTask = initiator.logonAsInitiatorAsync(
"localhost", ListenPort, HeartBtInt,
nullptr, SetResetSeqNumFlagOnLogon);
while(!logonTask.
isReady() && !timer.elapsed())
if(timer.elapsed())
{
std::clog << std::endl << "Cannot logon during the timeout." << std::endl;
initiator.breakConnection();
throw std::runtime_error("Cannot logon during the timeout.");
}
}
if(acceptorListener.failed() || initiatorListener.failed())
throw std::runtime_error("External-thread session failed.");
std::clog << "Press any key to send an order, disconnect the session and terminate the application." << std::endl;
while(!acceptorListener.finished() && !initiatorListener.finished() && !keyPressed())
Message order(
"D", FixProtocolVersion);
setOrderFields(&order);
initiator.send(&order);
{
while((!logoutTask.
isReady() || !initiatorListener.finished()) && !timer.elapsed())
if(timer.elapsed())
{
std::clog << std::endl << "Cannot logout during the timeout." << std::endl;
initiator.breakConnection();
throw std::runtime_error("Cannot logout during the timeout.");
}
}
if(acceptorListener.failed() || initiatorListener.failed())
throw std::runtime_error("External-thread session failed.");
initiator.shutdown();
acceptor.shutdown();
}
catch(const std::exception & ex)
{
processSampleException(ex.what());
return 1;
}
return 0;
}