iceoryx_doc  1.0.1
unix_domain_socket.hpp
1 // Copyright (c) 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_UNIX_DOMAIN_SOCKET_HPP
17 #define IOX_UTILS_POSIX_WRAPPER_UNIX_DOMAIN_SOCKET_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 #include "iceoryx_utils/platform/un.hpp"
27 
28 namespace iox
29 {
30 namespace posix
31 {
33 class UnixDomainSocket : public DesignPattern::Creation<UnixDomainSocket, IpcChannelError>
34 {
35  public:
37  {
38  };
39  static constexpr NoPathPrefix_t NoPathPrefix{};
40  static constexpr char PATH_PREFIX[] = "/tmp/";
41 
44  static constexpr size_t MAX_MESSAGE_SIZE = 2048U;
45  static constexpr size_t SHORTEST_VALID_NAME = 2U;
46  static constexpr size_t NULL_TERMINATOR_SIZE = 1;
48  static constexpr size_t LONGEST_VALID_NAME = sizeof(sockaddr_un::sun_path) - 1;
49  static constexpr int32_t ERROR_CODE = -1;
50  static constexpr int32_t INVALID_FD = -1;
51 
53 
55  friend class DesignPattern::Creation<UnixDomainSocket, IpcChannelError>;
56 
60  UnixDomainSocket() noexcept;
61 
62  UnixDomainSocket(const UnixDomainSocket& other) = delete;
63  UnixDomainSocket(UnixDomainSocket&& other) noexcept;
64  UnixDomainSocket& operator=(const UnixDomainSocket& other) = delete;
65  UnixDomainSocket& operator=(UnixDomainSocket&& other) noexcept;
66 
67  ~UnixDomainSocket() noexcept;
68 
72  static cxx::expected<bool, IpcChannelError> unlinkIfExists(const UdsName_t& name) noexcept;
73 
78  static cxx::expected<bool, IpcChannelError> unlinkIfExists(const NoPathPrefix_t, const UdsName_t& name) noexcept;
79 
81  cxx::expected<IpcChannelError> destroy() noexcept;
82 
86  cxx::expected<IpcChannelError> send(const std::string& msg) const noexcept;
87 
92  cxx::expected<IpcChannelError> timedSend(const std::string& msg, const units::Duration& timeout) const noexcept;
93 
96  cxx::expected<std::string, IpcChannelError> receive() const noexcept;
97 
101  cxx::expected<std::string, IpcChannelError> timedReceive(const units::Duration& timeout) const noexcept;
102 
105  cxx::expected<bool, IpcChannelError> isOutdated() noexcept;
106 
107  private:
115  const IpcChannelMode mode,
116  const IpcChannelSide channelSide,
117  const size_t maxMsgSize = MAX_MESSAGE_SIZE,
118  const uint64_t maxMsgNumber = 10U) noexcept;
119 
128  const UdsName_t& name,
129  const IpcChannelMode mode,
130  const IpcChannelSide channelSide,
131  const size_t maxMsgSize = MAX_MESSAGE_SIZE,
132  const uint64_t maxMsgNumber = 10U) noexcept;
133 
134 
138  cxx::expected<IpcChannelError> initalizeSocket(const IpcChannelMode mode) noexcept;
139 
142  IpcChannelError convertErrnoToIpcChannelError(const int32_t errnum) const noexcept;
143 
144  static bool isNameValid(const UdsName_t& name) noexcept;
145 
148  cxx::expected<IpcChannelError> closeFileDescriptor() noexcept;
149 
150  private:
151  UdsName_t m_name;
152  IpcChannelSide m_channelSide;
153  int32_t m_sockfd{INVALID_FD};
154  struct sockaddr_un m_sockAddr;
155  size_t m_maxMessageSize{MAX_MESSAGE_SIZE};
156 };
157 } // namespace posix
158 } // namespace iox
159 
160 #endif // IOX_UTILS_POSIX_WRAPPER_UNIX_DOMAIN_SOCKET_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 unix domain socket.
Definition: unix_domain_socket.hpp:34
cxx::expected< IpcChannelError > timedSend(const std::string &msg, const units::Duration &timeout) const noexcept
try to send a message for a given timeout duration using std::string
static constexpr size_t LONGEST_VALID_NAME
The name length is limited by the size of the sockaddr_un::sun_path buffer and the path prefix.
Definition: unix_domain_socket.hpp:48
static cxx::expected< bool, IpcChannelError > unlinkIfExists(const UdsName_t &name) noexcept
unlink the provided unix domain socket
static constexpr size_t MAX_MESSAGE_SIZE
Max message size is on linux = 4096 and on mac os = 2048. To have the same behavior on every platform...
Definition: unix_domain_socket.hpp:44
cxx::expected< bool, IpcChannelError > isOutdated() noexcept
checks whether the unix domain socket is outdated
cxx::expected< std::string, IpcChannelError > receive() const noexcept
receive message using std::string.
UnixDomainSocket() noexcept
default constructor. The result is an invalid UnixDomainSocket object which can be reassigned later b...
cxx::expected< IpcChannelError > send(const std::string &msg) const noexcept
send a message using std::string.
cxx::expected< std::string, IpcChannelError > timedReceive(const units::Duration &timeout) const noexcept
try to receive message for a given timeout duration using std::string.
cxx::expected< IpcChannelError > destroy() noexcept
close the unix domain socket.
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:28
Definition: unix_domain_socket.hpp:37