llfix
Low-latency FIX engine
sequence_store.h
1 // DISCLAIMER_PLACEHOLDER
2 #pragma once
3 
4 #include <cstdint>
5 #include <cstddef>
6 #include <string_view>
7 
8 #include "../../core/os/memory_mapped_file.h"
9 #include "../../core/utilities/filesystem_utilities.h"
10 
11 namespace llfix
12 {
13 
23 {
24  public:
25 
26  ~SequenceStore()
27  {
28  save_to_disc();
29  m_memory_mapped_file.close();
30  }
31 
32  bool open(const std::string_view& file_path)
33  {
34  auto folder = FileSystemUtilities::get_path_by_excluding_file(file_path);
35 
36  if (folder.length() > 0)
37  {
38  if (FileSystemUtilities::does_path_exist(folder) == false)
39  {
40  if (FileSystemUtilities::create_directory(folder) == false)
41  {
42  return false;
43  }
44  }
45  }
46 
47  bool success = m_memory_mapped_file.open(file_path, MEMORY_MAPPED_FILE_SIZE);
48 
49  if (success)
50  {
51  m_buffer = nullptr;
52  std::size_t buffer_size = 0;
53  m_memory_mapped_file.get_buffer(&m_buffer, buffer_size);
54  }
55 
56  return success;
57  }
58 
59  bool is_open() const
60  {
61  return m_memory_mapped_file.is_open();
62  }
63 
64  void save_to_disc()
65  {
66  if (m_memory_mapped_file.is_open())
67  {
68  m_memory_mapped_file.flush_to_disc();
69  }
70  }
71 
76  {
79  }
80 
81  // OUTGOING
82  void increment_outgoing_seq_no()
83  {
84  reinterpret_cast<uint32_t*>(m_buffer)[0]++;
85  }
86 
87  void decrement_outgoing_seq_no()
88  {
89  reinterpret_cast<uint32_t*>(m_buffer)[0]--;
90  }
91 
96  void set_outgoing_seq_no(uint32_t n)
97  {
98  reinterpret_cast<uint32_t*>(m_buffer)[0] = n;
99  }
100 
105  uint32_t get_outgoing_seq_no() const
106  {
107  return reinterpret_cast<uint32_t*>(m_buffer)[0];
108  }
109 
110  // INCOMING
111  void increment_incoming_seq_no()
112  {
113  reinterpret_cast<uint32_t*>(m_buffer)[1]++;
114  }
115 
120  void set_incoming_seq_no(uint32_t n)
121  {
122  reinterpret_cast<uint32_t*>(m_buffer)[1] = n;
123  }
124 
129  uint32_t get_incoming_seq_no() const
130  {
131  return reinterpret_cast<uint32_t*>(m_buffer)[1];
132  }
133 
134  protected:
135  MemoryMappedFile m_memory_mapped_file;
136  char* m_buffer = nullptr;
137  static inline constexpr std::size_t MEMORY_MAPPED_FILE_SIZE = 65536; // Even though a typical vm page is 4KB, on Windows page allocation granularity is 64KB
138 };
139 
140 } // namespace
llfix::SequenceStore::set_incoming_seq_no
void set_incoming_seq_no(uint32_t n)
Set the incoming FIX sequence number.
Definition: sequence_store.h:120
llfix::SequenceStore::set_outgoing_seq_no
void set_outgoing_seq_no(uint32_t n)
Set the outgoing FIX sequence number.
Definition: sequence_store.h:96
llfix::SequenceStore::get_incoming_seq_no
uint32_t get_incoming_seq_no() const
Get the incoming FIX sequence number.
Definition: sequence_store.h:129
llfix::SequenceStore
Persistent FIX sequence number store backed by a memory-mapped file.
Definition: sequence_store.h:22
llfix::SequenceStore::get_outgoing_seq_no
uint32_t get_outgoing_seq_no() const
Get the outgoing FIX sequence number.
Definition: sequence_store.h:105
llfix::SequenceStore::reset_numbers
void reset_numbers()
Reset both incoming and outgoing sequence numbers to zero.
Definition: sequence_store.h:75