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