OnixS C++ ICE Binary Order Entry Handler 1.0.0
API Documentation
Loading...
Searching...
No Matches
Gateway.h
Go to the documentation of this file.
1// Copyright Onix Solutions Limited [OnixS]. All rights reserved.
2//
3// This software owned by Onix Solutions Limited [OnixS] and is
4// protected by copyright law and international copyright treaties.
5//
6// Access to and use of the software is governed by the terms of the applicable
7// OnixS Software Services Agreement (the Agreement) and Customer end user license
8// agreements granting a non-assignable, non-transferable and non-exclusive license
9// to use the software for it's own data processing purposes under the terms defined
10// in the Agreement.
11//
12// Except as otherwise granted within the terms of the Agreement, copying or
13// reproduction of any part of this source code or associated reference material
14// to any other location for further reproduction or redistribution, and any
15// amendments to this copyright notice, are expressly prohibited.
16//
17// Any reproduction or redistribution for sale or hiring of the Software not in
18// accordance with the terms of the Agreement is a violation of copyright law.
19
20#pragma once
21
24
28
29#include <chrono>
30#include <atomic>
31
33namespace Testing {
34
35// Type aliases
42
50
53{
54public:
56 using Timeout = std::chrono::milliseconds;
57
58 Gateway(const SessionSettings::LicenseStores& licenseStores, int port, const char* host = "127.0.0.1",
59 const Timeout& acceptTimeout = defaultTimeout(),
60 const Timeout& sendReceiveTimeout = defaultTimeout());
61
63 virtual ~Gateway();
64
66 Gateway(const Gateway&) = delete;
67 Gateway& operator=(const Gateway&) = delete;
68
70 Gateway(Gateway&&) noexcept;
71 Gateway& operator=(Gateway&&) noexcept;
72
74 void swap(Gateway&) noexcept;
75
78 Gateway& acceptConnection(const Timeout& acceptTimeout = defaultTimeout());
79
82 bool tryAcceptConnection(const Timeout& acceptTimeout = defaultTimeout());
83
85 Gateway& acceptSession(SeqNumber outgoingSequenceNumber = 1);
86
93 ClientMessageListener* listener = nullptr, ClientSessionMessageListener* sessionMessageListener = nullptr,
94 std::atomic<bool>* stopRequested = nullptr);
95
98
101
103 std::string host() const;
104
107 int port() const noexcept;
108
110 Gateway& enableLogger(const std::string& logFileName, unsigned int logLevel = 3);
111
114 template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
115 Gateway& send(MessageHolder<SbeMessageType, MaxMessageSize, MessageInitializer>& msg);
116
119 template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
120 Gateway& send(MessageHolder<SbeMessageType, MaxMessageSize, MessageInitializer>&& msg);
121
124 template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
125 Gateway& send(MessageHolder<SbeMessageType, MaxMessageSize, MessageInitializer>& msg, SeqNumber num);
126
129 template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
130 Gateway& send(MessageHolder<SbeMessageType, MaxMessageSize, MessageInitializer>&& msg, SeqNumber num);
131
134 template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
135 Gateway& send(MessageHolder<SbeMessageType, MaxMessageSize, MessageInitializer>& msg, std::initializer_list<SeqNumber> numbers);
136
139 template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
140 Gateway& send(MessageHolder<SbeMessageType, MaxMessageSize, MessageInitializer>&& msg, std::initializer_list<SeqNumber> numbers);
141
142
146 template <typename TMsg>
148
153 Gateway& run(ClientMessageListener& listener, std::atomic<bool>* stopRequested = nullptr);
154
160 Gateway& run(ClientMessageListener& appMsgListener, ClientSessionMessageListener& sessionMsgListener, std::atomic<bool>* stopRequested = nullptr);
161
164 SeqNumber outSeqNum() const noexcept;
165
167 Gateway& outSeqNum(SeqNumber nextOutSeqNum) noexcept;
168
170 Gateway& tcpNoDelayOption(bool noDelay);
171
176 bool tcpNoDelayOption() const noexcept ;
177
183
189
194 int socketSendBufferSize() const noexcept;
195
201
202protected:
205 bool isConnectionClosed(const Timeout& timeout) const;
206
210 bool isConnectionClosed() const;
211
214
217 MessageBasePtr tryReceive(const std::chrono::microseconds& timeout = defaultTimeout()) const;
218
223
228
233
238
240 Gateway& sendTerminate(StrRef userId, Int32NULL exchangeCode = NullInt32NULL(), const std::string& text = {});
241
244
246 LogonReportContainer createNegotiationResponse(Int32NULL exchangeCode = NullInt32NULL(), const std::string& text = {});
247
249 static IPReportContainer createIpReport(StrRef userId, StrRef host, Int16NULL port, StrRef ipSessionToken, Int32NULL exchangeCode = NullInt32NULL(), const std::string& text = {});
250
252 static TraderLogonReportContainer createTraderLogonReport(StrRef traderId, int clientId, Int32NULL exchangeCode = NullInt32NULL(), const std::string& text = {});
253
255 static TraderLogoutReportContainer createTraderLogoutReport(StrRef traderId, Int32NULL exchangeCode = NullInt32NULL(), const std::string& text = {});
256
258 static LogoutReportContainer createLogoutReport(StrRef userId, Int32NULL exchangeCode = NullInt32NULL(), const std::string& text = {});
259
262
265
267 Gateway& sendData(const void* data, size_t size);
268
271
274 {
275 return Timeout(30000);
276 }
277
278private:
279 class Impl;
280 Impl* impl_;
281};
282
283
284template <typename TMsg>
289
290template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
292{
293 const auto messageSize = msg.setHeader();
294 auto* const header = msg.header();
295 assert(header);
296
297 return send(Messaging::SbeMessage(header + 1, messageSize), *header);
298}
299
300template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
305
306template <typename Holder>
307typename std::enable_if<std::is_base_of<TagBased::Message, typename Holder::MessageType>::value>::type
309{
310 throw std::runtime_error("");
311 //msg->set(TagBased::Tags::SeqNum, num);
312}
313
314template <typename Holder>
315typename std::enable_if<!std::is_base_of<TagBased::Message, typename Holder::MessageType>::value>::type
316 setSeqNum(Holder& msg, SeqNumber num)
317{
318 msg->sequenceId(num);
319}
320
321template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
327
328template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
334
335template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
337 MessageHolder<SbeMessageType, MaxMessageSize, MessageInitializer>& msg, std::initializer_list<SeqNumber> numbers)
338{
339 for(auto seqNum : numbers)
340 {
341 send(msg, seqNum);
342 }
343
344 return *this;
345}
346
347template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
349 MessageHolder<SbeMessageType, MaxMessageSize, MessageInitializer>&& msg, std::initializer_list<SeqNumber> numbers)
350{
351 for(auto seqNum : numbers)
352 {
353 send(msg, seqNum);
354 }
355
356 return *this;
357}
358
359}
#define ONIXS_ICEBOE_NAMESPACE_BEGIN
Definition ABI.h:94
#define ONIXS_ICEBOE_NAMESPACE_END
Definition ABI.h:98
#define ONIXS_ICEBOE_EXPORTED
Definition Compiler.h:153
#define ONIXS_ICEBOE_NODISCARD
Definition Compiler.h:154
Contains the SimpleOpenFramingHeader, the SBE message, and the data buffer.
const SimpleOpenFramingHeader * header() const noexcept
MessageSize setHeader() noexcept
Calculates the binary size of the message and updates the Simple Open Framing Header accordingly.
std::vector< std::string > LicenseStores
Folders that contain license file(s).
int socketSendBufferSize() const noexcept
Gateway & acceptConnection(const Timeout &acceptTimeout=defaultTimeout())
Accepts an incoming connection.
Gateway(const SessionSettings::LicenseStores &licenseStores, int port, const char *host="127.0.0.1", const Timeout &acceptTimeout=defaultTimeout(), const Timeout &sendReceiveTimeout=defaultTimeout())
bool tryAcceptConnection(const Timeout &acceptTimeout=defaultTimeout())
Accepts an incoming connection.
MessageBasePtr receive() const
Receives a message.
static TraderLogoutReportContainer createTraderLogoutReport(StrRef traderId, Int32NULL exchangeCode=NullInt32NULL(), const std::string &text={})
Creates a TraderLogoutReport message.
Gateway(const Gateway &)=delete
Deleted.
std::chrono::milliseconds Timeout
Timeout alias.
Definition Gateway.h:56
Gateway & send(Messaging::SbeMessage, Messaging::SimpleOpenFramingHeader &)
Sends the given message.
LogoutRequestPtr receiveLogoutRequest()
Receive the LogoutRequest message.
static LogoutReportContainer createLogoutReport(StrRef userId, Int32NULL exchangeCode=NullInt32NULL(), const std::string &text={})
Creates a LogoutReport message.
LogonRequestPtr acceptLogonRequest(UInt32 sequenceId)
Receives the LogonRequest and responds with the LogonReport message.
Gateway & acceptSession(SeqNumber outgoingSequenceNumber=1)
Accepts an incoming session.
LogonReportContainer createNegotiationResponse(Int32NULL exchangeCode=NullInt32NULL(), const std::string &text={})
Creates a NegotiationResponse501 message.
LogoutRequestPtr waitUntilTerminate(ClientMessageListener *listener=nullptr, ClientSessionMessageListener *sessionMessageListener=nullptr, std::atomic< bool > *stopRequested=nullptr)
Waits until the Terminate507 message is received.
Gateway & sendData(const void *data, size_t size)
Sends the given data.
SeqNumber outSeqNum() const noexcept
Gateway & run(ClientMessageListener &listener, std::atomic< bool > *stopRequested=nullptr)
Accepts an incoming connection and establishes the session.
Gateway & operator=(const Gateway &)=delete
MessageBasePtr tryReceive(const std::chrono::microseconds &timeout=defaultTimeout()) const
Receives a message using the provided timeout.
Gateway & enableLogger(const std::string &logFileName, unsigned int logLevel=3)
Enables logging.
Gateway & close()
Close the listening socket.
Gateway & disconnect()
Closes the accepted connection.
static IPReportContainer createIpReport(StrRef userId, StrRef host, Int16NULL port, StrRef ipSessionToken, Int32NULL exchangeCode=NullInt32NULL(), const std::string &text={})
Creates a IPReport message.
Gateway(Gateway &&) noexcept
Move semantic.
static TraderLogonReportContainer createTraderLogonReport(StrRef traderId, int clientId, Int32NULL exchangeCode=NullInt32NULL(), const std::string &text={})
Creates a TraderLogonReport message.
LogonRequestPtr receiveNegotiate()
Receives the LogonRequest message.
Gateway & sendSequence()
Sends the Sequence506 message.
static Timeout defaultTimeout()
Default timeout.
Definition Gateway.h:273
Gateway & sendTerminate(StrRef userId, Int32NULL exchangeCode=NullInt32NULL(), const std::string &text={})
Sends the Terminate507 message.
static HeartBeatContainer createSequence()
Creates a Heartbeat message.
bool isConnectionClosed(const Timeout &timeout) const
Checks whether the connection is closed by the counterparty.
MessagePtr< TMsg > receiveTypedMessage()
Receives the message of the particular type.
Definition Gateway.h:285
void swap(Gateway &) noexcept
Swap with other.
LogoutRequestPtr acceptTerminate()
Receive the LogoutRequest message and responds with the LogoutReport message.
Gateway & send(MessageHolder< SbeMessageType, MaxMessageSize, MessageInitializer > &msg)
Sends the given message.
Definition Gateway.h:291
LogoutRequestPtr terminate(StrRef userId)
Sends the Terminate507 message and waits until the response is received.
Gateway & tcpNoDelayOption(bool noDelay)
Sets the value of the option to improve latency at the expense of message throughput (the TCP_NODELAY...
IntegralConstant< Int32, -2147483647-1 > NullInt32NULL
Null value for an optional Int32NULL field.
Definition Fields.h:994
constexpr std::enable_if<!details::HasMemberTraits< Value >::value, size_t >::type size() noexcept
Definition Memory.h:303
Int32 Int32NULL
int32NULL.
Definition Fields.h:80
Int16 Int16NULL
int16NULL.
Definition Fields.h:74
std::basic_string_view< Char > StrRef
Definition StrRef.h:46
MessagePtr< LogonRequest > LogonRequestPtr
Definition Gateway.h:44
MessagePtr< IPRequest > IPRequestPtr
Definition Gateway.h:46
MessageHolder< HeartBeat > HeartBeatContainer
Definition Gateway.h:41
MessagePtr< TraderLogonRequest > TraderLogonRequestPtr
Definition Gateway.h:47
MessageHolder< IPReport > IPReportContainer
Definition Gateway.h:37
MessagePtr< TraderLogoutRequest > TraderLogoutRequestPtr
Definition Gateway.h:48
MessageHolder< TraderLogonReport > TraderLogonReportContainer
Definition Gateway.h:38
MessagePtr< HeartBeat > HeartBeatPtr
Definition Gateway.h:49
MessagePtr< LogoutRequest > LogoutRequestPtr
Definition Gateway.h:45
std::enable_if< std::is_base_of< TagBased::Message, typenameHolder::MessageType >::value >::type setSeqNum(Holder &, SeqNumber)
Definition Gateway.h:308
MessageHolder< LogoutReport > LogoutReportContainer
Definition Gateway.h:40
MessageHolder< TraderLogoutReport > TraderLogoutReportContainer
Definition Gateway.h:39
MessageHolder< LogonReport > LogonReportContainer
Definition Gateway.h:36
MessagePtr< AnotherType > cast(const MessagePtr< BaseType > &ptr)
Tries to cast to another type.
Definition MessagePtr.h:271
MessagePtr< SbeMessage > MessageBasePtr
Definition Gateway.h:43
Messaging::UInt32 SeqNumber
Definition Messaging.h:51