OnixS C++ FIX Engine  4.10.1
API Documentation
Session.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 
20 #pragma once
21 
22 #include <string>
23 #include <set>
24 #include <vector>
25 #include <utility>
26 #include <iosfwd>
27 #include <limits>
28 
32 
33 
35 
44 
46 
48 
51 
52 
53 namespace OnixS {
54 namespace FIX {
55 ONIXS_FIXENGINE_API_DECL(class, Message);
56 ONIXS_FIXENGINE_API_DECL(class, Dictionary);
57 ONIXS_FIXENGINE_API_DECL(class, Engine);
58 ONIXS_FIXENGINE_API_DECL(class, ISessionListener);
59 ONIXS_FIXENGINE_API_DECL(class, ISessionStorage);
60 
61 /// The session message mode.
62 struct MessageMode {
63  enum Enum {
64  /// The default mode. The `Message` class is used in inbound callbacks.
65  Message = 1,
66 
67  /// The `FlatMessage` class is used in inbound callbacks.
69  };
70 };
71 
72 namespace Core {
73 namespace Messaging {
74 class Message;
75 }
76 }
77 
78 template<typename MsgType>
80 {
82  typedef std::vector<FlatMessage*> MsgBatchType;
83  typedef std::vector<OnixS::FIX::Core::Messaging::Extras::FlatMessage*> CoreMsgBatchType;
84 };
85 
86 template<>
88 {
90  typedef std::vector<Message*> MsgBatchType;
91  typedef std::vector<OnixS::FIX::Core::Messaging::Message*> CoreMsgBatchType;
92 };
93 
94 /**
95 * The FIX Session - a bi-directional stream of ordered messages between two parties within a continuous sequence number series.
96 */
98 {
99 public:
100  /**
101  * Creates the FIX Session.
102  *
103  * @param senderCompId The assigned value used to identify the firm sending message (SenderCompID (tag=49) field value in outgoing messages).
104  * @param targetCompId The assigned value used to identify the receiving firm (TargetCompID (tag=56) field value in outgoing messages).
105  * @param dictionary The FIX dictionary to be used by the session.
106  * @param listener An instance of the ISessionListener interface, which will receive and handle miscellaneous Session events.
107  * @param storageType The session storage type.
108  * @param storage The pluggable session storage.
109  */
110  Session(
111  const std::string & senderCompId,
112  const std::string & targetCompId,
113  const Dictionary & dictionary,
114  ISessionListener * listener,
116  ISessionStorage * storage = ONIXS_FIXENGINE_NULLPTR);
117 
118  /**
119  * Creates the FIX Session.
120  *
121  * @param reactor An instance of an externally managed reactor.
122  * @param senderCompId The assigned value used to identify the firm sending message (SenderCompID (tag=49) field value in outgoing messages).
123  * @param targetCompId The assigned value used to identify the receiving firm (TargetCompID (tag=56) field value in outgoing messages).
124  * @param dictionary The FIX dictionary to be used by the session.
125  * @param listener An instance of the ISessionListener interface, which will receive and handle miscellaneous Session events.
126  * @param storageType The session storage type.
127  * @param storage The pluggable session storage.
128  */
129  Session(
130  ISessionReactor * reactor,
131  const std::string & senderCompId,
132  const std::string & targetCompId,
133  const Dictionary & dictionary,
134  ISessionListener * listener,
136  ISessionStorage * storage = ONIXS_FIXENGINE_NULLPTR);
137 
138  /**
139  * Creates the FIX Session.
140  *
141  * @param senderCompId The assigned value used to identify the firm sending message (SenderCompID (tag=49) field value in outgoing messages).
142  * @param targetCompId The assigned value used to identify the receiving firm (TargetCompID (tag=56) field value in outgoing messages).
143  * @param dictionary The FIX dictionary to be used by the session.
144  * @param keepSequenceNumbersAfterLogout The option to keep sequence numbers after the exchange of Logout (MsgType=5) messages.
145  * @param listener An instance of the ISessionListener interface which will receive and handle miscellaneous Session events.
146  * @param storageType The session storage type.
147  * @param storage The pluggable session storage.
148  */
149  Session(
150  const std::string & senderCompId,
151  const std::string & targetCompId,
152  const Dictionary & dictionary,
153  bool keepSequenceNumbersAfterLogout,
154  OnixS::FIX::ISessionListener * listener,
156  ISessionStorage * storage = ONIXS_FIXENGINE_NULLPTR);
157 
158 
159  /**
160  * Creates the FIX Session.
161  *
162  * @param reactor An instance of an externally managed reactor.
163  * @param senderCompId The assigned value used to identify the firm sending message (SenderCompID (tag=49) field value in outgoing messages).
164  * @param targetCompId The assigned value used to identify the receiving firm (TargetCompID (tag=56) field value in outgoing messages).
165  * @param dictionary The FIX dictionary to be used by the session.
166  * @param keepSequenceNumbersAfterLogout The option to keep sequence numbers after the exchange of Logout (MsgType=5) messages.
167  * @param listener An instance of the ISessionListener interface which will receive and handle miscellaneous Session events.
168  * @param storageType The session storage type.
169  * @param storage The pluggable session storage.
170  */
171  Session(
172  ISessionReactor * reactor,
173  const std::string & senderCompId,
174  const std::string & targetCompId,
175  const Dictionary & dictionary,
176  bool keepSequenceNumbersAfterLogout,
177  OnixS::FIX::ISessionListener * listener,
179  ISessionStorage * storage = ONIXS_FIXENGINE_NULLPTR);
180 
181  /**
182  * Creates the FIX Session.
183  *
184  * @param senderCompId The assigned value used to identify the firm sending message (SenderCompID (tag=49) field value in outgoing messages).
185  * @param targetCompId The assigned value used to identify the receiving firm (TargetCompID (tag=56) field value in outgoing messages).
186  * @param dictionary The FIX dictionary to be used by the session.
187  * @param keepSequenceNumbersAfterLogout The option to keep sequence numbers after the exchange of Logout (MsgType=5) messages.
188  * @param listener An instance of the ISessionListener interface which will receive and handle miscellaneous Session events.
189  * @param customSessionKey The custom key that can be used to distinguish sessions with the same values of the SenderCompID, TargetCompID and the FIX Protocol version.
190  * @param storageType The session storage type.
191  * @param storage The pluggable session storage.
192  */
193  Session(
194  const std::string & senderCompId,
195  const std::string & targetCompId,
196  const Dictionary & dictionary,
197  bool keepSequenceNumbersAfterLogout,
198  OnixS::FIX::ISessionListener * listener,
199  const std::string & customSessionKey,
201  ISessionStorage * storage = ONIXS_FIXENGINE_NULLPTR);
202 
203  /**
204  * Creates the FIX Session.
205  *
206  * @param reactor An instance of an externally managed reactor.
207  * @param senderCompId The assigned value used to identify the firm sending message (SenderCompID (tag=49) field value in outgoing messages).
208  * @param targetCompId The assigned value used to identify the receiving firm (TargetCompID (tag=56) field value in outgoing messages).
209  * @param dictionary The FIX dictionary to be used by the session.
210  * @param keepSequenceNumbersAfterLogout The option to keep sequence numbers after the exchange of Logout (MsgType=5) messages.
211  * @param listener An instance of the ISessionListener interface which will receive and handle miscellaneous Session events.
212  * @param customSessionKey The custom key that can be used to distinguish sessions with the same values of the SenderCompID, TargetCompID and the FIX Protocol version.
213  * @param storageType The session storage type.
214  * @param storage The pluggable Session storage.
215  */
216  Session(
217  ISessionReactor * reactor,
218  const std::string & senderCompId,
219  const std::string & targetCompId,
220  const Dictionary & dictionary,
221  bool keepSequenceNumbersAfterLogout,
222  OnixS::FIX::ISessionListener * listener,
223  const std::string & customSessionKey,
225  ISessionStorage * storage = ONIXS_FIXENGINE_NULLPTR);
226 
227  /**
228  * The destructor.
229  */
230  ~Session();
231 
232  /**
233  * Removes all messages from the outbound queue.
234  *
235  * @param removeFragmentedPacket The flag specifies whether to remove the last packet which is partially sent.
236  *
237  * @note This call is thread-safe.
238  */
239  Session & clearOutboundQueue(bool removeFragmentedPacket = false);
240 
241  /**
242  * Returns the Session's state.
243  *
244  * @note This call is thread-safe.
245  */
246  SessionState::Enum state() const;
247 
248  /**
249  * Returns the current encryption method.
250  *
251  * @note This call is thread-safe.
252  */
253  EncryptionMethod::Enum encryptionMethod() const;
254 
255  /**
256  * Sets the encryption method.
257  *
258  * @note This call is thread-safe.
259  */
260  Session & encryptionMethod(EncryptionMethod::Enum newEncryptionMethod);
261 
262  /**
263  * Returns the port to listen on for incoming FIX Connections in the Acceptor mode.
264  *
265  * @note This call is thread-safe.
266  */
267  int listenPort() const;
268 
269  /**
270  * Sets the port to listen on for incoming FIX Connections in the Acceptor mode.
271  *
272  * @note This call is thread-safe.
273  */
274  Session & listenPort(int listenPort);
275 
276  /**
277  * Returns the local network interface from which you intend to send and receive data.
278  *
279  * @note This call is thread-safe.
280  */
281  std::string localNetworkInterface() const;
282 
283  /**
284  * Sets the local network interface from which you intend to send and receive data.
285  *
286  * @note This call is thread-safe.
287  */
288  Session & localNetworkInterface(const std::string & localNetworkInterface);
289 
290  /**
291  * Returns the local port from which you intend to send and receive data.
292  *
293  * @note This call is thread-safe.
294  */
295  unsigned short localPort() const;
296  /**
297  * The local port range type.
298  */
299  typedef std::pair<unsigned short, unsigned short> LocalPortRange;
300 
301  /**
302  * Returns the local port range, the first available port from this range will be used to send and receive data.
303  *
304  * @note This call is thread-safe.
305  */
306  LocalPortRange localPortRange() const;
307 
308  /**
309  * Sets the local port range, the first available port from this range will be used to send and receive data.
310  *
311  * @note This call is thread-safe.
312  */
313  Session & localPortRange(LocalPortRange portRange);
314 
315  /**
316  * Returns the option to improve the latency at the expense of the message throughput (TCP_NODELAY socket option).
317  * When it is not set, the corresponding Engine setting's value is used ('true' by default).
318  */
319  bool tcpNoDelayOption() const;
320 
321  /**
322  * Sets the value of the option to improve the latency at the expense of the message throughput (TCP_NODELAY socket option).
323  */
324  Session & tcpNoDelayOption(bool improveLatency = true);
325 
326  /**
327  * Returns the option that controls whether to send the logout message before
328  * dropping the telecommunication link, in case of an exception.
329  * When it is not set, the corresponding Engine setting's value is used ('false' by default).
330  */
331  bool sendLogoutOnException() const;
332 
333  /**
334  * Sets the value of the option that controls whether to send the logout message before
335  * dropping the telecommunication link, in case of an exception.
336  */
337  Session & sendLogoutOnException(bool sendLogoutOnException);
338 
339  /**
340  * Returns the option to validate the presence of unknown FIX messages.
341  * When it is not set, the corresponding Engine setting's value is used ('false' by default).
342  */
343  bool validateUnknownMessages() const;
344 
345  /**
346  * Sets the option to validate the presence of unknown FIX messages.
347  */
348  Session & validateUnknownMessages(bool);
349 
350  /**
351  * Returns the option to validate the presence of unknown fields.
352  * When it is not set, the corresponding Engine setting's value is used ('false' by default).
353  */
354  bool validateUnknownFields() const;
355 
356  /**
357  * Sets the option to validate the presence of unknown fields.
358  */
359  Session & validateUnknownFields(bool);
360 
361  /**
362  * Returns the option to validate the presence of required fields in inbound and outbound messages.
363  * When it is not set, the corresponding Engine setting's value is used ('false' by default).
364  */
365  bool validateRequiredFields() const;
366 
367  /**
368  * Sets the option to validate the presence of required fields in inbound and outbound messages.
369  */
370  Session & validateRequiredFields(bool);
371 
372  /**
373  * Returns the option to validate the field values of FIX messages in accordance with the FIX protocol or its FIX Dictionary.
374  * When it is not set, the corresponding Engine setting's value is used ('false' by default).
375  */
376  bool validateFieldValues() const;
377 
378  /**
379  * Sets the option to validate the field values of FIX messages in accordance with the FIX protocol or its FIX Dictionary.
380  */
381  Session & validateFieldValues(bool);
382 
383  /**
384  * Returns the option to validate the empty field values of FIX messages in accordance with the FIX protocol or its FIX Dictionary.
385  * When it is not set, the corresponding Engine setting's value is used ('false' by default).
386  */
387  bool validateEmptyFieldValues() const;
388 
389  /**
390  * Sets the option to validate the empty field values of FIX messages in accordance with the FIX protocol or its FIX Dictionary.
391  */
392  Session & validateEmptyFieldValues(bool);
393 
394  /**
395  * Returns the option that controls the repeating group entry count validation.
396  * When it is not set, the corresponding Engine setting's value is used ('true' by default).
397  */
398  bool validateRepeatingGroupEntryCount() const;
399 
400  /**
401  * Sets the value of the option that controls the repeating group entry count validation.
402  */
403  Session & validateRepeatingGroupEntryCount(bool);
404 
405  /**
406  * Returns the option that controls the repeating group leading tag validation.
407  * When it is not set, the corresponding Engine setting's value is used ('false' by default).
408  */
409  bool validateRepeatingGroupLeadingTag() const;
410 
411  /**
412  * Sets the value of the option that controls the repeating group leading tag validation.
413  */
414  Session & validateRepeatingGroupLeadingTag(bool);
415 
416  /**
417  * Returns the option that controls the duplicated field validation.
418  * When it is not set, the corresponding Engine setting's value is used ('false' by default).
419  */
420  bool validateDuplicatedField() const;
421 
422  /**
423  * Sets value of the option that controls the duplicated field validation.
424  */
425  Session & validateDuplicatedField(bool);
426 
427  /**
428  * Returns the option that controls the validation of the checksum of the incoming message.
429  * When it is not set, the corresponding Engine setting's value is used ('false' by default).
430  */
431  bool validateChecksum() const;
432 
433  /**
434  * Sets the validation of the checksum of the incoming message.
435  */
436  Session & validateChecksum(bool);
437 
438  /**
439  * Returns the number of messages that should be written to the outgoing TCP buffer together.
440  * When it is not set, the corresponding Engine setting's value is used (0 by default).
441  */
442  unsigned messageGrouping() const;
443 
444  /**
445  * Sets the number of messages that should be written to the outgoing TCP buffer together.
446  */
447  Session & messageGrouping(unsigned numberOfMessagesToGroup);
448 
449  /**
450  * Returns the connection mode.
451  */
452  ThreadingModel::Enum threadingModel() const;
453 
454  /**
455  * Sets the connection mode.
456  */
457  Session & threadingModel(ThreadingModel::Enum value);
458 
459  /**
460  * Returns whether the Session uses the spin lock.
461  */
462  bool useSpinLock() const;
463 
464  /**
465  * Sets the Session to use the spin lock.
466  */
467  Session & useSpinLock(bool value);
468 
469  /**
470  * Returns the reasonable transmission time as % from the heartbeatIntervalSec value.
471  * When it is not set, the corresponding Engine setting's value is used (20% by default).
472  */
473  int reasonableTransmissionTime() const;
474 
475  /**
476  * Sets the reasonable transmission time as % from the heartbeatIntervalSec value.
477  *
478  * When either end of the connection has not received any data for (heartbeatIntervalSec() * (100 + reasonableTransmissionTime())/100) seconds,
479  * it will transmit a Test Request message.
480  *
481  * If there is still no a Heartbeat message received after (heartbeatIntervalSec * (100 + reasonableTransmissionTime())/100) seconds
482  * then the connection should be considered lost and a corrective action be initiated.
483  */
484  Session & reasonableTransmissionTime(int value);
485 
486  /**
487  * Sets the number of attempts to restore the FIX connection.
488  */
489  Session & reconnectAttempts(int value);
490 
491  /**
492  * Returns the number of attempts to restore the FIX connection.
493  * When it is not set, the corresponding Engine setting's value is used (3 by default).
494  */
495  int reconnectAttempts() const;
496 
497  /**
498  * Sets the time interval between the attempts to restore the FIX connection (in seconds).
499  */
500  Session & reconnectInterval(int value);
501 
502  /**
503  * Returns the time interval between the attempts to restore the FIX connection (in seconds).
504  * When it is not set, the corresponding Engine setting's value is used (180 by default).
505  */
506  int reconnectInterval() const;
507 
508  /**
509  * Sets the non-blocking receive spinning timeout (in microseconds) before the receiving thread enters into the blocking wait mode.
510  *
511  * @note Should not be greater than 1 sec (minimal heartbeatIntervalSec()).
512  */
513  Session & receiveSpinningTimeoutUsec(int usec);
514 
515  /**
516  * Returns the current receive spinning timeout value (in microseconds).
517  * By default, the value is zero and the receive spinning is not used.
518  */
519  int receiveSpinningTimeout() const;
520 
521  /**
522  * Sets the send spinning timeout (in microseconds) of the Session::send(..) method to wait for the socket sending buffer availability
523  * in the spin loop mode before placing the message to the outgoing queue (to be sent later by the sending thread).
524  *
525  * @note Should not be greater than 1 sec (minimal heartbeatIntervalSec()).
526  */
527  Session & sendSpinningTimeoutUsec(int usec);
528 
529  /**
530  * Returns the current send spinning timeout value (in microseconds).
531  * By default, the value is zero and send spinning is not used.
532  */
533  int sendSpinningTimeout() const;
534 
535  /**
536  * Sets the NextExpectedMsgSeqNum field (tag 789) support in Logon messages.
537  */
538  Session & supportNextExpectedMsgSeqNum(bool support);
539 
540  /**
541  * Returns 'true' if the NextExpectedMsgSeqNum field (tag 789) is supported in Logon messages, otherwise - 'false'.
542  * By default, the option value is false.
543  */
544  bool supportNextExpectedMsgSeqNum() const;
545 
546  /**
547  * Returns the SSL certificate file.
548  *
549  * @note This call is thread-safe.
550  */
551  std::string sslCertificateFile() const;
552 
553  /**
554  * Sets the SSL certificate file.
555  *
556  * @note Only the (Privacy Enhanced Mail) Base 64 Encoded (.pem) SSL certificates are supported.
557  *
558  * @note This call is thread-safe.
559  */
560  Session & sslCertificateFile(const std::string & file);
561 
562  /**
563  * Returns the SSL private key file.
564  *
565  * @note This call is thread-safe.
566  */
567  std::string sslPrivateKeyFile() const;
568 
569  /**
570  * Sets the SSL private key file.
571  *
572  * @note Only the (Privacy Enhanced Mail) Base 64 Encoded (.pem) SSL certificates are supported.
573  *
574  * @note This call is thread-safe.
575  */
576  Session & sslPrivateKeyFile(const std::string & file);
577 
578  /**
579  * Returns the SSL private key file password.
580  *
581  * @note This call is thread-safe.
582  */
583  Cryptography::SecureString sslPrivateKeyPassword() const;
584 
585  /**
586  * Sets the SSL private key file password.
587  *
588  * @note This call is thread-safe.
589  */
590  Session & sslPrivateKeyPassword(const Cryptography::SecureString & password);
591 
592  /**
593  * Set the option to request peer certificates and perform the certificate verification.
594  *
595  * @note This call is thread-safe.
596  */
597  Session & sslVerifyPeer(bool verify);
598 
599  /**
600  * Returns the option to request peer certificates and perform the certificate verification.
601  * When it is not set, the corresponding Engine setting's value is used ('false' by default).
602  *
603  * @note This call is thread-safe.
604  */
605  bool sslVerifyPeer() const;
606 
607  /**
608  * Sets the path to the trusted certification authority certificate file in (Privacy Enhanced Mail) Base64 encoded (.pem) format.
609  *
610  * @note This call is thread-safe.
611  */
612  Session & sslCaFile(const std::string & value);
613 
614  /**
615  * Returns the path to the trusted certification authority certificate file in the (Privacy Enhanced Mail) Base64 encoded (.pem) format.
616  *
617  * @note This call is thread-safe.
618  */
619  std::string sslCaFile() const;
620 
621  /**
622  * Establishes the FIX Connection as an Acceptor.
623  *
624  * The Acceptor is the receiving party of the FIX session. It listens for the incoming connection on the pre-defined port.
625  * The acceptor has the responsibility to perform the first level authentication and formally declares
626  * the connection request "accepted" through the transmission of an acknowledgment Logon message.
627  *
628  * @note This call is thread-safe.
629  */
630  Session & logonAsAcceptor();
631 
632  /**
633  * Establishes the FIX Connection as an Initiator using the ResetSeqNumFlag (tag 141) field.
634  *
635  * The Initiator establishes the telecommunications link and initiates the session via the transmission
636  * of the initial Logon message.
637  * The method is blocked until the acknowledgment Logon message is received or the timeout expired.
638  * The timeout value is equal to the triple heartbeat interval.
639  *
640  * @note This call is thread-safe.
641  */
642  Session & logonAsInitiator(const std::string & host, int port, bool setResetSeqNumFlag);
643 
644  /**
645  * Establishes the FIX Connection as an Initiator.
646  *
647  * The Initiator establishes the telecommunications link and initiates the session via the transmission
648  * of the initial Logon message.
649  * The method is blocked until the acknowledgment Logon message is received or the timeout expired.
650  * The timeout value is equal to the triple heartbeat interval.
651  *
652  * @note This call is thread-safe.
653  */
654  Session & logonAsInitiator(const std::string & host, int port);
655 
656  /**
657  * Establishes the FIX Connection as an Initiator using the given Heartbeat interval (seconds).
658  * The method is blocked until the acknowledgment Logon message is received or the timeout expired.
659  * The timeout value is equal to the triple heartbeat interval.
660  *
661  * @note This call is thread-safe.
662  */
663  Session & logonAsInitiator(const std::string & host, int port, int heartbeatIntervalSec);
664 
665  /**
666  * Establishes the FIX Connection as an Initiator using the given Heartbeat interval (seconds) and the ResetSeqNumFlag(141) field.
667  * The method is blocked until the acknowledgment Logon message is received or the timeout expired.
668  * The timeout value is equal to the triple heartbeat interval.
669  *
670  * @note This call is thread-safe.
671  */
672  Session & logonAsInitiator(const std::string & host, int port, int heartbeatIntervalSec, bool setResetSeqNumFlag);
673 
674  /**
675  * Establishes the FIX Connection as an Initiator using the custom Logon message.
676  * The method is blocked until the acknowledgment Logon message is received or the timeout expired.
677  * The timeout value is equal to the triple heartbeat interval.
678  *
679  * @note This call is thread-safe.
680  */
681  Session & logonAsInitiator(const std::string & host, int port, int heartbeatIntervalSec, Message * customLogonMsg);
682 
683  /**
684  * Establishes the FIX Connection as an Initiator using the custom Logon message and the ResetSeqNumFlag(141) field.
685  * The method is blocked until the acknowledgment Logon message is received or the timeout expired.
686  * The timeout value is equal to the triple heartbeat interval.
687  *
688  * @note This call is thread-safe.
689  */
690  Session & logonAsInitiator(const std::string & host, int port, int heartbeatIntervalSec, Message * customLogonMsg, bool setResetSeqNumFlag);
691 
692  /**
693  * Establishes the FIX Connection asynchronously as an Initiator using the custom Logon message and the ResetSeqNumFlag(141) field.
694  * The method returns immediately without waiting the acknowledgment Logon message.
695  * Returns the `SharedFuture` object that can be used to wait for the result of the asynchronous logon method.
696  *
697  * @note This call is thread-safe.
698  */
699  Threading::SharedFuture<void> logonAsInitiatorAsync(const std::string & host, int port, int heartbeatIntervalSec, Message * customLogonMsg, bool setResetSeqNumFlag);
700 
701  /**
702  * Terminates the FIX Connection.
703  *
704  * The initial Logout message is sent to the counterparty and the method is blocked until the acknowledgment
705  * Logout message is received or the timeout expired.
706  * The timeout value is equal to the triple heartbeat interval or,
707  * in case there are no incoming messages, heartbeat interval plus reasonable transmission time.
708  *
709  * @note This call is thread-safe.
710  */
711  Session & logout();
712 
713  /**
714  * Terminates the FIX Connection.
715  *
716  * The initial Logout message is sent to the counterparty and the method is blocked until the acknowledgment
717  * Logout message is received or the timeout expired.
718  * The timeout value is equal to the triple heartbeat interval or,
719  * in case there are no incoming messages, heartbeat interval plus reasonable transmission time.
720  *
721  * @param text Free format text string that is sent to the counterparty in the Text(58) field of the initial
722  * Logout message.
723  *
724  * @note This call is thread-safe.
725  */
726  Session & logout(const std::string & text);
727 
728  /**
729  * Terminates the FIX Connection.
730  *
731  * The initial Logout message is sent to the counterparty and the method returns immediately
732  * without waiting for the acknowledgment Logout message.
733  * Returns the `SharedFuture` object that can be used to wait for the result of the asynchronous logout method.
734  *
735  * @param text The free format text string that is sent to the counterparty in the Text(58) field of the initial
736  * Logout message.
737  *
738  * @note This call is thread-safe.
739  */
740  Threading::SharedFuture<void> logoutAsync(const std::string & text);
741 
742  /**
743  * Terminates the FIX Connection.
744  *
745  * The initial Logout message is sent to the counterparty and the method is blocked until the acknowledgment
746  * Logout message is received or the timeout expired.
747  * The timeout value is equal to the triple heartbeat interval or,
748  * in case there are no incoming messages, the heartbeat interval plus reasonable transmission time.
749  *
750  * @param customLogoutMessage The custom Logout message that is sent to the counterparty.
751  *
752  * @note This call is thread-safe.
753  */
754  Session & logout(Message * customLogoutMessage);
755  Session & logout(FlatMessage * customLogoutMessage);
756 
757  /**
758  * Terminates the FIX Connection.
759  *
760  * The initial Logout message is sent to the counterparty and the method returns immediately
761  * without waiting the acknowledgment Logout message.
762  * Returns the `SharedFuture` object that can be used to wait for the result of the asynchronous logout method.
763  *
764  * @param customLogoutMessage The custom Logout message that is sent to the counterparty.
765  *
766  * @note This call is thread-safe.
767  */
768  Threading::SharedFuture<void> logoutAsync(Message * customLogoutMessage);
769  Threading::SharedFuture<void> logoutAsync(FlatMessage * customLogoutMessage);
770 
771  /**
772  * Terminates the FIX connection in the non-graceful way (without the exchange of Logout (MsgType=5) messages).
773  *
774  * @note This call is thread-safe.
775  */
776  Session & breakConnection();
777 
778  /**
779  * Sends the message to the counterparty.
780  *
781  * As soon as a session is created, it is possible to start sending messages via the session.
782  * If the session is not established, the messages are stored in the session storage and will be sent in replay to the resend request
783  * when the connection is established with the counterparty and the sequence numbers mismatch is detected.
784  *
785  * @note This call is asynchronous.
786  *
787  * @note This call is thread-safe.
788  */
789  Session & send(Message * msg);
790  Session & send(FlatMessage * msg);
791 
792  /**
793  * Performs sending via a specific API (e.g. zero-copy send) of the message to the counterparty.
794  *
795  * As soon as a session is created, it is possible to start sending messages via the session.
796  * If the session is not established, the messages are stored in the session storage and will be sent in replay to the resend request
797  * when the connection is established with the counterparty and the sequence numbers mismatch is detected.
798  *
799  * @note This call is asynchronous.
800  *
801  * @note This call is thread-safe.
802  */
803  Session & send(FlatMessage * msg, SessionSendMode::Enum mode);
804 
805  /**
806  * Message batch types.
807  */
808  template<typename MsgType> class MsgBatch;
809 
811  {
814 
815  static void init(MBT::CoreMsgBatchType & coreBatch);
816  static void init(SBT::CoreMsgBatchType & coreBatch);
817  static void add(MBT::MsgBatchType & batch, MBT::CoreMsgBatchType & coreBatch, MBT::MessagePtr & msg);
818  static void add(SBT::MsgBatchType & batch, SBT::CoreMsgBatchType & coreBatch, SBT::MessagePtr & msg);
819  static void clear(MBT::MsgBatchType & batch);
820  static void clear(SBT::MsgBatchType & batch);
821 
822  template<typename MsgType> friend class MsgBatch;
823  };
824 
825  template<typename MsgType>
826  class MsgBatch
827  {
828  typedef MsgBatchTraits<MsgType> Traits;
829 
830  friend class Session;
831 
832  typename Traits::MsgBatchType batch_;
833  typename Traits::CoreMsgBatchType coreBatch_;
834 
835  MsgBatch(const MsgBatch&);
836  MsgBatch & operator=(const MsgBatch&);
837 
838  public:
839 
840  typedef typename Traits::MessagePtr MessagePtr;
841  typedef typename Traits::MsgBatchType::iterator iterator;
842  typedef typename Traits::MsgBatchType::const_iterator const_iterator;
843 
844  MsgBatch() { MsgBatchHelper::init(coreBatch_); }
846 
847  void add(MessagePtr & msg) { MsgBatchHelper::add(batch_, coreBatch_, msg); }
848 
849  MsgType & operator[](size_t index) { return *batch_[index]; }
850  const MsgType & operator[](size_t index) const { return *batch_[index]; }
851 
852  size_t size() const { return batch_.size(); }
853 
854  const_iterator begin() const { return batch_.begin(); }
855  iterator begin() { return batch_.begin(); }
856 
857  const_iterator end() const { return batch_.end(); }
858  iterator end() { return batch_.end(); }
859  };
860 
863  typedef FlatMessageBatch SerializedMessageBatch;
864 
865  /**
866  * Sends messages in a batch to the counterparty.
867  *
868  * As soon as a session is created, it is possible to start sending messages via the session.
869  * If the session is not established, the messages are stored in the session storage and will be sent in replay to the resend request
870  * when the connection is established with the counterparty and the sequence numbers mismatch is detected.
871  *
872  *@param msgs The message batch to send.
873  *@param maxPacketSize The maximum number of bytes written to the socket's send buffer together.
874  * This parameter could be used to reduce the probability that the operating system will fragment a FIX message across multiple TCP packets.
875  *
876  * @note The message batch should not be empty and the maxPacketSize parameter should not be less than any message size in the batch.
877  * Otherwise, the method call can produce an error and the session can close the connection.
878  *
879  * @note This call is asynchronous.
880  *
881  * @note This call is thread-safe.
882  */
883  Session & send(MessageBatch & msgs, size_t maxPacketSize = (std::numeric_limits<size_t>::max)());
884  Session & send(FlatMessageBatch & msgs, size_t maxPacketSize = (std::numeric_limits<size_t>::max)());
885 
886  /**
887  * Performs sending via a specific API (e.g. zero-copy send) of messages to the counterparty.
888  *
889  * As soon as a session is created, it is possible to start sending messages via the session.
890  * If the session is not established, the messages are stored in the session storage and will be sent in replay to the resend request
891  * when the connection is established with the counterparty and the sequence numbers mismatch is detected.
892  *
893  * @note This call is asynchronous.
894  *
895  * @note This call is thread-safe.
896  */
897  Session & send(FlatMessageBatch & msgs, SessionSendMode::Enum mode, size_t maxPacketSize = (std::numeric_limits<size_t>::max)());
898 
899  /**
900  * Sends a serialized message(s) to the counterparty without any fields updating.
901  * One can use the preFill method to prepare a message(s) for sending.
902  *
903  * As soon as a session is created, it is possible to start sending messages via the session.
904  * If the session is not established, the messages are stored in the session storage and will be sent in replay to the resend request
905  * when the connection is established with the counterparty and the sequence numbers mismatch is detected.
906  *
907  * @note This call is asynchronous.
908  *
909  * @note This call is thread-safe.
910  */
911  Session & sendAsIs(FlatMessage * msg);
912 
913  /**
914  * Sends a serialized message(s) to the counterparty without any fields updating.
915  * One can use the preFill method to prepare a message(s) for sending.
916  *
917  * As soon as a session is created, it is possible to start sending messages via the session.
918  * If the session is not established, the messages are stored in the session storage and will be sent in replay to the resend request
919  * when the connection is established with the counterparty and the sequence numbers mismatch is detected.
920  *
921  *@param msgs The message batch to send.
922  *@param maxPacketSize The maximum number of bytes written to the socket's send buffer together.
923  * This parameter could be used to reduce the probability that the operating system will fragment a FIX message across multiple TCP packets.
924  *
925  * @note The message batch should not be empty and the maxPacketSize parameter should not be less than any message size in the batch.
926  * Otherwise, the method call can produce an error and the session can close the connection.
927  *
928  * @note This call is asynchronous.
929  *
930  * @note This call is thread-safe.
931  */
932  Session & sendAsIs(FlatMessageBatch & msgs, size_t maxPacketSize = (std::numeric_limits<size_t>::max)());
933 
934  /**
935  * Performs the throttling of a session that must be called before each of a send function call.
936  * If the count of messages per a time unit exceeds a throttling limit, the function will be blocked until the given time interval is passed.
937  *
938  * @note This call is thread-safe.
939  */
940  Session & throttle();
941 
942  /**
943  * Sets throttling limit parameters.
944  *
945  *@param messagesCount The message limit per a time unit.
946  *@param intervalInMs The time interval to limit messages.
947  *
948  * @note This call is thread-safe.
949  */
950  Session & throttlingLimit(size_t messagesCount, size_t intervalInMs = 1000);
951 
952  /**
953  * This method warms up the sending path.
954  *
955  *@param msg The message to warm up the assembly part of the sending path.
956  *@param warmupFlags Specific flags which can be used to turn on the warmup feature for a specific NIC.
957 
958  * @note This call is thread-safe.
959  */
960  Session & warmUp(FlatMessage* msg, int warmupFlags = 0);
961 
962  /**
963  * Returns 'true' if the given flags work as expected and a real data is not sent to the wire, otherwise - 'false'.
964  *
965  *@param warmupFlags Warmup flags for checking.
966  *@param baseListenPort The base listen port to use during the checking.
967  *@param localNetworkInterface The IP address to bind for listen and outgoing test sockets.
968  *
969  * @note This call is thread-safe.
970  */
971  static bool checkWarmupFlags(int warmupFlags, unsigned short baseListenPort = 5000, const std::string & localNetworkInterface = "127.0.0.1");
972 
973  /**
974  * Pre-fills session-level fields that are constant during the session's lifetime - SenderCompId, TargetCompId, SenderLocationId, TargetLocationID, SenderSubID, TargetSubID.
975  *
976  * @note This call is thread-safe.
977  */
978  void preFill(Message & msg) const;
979 
980  /**
981  * Pre-fills the following fields in the message or messages batch for sending as is:
982  * - Session-level fields that are constant during the session's lifetime - SenderCompId, TargetCompId, SenderLocationId, TargetLocationI, SenderSubID, TargetSubID.
983  * - The MsgSeqNum field in accordance with the session outgoing sequence number.
984  * - The SendingTime field.
985  * - BodyLength and CheckSum fields.
986  *
987  * @note This call is thread-safe.
988  */
989  void preFill(FlatMessage & msg) const;
990  void preFill(FlatMessageBatch & msgs) const;
991 
992  /**
993  * Shutdowns the session.
994  *
995  * @note This call is thread-safe.
996  */
997  Session & shutdown();
998 
999  /**
1000  * Returns the text description of this session.
1001  *
1002  * @note This call is thread-safe.
1003  */
1004  operator const std::string & () const;
1005 
1006  /**
1007  * Returns the underlying storage Id.
1008  *
1009  * @note This call is thread-safe.
1010  */
1011  const std::string & storageId() const;
1012 
1013  /**
1014  * Flushes all internal buffers of the underlying storage.
1015  *
1016  * @note This call is thread-safe.
1017  */
1018  Session & flushSessionStorage();
1019 
1020  /**
1021  * Returns the counterparty host name.
1022  *
1023  * @note The method performs a DNS request for the first time.
1024  * Therefore, it can block the thread execution for a long time until the DNS request is completed.
1025  *
1026  * @note This call is thread-safe.
1027  */
1028  std::string counterpartyHost() const;
1029 
1030  /**
1031  * Returns the counterparty address.
1032  *
1033  * @note This call is thread-safe.
1034  */
1035  std::string counterpartyIpAddress() const;
1036 
1037  /**
1038  * Returns the counterparty port number.
1039  *
1040  * @note This call is thread-safe.
1041  */
1042  size_t counterpartyPort() const;
1043 
1044  /**
1045  * Returns the Session's Custom Key.
1046  *
1047  * @note This call is thread-safe.
1048  */
1049  const std::string & customKey() const;
1050 
1051  /**
1052  * Returns the total number of bytes in the outbound queue.
1053  *
1054  * @note This call is thread-safe.
1055  */
1056  size_t outboundQueueBytes() const;
1057 
1058  /**
1059  * Returns the Heartbeat interval (in seconds).
1060  */
1061  int heartbeatIntervalSec() const;
1062 
1063  /**
1064  * Returns 'true' if inbound messages are logged, otherwise - 'false'.
1065  * By default, the value is 'true'.
1066  */
1067  bool logInboundMessages() const;
1068 
1069  /**
1070  * The option to log inbound messages.
1071  */
1072  Session & logInboundMessages(bool value);
1073 
1074  /**
1075  * Returns 'true' if outbound messages are logged, otherwise - 'false'.
1076  * By default, the value is 'true'.
1077  */
1078  bool logOutboundMessages() const;
1079 
1080  /**
1081  * The option to log outbound messages.
1082  */
1083  Session & logOutboundMessages(bool value);
1084 
1085  /**
1086  * Incoming message types to be filtered out from the logs.
1087  */
1088  typedef std::set<std::string> InboundMessageLogFilter;
1089 
1090  /**
1091  * Returns incoming message types to be filtered out from the logs.
1092  *
1093  * @note This call is thread-safe.
1094  */
1095  InboundMessageLogFilter inboundMessageLogFilter() const;
1096 
1097  /**
1098  * Sets incoming message types to be filtered out from the logs.
1099  *
1100  * @note This call is thread-safe.
1101  */
1102  Session & inboundMessageLogFilter(const InboundMessageLogFilter & filter);
1103 
1104  /**
1105  * Outgoing message types to be filtered out from the logs.
1106  */
1107  typedef std::set<std::string> OutboundMessageLogFilter;
1108 
1109  /**
1110  * Returns outgoing message types to be filtered out from the logs.
1111  *
1112  * @note This call is thread-safe.
1113  */
1114  OutboundMessageLogFilter outboundMessageLogFilter() const;
1115 
1116  /**
1117  * Sets outgoing message types to be filtered out from the logs.
1118  *
1119  * @note This call is thread-safe.
1120  */
1121  Session & outboundMessageLogFilter(const OutboundMessageLogFilter & filter);
1122 
1123  /**
1124  * Returns 'true' if outbound messages are logged before sending, otherwise - 'false'.
1125  * By default, the value is 'true'.
1126  *
1127  * @note This call is thread-safe.
1128  */
1129  bool logBeforeSending() const;
1130 
1131  /**
1132  * The option to switch on/off the logging of outbound messages before/after sending.
1133  *
1134  * @note This call is thread-safe.
1135  */
1136  Session & logBeforeSending(bool value);
1137 
1138  /**
1139  * Returns the expected sequence number of the next incoming message.
1140  *
1141  * @note This call is thread-safe.
1142  */
1143  SequenceNumber inSeqNum() const;
1144 
1145  /**
1146  * Sets the expected sequence number of the next incoming message.
1147  *
1148  * @note This call is thread-safe.
1149  */
1150  Session & inSeqNum(SequenceNumber seqNum);
1151 
1152  /**
1153  * Returns the sequence number of the next outgoing message.
1154  *
1155  * @note This call is thread-safe.
1156  */
1157  SequenceNumber outSeqNum() const;
1158 
1159  /**
1160  * Sets the sequence number of the next outgoing message.
1161  *
1162  * @note This call is thread-safe.
1163  */
1164  Session & outSeqNum(SequenceNumber seqNum);
1165 
1166  /**
1167  * Represents an unlimited number of messages to be requested in one Resend Request (MsgType=2) message.
1168  * The default value is 0.
1169  */
1170  static const unsigned int ResendRequestMaximumRangeNoLimit;
1171 
1172  /**
1173  * Sets the maximum number of messages to be requested in one Resend Request (MsgType=2) message.
1174  *
1175  * @see Session::ResendRequestMaximumRangeNoLimit.
1176  *
1177  * @param range The maximum number of messages to be requested in one Resend Request (MsgType=2) message. Cannot be negative.
1178  *
1179  * @note This call is thread-safe.
1180  */
1181  Session & resendRequestMaximumRange(int range);
1182 
1183  /**
1184  * Returns the maximum number of messages to be requested in one Resend Request (MsgType=2) message.
1185  * By default, the value is zero (no limit).
1186  *
1187  * @see Session::ResendRequestMaximumRangeNoLimit.
1188  *
1189  * @note This call is thread-safe.
1190  */
1191  int resendRequestMaximumRange() const;
1192 
1193  /**
1194  * Sets the number of sent messages that are available for resending on the counterparty's Resend Request <2> message.
1195  *
1196  * @note This call is thread-safe.
1197  */
1198  Session & resendingQueueSize(size_t value);
1199 
1200  /**
1201  * Returns the number of sent messages that are available for resending on the counterparty's Resend Request <2> message.
1202  * By default, the value is -1 that means that the corresponding Engine setting's value is used ('1000' by default).
1203  *
1204  * @note This call is thread-safe.
1205  */
1206  size_t resendingQueueSize() const;
1207 
1208  /**
1209  * Returns the session role.
1210  *
1211  * @note This call is thread-safe.
1212  */
1213  SessionRole::Enum role() const;
1214 
1215  /**
1216  * Returns the assigned value used to identify the firm sending message (the SenderCompID (tag 49) field value in outgoing messages).
1217  *
1218  * @note This call is thread-safe.
1219  */
1220  const std::string & senderCompId() const;
1221 
1222  /**
1223  * Returns 'true' if the LastMsgSeqNumProcessed (tag 369) field is specified on every message sent, otherwise - 'false'.
1224  * By default, the option value is false.
1225  */
1226  bool specifyLastMsgSeqNumProcessed() const;
1227 
1228  /**
1229  * The option to specify the LastMsgSeqNumProcessed (tag 369) field on every message sent.
1230  * Useful for detecting a backlog with a counterparty.
1231  *
1232  * @note The option does not affect the FlatMessage send.
1233  */
1234  Session & specifyLastMsgSeqNumProcessed(bool specify);
1235 
1236  /**
1237  * Returns 'true' if updating of the SendingTime is turn on for every message, otherwise - 'false'.
1238  * By default, the option value is true.
1239  */
1240  bool updateSendingTimeField() const;
1241 
1242  /**
1243  * The option to specify updating of the SendingTime of every sent message.
1244  */
1245  Session & updateSendingTimeField(bool specify);
1246 
1247  /**
1248  * Sets the time format of the SendingTime of every sent message.
1249  * By default, the YYYYMMDDHHMMSSMsec format is used.
1250  */
1251  Session & sendingTimeFormat(TimestampFormat::Enum format);
1252 
1253  /**
1254  * Returns the assigned value used to identify the receiving firm (the TargetCompID (tag 56) field value in outgoing messages).
1255  *
1256  * @note This call is thread-safe.
1257  */
1258  const std::string & targetCompId() const;
1259 
1260  /**
1261  * The instance of the FIX dictionary or standard FIX messages dictionary which is used by the session.
1262  */
1263  Dictionary dictionary() const;
1264 
1265  /**
1266  * Returns the CPU affinity of the receiving thread.
1267  */
1268  const OnixS::Threading::CpuIndexes & receivingThreadAffinity() const;
1269 
1270  /**
1271  * Sets the CPU affinity of the receiving thread.
1272  */
1273  Session & receivingThreadAffinity(const OnixS::Threading::CpuIndexes & cpuIndexes);
1274 
1275  /**
1276  * Sets the CPU affinity of the receiving thread to a CPU.
1277  */
1278  Session & receivingThreadAffinity(const OnixS::Threading::CpuIndex cpuIndex);
1279 
1280  /**
1281  * Returns the CPU affinity of the sending thread.
1282  */
1283  const OnixS::Threading::CpuIndexes & sendingThreadAffinity() const;
1284 
1285  /**
1286  * Sets the CPU affinity index of the sending thread.
1287  */
1288  Session & sendingThreadAffinity(const OnixS::Threading::CpuIndexes & cpuIndexes);
1289 
1290  /**
1291  * Sets the CPU affinity index of the sending thread a CPU.
1292  */
1293  Session & sendingThreadAffinity(const OnixS::Threading::CpuIndex cpuIndex);
1294 
1295  /**
1296  * Represents an undefined value of priority and policy.
1297  */
1298  static const int UndefinedPriorityAndPolicy;
1299 
1300  /**
1301  * Returns the priority of the receiving thread.
1302  */
1303  int receivingThreadPriority() const;
1304 
1305  /**
1306  * Sets the priority of the receiving thread.
1307  */
1308  Session & receivingThreadPriority(int priority);
1309 
1310  /**
1311  * Returns the priority of the sending thread.
1312  */
1313  int sendingThreadPriority() const;
1314 
1315  /**
1316  * Sets the priority of the sending thread.
1317  */
1318  Session & sendingThreadPriority(int priority);
1319 
1320  /**
1321  * Returns the scheduling policy of the receiving thread.
1322  */
1323  int receivingThreadPolicy() const;
1324 
1325  /**
1326  * Sets the scheduling policy of the receiving thread.
1327  */
1328  Session & receivingThreadPolicy(int policy);
1329 
1330  /**
1331  * Returns the scheduling policy of the sending thread.
1332  */
1333  int sendingThreadPolicy() const;
1334 
1335  /**
1336  * Sets the scheduling policy of the sending thread.
1337  */
1338  Session & sendingThreadPolicy(int policy);
1339 
1340  /**
1341  * Returns the sent message if it can be found by the given message sequence number, otherwise - NULL.
1342  *
1343  * @note The returned message is owned by the application and should be released after usage.
1344  *
1345  * @note This call is thread-safe.
1346  */
1347  Message * findSentMessage(SequenceNumber messageSequenceNumber);
1348  FlatMessage * findSentFlatMessage(SequenceNumber messageSequenceNumber);
1349 
1350  /**
1351  * Returns an array of sent messages if it can be found by the given message sequence number range, otherwise - empty array.
1352  *
1353  * @note The returned messages are owned by the application and should be released after usage.
1354  *
1355  * @note This call is thread-safe.
1356  */
1357  MsgBatchTraits<Message>::MsgBatchType findSentMessages(SequenceNumber beginSequenceNumber, SequenceNumber endSequenceNumber);
1358  MsgBatchTraits<FlatMessage>::MsgBatchType findSentFlatMessages(SequenceNumber beginSequenceNumber, SequenceNumber endSequenceNumber);
1359 
1360  /**
1361  * When the message gap is detected the "Resend Request" FIX Message is sent and the Session state is changed to "AwaitReplyOnResendRequest".
1362  * By default, in this state the incoming new messages (without the PossDupFlag (tag #43) flag) are ignored because we expect them to be re-sent later again with the PossDupFlag flag.
1363  *
1364  * This property allows to change this behavior and report the new messages anyway.
1365  *
1366  * @note In this mode, messages could be reported out of the original order: e.g. MsgSeqNum could be: 3 (original), 4 (original), 2 (PossDupFlag='Y'), 3 (PossDupFlag='Y'), 4 (PossDupFlag='Y')
1367  * and some messages could be received two times: first time without PossDupFlag='Y' and second time - with this flag.
1368  */
1369  Session & reportNewMessagesWhileWaitingForMissedMessages(bool report);
1370 
1371  /**
1372  * Returns 'true' if the new messages are reported even when the message gap is detected and the reply on the "Resend Request" message is expected, otherwise - 'false'.
1373  */
1374  bool reportNewMessagesWhileWaitingForMissedMessages() const;
1375 
1376  /**
1377  * By default, the "Resend Request" message is sent only once - when the first gap is detected.
1378  * This property allows to change this behavior and send the "Resend Request" message on each detected message sequence number gap.
1379  */
1380  Session & sendResendRequestOnEachMessageGap(bool send);
1381 
1382  /**
1383  * Returns 'true' if the "Resend Request" message is sent on each detected message sequence number gap, otherwise - 'false'.
1384  */
1385  bool sendResendRequestOnEachMessageGap() const;
1386 
1387  /**
1388  * By default, the "Resend Request" message requests all messages begin from the first missed one.
1389  * This property allows to change this behavior and request only missed messages.
1390  *
1391  * @note This method can be called only when the session is disconnected.
1392  */
1393  Session & requestOnlyMissedMessages(bool request);
1394 
1395  /**
1396  * Returns 'true' if the "Resend Request" message requests only missed messages, otherwise - 'false'.
1397  */
1398  bool requestOnlyMissedMessages() const;
1399 
1400  /**
1401  * By default, if the "Session Level Reject" message is received in replay to the "Resend Request" then this is considered as a serious problem and the FIX connection will be closed.
1402  * This property allows to change this behavior and consider the "Session Level Reject" on the "Resend Request" as the "Sequence Reset Gap Fill" message.
1403  */
1404  Session & considerRejectOnResendRequestAsGapFill(bool consider);
1405 
1406  /**
1407  * Returns 'true' if the "Reject" on the "Resend Request" is considered as the "Sequence Reset Gap Fill" message, otherwise - 'false'.
1408  */
1409  bool considerRejectOnResendRequestAsGapFill() const;
1410 
1411  /**
1412  * By default, if the incoming message has a sequence number less than expected and the PossDupFlag is not set, it indicates
1413  * a serious error and leads to FIX connection closing.
1414  * This property allows to change this behavior and consider the sequence number of the incoming message, which less than expected, as an expected one.
1415  */
1416  Session & ignoreLessThanExpectedSequenceNumber(bool ignore);
1417 
1418  /**
1419  * Returns 'true' if the sequence number of the incoming message, which less than expected, is considered as an expected one, otherwise - 'false'.
1420  */
1421  bool ignoreLessThanExpectedSequenceNumber() const;
1422 
1423  /**
1424  * The option to automatically reset the local sequence numbers to 1 during every logon.
1425  */
1426  Session & resetLocalSequenceNumbersOnLogon(bool reset);
1427 
1428  /**
1429  * Returns 'true' if local sequence numbers are reset automatically to 1 during every logon, otherwise - 'false'.
1430  */
1431  bool resetLocalSequenceNumbersOnLogon() const;
1432 
1433  /**
1434  * If the requestOnlyMissedMessages option is true and the sequence number of an incoming FIX message
1435  * greater that expected then the resend functionality is run and this incoming message is stored
1436  * in the incoming message gap queue for further processing.
1437  * This property allows to set maximum size of that queue.
1438  */
1439  Session & incomingMessageGapQueueMaximumSize(size_t maxSize);
1440 
1441  /**
1442  * Returns the maximum size of the incoming message gap queue.
1443  * By default, the value equal to 1000.
1444  */
1445  size_t incomingMessageGapQueueMaximumSize() const;
1446 
1447  /**
1448  * Registers the Session listener.
1449  *
1450  * @throw An exception if the listener is already registered.
1451  */
1452  Session & registerListener(ISessionListener * listener);
1453 
1454  /**
1455  * Unregisters the Session listener.
1456  */
1457  Session & unregisterListener(ISessionListener * listener);
1458 
1459  /**
1460  * Unregisters all Session listener.
1461  */
1462  Session & unregisterAllListeners();
1463 
1464  /**
1465  * Backups the current log files and resets the local sequence numbers to 1.
1466  *
1467  * @note This method can be called only when the session is disconnected.
1468  *
1469  * @note This call is thread-safe.
1470  */
1471  Session & resetLocalSequenceNumbers();
1472 
1473  /**
1474  * Sends a Logon message with the ResetSeqNumFlag set.
1475  *
1476  * @note This call is thread-safe.
1477  */
1478  Session & resetSeqNumViaLogonExchange();
1479 
1480  /**
1481  * Returns the SenderSubID (tag 50) field values for all outgoing messages.
1482  *
1483  * @note This call is thread-safe.
1484  */
1485  std::string senderSubId() const;
1486 
1487  /**
1488  * Sets the SenderSubID (tag 50) field values for all outgoing messages.
1489  *
1490  * @note This call is thread-safe.
1491  */
1492  Session & senderSubId(const std::string & value);
1493 
1494  /**
1495  * Returns the TargetSubID (tag 57) field values for all outgoing messages.
1496  *
1497  * @note This call is thread-safe.
1498  */
1499  std::string targetSubId() const;
1500 
1501  /**
1502  * Sets the TargetSubID (tag 57) field values for all outgoing messages.
1503  *
1504  * @note This call is thread-safe.
1505  */
1506  Session & targetSubId(const std::string & value);
1507 
1508  /**
1509  * Returns the SenderLocationID (tag 142) field values for all outgoing messages.
1510  *
1511  * @note This call is thread-safe.
1512  */
1513  std::string senderLocationId() const;
1514 
1515  /**
1516  * Sets the SenderLocationID (tag 142) field values for all outgoing messages.
1517  *
1518  * @note This call is thread-safe.
1519  */
1520  Session & senderLocationId(const std::string & value);
1521 
1522  /**
1523  * Returns the TargetLocationID (tag 143) field values for all outgoing messages.
1524  *
1525  * @note This call is thread-safe.
1526  */
1527  std::string targetLocationId() const;
1528 
1529  /**
1530  * Sets the TargetLocationID (tag 143) field values for all outgoing messages.
1531  *
1532  * @note This call is thread-safe.
1533  */
1534  Session & targetLocationId(const std::string & value);
1535 
1536  /**
1537  * Options to turn on/off incoming message sequence numbers validation.
1538  */
1539  Session & validateSequenceNumbers(bool value);
1540 
1541  /**
1542  * Returns the current status of the incoming message sequence numbers validation.
1543  * By default, the option value is true.
1544  */
1545  bool validateSequenceNumbers() const;
1546 
1547  /**
1548  * Returns the thread name suffix for receiving (R:threadNameSuffix) and sending (S:threadNameSuffix) threads.
1549  *
1550  * @note This call is thread-safe.
1551  */
1552  std::string threadNameSuffix() const;
1553 
1554  /**
1555  * Sets the thread name suffix for receiving ("R:threadNameSuffix") and sending ("S:threadNameSuffix") threads.
1556  **
1557  * If the threadNameSuffix is not specified, it is set by default to ("R:senderCompId-targetCompId")("S:senderCompId-targetCompId").
1558  **
1559  * @note This method can be called only when the session is disconnected.
1560  * @throw An exception if the session state is not Disconnected.
1561  *
1562  * @note This call is thread-safe.
1563  */
1564  Session & threadNameSuffix(const std::string & value);
1565 
1566  /**
1567  * Sends the Test Request (MsgType 1) message.
1568  * If the timeout is specified, the method blocks until the heartbeat message is received from the counterparty or timeout ended.
1569  *
1570  * @param testReqId The identifier included in the Test Request (MsgType 1) message to be returned in the resulting Heartbeat (MsgType 0) message from the counterparty.
1571  * @param timeout The maximum interval to wait until the reply is received from the counterparty.
1572  *
1573  * @note This call is thread-safe.
1574  */
1575  Session & sendTestRequest(const std::string & testReqId = "", const TimeSpan & timeout = TimeSpan::zero());
1576 
1577  /**
1578  * Sends the Resend Request (MsgType 2) message.
1579  *
1580  * @note Normally, the "Resend Request" message is sent automatically. This method allows to simulate a sequence gap by sending this message manually.
1581  * This can be needed in some venue specific cases. This method should not be used in the normal work.
1582  *
1583  * @param beginSeqNumber The begin sequence number of the first requested message in the range.
1584  *
1585  * @note This call is thread-safe.
1586  */
1587  Session & sendResendRequest(SequenceNumber beginSeqNumber);
1588 
1589  /**
1590  * Sends the Reject (MsgType 3) message.
1591  *
1592  * @param refSeqNumber The MsgSeqNum of rejected message.
1593  * @param text The message to explain the reason for the rejection.
1594  *
1595  * @note This call is thread-safe.
1596  */
1597  Session & sendReject(SequenceNumber refSeqNumber, const std::string & text);
1598 
1599  /**
1600  * Returns the time when logical session was created or the last sequence number reset operation was performed.
1601  * The originating value is extracted from the attached storage using the ISessionStorage::sessionCreationTime() method.
1602  *
1603  * @note This call is thread-safe.
1604  */
1605  Timestamp creationTime() const;
1606 
1607  /**
1608  * Returns the session string presentation.
1609  *
1610  * @note This call is thread-safe.
1611  */
1612  std::string toString() const;
1613 
1614  /**
1615  * Represents an invalid value of the socket handle.
1616  */
1618 
1619  /**
1620  * Returns the socket handle which the session uses to transmit the FIX data.
1621  *
1622  * @note This call is thread-safe.
1623  */
1624  OnixS::Sockets::Handle socketHandle();
1625 
1626  /**
1627  * Additional options, which should be set to the session socket when it is created.
1628  * For initiator sessions, options are set before the socket connect(..) call.
1629  * For acceptor sessions, options are set after the socket accept(..) call.
1630  */
1631  Session & socketOptions(const OnixS::Sockets::SocketOptions & options);
1632 
1633  /**
1634  * Inbound and outbound messages, the session's state data are stored in this directory.
1635  *
1636  * @note This method can be called only when the session is disconnected.
1637  * @throw An exception if the session state is not Disconnected.
1638  *
1639  * @note This call is thread-safe.
1640  */
1641  Session & logDirectory(const std::string & value);
1642 
1643  /**
1644  * Inbound and outbound messages, the session's state data are stored in this directory.
1645  *
1646  * @note This call is thread-safe.
1647  */
1648  const std::string & logDirectory() const;
1649 
1650  /**
1651  * Sets the timeout during which counterparty should send a reply to the "Resend Request" message.
1652  * If a counterparty does not reply or reply incorrectly during this timeout, then the ISessionListener::onWarning is called.
1653  * By default, the resend request timeout equal to zero, which means an infinite timeout.
1654  *
1655  * @note The resend timeout is checked when an incoming message is received or when the receive timeout is elapsed.
1656  * Therefore, the actual timeout can be greater than the specified one or can be rounded to the value equal to the heartbeat interval + reasonable transmission time.
1657  *
1658  * @note This call is thread-safe.
1659  */
1660  Session & resendTimeout(const TimeSpan & timeout);
1661 
1662  /**
1663  * Returns the timeout during which the counterparty should send a reply to the "Resend Request" message.
1664  *
1665  * @note This call is thread-safe.
1666  */
1667  TimeSpan resendTimeout() const;
1668 
1669  /**
1670  * The collection of scrambled tags.
1671  */
1672  typedef std::vector<Tag> ScrambledFields;
1673 
1674  /**
1675  * Sets scrambled fields in the Logon(A) message, in the session storage, for security reasons.
1676  *
1677  * @note This call is thread-safe.
1678  */
1679  Session & scrambleLogonFields(const ScrambledFields & fields);
1680 
1681  /**
1682  * Returns scrambled fields in the Logon(A) message.
1683  */
1684  ScrambledFields scrambleLogonFields() const;
1685 
1686  /**
1687  * Sets the send queue maximum size in bytes.
1688  * The TCP connection will be disconnected when an overflow issue happens and the sending queue size exceeds this size.
1689  *
1690  * @note This call is thread-safe.
1691  */
1692  Session & sendQueueMaxSize(size_t size);
1693 
1694  /**
1695  * Returns the send queue maximum size in bytes.
1696  * When it is not set, the corresponding Engine setting's value is used (0.5 GB by default).
1697  */
1698  size_t sendQueueMaxSize() const;
1699 
1700  /**
1701  * Sets the socket TCP connect timeout.
1702  *
1703  * @note This call is thread-safe.
1704  */
1705  Session & connectTimeout(const TimeSpan & timeout);
1706 
1707  /**
1708  * Returns the socket TCP connect timeout (30 sec by default).
1709  *
1710  * @note This call is thread-safe.
1711  */
1712  TimeSpan connectTimeout() const;
1713 
1714  /**
1715  * Returns the size of the TCP buffer allocated to the FIX connection for receiving data.
1716  * If '-1' then the default operating system value is used.
1717  *
1718  * @note This call is thread-safe.
1719  */
1720  int receiveBufferSize() const;
1721 
1722  /**
1723  * Sets the size of the TCP buffer allocated to the FIX connection for the receiving data.
1724  * If '-1' then the default operating system value is used.
1725  *
1726  * @note This call is thread-safe.
1727  */
1728  Session & receiveBufferSize(int value);
1729 
1730  /**
1731  * Returns the size of the TCP buffer allocated to the FIX connection for the sending data.
1732  * If '-1' then the default operating system value is used.
1733  *
1734  * @note This call is thread-safe.
1735  */
1736  int sendBufferSize() const;
1737 
1738  /**
1739  * Sets the size of the TCP buffer allocated to the FIX connection for the sending data.
1740  * If '-1' then the default operating system value is used.
1741  *
1742  * @note This call is thread-safe.
1743  */
1744  Session & sendBufferSize(int value);
1745 
1746  /**
1747  * Schedule the memory usage optimization.
1748  * The session will perform the actual memory usage optimization in two phases:
1749  * - After receiving and processing the next incoming session-level message (receiving part).
1750  * - Before sending the next outgoing session-level message (sending part).
1751  */
1752  void scheduleShrinkToFit();
1753 
1754  /**
1755  * HTTP proxy settings.
1756  */
1758  {
1760  :host(), port(), username(), password()
1761  {}
1762 
1763  ProxySettings(const std::string & proxyHost, int proxyPort, const std::string & proxyUsername = std::string(), const std::string & proxyPassword = std::string())
1764  :host(proxyHost), port(proxyPort), username(proxyUsername), password(proxyPassword)
1765  {}
1766 
1767  std::string host;
1768  int port;
1769  std::string username;
1770  std::string password;
1771  };
1772 
1773  /**
1774  * Sets HTTP proxy settings.
1775  */
1776  Session & proxySettings(const ProxySettings & settings);
1777 
1778  /**
1779  * Returns HTTP proxy settings.
1780  */
1781  ProxySettings proxySettings() const;
1782 
1783  /**
1784  * Returns the session message mode.
1785  */
1786  MessageMode::Enum messageMode() const;
1787 
1788  /**
1789  * Sets the session message mode.
1790  */
1791  void messageMode(MessageMode::Enum mode);
1792 
1793  struct Impl; ///< Implementation details.
1794 
1795 private:
1796  void init(
1797  const std::string & senderCompId,
1798  const std::string & targetCompId,
1799  const Dictionary & dictionary,
1800  ISessionListener * listener,
1801  bool keepSequenceNumbersAfterLogout,
1802  SessionStorageType::Enum storageType,
1803  ISessionStorage * storage,
1804  const std::string & customSessionKey,
1805  ISessionReactor * reactor);
1806 
1807  Impl * impl_;
1808 
1809  friend class Engine::Impl;
1810 };
1811 
1812 /// Stream output.
1814 std::ostream & operator << (std::ostream & os, const Session & session);
1815 }
1816 }
The FIX Session - a bi-directional stream of ordered messages between two parties within a continuous...
Definition: Session.h:97
The file based storage. The session&#39;s state and messages are persisted to the file system...
ONIXS_FIXENGINE_API std::ostream & operator<<(std::ostream &os, const Group &group)
Stream output.
Traits::MsgBatchType::iterator iterator
Definition: Session.h:841
MsgType & operator[](size_t index)
Definition: Session.h:849
std::set< std::string > InboundMessageLogFilter
Incoming message types to be filtered out from the logs.
Definition: Session.h:1088
static const OnixS::Sockets::Handle InvalidSocketHandle
Represents an invalid value of the socket handle.
Definition: Session.h:1617
const_iterator begin() const
Definition: Session.h:854
size_t CpuIndex
Logical processors that a thread is allowed to run on (first logical CPU has index 0)...
Definition: Thread.h:31
static TimeSpan zero()
The zero value of the time span.
int Handle
Type alias for socket handle.
Definition: Definitions.h:50
std::set< std::string > OutboundMessageLogFilter
Outgoing message types to be filtered out from the logs.
Definition: Session.h:1107
size_t size() const
Definition: Session.h:852
The session&#39;s Listener.
FlatMessageBatch SerializedMessageBatch
Definition: Session.h:863
std::vector< Tag > ScrambledFields
The collection of scrambled tags.
Definition: Session.h:1672
std::vector< OnixS::FIX::Core::Messaging::Message * > CoreMsgBatchType
Definition: Session.h:91
const MsgType & operator[](size_t index) const
Definition: Session.h:850
#define ONIXS_FIXENGINE_API
Definition: ABI.h:45
HTTP proxy settings.
Definition: Session.h:1757
const_iterator end() const
Definition: Session.h:857
PtrTraits< Message >::UniquePtr MessagePtr
Definition: Session.h:89
Provides an access to FIX fields from a flat (tag=value) message.
Definition: FlatMessage.h:88
virtual size_t resendingQueueSize() const =0
Returns the number of sent messages that are available for resending on counterparty&#39;s Resend Request...
Message batch types.
Definition: Session.h:808
ProxySettings(const std::string &proxyHost, int proxyPort, const std::string &proxyUsername=std::string(), const std::string &proxyPassword=std::string())
Definition: Session.h:1763
std::set< CpuIndex > CpuIndexes
Definition: Thread.h:32
MsgBatch< FlatMessage > FlatMessageBatch
Definition: Session.h:862
Identifies the FIX messages dictionary.
Definition: Dictionary.h:73
std::vector< Message * > MsgBatchType
Definition: Session.h:90
static const int UndefinedPriorityAndPolicy
Represents an undefined value of priority and policy.
Definition: Session.h:1298
Traits::MsgBatchType::const_iterator const_iterator
Definition: Session.h:842
The session message mode.
Definition: Session.h:62
ONIXS_FIXENGINE_API_DECL(class, IEngineListener)
virtual void clear()=0
Clears the storage.
The immutable secure string.
Definition: SecureString.h:29
std::pair< unsigned short, unsigned short > LocalPortRange
The local port range type.
Definition: Session.h:299
PtrTraits< MsgType >::UniquePtr MessagePtr
Definition: Session.h:81
The time span related functionality.
Definition: TimeSpan.h:93
std::vector< FlatMessage * > MsgBatchType
Definition: Session.h:82
MsgBatch< Message > MessageBatch
Definition: Session.h:861
The session&#39;s pluggable storage.
The session&#39;s network stack reactor interface.
unsigned int SequenceNumber
Alias for the sequence number.
std::vector< OnixS::FIX::Core::Messaging::Extras::FlatMessage * > CoreMsgBatchType
Definition: Session.h:83
Represents a future result of an asynchronous operation - a result that will eventually appear in the...
Definition: Future.h:253
Encapsulates operations over a FIX Message.
Definition: Message.h:49
virtual SequenceNumber outSeqNum()=0
Returns the last outgoing sequence number.
std::vector< SocketOption > SocketOptions
Storage type of socket options.
Definition: Definitions.h:67
std::auto_ptr< T > UniquePtr
Definition: Definitions.h:32
Traits::MessagePtr MessagePtr
Definition: Session.h:840
void add(MessagePtr &msg)
Definition: Session.h:847
The timestamps related functionality.
Definition: Timestamp.h:91
static const unsigned int ResendRequestMaximumRangeNoLimit
Represents an unlimited number of messages to be requested in one Resend Request (MsgType=2) message...
Definition: Session.h:1170
virtual SequenceNumber inSeqNum()=0
Returns the last inbound sequence number.