OnixS C++ ICE Binary Order Entry Handler 1.1.1
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
83 bool tryAcceptConnection(const Timeout& acceptTimeout = defaultTimeout());
84
86 Gateway& acceptSession(SeqNumber outgoingSequenceNumber = 1);
87
92 ClientMessageListener* listener = nullptr, ClientSessionMessageListener* sessionMessageListener = nullptr,
93 std::atomic<bool>* stopRequested = nullptr);
94
97
100
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
145 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
182 int socketReceiveBufferSize() const noexcept;
183
189
194 int socketSendBufferSize() const noexcept;
195
201
204 LogonReportContainer createLogonReport(Int32NULL exchangeCode = NullInt32NULL(), std::string text = {});
205
208 static IPReportContainer createIpReport(StrRef userId, StrRef host, Int16NULL port, StrRef ipSessionToken, Int32NULL exchangeCode = NullInt32NULL(), std::string text = {});
209
212 static TraderLogonReportContainer createTraderLogonReport(StrRef traderId, int clientId, Int32NULL exchangeCode = NullInt32NULL(), std::string text = {});
213
216 static TraderLogoutReportContainer createTraderLogoutReport(StrRef traderId, Int32NULL exchangeCode = NullInt32NULL(), std::string text = {});
217
220 static LogoutReportContainer createLogoutReport(StrRef userId, Int32NULL exchangeCode = NullInt32NULL(), std::string text = {});
221
225
226protected:
229 bool isConnectionClosed(const Timeout& timeout) const;
230
234 bool isConnectionClosed() const;
235
239
243 MessageBasePtr tryReceive(const std::chrono::microseconds& timeout = defaultTimeout()) const;
244
250
256
262
268
270 Gateway& sendLogoutRequest(StrRef userId, Int32NULL exchangeCode = NullInt32NULL(), std::string text = {});
271
275
278
280 Gateway& sendData(const void* data, size_t size);
281
284
287 {
288 return Timeout(30000);
289 }
290
291private:
292 class Impl;
293 std::unique_ptr<Impl> impl_;
294
297};
298
299
300template <typename TMsg>
305
306template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
308{
309 const auto messageSize = msg.setHeader();
310 auto* const header = msg.header();
311 assert(header);
312
313 return send(Messaging::SbeMessage(header + 1, messageSize), *header);
314}
315
316template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
321
322template <typename Holder>
323typename std::enable_if<std::is_base_of<TagBased::Message, typename Holder::MessageType>::value>::type
325{
326 throw std::runtime_error("Not implemented.");
327}
328
329template <typename Holder>
330typename std::enable_if<!std::is_base_of<TagBased::Message, typename Holder::MessageType>::value>::type
331 setSeqNum(Holder& msg, SeqNumber num)
332{
333 msg->sequenceId(num);
334}
335
336template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
342
343template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
349
350template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
352 MessageHolder<SbeMessageType, MaxMessageSize, MessageInitializer>& msg, std::initializer_list<SeqNumber> numbers)
353{
354 for(auto seqNum : numbers)
355 {
356 send(msg, seqNum);
357 }
358
359 return *this;
360}
361
362template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
364 MessageHolder<SbeMessageType, MaxMessageSize, MessageInitializer>&& msg, std::initializer_list<SeqNumber> numbers)
365{
366 for(auto seqNum : numbers)
367 {
368 send(msg, seqNum);
369 }
370
371 return *this;
372}
373
374}
#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:155
#define ONIXS_ICEBOE_NODISCARD
Definition Compiler.h:156
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.
Gateway(const Gateway &)=delete
Deleted.
std::chrono::milliseconds Timeout
Timeout alias.
Definition Gateway.h:56
int socketReceiveBufferSize() const noexcept
Gateway & send(Messaging::SbeMessage, Messaging::SimpleOpenFramingHeader &)
Sends the given message.
friend class ClientSessionMessageListener
Definition Gateway.h:296
LogoutRequestPtr receiveLogoutRequest()
Receive the LogoutRequest message.
LogonRequestPtr acceptLogonRequest(UInt32 sequenceId)
Receives the LogonRequest and responds with the LogonReport message.
Gateway & acceptSession(SeqNumber outgoingSequenceNumber=1)
Accepts an incoming session.
static IPReportContainer createIpReport(StrRef userId, StrRef host, Int16NULL port, StrRef ipSessionToken, Int32NULL exchangeCode=NullInt32NULL(), std::string text={})
Creates a IPReport message.
LogoutRequestPtr waitUntilTerminate(ClientMessageListener *listener=nullptr, ClientSessionMessageListener *sessionMessageListener=nullptr, std::atomic< bool > *stopRequested=nullptr)
Waits until the LogoutRequest message is received.
Gateway & sendData(const void *data, size_t size)
Sends the given data.
bool isConnectionClosed() const
Checks whether the connection is closed by the counterparty using the default timeout (provided in th...
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.
LogonReportContainer createLogonReport(Int32NULL exchangeCode=NullInt32NULL(), std::string text={})
Creates a LogonReport message.
Gateway & enableLogger(const std::string &logFileName, unsigned int logLevel=3)
Enables logging.
Gateway & close()
Close the listening socket.
Gateway & disconnect()
Closes the accepted connection.
Gateway(Gateway &&) noexcept
Move semantic.
LogonRequestPtr receiveLogonRequest()
Receives the LogonRequest message.
static HeartBeatContainer createHeartBeat()
Creates a Heartbeat message.
static TraderLogoutReportContainer createTraderLogoutReport(StrRef traderId, Int32NULL exchangeCode=NullInt32NULL(), std::string text={})
Creates a TraderLogoutReport message.
Gateway & sendSequence()
Sends the HeartBeat message.
LogoutRequestPtr acceptLogoutRequest()
Receive the LogoutRequest message and responds with the LogoutReport message.
static Timeout defaultTimeout()
Default timeout.
Definition Gateway.h:286
static LogoutReportContainer createLogoutReport(StrRef userId, Int32NULL exchangeCode=NullInt32NULL(), std::string text={})
Creates a LogoutReport 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:301
void swap(Gateway &) noexcept
Swap with other.
static TraderLogonReportContainer createTraderLogonReport(StrRef traderId, int clientId, Int32NULL exchangeCode=NullInt32NULL(), std::string text={})
Creates a TraderLogonReport message.
Gateway & send(MessageHolder< SbeMessageType, MaxMessageSize, MessageInitializer > &msg)
Sends the given message.
Definition Gateway.h:307
LogoutRequestPtr terminate(StrRef userId)
Sends the LogoutReport 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...
Gateway & sendLogoutRequest(StrRef userId, Int32NULL exchangeCode=NullInt32NULL(), std::string text={})
Sends the LogoutReport message.
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:324
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:263
MessagePtr< SbeMessage > MessageBasePtr
Definition Gateway.h:43
decltype(std::declval< const Messaging::SbeMessage & >().sequenceId()) SeqNumber
Definition Messaging.h:53