Skip to content

iceoryx_utils/internal/posix_wrapper/message_queue.hpp🔗

Namespaces🔗

Name
iox
building block to easily create free function for logging in a library context
iox::posix

Classes🔗

Name
class iox::posix::MessageQueue
Wrapper class for posix message queue.

Source code🔗

// Copyright (c) 2019, 2020 by Robert Bosch GmbH, Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
#ifndef IOX_UTILS_POSIX_WRAPPER_MESSAGE_QUEUE_HPP
#define IOX_UTILS_POSIX_WRAPPER_MESSAGE_QUEUE_HPP

#include "iceoryx_utils/cxx/optional.hpp"
#include "iceoryx_utils/design_pattern/creation.hpp"
#include "iceoryx_utils/internal/posix_wrapper/ipc_channel.hpp"
#include "iceoryx_utils/internal/units/duration.hpp"
#include "iceoryx_utils/platform/fcntl.hpp"
#include "iceoryx_utils/platform/mqueue.hpp"
#include "iceoryx_utils/platform/stat.hpp"

#include <iostream>

namespace iox
{
namespace posix
{

class MessageQueue : public DesignPattern::Creation<MessageQueue, IpcChannelError>
{
  public:
    static constexpr mqd_t INVALID_DESCRIPTOR = -1;
    static constexpr int32_t ERROR_CODE = -1;
    static constexpr size_t SHORTEST_VALID_QUEUE_NAME = 2;
    static constexpr size_t NULL_TERMINATOR_SIZE = 1;
    static constexpr size_t MAX_MESSAGE_SIZE = 4096;

    friend class DesignPattern::Creation<MessageQueue, IpcChannelError>;

    MessageQueue();

    MessageQueue(const MessageQueue& other) = delete;
    MessageQueue(MessageQueue&& other);
    MessageQueue& operator=(const MessageQueue& other) = delete;
    MessageQueue& operator=(MessageQueue&& other);

    ~MessageQueue();

    static cxx::expected<bool, IpcChannelError> unlinkIfExists(const IpcChannelName_t& name);

    cxx::expected<IpcChannelError> destroy();

    cxx::expected<IpcChannelError> send(const std::string& msg) const;


    cxx::expected<std::string, IpcChannelError> receive() const;

    cxx::expected<std::string, IpcChannelError> timedReceive(const units::Duration& timeout) const;

    cxx::expected<IpcChannelError> timedSend(const std::string& msg, const units::Duration& timeout) const;

    cxx::expected<bool, IpcChannelError> isOutdated();

  private:
    MessageQueue(const IpcChannelName_t& name,
                 const IpcChannelMode mode,
                 const IpcChannelSide channelSide,
                 const size_t maxMsgSize = MAX_MESSAGE_SIZE,
                 const uint64_t maxMsgNumber = 10u);

    cxx::expected<int32_t, IpcChannelError>
    open(const IpcChannelName_t& name, const IpcChannelMode mode, const IpcChannelSide channelSide);

    cxx::expected<IpcChannelError> close();
    cxx::expected<IpcChannelError> unlink();
    cxx::error<IpcChannelError> createErrorFromErrnum(const int32_t errnum) const;
    static cxx::error<IpcChannelError> createErrorFromErrnum(const IpcChannelName_t& name, const int32_t errnum);
    static cxx::expected<IpcChannelName_t, IpcChannelError>
    sanitizeIpcChannelName(const IpcChannelName_t& name) noexcept;

  private:
    IpcChannelName_t m_name;
    struct mq_attr m_attributes;
    mqd_t m_mqDescriptor = INVALID_DESCRIPTOR;
    IpcChannelSide m_channelSide;

#ifdef __QNX__
    static constexpr int TIMEOUT_ERRNO = EINTR;
#else
    static constexpr int TIMEOUT_ERRNO = ETIMEDOUT;
#endif
    // read/write permissions
    static constexpr mode_t m_filemode{S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH};
};
} // namespace posix
} // namespace iox

#endif // IOX_UTILS_POSIX_WRAPPER_MESSAGE_QUEUE_HPP

Updated on 17 June 2021 at 11:15:26 CEST