Skip to content

iceoryx_utils/internal/posix_wrapper/unix_domain_socket.hpp🔗

Namespaces🔗

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

Classes🔗

Name
class iox::posix::UnixDomainSocket
Wrapper class for unix domain socket.
struct iox::posix::UnixDomainSocket::NoPathPrefix_t

Source code🔗

// Copyright (c) 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_UNIX_DOMAIN_SOCKET_HPP
#define IOX_UTILS_POSIX_WRAPPER_UNIX_DOMAIN_SOCKET_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 "iceoryx_utils/platform/un.hpp"

namespace iox
{
namespace posix
{
class UnixDomainSocket : public DesignPattern::Creation<UnixDomainSocket, IpcChannelError>
{
  public:
    struct NoPathPrefix_t
    {
    };
    static constexpr NoPathPrefix_t NoPathPrefix{};
    static constexpr char PATH_PREFIX[] = "/tmp/";

    static constexpr size_t MAX_MESSAGE_SIZE = 2048U;
    static constexpr size_t SHORTEST_VALID_NAME = 2U;
    static constexpr size_t NULL_TERMINATOR_SIZE = 1;
    static constexpr size_t LONGEST_VALID_NAME = sizeof(sockaddr_un::sun_path) - 1;
    static constexpr int32_t ERROR_CODE = -1;
    static constexpr int32_t INVALID_FD = -1;

    using UdsName_t = cxx::string<LONGEST_VALID_NAME>;

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

    UnixDomainSocket() noexcept;

    UnixDomainSocket(const UnixDomainSocket& other) = delete;
    UnixDomainSocket(UnixDomainSocket&& other) noexcept;
    UnixDomainSocket& operator=(const UnixDomainSocket& other) = delete;
    UnixDomainSocket& operator=(UnixDomainSocket&& other) noexcept;

    ~UnixDomainSocket() noexcept;

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

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

    cxx::expected<IpcChannelError> destroy() noexcept;

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

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

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

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

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

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

    UnixDomainSocket(const NoPathPrefix_t,
                     const UdsName_t& name,
                     const IpcChannelMode mode,
                     const IpcChannelSide channelSide,
                     const size_t maxMsgSize = MAX_MESSAGE_SIZE,
                     const uint64_t maxMsgNumber = 10U) noexcept;


    cxx::expected<IpcChannelError> initalizeSocket(const IpcChannelMode mode) noexcept;

    IpcChannelError convertErrnoToIpcChannelError(const int32_t errnum) const noexcept;

    static bool isNameValid(const UdsName_t& name) noexcept;

    cxx::expected<IpcChannelError> closeFileDescriptor() noexcept;

  private:
    UdsName_t m_name;
    IpcChannelSide m_channelSide;
    int32_t m_sockfd{INVALID_FD};
    struct sockaddr_un m_sockAddr;
    size_t m_maxMessageSize{MAX_MESSAGE_SIZE};
};
} // namespace posix
} // namespace iox

#endif // IOX_UTILS_POSIX_WRAPPER_UNIX_DOMAIN_SOCKET_HPP

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