OnixS C++ CME iLink 3 Binary Order Entry Handler 1.19.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
28
29#include <chrono>
30#include <atomic>
31
32namespace OnixS {
33namespace CME {
34namespace iLink3 {
35namespace Testing {
36
37// Assume NegotiationResponse501::credentials is empty
38constexpr
40{
41 using Message = NegotiationResponse501;
42
43 return
44 Message::blockLength(Message::Schema::Version)
46 + Message::getMinimalVariableFieldsSize(Message::Schema::Version);
47}
48
49// Type aliases
56
62
65{
66public:
68 using Timeout = std::chrono::milliseconds;
69
70 Gateway(const SessionSettings::LicenseStores& licenseStores, int port, const char* host = "127.0.0.1",
71 const Timeout& acceptTimeout = defaultTimeout(),
72 const Timeout& sendReceiveTimeout = defaultTimeout());
73
75 virtual ~Gateway();
76
78 Gateway(const Gateway&) = delete;
79 Gateway& operator=(const Gateway&) = delete;
80
82 Gateway(Gateway&&) noexcept;
83 Gateway& operator=(Gateway&&) noexcept;
84
86 void swap(Gateway&) noexcept;
87
90 Gateway& acceptConnection(const Timeout& acceptTimeout = defaultTimeout());
91
94 bool tryAcceptConnection(const Timeout& acceptTimeout = defaultTimeout());
95
97 Gateway& acceptSession(SeqNumber outgoingSequenceNumber = 1, UInt64 previousUuid = 0, UInt32 previousSeqNo = 0);
98
105 ClientMessageListener* listener = nullptr, ClientSessionMessageListener* sessionMessageListener = nullptr,
106 std::atomic<bool>* stopRequested = nullptr);
107
108
114 bool dispatch(MessageBasePtr msg, ClientMessageListener* listener = nullptr, ClientSessionMessageListener* sessionMessageListener = nullptr);
115
117 Gateway& sendSequence(UInt32 nextSeqNo, KeepAliveLapsed::Enum keepAliveLapsed = KeepAliveLapsed::NotLapsed, UInt64 uuid = 0);
118
121
123 const char * host() const noexcept;
124
127 int port() const noexcept;
128
130 Gateway& enableLogger(const std::string& logFileName, unsigned int logLevel = 3);
131
134 template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
135 Gateway& send(iLink3::Messaging::MessageHolder<SbeMessageType, MaxMessageSize, MessageInitializer>& msg);
136
139 template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
140 Gateway& send(iLink3::Messaging::MessageHolder<SbeMessageType, MaxMessageSize, MessageInitializer>&& msg);
141
144 template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
145 Gateway& send(iLink3::Messaging::MessageHolder<SbeMessageType, MaxMessageSize, MessageInitializer>& msg, SeqNumber num);
146
149 template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
150 Gateway& send(iLink3::Messaging::MessageHolder<SbeMessageType, MaxMessageSize, MessageInitializer>&& msg, SeqNumber num);
151
154 template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
155 Gateway& send(iLink3::Messaging::MessageHolder<SbeMessageType, MaxMessageSize, MessageInitializer>& msg, std::initializer_list<SeqNumber> numbers);
156
159 template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
160 Gateway& send(iLink3::Messaging::MessageHolder<SbeMessageType, MaxMessageSize, MessageInitializer>&& msg, std::initializer_list<SeqNumber> numbers);
161
162
166 template <typename TMsg>
168
174
180 Gateway& run(ClientMessageListener& appMsgListener, ClientSessionMessageListener& sessionMsgListener);
181
184 SeqNumber outSeqNum() const noexcept;
185
187 Gateway& outSeqNum(SeqNumber nextOutSeqNum) noexcept;
188
191 UInt64 uuid() const noexcept
192 {
193 return uId_;
194 }
195
199 {
200 return faultToleranceIndicator_;
201 }
202
205 {
206 faultToleranceIndicator_ = value;
207
208 return *this;
209 }
210
212 Gateway& tcpNoDelayOption(bool noDelay);
213
218 bool tcpNoDelayOption() const noexcept ;
219
224 int socketReceiveBufferSize() const noexcept;
225
231
236 int socketSendBufferSize() const noexcept;
237
243
244protected:
247 bool isConnectionClosed(const Timeout& timeout) const;
248
252 bool isConnectionClosed() const;
253
256
259 MessageBasePtr tryReceive(const std::chrono::microseconds& timeout = defaultTimeout()) const;
260
265
270
275 const std::string & reason = std::string());
276
281
285 Establish503Ptr acceptEstablish(UInt32 outgoingSequenceNumber = 1, UInt64 previousUuid = 0, UInt32 previousSeqNo = 0);
286
290 Establish503Ptr rejectEstablish(UInt32 outgoingSequenceNumber = 1, EstablishmentRejectErrorCode::Enum errorCodes = EstablishmentRejectErrorCode::Enum(), const std::string & reason = std::string());
291
296
301
303 Gateway& sendTerminate(const std::string& reason = {}, UInt16 errCode = TerminateErrorCode::Other);
304
306 Terminate507Ptr terminate(const std::string& reason = {}, UInt16 errCode = TerminateErrorCode::Other);
307
310
313 const std::string & reason);
314
317
320 EstablishmentRejectErrorCode::Enum errorCodes, const std::string & reason);
321
324
326 Terminate507Container createTerminate(const std::string& reason, UInt16 errCode);
327
330
333
335 Gateway& sendData(const void* data, size_t size);
336
339
342 {
343 return Timeout(30000);
344 }
345
348
349private:
350 Messaging::FTI::Enum faultToleranceIndicator_{Messaging::FTI::Enum::Primary};
351
352 class Impl;
353 Impl* impl_;
354};
355
356
357template <typename TMsg>
362
363template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
365{
366 const auto messageSize = msg.setHeader();
367 auto* const header = msg.header();
368 assert(header);
369
370 return send(Messaging::SbeMessage(header + 1, messageSize), *header);
371}
372
373template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
378
379template <typename Holder>
380typename std::enable_if<std::is_base_of<TagBased::Message, typename Holder::MessageType>::value>::type
381 setSeqNum(Holder& msg, SeqNumber num)
382{
383 msg->set(TagBased::Tags::SeqNum, num);
384}
385
386template <typename Holder>
387typename std::enable_if<!std::is_base_of<TagBased::Message, typename Holder::MessageType>::value>::type
388 setSeqNum(Holder& msg, SeqNumber num)
389{
390 msg->setSeqNum(num);
391}
392
393template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
399
400template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
406
407template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
410{
411 for(auto seqNum : numbers)
412 {
413 send(msg, seqNum);
414 }
415
416 return *this;
417}
418
419template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
422{
423 for(auto seqNum : numbers)
424 {
425 send(msg, seqNum);
426 }
427
428 return *this;
429}
430
431
432}}}}
#define ONIXS_ILINK3_NODISCARD
Definition Compiler.h:147
#define ONIXS_ILINK3_EXPORTED
Definition Compiler.h:145
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 & send(iLink3::Messaging::MessageHolder< SbeMessageType, MaxMessageSize, MessageInitializer > &msg)
Sends the given message.
Definition Gateway.h:364
Terminate507Ptr receiveTerminate()
Receive the Terminate507 message.
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.
Negotiate500Ptr acceptNegotiate(UInt32 previousSeqNo=0)
Receives the Negotiate500 and responds with the NegotiationResponse501 message.
MessageBasePtr receive() const
Receives a message.
Gateway(const Gateway &)=delete
Deleted.
Establish503Ptr receiveEstablish()
Receives the Establish503 message.
Sequence506Container createSequence(UInt32 nextSeqNo, KeepAliveLapsed::Enum keepAliveLapsed=KeepAliveLapsed::NotLapsed, UInt64 uuid=0)
Creates a Sequence506 message.
std::chrono::milliseconds Timeout
Timeout alias.
Definition Gateway.h:68
NegotiationReject502Container createNegotiationReject(const Negotiate500Ptr &request, NegotiationRejectErrorCode::Enum errorCodes, const std::string &reason)
Creates a NegotiationReject502 message.
int socketReceiveBufferSize() const noexcept
Establish503Ptr rejectEstablish(UInt32 outgoingSequenceNumber=1, EstablishmentRejectErrorCode::Enum errorCodes=EstablishmentRejectErrorCode::Enum(), const std::string &reason=std::string())
Receives the Establish503 message and responds with the EstablishmentReject505 message.
Gateway & send(Messaging::SbeMessage, Messaging::SimpleOpenFramingHeader &)
Sends the given message.
FTI::Enum faultToleranceIndicator() const noexcept
Definition Gateway.h:198
EstablishmentReject505Container createEstablishmentReject(const Establish503Ptr &request, UInt32 outgoingSequenceNumber, EstablishmentRejectErrorCode::Enum errorCodes, const std::string &reason)
Creates a EstablishmentReject505 message.
Gateway & acceptSession(SeqNumber outgoingSequenceNumber=1, UInt64 previousUuid=0, UInt32 previousSeqNo=0)
Accepts an incoming iLink 3 session.
NegotiationResponse501Container createNegotiationResponse(const Negotiate500Ptr &request, UInt32 previousSeqNo)
Creates a NegotiationResponse501 message.
Gateway & sendData(const void *data, size_t size)
Sends the given data.
SeqNumber outSeqNum() const noexcept
Terminate507Ptr terminate(const std::string &reason={}, UInt16 errCode=TerminateErrorCode::Other)
Sends the Terminate507 message and waits until the response is received.
Gateway & operator=(const Gateway &)=delete
MessageBasePtr tryReceive(const std::chrono::microseconds &timeout=defaultTimeout()) const
Receives a message using the provided timeout.
Gateway & sendTerminate(const std::string &reason={}, UInt16 errCode=TerminateErrorCode::Other)
Sends the Terminate507 message.
Gateway & enableLogger(const std::string &logFileName, unsigned int logLevel=3)
Enables logging.
Negotiate500Ptr receiveNegotiate()
Receives the Negotiate500 message.
Gateway & close()
Close the listening socket.
Gateway & disconnect()
Closes the accepted connection.
bool dispatch(MessageBasePtr msg, ClientMessageListener *listener=nullptr, ClientSessionMessageListener *sessionMessageListener=nullptr)
Dispatches the provided message for the established session.
Gateway(Gateway &&) noexcept
Move semantic.
Gateway & faultToleranceIndicator(FTI::Enum value)
Sets the Fault Tolerance Indicator.
Definition Gateway.h:204
bool tcpNoDelayOption() const noexcept
Negotiate500Ptr rejectNegotiate(NegotiationRejectErrorCode::Enum errorCodes=NegotiationRejectErrorCode::Enum(), const std::string &reason=std::string())
Receives the Negotiate500 message and responds with the NegotiationReject502 message.
Establish503Ptr acceptEstablish(UInt32 outgoingSequenceNumber=1, UInt64 previousUuid=0, UInt32 previousSeqNo=0)
Receive the Establish503 message and respond with the EstablishmentAck504 message.
Terminate507Container createTerminate(const Terminate507Ptr &request)
Creates a Terminate507 message.
EstablishmentAck504Container createEstablishmentAck(const Establish503Ptr &request, UInt32 outgoingSequenceNumber)
Creates a EstablishmentAck504 message.
Terminate507Container createTerminate(const std::string &reason, UInt16 errCode)
Creates a Terminate507 message.
Gateway & sendSequence(UInt32 nextSeqNo, KeepAliveLapsed::Enum keepAliveLapsed=KeepAliveLapsed::NotLapsed, UInt64 uuid=0)
Sends the Sequence506 message.
UInt64 uuid() const noexcept
Definition Gateway.h:191
Gateway & run(ClientMessageListener &listener)
Accepts an incoming connection and establishes the session.
static Timeout defaultTimeout()
Default timeout.
Definition Gateway.h:341
Terminate507Ptr waitUntilTerminate(ClientMessageListener *listener=nullptr, ClientSessionMessageListener *sessionMessageListener=nullptr, std::atomic< bool > *stopRequested=nullptr)
Waits until the Terminate507 message is received.
const char * host() const noexcept
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:358
void swap(Gateway &) noexcept
Swap with other.
Gateway & tcpNoDelayOption(bool noDelay)
Sets the value of the option to improve latency at the expense of message throughput (the TCP_NODELAY...
Terminate507Ptr acceptTerminate()
Receive the Terminate507 message and responds with the Terminate507 message.
std::enable_if< std::is_base_of< TagBased::Message, typenameHolder::MessageType >::value >::type setSeqNum(Holder &msg, SeqNumber num)
Definition Gateway.h:381
MessageHolder< Sequence506 > Sequence506Container
Definition Gateway.h:55
MessageHolder< Terminate507 > Terminate507Container
Definition Gateway.h:54
MessagePtr< Negotiate500 > Negotiate500Ptr
Definition Gateway.h:58
MessagePtr< Sequence506 > Sequence506Ptr
Definition Gateway.h:61
MessageHolder< NegotiationResponse501, calculateNegotiationResponse501MaxSize()> NegotiationResponse501Container
Definition Gateway.h:50
MessageHolder< EstablishmentAck504 > EstablishmentAck504Container
Definition Gateway.h:52
MessagePtr< Terminate507 > Terminate507Ptr
Definition Gateway.h:60
MessageHolder< EstablishmentReject505 > EstablishmentReject505Container
Definition Gateway.h:53
MessagePtr< Establish503 > Establish503Ptr
Definition Gateway.h:59
MessageHolder< NegotiationReject502 > NegotiationReject502Container
Definition Gateway.h:51
constexpr UInt16 calculateNegotiationResponse501MaxSize()
Definition Gateway.h:39
MessagePtr< AnotherType > cast(const MessagePtr< BaseType > &ptr)
Tries to cast to another type.
Definition MessagePtr.h:273
MessagePtr< SbeMessage > MessageBasePtr
Definition Gateway.h:57
Messaging::UInt32 SeqNumber
Definition Messaging.h:60
The Establishment Reject message error codes.
The Negotiation Reject message error codes.
@ Other
any other error condition not mentioned above