OnixS C++ FIX Engine  4.13.0
API Documentation
TCPDirect Buy Side Sample

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 Solarflare TCPDirect© technology.


Source code:


TcpDirectListener.h:

#include "../Common/Settings.h"
#include "../Common/Helpers.h"
using namespace Settings;
class TcpDirectListener : public OnixS::FIX::ISessionListener
{
public:
TcpDirectListener() ONIXS_FIXENGINE_NOTHROW : started_(false), finished_(false) {}
~TcpDirectListener() ONIXS_FIXENGINE_OVERRIDE ONIXS_FIXENGINE_DEFAULT;
bool started() const ONIXS_FIXENGINE_NOTHROW
{
return started_;
}
bool finished() const ONIXS_FIXENGINE_NOTHROW
{
return finished_;
}
/// Is called when the application-level message is received from the counterparty.
void onInboundApplicationMsg(OnixS::FIX::Message & msg, OnixS::FIX::Session * sn) ONIXS_FIXENGINE_FINAL
{
std::clog << "\nIncoming application-level message:\n" << msg << std::endl;
try
{
if(msg.type() == OnixS::FIX::FIX44::Values::MsgType::NewOrderSingle)
{ // New Order - Single
const OnixS::FIX::Message & order = msg;
OnixS::FIX::Message execReport(OnixS::FIX::FIX44::Values::MsgType::ExecutionReport, FixProtocolVersion);
execReport.set(OnixS::FIX::FIX44::Tags::OrderID, order.get(OnixS::FIX::FIX44::Tags::ClOrdID).toString())
.set(OnixS::FIX::FIX44::Tags::ExecType, "0")
.set(OnixS::FIX::FIX44::Tags::ExecTransType, "0") // New
.set(OnixS::FIX::FIX44::Tags::OrdStatus, "0") // New
.set(OnixS::FIX::FIX44::Tags::Symbol, order.get(OnixS::FIX::FIX44::Tags::Symbol).toString())
.set(OnixS::FIX::FIX44::Tags::Side, order.get(OnixS::FIX::FIX44::Tags::Side).toString())
.set(OnixS::FIX::FIX44::Tags::OrderQty, order.get(OnixS::FIX::FIX44::Tags::OrderQty).toString())
.set(OnixS::FIX::FIX44::Tags::CumQty, order.get(OnixS::FIX::FIX44::Tags::OrderQty).toString())
.set(OnixS::FIX::FIX44::Tags::AvgPx, 100.0)
.set(OnixS::FIX::FIX44::Tags::LastPx, 0)
.set(OnixS::FIX::FIX44::Tags::LeavesQty, order.get(OnixS::FIX::FIX44::Tags::OrderQty).toString());
sn->send(&execReport);
std::clog << "\nSent to the counterparty:\n" << execReport << std::endl;
}
}
catch(const std::exception & ex)
{
std::clog << "Exception during the processing of incoming message: " << ex.what() << std::endl;
}
}
/// Is called when the session-level message is received from the counterparty.
{
std::clog << "\nIncoming session-level message:\n" << msg << std::endl;
}
{
std::clog
<< "\nSession's state is changed, prevState="
<< ", newState="
<< std::endl;
started_ = true;
finished_ = true;
}
void onError(OnixS::FIX::ErrorReason::Enum, const std::string & description, OnixS::FIX::Session *) ONIXS_FIXENGINE_FINAL
{
std::cerr << "\nSession-level error:" << description << std::endl;
finished_ = true;
}
void onWarning(OnixS::FIX::WarningReason::Enum, const std::string & description, OnixS::FIX::Session *) ONIXS_FIXENGINE_FINAL
{
std::cerr << "\nSession-level warning:" << description << std::endl;
}
private:
bool started_;
bool finished_;
};

TCPDirectBuySide.cpp:

#include "../../Common/TcpDirectListener.h"
using namespace Settings;
using namespace OnixS::FIX;
using namespace OnixS::FIX::FIX44;
int main()
{
std::clog << "TCPDirect BuySide sample." << std::endl << std::endl;
const std::string counterpartyHost = "";
const std::string networkInterfaceName = "";
if(counterpartyHost.empty() || networkInterfaceName.empty())
{
std::cerr << "Please perform the setup." << std::endl;
return 1;
}
try
{
manageLinuxSignals();
EngineSettings settings;
settings.licenseStore(LicenseStore);
Engine::init(settings);
attr.networkInterface(networkInterfaceName);
TCPDirect::Stack stack(attr);
TcpDirectListener listener;
Session session(&stack, SenderCompId, TargetCompId, FixProtocolVersion, &listener);
const bool setResetSeqNumFlagOnLogon = true;
const int counterpartyPort = ListenPort;
const int heartBtInt = 30;
session.logonAsInitiatorAsync(counterpartyHost, counterpartyPort, heartBtInt, ONIXS_FIXENGINE_NULLPTR, setResetSeqNumFlagOnLogon);
std::clog << "Press any key to send the order..." << std::endl;
// The TCPDirect middleware is NOT thread-safe.
// Therefore, the event dispatching and message sending should be performed from the same thread.
while(!listener.finished() && !keyPressed())
stack.dispatchEvents();
Message order(Values::MsgType::NewOrderSingle, FixProtocolVersion);
setOrderFields(&order);
session.send(&order);
std::clog << "The order " << std::endl << order << std::endl << " was sent" << std::endl;
std::clog << "Press any key to disconnect the session and terminate the application." << std::endl;
// The TCPDirect middleware is NOT thread-safe.
// Therefore, the event dispatching and message sending should be performed from the same thread.
while(!listener.finished() && !keyPressed())
stack.dispatchEvents();
session.logoutAsync("The session is disconnected by TCPDirect");
while(!listener.finished())
stack.dispatchEvents();
session.shutdown();
Engine::shutdown();
while(!stack.isQuiescent())
stack.dispatchEvents();
}
catch(const std::exception & ex)
{
processSampleException(ex.what());
return 1;
}
return 0;
}