This sample shows how to connect TCPDirect session to the pre-defined host and port.
The TCP loopback is not supported by TCPDirect technology, so the emulator should be started on a remote host (or on a different physically connected card with a configured route to it).
#include "../Common/Helpers.h"
#include "../Common/Listener.h"
#include "../Settings/Defaults.h"
using namespace Samples;
using namespace Threading;
int securityId;
Int64 priceMantissa;
int main(int argc, char * argv[]) {
clog << "CME iLink 3 TCPDirectGettingStarted Sample." << endl << endl;
int marketSegmentId = 99;
string host = "192.168.0.1";
bool useEmulator = false;
if(argc < 4) {
#if defined(ONIXS_ILINK3_HAS_GATEWAY_EMULATOR)
useEmulator = true;
#else
std::cerr << "usage: [MarketSegmentId] [Host] [Port] (SecurityId) (PriceMantissa)" << std::endl;
return 1;
#endif
}
else {
marketSegmentId = atoi(argv[1]);
host = argv[2];
port = atoi(argv[3]);
}
securityId = argc > 4 ? atoi(argv[4]) : DefaultSecurityId;
priceMantissa = argc > 5 ? fromStr<Int64>(argv[5]) : DefaultPriceMantissa;
#if defined(ONIXS_ILINK3_HAS_GATEWAY_EMULATOR)
std::unique_ptr<GatewayEmulatorThread> gateway;
#endif
int result = 0;
try {
SignalHelper::manageLinuxSignals();
const SessionSettings settings = fillSettings(useEmulator);
#if defined(ONIXS_ILINK3_HAS_GATEWAY_EMULATOR)
if(useEmulator)
gateway.reset(new GatewayEmulatorThread(settings.licenseStores(), host, port));
#endif
TcpDirectAttr attr;
TcpDirectStack tcpDirectStack(attr);
bool finished = false;
class TcpDirectListener : public Listener
{
public:
explicit TcpDirectListener(bool & finished)
: finished_(finished) {}
{
Listener::onStateChange(newState, prevState, session);
switch(newState) {
case SessionStateId::Established:
{
NewOrderSingle order;
Helper::setOrderFields(order, PartyDetailsListReqID, securityId, priceMantissa);
clog << "\nThe order was sent." << endl;
break;
}
case SessionStateId::Disconnected:
finished_ = true;
break;
default:
break;
}
}
void onError(
{
Listener::onError(reason, text, session, msg);
finished_ = true;
}
private:
bool & finished_;
}
listener(finished);
Session session(tcpDirectStack, settings, marketSegmentId, &listener);
while(!finished)
{
tcpDirectStack.dispatchEvents();
if(Helper::keyPressed())
break;
}
while(!tcpDirectStack.isQuiescent())
tcpDirectStack.dispatchEvents();
}
catch(const std::exception & ex) {
cerr << "\nEXCEPTION: " << ex.what() << endl;
result = 1;
}
#if defined(ONIXS_ILINK3_HAS_GATEWAY_EMULATOR)
if(gateway) {
try {
gateway->wait();
}
catch(...) {
}
}
#endif
return result;
}