OnixS C++ CBOE CFE Binary Order Entry (BOE) Handler  1.11.0
API documentation
Gateway.h
Go to the documentation of this file.
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  */
19 #pragma once
20 
21 #include <OnixS/CboeCFE/Trading/BOE/Compiler.h>
22 
23 #if ONIXS_BATS_BOE_HAS_GATEWAY_EMULATOR
24 
27 
28 #include <chrono>
29 
30 namespace OnixS {
31 namespace CboeCFE {
32 namespace Trading {
33 namespace BOE {
34 namespace Testing {
35 
36 
37 class ONIXS_CBOE_CFE_BOE_API Gateway
38 {
39 public:
40  explicit Gateway(int port, const char* host = "127.0.0.1", const std::chrono::seconds& acceptTimeout = std::chrono::seconds(10),
41  const std::chrono::seconds& sendReceiveTimeout = std::chrono::seconds(10));
42 
43  ///
44  virtual ~Gateway();
45 
46  /// Deleted.
47  Gateway(const Gateway&) = delete;
48  Gateway& operator=(const Gateway&) = delete;
49 
50  /// Move semantic.
51  Gateway(Gateway&&) noexcept;
52  Gateway& operator=(Gateway&&) noexcept;
53 
54  /// Swap with other.
55  void swap(Gateway&) noexcept;
56 
57  /// @return listen host.
58  const char * host() const noexcept;
59 
60  /// \return the listen port.
61  int port() const noexcept;
62 
63  /// Accepts an incoming connection.
64  /// If the `acceptTimeout` is zero, the `acceptTimeout` provided in the constructor is used.
65  Gateway& acceptConnection(const std::chrono::seconds& acceptTimeout = std::chrono::seconds(0));
66 
67  /// Sends the given application-level message message.
68  Gateway& sendApplicationMessage(const OutgoingMessage& msg, SeqNumber seqNumber = SeqNumberTraits::InvalidValue);
69 
70  /// Receives the message of the particular type.
71  /// \throw std::logic_error if an unexpected type is received.
72  template <BOE::MessageType::Enum Type>
73  MessagePtr<typename MessageTypeTraits<Type>::MessageType> receiveTypedMessage() const;
74 
75  /// Send the ServerHeartbeat message
76  void sendHeartbeat();
77 
78  /// Waits until the LogoutRequested message is received.
79  ///
80  /// If the listener is provided, then receives and reports application-level messages via the corresponding callback.
81  /// Responds to the ClientHeartbeat message if received.
82  LogoutRequestPtr waitUntilLogoutRequested(
83  ClientMessageListener* listener = nullptr, ClientSessionMessageListener* sessionMessageListener = nullptr);
84 
85  /// Accepts an incoming connection and establishes the session.
86  ///
87  /// Receives and reports application-level messages via the corresponding callback.
88  /// Responds to the ClientHeartbeat message if received.
89  Gateway& run(ClientMessageListener& listener);
90 
91  /// Accepts an incoming connection and establishes the session.
92  ///
93  /// Receives and reports application-level messages via the corresponding callback.
94  /// Receives and reports session-level messages via the corresponding callback.
95  Gateway& run(ClientMessageListener& appMsgListener, ClientSessionMessageListener& sessionMsgListener);
96 
97 protected:
98  /// Checks whether the connection is closed by the counterparty.
99  bool isConnectionClosed(const std::chrono::seconds& timeout) const;
100 
101  /// Checks whether the connection is closed by the counterparty
102  /// using the default timeout (provided in the constructor).
103  bool isConnectionClosed() const;
104 
105  /// Close the listening socket
106  Gateway& close();
107 
108  /// Closes the accepted connection.
109  Gateway& disconnect();
110 
111  /// Receives a message.
112  MessageBasePtr receive() const;
113 
114  /// Receives a message into provided container
115  MessageBasePtr receive(PacketContainer && container, MessageSize containerSize);
116 
117  /// Sends the given data
118  Gateway& sendData(const void* data, size_t size);
119 
120  /// Sends the given session-level message message.
121  Gateway& sendSessionMessage(const OutgoingMessage& msg);
122 
123  /// Receives the LoginRequest message.
124  ///
125  /// \throw std::logic_error if an unexpected type is received.
126  LoginRequestPtr receiveLoginRequest();
127 
128  /// Receives the LogoutRequest message.
129  ///
130  /// \throw std::logic_error if an unexpected type is received.
131  LogoutRequestPtr receiveLogoutRequest();
132 
133  /// Receives the ClientHeartbeat message.
134  ///
135  /// \throw std::logic_error if an unexpected type is received.
136  ClientHeartbeatPtr receiveClientHeartbeat();
137 
138  /// Receives the LoginRequest and responds with the LoginResponse message.
139  ///
140  /// \throw std::logic_error if an unexpected type is received.
141  LoginRequestPtr acceptLogin(SeqNumber clientSequence = 1);
142 
143  /// Receives the LoginRequest and responds with the LoginResponse message.
144  ///
145  /// \throw std::logic_error if an unexpected type is received.
146  LoginRequestPtr rejectLogin();
147 
148  /// Receives the LogoutRequest and responds with the LogoutResponse message.
149  ///
150  /// \throw std::logic_error if an unexpected type is received.
151  LogoutRequestPtr acceptLogout();
152 
153  /// Accepts the incoming connection, Receives the LoginRequest and responds with the LoginResponse message.
154  ///
155  /// \throw std::logic_error if an unexpected type is received.
156  LoginRequestPtr acceptSession(SeqNumber clientSequence = 1);
157 
158  /// Send the LogoutResponse message
159  void sendLogout(LogoutResponseReason::Enum reason);
160 
161  /// Send the ReplayComplete message
162  void sendReplayComplete();
163 
164 private:
165  class Impl;
166  Impl* impl_;
167 };
168 
169 template <BOE::MessageType::Enum Type>
170 MessagePtr<typename MessageTypeTraits<Type>::MessageType> Gateway::receiveTypedMessage() const
171 {
172  return cast<Type>(receive());
173 }
174 
175 
176 }}}}}
177 
178 #endif
STL namespace.
Binary2 MessageSize
Aliases message length type.