OnixS C++ ICE Binary Order Entry Handler 1.2.3
API Documentation
Loading...
Searching...
No Matches
TCPDirect Getting Started Sample

This sample demonstrates how to connect TCPDirect session to the pre-defined host and port.

When the session is established, the NewOrderRequest message is sent to the counterparty.

Using TCPDirect Network Stack

TCPDirect is part of Solarflare's suite of network acceleration technologies.

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).

For developing/debugging purposes OnixS provides TCPDirect emulation for Windows over OS sockets, so TCPDirectGettingStarted sample can be run in loopback mode under Windows.

Source code

1/*
2 * Copyright Onix Solutions Limited [OnixS]. All rights reserved.
3 *
4 * This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
5 * and international copyright treaties.
6 *
7 * Access to and use of the software is governed by the terms of the applicable ONIXS Software
8 * Services Agreement (the Agreement) and Customer end user license agreements granting
9 * a non-assignable, non-transferable and non-exclusive license to use the software
10 * for it's own data processing purposes under the terms defined in the Agreement.
11 *
12 * Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
13 * of this source code or associated reference material to any other location for further reproduction
14 * or redistribution, and any amendments to this copyright notice, are expressly prohibited.
15 *
16 * Any reproduction or redistribution for sale or hiring of the Software not in accordance with
17 * the terms of the Agreement is a violation of copyright law.
18 */
20
21using namespace ONIXS_ICEBOE_NAMESPACE;
23
24#include <Settings/Defaults.h>
25#include <Common/Helpers.h>
26#include <Common/Listener.h>
27#include <Common/Settings.h>
28#include <Common/Signal.h>
29#include <Common/Tools.h>
30#include <Common/Emulator.h>
31
32using namespace Samples;
33using namespace Threading;
34
35int main(int argc, char * argv[])
36{
37 // `--help` to show options.
38 const AppConfiguration<
44 > cfg{"TCPDirectGettingStarted", argc, argv};
45
46 try
47 {
49
50 auto settings = fillSettings(cfg);
51 const auto emulator = createEmulator(settings, cfg, true);
52
53 // Request Binary Order Gateway credentials from Binary Utility Service Gateway (using OS sockets)
54 const auto bgwCredentials = receiveBgwCredentials(settings, cfg.host(), cfg.port());
55
56 class TcpDirectListener : public Listener
57 {
58 public:
59 explicit TcpDirectListener(const LogonConfiguration& cfg)
60 : traderLogonRequest_(Helper::createTraderLogonRequest(cfg.traderId(), cfg.traderPwd()))
61 , order_(Helper::createOrder(cfg.traderId()))
62 {
63 }
64
65 void onTraderLogonReport(const Messaging::TraderLogonReport msg, Session* session) override
66 {
67 Listener::onTraderLogonReport(msg, session);
68
69 try
70 {
71 traderLoggedOn.get_future().get();
72 }
73 catch(const std::exception& e)
74 {
75 onError(SessionErrorReason::UnsuccessfulLogonReportInReplyOnLogonRequest, e.what(), session, msg);
76 return;
77 }
78
79 session->send(order_);
80 }
81
82 void onStateChange(SessionStateId::Enum newState, SessionStateId::Enum prevState, Session * session) override
83 {
84 Listener::onStateChange(newState, prevState, session);
85
86 switch(newState)
87 {
88 case SessionStateId::Established:
89 {
90 // The TCPDirect technology is NOT thread-safe.
91 // Therefore, the event dispatching and message sending should be performed from the same thread.
92 session->send(traderLogonRequest_);
93 break;
94 }
95
96 case SessionStateId::Disconnected:
97 finished_ = true;
98 break;
99
100 default:
101 break;
102 }
103 }
104
106
107 void onError(
108 SessionErrorReason::Enum reason, const std::string & text, Session * session, Messaging::SbeMessage msg) override
109 {
110 Listener::onError(reason, text, session, msg);
111 finished_ = true;
112 }
113
114 bool finished() const noexcept {return finished_;}
115
116 private:
117 bool finished_ {false};
118 MessageHolder<TraderLogonRequest> traderLogonRequest_;
119 MessageHolder<NewOrderRequest> order_;
120 }
121 listener(cfg);
122
123
124 TcpDirectAttr attr;
125
126 // The default interface name could be provided by the environment variable
127 // ZF_ATTR="interface=<interface-name>", see TCPDirect specs,
128 attr.set("interface", cfg.nif());
129
130 TcpDirectStack stack{attr};
131
132 // A 'BGW' session
133 const auto bgwSession= makeUnique<BgwSession>(stack, settings, &listener, cfg.storage());
134 bgwSession->connectAsync(bgwCredentials);
135
136 std::clog << "\nPress <Ctrl+C> to disconnect the BGW session and terminate the application." << std::endl;
137
138 while(!listener.finished())
139 {
140 // The TCPDirect technology is NOT thread-safe.
141 // Therefore, the event dispatching and message sending should be performed from the same thread.
142 stack.dispatchEvents();
143
145 break;
146 }
147
148 bgwSession->disconnectAsync();
149
150 while(!stack.isQuiescent())
151 stack.dispatchEvents();
152 }
153 catch(const std::exception & ex)
154 {
155 std::cerr << "\nEXCEPTION: " << ex.what() << std::endl;
156 return 1;
157 }
158
159 return 0;
160}
#define ONIXS_ICEBOE_NAMESPACE
Definition ABI.h:113
#define ONIXS_ICEBOE_MESSAGING_NAMESPACE
Definition ABI.h:114
int main(int argc, char *argv[])
Session & send(Messaging::MessageHolder< SbeMessageType, MaxMessageSize, MessageInitializer > &msg, Messaging::Timestamp sendingTime=UtcWatch::now())
Sends the message.
Definition Session.h:659
TCPDirect Attributes to pass configuration details (a wrapper around the zf_attr struct).
void set(const std::string &name, const std::string &value)
Sets the attribute to the given value.
A high-level wrapper over the TCPDirect network stack.
bool isQuiescent() const noexcept override
void dispatchEvents() noexcept override
This function processes events on a stack and performs the necessary handling.
void onError(const Messaging::Error msg, Session *) override
Called when an Error message is received from the counterparty.
Definition Listener.h:79
std::pair< std::unique_ptr< GatewayEmulatorThread< BusSessionGatewayListener > >, std::unique_ptr< GatewayEmulatorThread< GatewayListener > > > createEmulator(const SessionSettings &settings, const ConnectivityConfiguration &cfg, bool tcpDirect=false)
Definition Emulator.h:159
BgwCredentials receiveBgwCredentials(SessionSettings settings, std::string host, Port port)
Definition Listener.h:265
SessionSettings fillSettings(const LogonConfiguration &logonCfg, const ConnectivityConfiguration &connCfg, const SettingsConfiguration &settingsCfg)
Definition Settings.h:32
std::unique_ptr< T > makeUnique(Args &&...args)
Definition Tools.h:30
static ONIXS_ICEBOE_FORCEINLINE bool interruptDetected() noexcept
Definition Signal.h:52
static void manageSignals() noexcept
Definition Signal.h:46