iceoryx_doc  1.0.1
message_queue.hpp
1 // Copyright (c) 2019, 2020 by Robert Bosch GmbH, Apex.AI Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // SPDX-License-Identifier: Apache-2.0
16 #ifndef IOX_UTILS_POSIX_WRAPPER_MESSAGE_QUEUE_HPP
17 #define IOX_UTILS_POSIX_WRAPPER_MESSAGE_QUEUE_HPP
18 
19 #include "iceoryx_utils/cxx/optional.hpp"
20 #include "iceoryx_utils/design_pattern/creation.hpp"
21 #include "iceoryx_utils/internal/posix_wrapper/ipc_channel.hpp"
22 #include "iceoryx_utils/internal/units/duration.hpp"
23 #include "iceoryx_utils/platform/fcntl.hpp"
24 #include "iceoryx_utils/platform/mqueue.hpp"
25 #include "iceoryx_utils/platform/stat.hpp"
26 
27 #include <iostream>
28 
29 namespace iox
30 {
31 namespace posix
32 {
33 
49 class MessageQueue : public DesignPattern::Creation<MessageQueue, IpcChannelError>
50 {
51  public:
52  static constexpr mqd_t INVALID_DESCRIPTOR = -1;
53  static constexpr int32_t ERROR_CODE = -1;
54  static constexpr size_t SHORTEST_VALID_QUEUE_NAME = 2;
55  static constexpr size_t NULL_TERMINATOR_SIZE = 1;
56  static constexpr size_t MAX_MESSAGE_SIZE = 4096;
57 
59  friend class DesignPattern::Creation<MessageQueue, IpcChannelError>;
60 
64 
65  MessageQueue(const MessageQueue& other) = delete;
66  MessageQueue(MessageQueue&& other);
67  MessageQueue& operator=(const MessageQueue& other) = delete;
68  MessageQueue& operator=(MessageQueue&& other);
69 
70  ~MessageQueue();
71 
72  static cxx::expected<bool, IpcChannelError> unlinkIfExists(const IpcChannelName_t& name);
73 
75  cxx::expected<IpcChannelError> destroy();
76 
79  cxx::expected<IpcChannelError> send(const std::string& msg) const;
80 
82 
85  cxx::expected<std::string, IpcChannelError> receive() const;
86 
90  cxx::expected<std::string, IpcChannelError> timedReceive(const units::Duration& timeout) const;
91 
93  cxx::expected<IpcChannelError> timedSend(const std::string& msg, const units::Duration& timeout) const;
94 
95  cxx::expected<bool, IpcChannelError> isOutdated();
96 
97  private:
98  MessageQueue(const IpcChannelName_t& name,
99  const IpcChannelMode mode,
100  const IpcChannelSide channelSide,
101  const size_t maxMsgSize = MAX_MESSAGE_SIZE,
102  const uint64_t maxMsgNumber = 10u);
103 
104  cxx::expected<int32_t, IpcChannelError>
105  open(const IpcChannelName_t& name, const IpcChannelMode mode, const IpcChannelSide channelSide);
106 
107  cxx::expected<IpcChannelError> close();
108  cxx::expected<IpcChannelError> unlink();
109  cxx::error<IpcChannelError> createErrorFromErrnum(const int32_t errnum) const;
110  static cxx::error<IpcChannelError> createErrorFromErrnum(const IpcChannelName_t& name, const int32_t errnum);
111  static cxx::expected<IpcChannelName_t, IpcChannelError>
112  sanitizeIpcChannelName(const IpcChannelName_t& name) noexcept;
113 
114  private:
115  IpcChannelName_t m_name;
116  struct mq_attr m_attributes;
117  mqd_t m_mqDescriptor = INVALID_DESCRIPTOR;
118  IpcChannelSide m_channelSide;
119 
120 #ifdef __QNX__
121  static constexpr int TIMEOUT_ERRNO = EINTR;
122 #else
123  static constexpr int TIMEOUT_ERRNO = ETIMEDOUT;
124 #endif
125  // read/write permissions
126  static constexpr mode_t m_filemode{S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH};
127 };
128 } // namespace posix
129 } // namespace iox
130 
131 #endif // IOX_UTILS_POSIX_WRAPPER_MESSAGE_QUEUE_HPP
This pattern can be used if you write an abstraction where you have to throw an exception in the cons...
Definition: creation.hpp:99
Wrapper class for posix message queue.
Definition: message_queue.hpp:50
cxx::expected< std::string, IpcChannelError > receive() const
receive message from queue using std::string.
cxx::expected< std::string, IpcChannelError > timedReceive(const units::Duration &timeout) const
try to receive message from queue for a given timeout duration using std::string. Only defined for NO...
cxx::expected< IpcChannelError > destroy()
close and remove message queue.
cxx::expected< IpcChannelError > timedSend(const std::string &msg, const units::Duration &timeout) const
try to send a message to the queue for a given timeout duration using std::string
cxx::expected< IpcChannelError > send(const std::string &msg) const
send a message to queue using std::string.
Definition: duration.hpp:77
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:28
helper struct to create an expected which is signalling an error more easily
Definition: expected.hpp:108