OnixS C++ ICE Binary Order Entry Handler 1.1.1
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/Emulator.h>
30
31using namespace Samples;
32using namespace Threading;
33
34int main(int argc, char * argv[])
35{
36 // `--help` to show options.
37 const AppConfiguration<
43 > cfg{"TCPDirectGettingStarted", argc, argv};
44
45 try
46 {
48
49 auto settings = fillSettings(cfg);
50 const auto emulator = createEmulator(settings, cfg, true);
51
52 // Request Binary Order Gateway credentials from Binary Utility Service Gateway (using OS sockets)
53 const auto bgwCredentials = receiveBgwCredentials(settings, cfg.host(), cfg.port());
54
55 class TcpDirectListener : public Listener
56 {
57 public:
58 explicit TcpDirectListener(const LogonConfiguration& cfg)
59 : traderLogonRequest_(Helper::createTraderLogonRequest(cfg.traderId(), cfg.traderPwd()))
60 , order_(Helper::createOrder(cfg.traderId()))
61 {
62 }
63
64 void onTraderLogonReport(const Messaging::TraderLogonReport msg, Session* session) override
65 {
66 Listener::onTraderLogonReport(msg, session);
67
68 try
69 {
70 traderLoggedOn.get_future().get();
71 }
72 catch(const std::exception& e)
73 {
74 onError(SessionErrorReason::UnsuccessfulLogonReportInReplyOnLogonRequest, e.what(), session, msg);
75 return;
76 }
77
78 session->send(order_);
79 }
80
81 void onStateChange(SessionStateId::Enum newState, SessionStateId::Enum prevState, Session * session) override
82 {
83 Listener::onStateChange(newState, prevState, session);
84
85 switch(newState)
86 {
87 case SessionStateId::Established:
88 {
89 // The TCPDirect technology is NOT thread-safe.
90 // Therefore, the event dispatching and message sending should be performed from the same thread.
91 session->send(traderLogonRequest_);
92 break;
93 }
94
95 case SessionStateId::Disconnected:
96 finished_ = true;
97 break;
98
99 default:
100 break;
101 }
102 }
103
105
106 void onError(
107 SessionErrorReason::Enum reason, const std::string & text, Session * session, Messaging::SbeMessage msg) override
108 {
109 Listener::onError(reason, text, session, msg);
110 finished_ = true;
111 }
112
113 bool finished() const noexcept {return finished_;}
114
115 private:
116 bool finished_ {false};
117 MessageHolder<TraderLogonRequest> traderLogonRequest_;
118 MessageHolder<NewOrderRequest> order_;
119 }
120 listener(cfg);
121
122
123 TcpDirectAttr attr;
124
125 // The default interface name could be provided by the environment variable
126 // ZF_ATTR="interface=<interface-name>", see TCPDirect specs,
127 attr.set("interface", cfg.nif());
128
129 TcpDirectStack stack{attr};
130
131 // A 'BGW' session
132 const auto bgwSession= std::make_unique<BgwSession>(stack, settings, &listener, cfg.storage());
133 bgwSession->connectAsync(bgwCredentials);
134
135 std::clog << "\nPress <Ctrl+C> to disconnect the BGW session and terminate the application." << std::endl;
136
137 while(!listener.finished())
138 {
139 // The TCPDirect technology is NOT thread-safe.
140 // Therefore, the event dispatching and message sending should be performed from the same thread.
141 stack.dispatchEvents();
142
144 break;
145 }
146
147 bgwSession->disconnectAsync();
148
149 while(!stack.isQuiescent())
150 stack.dispatchEvents();
151 }
152 catch(const std::exception & ex)
153 {
154 std::cerr << "\nEXCEPTION: " << ex.what() << std::endl;
155 return 1;
156 }
157
158 return 0;
159}
#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:157
BgwCredentials receiveBgwCredentials(SessionSettings settings, std::string host, Port port)
Definition Listener.h:264
SessionSettings fillSettings(const LogonConfiguration &logonCfg, const ConnectivityConfiguration &connCfg, const SettingsConfiguration &settingsCfg)
Definition Settings.h:32
static ONIXS_ICEBOE_FORCEINLINE bool interruptDetected() noexcept
Definition Signal.h:52
static void manageSignals() noexcept
Definition Signal.h:46