OnixS C++ CME iLink 3 Binary Order Entry Handler 1.18.9
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
25#if defined(ONIXS_ILINK3_HAS_GATEWAY_EMULATOR)
26
31
32#include <chrono>
33#include <atomic>
34
35namespace OnixS {
36namespace CME {
37namespace iLink3 {
38namespace Testing {
39
40// Assume NegotiationResponse501::credentials is empty
41constexpr
43{
44 using Message = NegotiationResponse501;
45
46 return
47 Message::blockLength(Message::Schema::Version)
49 + Message::getMinimalVariableFieldsSize(Message::Schema::Version);
50}
51
52// Type aliases
59
65
68{
69public:
71 using Timeout = std::chrono::milliseconds;
72
73 Gateway(const SessionSettings::LicenseStores& licenseStores, int port, const char* host = "127.0.0.1",
74 const Timeout& acceptTimeout = defaultTimeout(),
75 const Timeout& sendReceiveTimeout = defaultTimeout());
76
78 virtual ~Gateway();
79
81 Gateway(const Gateway&) = delete;
82 Gateway& operator=(const Gateway&) = delete;
83
85 Gateway(Gateway&&) noexcept;
86 Gateway& operator=(Gateway&&) noexcept;
87
89 void swap(Gateway&) noexcept;
90
93 Gateway& acceptConnection(const Timeout& acceptTimeout = defaultTimeout());
94
97 bool tryAcceptConnection(const Timeout& acceptTimeout = defaultTimeout());
98
100 Gateway& acceptSession(SeqNumber outgoingSequenceNumber = 1, UInt64 previousUuid = 0, UInt32 previousSeqNo = 0);
101
108 ClientMessageListener* listener = nullptr, ClientSessionMessageListener* sessionMessageListener = nullptr,
109 std::atomic<bool>* stopRequested = nullptr);
110
112 Gateway& sendSequence(UInt32 nextSeqNo, KeepAliveLapsed::Enum keepAliveLapsed = KeepAliveLapsed::NotLapsed, UInt64 uuid = 0);
113
116
118 const char * host() const noexcept;
119
122 int port() const noexcept;
123
125 Gateway& enableLogger(const std::string& logFileName, unsigned int logLevel = 3);
126
129 template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
130 Gateway& send(iLink3::Messaging::MessageHolder<SbeMessageType, MaxMessageSize, MessageInitializer>& msg);
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, SeqNumber num);
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, std::initializer_list<SeqNumber> numbers);
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
157
161 template <typename TMsg>
163
169
175 Gateway& run(ClientMessageListener& appMsgListener, ClientSessionMessageListener& sessionMsgListener);
176
179 SeqNumber outSeqNum() const noexcept;
180
182 Gateway& outSeqNum(SeqNumber nextOutSeqNum) noexcept;
183
186 UInt64 uuid() const noexcept
187 {
188 return uId_;
189 }
190
194 {
195 return faultToleranceIndicator_;
196 }
197
200 {
201 faultToleranceIndicator_ = value;
202
203 return *this;
204 }
205
207 Gateway& tcpNoDelayOption(bool noDelay);
208
213 bool tcpNoDelayOption() const noexcept ;
214
220
226
231 int socketSendBufferSize() const noexcept;
232
238
239protected:
242 bool isConnectionClosed(const Timeout& timeout) const;
243
247 bool isConnectionClosed() const;
248
251
255
260
265
270 const std::string & reason = std::string());
271
276
280 Establish503Ptr acceptEstablish(UInt32 outgoingSequenceNumber = 1, UInt64 previousUuid = 0, UInt32 previousSeqNo = 0);
281
285 Establish503Ptr rejectEstablish(UInt32 outgoingSequenceNumber = 1, EstablishmentRejectErrorCode::Enum errorCodes = EstablishmentRejectErrorCode::Enum(), const std::string & reason = std::string());
286
291
296
298 Gateway& sendTerminate(const std::string& reason = {}, UInt16 errCode = TerminateErrorCode::Other);
299
301 Terminate507Ptr terminate(const std::string& reason = {}, UInt16 errCode = TerminateErrorCode::Other);
302
305
308 const std::string & reason);
309
312
315 EstablishmentRejectErrorCode::Enum errorCodes, const std::string & reason);
316
319
321 Terminate507Container createTerminate(const std::string& reason, UInt16 errCode);
322
325
328
330 Gateway& sendData(const void* data, size_t size);
331
334
337 {
338 return Timeout(30000);
339 }
340
343
344private:
345 Messaging::FTI::Enum faultToleranceIndicator_{Messaging::FTI::Enum::Primary};
346
347 class Impl;
348 Impl* impl_;
349};
350
351
352template <typename TMsg>
357
358template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
360{
361 const auto messageSize = msg.setHeader();
362 auto* const header = msg.header();
363 assert(header);
364
365 return send(Messaging::SbeMessage(header + 1, messageSize), *header);
366}
367
368template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
373
374template <typename Holder>
375typename std::enable_if<std::is_base_of<TagBased::Message, typename Holder::MessageType>::value>::type
376 setSeqNum(Holder& msg, SeqNumber num)
377{
378 msg->set(TagBased::Tags::SeqNum, num);
379}
380
381template <typename Holder>
382typename std::enable_if<!std::is_base_of<TagBased::Message, typename Holder::MessageType>::value>::type
383 setSeqNum(Holder& msg, SeqNumber num)
384{
385 msg->setSeqNum(num);
386}
387
388template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
394
395template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
401
402template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
405{
406 for(auto seqNum : numbers)
407 {
408 send(msg, seqNum);
409 }
410
411 return *this;
412}
413
414template <typename SbeMessageType, size_t MaxMessageSize, typename MessageInitializer>
417{
418 for(auto seqNum : numbers)
419 {
420 send(msg, seqNum);
421 }
422
423 return *this;
424}
425
426
427}}}}
428
429
430#endif
#define ONIXS_ILINK3_NODISCARD
Definition Compiler.h:185
#define ONIXS_ILINK3_EXPORTED
Definition Compiler.h:175
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:359
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:71
NegotiationReject502Container createNegotiationReject(const Negotiate500Ptr &request, NegotiationRejectErrorCode::Enum errorCodes, const std::string &reason)
Creates a NegotiationReject502 message.
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:193
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
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.
Gateway(Gateway &&) noexcept
Move semantic.
Gateway & faultToleranceIndicator(FTI::Enum value)
Sets the Fault Tolerance Indicator.
Definition Gateway.h:199
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:186
Gateway & run(ClientMessageListener &listener)
Accepts an incoming connection and establishes the session.
static Timeout defaultTimeout()
Default timeout.
Definition Gateway.h:336
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:353
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...
MessageBasePtr tryReceive(const Timeout &timeout=defaultTimeout()) const
Receives a message using the provided timeout.
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:376
MessageHolder< Sequence506 > Sequence506Container
Definition Gateway.h:58
MessageHolder< Terminate507 > Terminate507Container
Definition Gateway.h:57
MessagePtr< Negotiate500 > Negotiate500Ptr
Definition Gateway.h:61
MessagePtr< Sequence506 > Sequence506Ptr
Definition Gateway.h:64
MessageHolder< NegotiationResponse501, calculateNegotiationResponse501MaxSize()> NegotiationResponse501Container
Definition Gateway.h:53
MessageHolder< EstablishmentAck504 > EstablishmentAck504Container
Definition Gateway.h:55
MessagePtr< Terminate507 > Terminate507Ptr
Definition Gateway.h:63
MessageHolder< EstablishmentReject505 > EstablishmentReject505Container
Definition Gateway.h:56
MessagePtr< Establish503 > Establish503Ptr
Definition Gateway.h:62
MessageHolder< NegotiationReject502 > NegotiationReject502Container
Definition Gateway.h:54
constexpr UInt16 calculateNegotiationResponse501MaxSize()
Definition Gateway.h:42
MessagePtr< AnotherType > cast(const MessagePtr< BaseType > &ptr)
Tries to cast to another type.
Definition MessagePtr.h:276
MessagePtr< SbeMessage > MessageBasePtr
Definition Gateway.h:60
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