llfix
Low-latency FIX engine
sequence_store.h
1 /*
2 MIT License
3 
4 Copyright (c) 2026 Coreware Limited
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 #pragma once
25 
26 #include <cstdint>
27 #include <cstddef>
28 #include <string_view>
29 
30 #include "../../core/os/memory_mapped_file.h"
31 #include "../../core/utilities/filesystem_utilities.h"
32 
33 namespace llfix
34 {
35 
45 {
46  public:
47 
48  ~SequenceStore()
49  {
50  save_to_disc();
51  m_memory_mapped_file.close();
52  }
53 
54  bool open(const std::string_view& file_path)
55  {
56  auto folder = FileSystemUtilities::get_path_by_excluding_file(file_path);
57 
58  if (folder.length() > 0)
59  {
60  if (FileSystemUtilities::does_path_exist(folder) == false)
61  {
62  if (FileSystemUtilities::create_directory(folder) == false)
63  {
64  return false;
65  }
66  }
67  }
68 
69  bool success = m_memory_mapped_file.open(file_path, MEMORY_MAPPED_FILE_SIZE);
70 
71  if (success)
72  {
73  m_buffer = nullptr;
74  std::size_t buffer_size = 0;
75  m_memory_mapped_file.get_buffer(&m_buffer, buffer_size);
76  }
77 
78  return success;
79  }
80 
81  bool is_open() const
82  {
83  return m_memory_mapped_file.is_open();
84  }
85 
86  void save_to_disc()
87  {
88  if (m_memory_mapped_file.is_open())
89  {
90  m_memory_mapped_file.flush_to_disc();
91  }
92  }
93 
98  {
101  }
102 
103  // OUTGOING
104  void increment_outgoing_seq_no()
105  {
106  reinterpret_cast<uint32_t*>(m_buffer)[0]++;
107  }
108 
109  void decrement_outgoing_seq_no()
110  {
111  reinterpret_cast<uint32_t*>(m_buffer)[0]--;
112  }
113 
118  void set_outgoing_seq_no(uint32_t n)
119  {
120  reinterpret_cast<uint32_t*>(m_buffer)[0] = n;
121  }
122 
127  uint32_t get_outgoing_seq_no() const
128  {
129  return reinterpret_cast<uint32_t*>(m_buffer)[0];
130  }
131 
132  // INCOMING
133  void increment_incoming_seq_no()
134  {
135  reinterpret_cast<uint32_t*>(m_buffer)[1]++;
136  }
137 
142  void set_incoming_seq_no(uint32_t n)
143  {
144  reinterpret_cast<uint32_t*>(m_buffer)[1] = n;
145  }
146 
151  uint32_t get_incoming_seq_no() const
152  {
153  return reinterpret_cast<uint32_t*>(m_buffer)[1];
154  }
155 
156  protected:
157  MemoryMappedFile m_memory_mapped_file;
158  char* m_buffer = nullptr;
159  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
160 };
161 
162 } // 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:142
llfix::SequenceStore::set_outgoing_seq_no
void set_outgoing_seq_no(uint32_t n)
Set the outgoing FIX sequence number.
Definition: sequence_store.h:118
llfix::SequenceStore::get_incoming_seq_no
uint32_t get_incoming_seq_no() const
Get the incoming FIX sequence number.
Definition: sequence_store.h:151
llfix::SequenceStore
Persistent FIX sequence number store backed by a memory-mapped file.
Definition: sequence_store.h:44
llfix::SequenceStore::get_outgoing_seq_no
uint32_t get_outgoing_seq_no() const
Get the outgoing FIX sequence number.
Definition: sequence_store.h:127
llfix::SequenceStore::reset_numbers
void reset_numbers()
Reset both incoming and outgoing sequence numbers to zero.
Definition: sequence_store.h:97