-
Notifications
You must be signed in to change notification settings - Fork 396
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2016 from elBoberido/iox-1036-builder-pattern-for…
…-unix-domain-socket iox-#1036 builder pattern for unix domain socket
- Loading branch information
Showing
11 changed files
with
267 additions
and
265 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
// Copyright (c) 2020 by Robert Bosch GmbH. All rights reserved. | ||
// Copyright (c) 2020 - 2021 by Apex.AI Inc. All rights reserved. | ||
// Copyright (c) 2023 by Mathias Kraus <[email protected]>. 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. | ||
|
@@ -24,13 +25,16 @@ | |
#include "iceoryx_platform/socket.hpp" | ||
#include "iceoryx_platform/stat.hpp" | ||
#include "iceoryx_platform/un.hpp" | ||
#include "iox/builder.hpp" | ||
#include "iox/duration.hpp" | ||
#include "iox/optional.hpp" | ||
|
||
namespace iox | ||
{ | ||
namespace posix | ||
{ | ||
class UnixDomainSocketBuilder; | ||
|
||
/// @brief Wrapper class for unix domain socket | ||
class UnixDomainSocket | ||
{ | ||
|
@@ -41,9 +45,12 @@ class UnixDomainSocket | |
static constexpr NoPathPrefix_t NoPathPrefix{}; | ||
static constexpr uint64_t NULL_TERMINATOR_SIZE = 1U; | ||
static constexpr uint64_t MAX_MESSAGE_SIZE = platform::IOX_UDS_SOCKET_MAX_MESSAGE_SIZE - NULL_TERMINATOR_SIZE; | ||
static constexpr uint64_t MAX_NUMBER_OF_MESSAGES = 10; | ||
/// @brief The name length is limited by the size of the sockaddr_un::sun_path buffer and the IOX_SOCKET_PATH_PREFIX | ||
static constexpr size_t LONGEST_VALID_NAME = sizeof(sockaddr_un::sun_path) - 1; | ||
|
||
using Builder_t = UnixDomainSocketBuilder; | ||
|
||
using UdsName_t = string<LONGEST_VALID_NAME>; | ||
using Message_t = string<MAX_MESSAGE_SIZE>; | ||
|
||
|
@@ -55,18 +62,6 @@ class UnixDomainSocket | |
|
||
~UnixDomainSocket() noexcept; | ||
|
||
/// @brief factory method which guarantees that either a working object is produced | ||
/// or an error value describing the error during construction | ||
/// @tparam Targs the argument types which will be forwarded to the ctor | ||
/// @param[in] args the argument values which will be forwarded to the ctor | ||
/// @return returns an expected which either contains the object in a valid | ||
/// constructed state or an error value stating why the construction failed. | ||
template <typename... Targs> | ||
static expected<UnixDomainSocket, IpcChannelError> create(Targs&&... args) noexcept; | ||
|
||
/// @brief returns true if the object was constructed successfully, otherwise false | ||
bool isInitialized() const noexcept; | ||
|
||
/// @brief unlink the provided unix domain socket | ||
/// @param name of the unix domain socket to unlink | ||
/// @return true if the unix domain socket could be unlinked, false otherwise, IpcChannelError if error occured | ||
|
@@ -99,51 +94,77 @@ class UnixDomainSocket | |
expected<std::string, IpcChannelError> timedReceive(const units::Duration& timeout) const noexcept; | ||
|
||
private: | ||
UnixDomainSocket(const IpcChannelName_t& name, | ||
const IpcChannelSide channelSide, | ||
const uint64_t maxMsgSize = MAX_MESSAGE_SIZE, | ||
const uint64_t maxMsgNumber = 10U) noexcept; | ||
friend class UnixDomainSocketBuilderNoPathPrefix; | ||
|
||
UnixDomainSocket(const NoPathPrefix_t, | ||
const UdsName_t& name, | ||
UnixDomainSocket(const UdsName_t& name, | ||
const IpcChannelSide channelSide, | ||
const uint64_t maxMsgSize = MAX_MESSAGE_SIZE, | ||
const uint64_t maxMsgNumber = 10U) noexcept; | ||
const int32_t sockfd, | ||
const sockaddr_un sockAddr, | ||
const uint64_t maxMsgSize) noexcept; | ||
|
||
expected<void, IpcChannelError> destroy() noexcept; | ||
|
||
expected<void, IpcChannelError> initalizeSocket() noexcept; | ||
|
||
IpcChannelError convertErrnoToIpcChannelError(const int32_t errnum) const noexcept; | ||
IpcChannelError errnoToEnum(const int32_t errnum) const noexcept; | ||
static IpcChannelError errnoToEnum(const UdsName_t& name, const int32_t errnum) noexcept; | ||
|
||
expected<void, IpcChannelError> closeFileDescriptor() noexcept; | ||
static expected<void, IpcChannelError> closeFileDescriptor(const UdsName_t& name, | ||
const int sockfd, | ||
const sockaddr_un& sockAddr, | ||
IpcChannelSide channelSide) noexcept; | ||
|
||
private: | ||
static constexpr int32_t ERROR_CODE = -1; | ||
static constexpr int32_t INVALID_FD = -1; | ||
|
||
bool m_isInitialized{false}; | ||
IpcChannelError m_errorValue{IpcChannelError::NOT_INITIALIZED}; | ||
|
||
UdsName_t m_name; | ||
IpcChannelSide m_channelSide = IpcChannelSide::CLIENT; | ||
int32_t m_sockfd{INVALID_FD}; | ||
sockaddr_un m_sockAddr{}; | ||
uint64_t m_maxMessageSize{MAX_MESSAGE_SIZE}; | ||
}; | ||
|
||
class UnixDomainSocketBuilder | ||
{ | ||
/// @brief Defines the socket name | ||
IOX_BUILDER_PARAMETER(IpcChannelName_t, name, "") | ||
|
||
/// @brief Defines how the socket is opened, i.e. as client or server | ||
IOX_BUILDER_PARAMETER(IpcChannelSide, channelSide, IpcChannelSide::CLIENT) | ||
|
||
/// @brief Defines the max message size of the socket | ||
IOX_BUILDER_PARAMETER(uint64_t, maxMsgSize, UnixDomainSocket::MAX_MESSAGE_SIZE) | ||
|
||
/// @brief Defines the max number of messages for the socket. | ||
IOX_BUILDER_PARAMETER(uint64_t, maxMsgNumber, UnixDomainSocket::MAX_NUMBER_OF_MESSAGES) | ||
|
||
template <typename... Targs> | ||
expected<UnixDomainSocket, IpcChannelError> UnixDomainSocket::create(Targs&&... args) noexcept | ||
public: | ||
/// @brief create a unix domain socket | ||
/// @return On success a 'UnixDomainSocket' is returned and on failure an 'IpcChannelError'. | ||
expected<UnixDomainSocket, IpcChannelError> create() const noexcept; | ||
}; | ||
|
||
class UnixDomainSocketBuilderNoPathPrefix | ||
{ | ||
UnixDomainSocket newObject{std::forward<Targs>(args)...}; | ||
if (!newObject.m_isInitialized) | ||
{ | ||
return err(newObject.m_errorValue); | ||
} | ||
/// @brief Defines the socket name | ||
IOX_BUILDER_PARAMETER(UnixDomainSocket::UdsName_t, name, "") | ||
|
||
/// @brief Defines how the socket is opened, i.e. as client or server | ||
IOX_BUILDER_PARAMETER(IpcChannelSide, channelSide, IpcChannelSide::CLIENT) | ||
|
||
return ok(std::move(newObject)); | ||
} | ||
/// @brief Defines the max message size of the socket | ||
IOX_BUILDER_PARAMETER(uint64_t, maxMsgSize, UnixDomainSocket::MAX_MESSAGE_SIZE) | ||
|
||
/// @brief Defines the max number of messages for the socket. | ||
IOX_BUILDER_PARAMETER(uint64_t, maxMsgNumber, UnixDomainSocket::MAX_NUMBER_OF_MESSAGES) | ||
|
||
public: | ||
/// @brief create a unix domain socket | ||
/// @return On success a 'UnixDomainSocket' is returned and on failure an 'IpcChannelError'. | ||
expected<UnixDomainSocket, IpcChannelError> create() const noexcept; | ||
}; | ||
|
||
} // namespace posix | ||
} // namespace iox | ||
|
Oops, something went wrong.