Skip to content

Commit

Permalink
iox-eclipse-iceoryx#381 Change return value of createSocket() and ren…
Browse files Browse the repository at this point in the history
…ame it to initalizeSocket()

Signed-off-by: Simon Hoinkis <simon.hoinkis@apex.ai>
  • Loading branch information
mossmaurice committed Feb 11, 2021
1 parent dc55e69 commit bf023ee
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class UnixDomainSocket : public DesignPattern::Creation<UnixDomainSocket, IpcCha
/// @brief creates the unix domain socket
/// @param mode blocking or non_blocking
/// @return int with the socket file descriptor, IpcChannelError if error occured
cxx::expected<int32_t, IpcChannelError> createSocket(const IpcChannelMode mode) noexcept;
cxx::expected<IpcChannelError> initalizeSocket(const IpcChannelMode mode) noexcept;

/// @brief create an IpcChannelError from the provides error code
/// @return IpcChannelError if error occured
Expand All @@ -143,7 +143,7 @@ class UnixDomainSocket : public DesignPattern::Creation<UnixDomainSocket, IpcCha
/// @brief Tries to close the given file descriptor
/// @param[in] file descriptor that shall be closed
/// @return IpcChannelError if error occured
cxx::expected<IpcChannelError> closeFileDescriptor(const int32_t fileDescriptor) noexcept;
cxx::expected<IpcChannelError> closeFileDescriptor() noexcept;

private:
UdsName_t m_name;
Expand Down
34 changes: 16 additions & 18 deletions iceoryx_utils/source/posix_wrapper/unix_domain_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,16 @@ UnixDomainSocket::UnixDomainSocket(const NoPathPrefix_t,
else
{
m_maxMessageSize = maxMsgSize;
auto createResult = createSocket(mode);
auto initalizeResult = initalizeSocket(mode);

if (!createResult.has_error())
if (!initalizeResult.has_error())
{
this->m_isInitialized = true;
this->m_errorValue = IpcChannelError::UNDEFINED;
this->m_sockfd = createResult.value();
}
else
{
this->m_isInitialized = false;
this->m_errorValue = createResult.get_error();
this->m_errorValue = initalizeResult.get_error();
}
}
}
Expand Down Expand Up @@ -163,10 +161,10 @@ cxx::expected<bool, IpcChannelError> UnixDomainSocket::unlinkIfExists(const NoPa
}
}

cxx::expected<IpcChannelError> UnixDomainSocket::closeFileDescriptor(const int32_t fileDescriptor) noexcept
cxx::expected<IpcChannelError> UnixDomainSocket::closeFileDescriptor() noexcept
{
auto closeCall = cxx::makeSmartC(
closePlatformFileHandle, cxx::ReturnMode::PRE_DEFINED_ERROR_CODE, {ERROR_CODE}, {}, fileDescriptor);
auto closeCall =
cxx::makeSmartC(closePlatformFileHandle, cxx::ReturnMode::PRE_DEFINED_ERROR_CODE, {ERROR_CODE}, {}, m_sockfd);

if (!closeCall.hasErrors())
{
Expand All @@ -190,7 +188,7 @@ cxx::expected<IpcChannelError> UnixDomainSocket::destroy() noexcept
{
if (m_isInitialized)
{
return closeFileDescriptor(m_sockfd);
return closeFileDescriptor();
}

return cxx::success<void>();
Expand Down Expand Up @@ -336,7 +334,7 @@ cxx::expected<std::string, IpcChannelError> UnixDomainSocket::timedReceive(const
}


cxx::expected<int32_t, IpcChannelError> UnixDomainSocket::createSocket(const IpcChannelMode mode) noexcept
cxx::expected<IpcChannelError> UnixDomainSocket::initalizeSocket(const IpcChannelMode mode) noexcept
{
// initialize the sockAddr data structure with the provided name
memset(&m_sockAddr, 0, sizeof(m_sockAddr));
Expand All @@ -363,7 +361,7 @@ cxx::expected<int32_t, IpcChannelError> UnixDomainSocket::createSocket(const Ipc
return createErrorFromErrnum(socketCall.getErrNum());
}

int32_t sockfd = socketCall.getReturnValue();
m_sockfd = socketCall.getReturnValue();

if (IpcChannelSide::SERVER == m_channelSide)
{
Expand All @@ -373,17 +371,17 @@ cxx::expected<int32_t, IpcChannelError> UnixDomainSocket::createSocket(const Ipc
cxx::ReturnMode::PRE_DEFINED_ERROR_CODE,
{ERROR_CODE},
{},
sockfd,
m_sockfd,
reinterpret_cast<struct sockaddr*>(&m_sockAddr),
static_cast<socklen_t>(sizeof(m_sockAddr)));

if (!bindCall.hasErrors())
{
return cxx::success<int32_t>(sockfd);
return cxx::success<>();
}
else
{
closeFileDescriptor(sockfd);
closeFileDescriptor();
// possible errors in closeFileDescriptor() are masked and we inform the user about the actual error
return createErrorFromErrnum(bindCall.getErrNum());
}
Expand All @@ -396,25 +394,25 @@ cxx::expected<int32_t, IpcChannelError> UnixDomainSocket::createSocket(const Ipc
cxx::ReturnMode::PRE_DEFINED_ERROR_CODE,
{ERROR_CODE},
{ENOENT},
sockfd,
m_sockfd,
(struct sockaddr*)&m_sockAddr,
static_cast<socklen_t>(sizeof(m_sockAddr)));

if (connectCall.hasErrors())
{
closeFileDescriptor(sockfd);
closeFileDescriptor();
// possible errors in closeFileDescriptor() are masked and we inform the user about the actual error
return createErrorFromErrnum(connectCall.getErrNum());
}
else if (connectCall.getErrNum() == ENOENT)
{
closeFileDescriptor(sockfd);
closeFileDescriptor();
// possible errors in closeFileDescriptor() are masked and we inform the user about the actual error
return createErrorFromErrnum(connectCall.getErrNum());
}
else
{
return cxx::success<int32_t>(sockfd);
return cxx::success<>();
}
}
}
Expand Down

0 comments on commit bf023ee

Please sign in to comment.