CommRaT 2.0.0
C++20 Real-Time Messaging Framework
Loading...
Searching...
No Matches
tims_wrapper.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "../messages.hpp"
4#include "timestamp.hpp"
5#include <memory>
6#include <string>
7#include <functional>
8#include <optional>
9#include <atomic>
10#include <span>
11#include <array>
12
13// TIMS API includes
14#include <main/tims/tims.h>
15#include <main/tims/tims_api.h>
16
17namespace commrat {
18
19// TiMS configuration
20struct TimsConfig {
21 std::string mailbox_name;
22 uint32_t mailbox_id;
24 uint32_t priority;
26
28 : mailbox_name("default")
29 , mailbox_id(0)
30 , max_msg_size(4096)
31 , priority(0)
32 , realtime(false) {}
33};
34
35// Result type for operations
36enum class TimsResult {
37 SUCCESS = 0,
38 ERROR_INIT = -1,
39 ERROR_SEND = -2,
40 ERROR_RECEIVE = -3,
41 ERROR_TIMEOUT = -4,
44};
45
46// Modern C++ wrapper around TiMS with compile-time safety
48public:
49 explicit TimsWrapper(const TimsConfig& config);
51
52 // Delete copy, allow move
53 TimsWrapper(const TimsWrapper&) = delete;
56 TimsWrapper& operator=(TimsWrapper&&) noexcept;
57
58 // Initialize the mailbox
60
61 // Shutdown the mailbox
62 void shutdown();
63
64 // Send a message with compile-time type safety and SeRTial serialization
65 template<typename T>
66 TimsResult send(T& message, uint32_t dest_mailbox_id) {
67 static_assert(is_commrat_message_v<T>, "T must be a CommRaT message type");
68
69 if (!is_initialized_) {
71 }
72
73 // Serialize the message using SeRTial (returns Result with std::byte buffer)
74 auto result = serialize_message(message);
75
76 // Convert std::byte span to void* for TIMS
77 auto view = result.view();
78 return send_raw(view.data(), view.size(), dest_mailbox_id);
79 }
80
81 // Receive a message with compile-time type safety and SeRTial deserialization
82 template<typename T>
83 std::optional<T> receive(Milliseconds timeout = Milliseconds(0)) {
84 static_assert(is_commrat_message_v<T>, "T must be a CommRaT message type");
85
86 if (!is_initialized_) {
87 return std::nullopt;
88 }
89
90 // Use compile-time sized buffer from SeRTial
91 constexpr size_t buffer_size = sertial::Message<T>::max_buffer_size;
92 std::array<uint8_t, buffer_size> buffer;
93
94 ssize_t received_size = receive_raw(buffer.data(), buffer.size(), timeout);
95
96 if (received_size > 0) {
97 auto result = deserialize_message<T>(buffer.data(), static_cast<size_t>(received_size));
98 if (result) {
99 return *result; // DeserializeResult has operator*
100 }
101 }
102
103 return std::nullopt;
104 }
105
106 // Check if there's a message waiting
107 bool has_message() const;
108
109 // Get mailbox info
110 uint32_t get_mailbox_id() const { return config_.mailbox_id; }
111 const std::string& get_mailbox_name() const { return config_.mailbox_name; }
112 bool is_initialized() const { return is_initialized_; }
113
114 // Statistics
115 uint64_t get_messages_sent() const { return messages_sent_.load(); }
116 uint64_t get_messages_received() const { return messages_received_.load(); }
117
118 // Modern C++ interface using std::span<std::byte>
119 // Casting to void* happens here at the TiMS boundary
120 ssize_t receive_raw_bytes(std::span<std::byte> buffer, Milliseconds timeout) {
121 return receive_raw(buffer.data(), buffer.size(), timeout);
122 }
123
124private:
125 TimsResult send_raw(const void* data, size_t size, uint32_t dest_mailbox_id);
126 ssize_t receive_raw(void* buffer, size_t buffer_size, Milliseconds timeout);
127
128 TimsConfig config_;
129 int tims_fd_; // TIMS file descriptor
130 std::atomic<bool> is_initialized_;
131 std::atomic<uint64_t> messages_sent_;
132 std::atomic<uint64_t> messages_received_;
133 uint32_t sequence_number_;
134};
135
136} // namespace commrat
uint64_t get_messages_sent() const
const std::string & get_mailbox_name() const
bool has_message() const
TimsWrapper & operator=(const TimsWrapper &)=delete
uint64_t get_messages_received() const
ssize_t receive_raw_bytes(std::span< std::byte > buffer, Milliseconds timeout)
uint32_t get_mailbox_id() const
TimsWrapper(const TimsConfig &config)
std::optional< T > receive(Milliseconds timeout=Milliseconds(0))
TimsResult send(T &message, uint32_t dest_mailbox_id)
TimsResult initialize()
TimsWrapper(TimsWrapper &&) noexcept
bool is_initialized() const
TimsWrapper(const TimsWrapper &)=delete
CommRaT - Modern C++ Real-Time Communication Framework.
std::chrono::milliseconds Milliseconds
Definition timestamp.hpp:39
auto serialize_message(T &message) -> typename sertial::Message< T >::Result
Definition messages.hpp:209
std::string mailbox_name
Unified timestamp and time utility abstractions for CommRaT.