This sample listens an incoming connection on the pre-defined interface and port (acts as a FIX Acceptor). When the session is established, and the SingleOrder - New is received, the ExecutionReport FIX message is sent to the counterparty. This is done using Solarflare TCPDirect© technology.
#include "../Common/Settings.h"
#include "../Common/Helpers.h"
using namespace Settings;
{
public:
TcpDirectListener() noexcept : started_(false), finished_(false), failed_(false) {}
~TcpDirectListener() override = default;
bool started() const noexcept
{
return started_;
}
bool finished() const noexcept
{
return finished_;
}
bool failed() const noexcept
{
return failed_;
}
void onInboundApplicationMsg(OnixS::FIX::Message & msg, OnixS::FIX::Session * sn) final
{
std::clog << "\nIncoming application-level message:\n" << msg << std::endl;
try
{
if(msg.type() == OnixS::FIX::FIX44::Values::MsgType::NewOrderSingle)
{
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")
.set(OnixS::FIX::FIX44::Tags::OrdStatus, "0")
.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)
{
failed_ = true;
finished_ = true;
std::clog << "Exception during the processing of incoming message: " << ex.what() << std::endl;
}
}
void onInboundSessionMsg(OnixS::FIX::Message & msg, OnixS::FIX::Session *) final
{
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;
}
{
std::cerr << "\nSession-level error:" << description << std::endl;
failed_ = true;
finished_ = true;
}
{
std::cerr << "\nSession-level warning:" << description << std::endl;
}
private:
bool started_;
bool finished_;
bool failed_;
};
#include "../../Common/TcpDirectListener.h"
using namespace Settings;
int main()
{
std::clog << "TCPDirect SellSide sample." << std::endl << std::endl;
const std::string listenAddress = "";
const std::string networkInterfaceName = "";
if(listenAddress.empty() || networkInterfaceName.empty())
{
std::cerr << "Please perform the setup." << std::endl;
return 1;
}
try
{
manageLinuxSignals();
ifaces.push_back(listenAddress);
EngineShutdownGuard engineShutdownGuard(&stack);
TcpDirectListener listener;
Session session(&stack, TargetCompId, SenderCompId, FixProtocolVersion, &listener);
session.logonAsAcceptor();
std::clog <<
"Awaiting session-initiator on port " << settings.
listenPort() <<
"..." << std::endl;
while(!listener.started() && !listener.failed())
stack.dispatchEvents();
if(listener.failed())
throw std::runtime_error("TCPDirect session failed.");
std::clog << "Press any key to disconnect the session and terminate the application." << std::endl;
while(!listener.finished() && !keyPressed())
stack.dispatchEvents();
if(listener.failed())
throw std::runtime_error("TCPDirect session failed.");
session.logoutAsync("The session is disconnected by TCPDirect");
while(!listener.finished())
stack.dispatchEvents();
if(listener.failed())
throw std::runtime_error("TCPDirect session failed.");
session.shutdown();
}
catch(const std::exception & ex)
{
processSampleException(ex.what());
return 1;
}
return 0;
}