Skip to content

iceoryx_posh/internal/runtime/ipc_interface_base.hpp🔗

Namespaces🔗

Name
iox
iox::runtime

Classes🔗

Name
class iox::runtime::IpcInterfaceBase
Base-Class should never be used by the end-user. Handles the common properties and methods for the childs. The handling of the IPC channels must be done by the children.

Source code🔗

// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 by 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_POSH_RUNTIME_IPC_INTERFACE_BASE_HPP
#define IOX_POSH_RUNTIME_IPC_INTERFACE_BASE_HPP

#include "iceoryx_posh/iceoryx_posh_types.hpp"
#include "iceoryx_posh/internal/runtime/ipc_message.hpp"
#include "iceoryx_utils/cxx/deadline_timer.hpp"
#include "iceoryx_utils/cxx/optional.hpp"
#include "iceoryx_utils/internal/posix_wrapper/unix_domain_socket.hpp"
#include "iceoryx_utils/internal/relocatable_pointer/relative_pointer.hpp"
#include "iceoryx_utils/internal/units/duration.hpp"
#include "iceoryx_utils/platform/fcntl.hpp"
#include "iceoryx_utils/platform/stat.hpp"
#include "iceoryx_utils/platform/types.hpp"
#include "iceoryx_utils/platform/unistd.hpp"

#include <cstdint>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>

#if defined(QNX) || defined(QNX__) || defined(__QNX__)
#include <process.h>
#endif

namespace iox
{
namespace runtime
{
enum class IpcMessageType : int32_t
{
    BEGIN = -1,
    NOTYPE = 0,
    REG, // register app
    REG_ACK,
    CREATE_PUBLISHER,
    CREATE_PUBLISHER_ACK,
    CREATE_SUBSCRIBER,
    CREATE_SUBSCRIBER_ACK,
    CREATE_INTERFACE,
    CREATE_INTERFACE_ACK,
    CREATE_APPLICATION,
    CREATE_APPLICATION_ACK,
    CREATE_CONDITION_VARIABLE,
    CREATE_CONDITION_VARIABLE_ACK,
    CREATE_NODE,
    CREATE_NODE_ACK,
    FIND_SERVICE,
    KEEPALIVE,
    TERMINATION,
    TERMINATION_ACK,
    PREPARE_APP_TERMINATION,
    PREPARE_APP_TERMINATION_ACK,
    ERROR,
    APP_WAIT,
    WAKEUP_TRIGGER,
    REPLAY,
    SERVICE_REGISTRY_CHANGE_COUNTER,
    MESSAGE_NOT_SUPPORTED,
    // etc..
    END,
};

enum class IpcMessageErrorType : int32_t
{
    BEGIN,
    INVALID_STATE,
    NOTYPE,
    NO_UNIQUE_CREATED,
    REQUEST_PUBLISHER_WRONG_IPC_MESSAGE_RESPONSE,
    REQUEST_PUBLISHER_NO_WRITABLE_SHM_SEGMENT,
    REQUEST_SUBSCRIBER_WRONG_IPC_MESSAGE_RESPONSE,
    REQUEST_CONDITION_VARIABLE_WRONG_IPC_MESSAGE_RESPONSE,
    REQUEST_EVENT_VARIABLE_WRONG_IPC_MESSAGE_RESPONSE,
    PUBLISHER_LIST_FULL,
    SUBSCRIBER_LIST_FULL,
    CONDITION_VARIABLE_LIST_FULL,
    EVENT_VARIABLE_LIST_FULL,
    NODE_DATA_LIST_FULL,
    END,
};


IpcMessageType stringToIpcMessageType(const char* str) noexcept;

std::string IpcMessageTypeToString(const IpcMessageType msg) noexcept;

IpcMessageErrorType stringToIpcMessageErrorType(const char* str) noexcept;
std::string IpcMessageErrorTypeToString(const IpcMessageErrorType msg) noexcept;

class IpcInterfaceUser;
class IpcInterfaceCreator;

class IpcInterfaceBase
{
  public:
    bool receive(IpcMessage& answer) const noexcept;

    bool timedReceive(const units::Duration timeout, IpcMessage& answer) const noexcept;

    bool send(const IpcMessage& msg) const noexcept;

    bool timedSend(const IpcMessage& msg, const units::Duration timeout) const noexcept;

    const RuntimeName_t& getRuntimeName() const noexcept;

    bool isInitialized() const noexcept;

    static void cleanupOutdatedIpcChannel(const RuntimeName_t& name) noexcept;

    friend class IpcInterfaceUser;
    friend class IpcInterfaceCreator;
    friend class IpcRuntimeInterface;

  protected:
    bool reopen() noexcept;

    bool ipcChannelMapsToFile() noexcept;

    IpcInterfaceBase() = delete;

    IpcInterfaceBase(const RuntimeName_t& runtimeName, const uint64_t maxMessages, const uint64_t messageSize) noexcept;
    virtual ~IpcInterfaceBase() noexcept = default;

    IpcInterfaceBase(const IpcInterfaceBase&) = delete;
    IpcInterfaceBase(IpcInterfaceBase&&) = delete;
    IpcInterfaceBase& operator=(const IpcInterfaceBase&) = delete;
    IpcInterfaceBase& operator=(IpcInterfaceBase&&) = delete;

    static bool setMessageFromString(const char* buffer, IpcMessage& answer) noexcept;

    bool openIpcChannel(const posix::IpcChannelSide channelSide) noexcept;

    bool closeIpcChannel() noexcept;

    bool hasClosableIpcChannel() const noexcept;

  protected:
    RuntimeName_t m_runtimeName;
    uint64_t m_maxMessageSize{0U};
    uint64_t m_maxMessages{0U};
    iox::posix::IpcChannelSide m_channelSide{posix::IpcChannelSide::CLIENT};
    IpcChannelType m_ipcChannel;
};

} // namespace runtime
} // namespace iox

#endif // IOX_POSH_RUNTIME_IPC_INTERFACE_BASE_HPP

Updated on 26 April 2021 at 15:31:02 CEST