-
Notifications
You must be signed in to change notification settings - Fork 402
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
iox-eclipse-iceoryx#1693 support for iox::string in NamedPipe
By default, the NamedPipe class relies on std::string objects to send and receive data, as defined by its interface. However, this approach can potentially lead to dynamic memory allocation when handling larger data payloads that exceed the stack space optimization (SSO) limit. To address this issue, an alternative API has been introduced that enables data transmission and reception using iox::string. This approach allows for direct data manipulation within stack memory, effectively eliminating the need for dynamic memory allocation. Signed-off-by: Luca Bartoli <[email protected]>
- Loading branch information
1 parent
38e9b6b
commit 6c017e9
Showing
4 changed files
with
200 additions
and
57 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
128 changes: 128 additions & 0 deletions
128
iceoryx_hoofs/posix/ipc/include/iox/detail/named_pipe.inl
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 |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// Copyright 2024, Eclipse Foundation and the iceoryx contributors. 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_HOOFS_POSIX_IPC_NAMED_PIPE_INL | ||
#define IOX_HOOFS_POSIX_IPC_NAMED_PIPE_INL | ||
|
||
#include "iox/duration.hpp" | ||
#include "iox/into.hpp" | ||
#include "iox/named_pipe.hpp" | ||
|
||
namespace iox | ||
{ | ||
template <uint64_t N> | ||
expected<void, PosixIpcChannelError> NamedPipe::trySend(const iox::string<N>& message) const noexcept | ||
{ | ||
static_assert(N <= MAX_MESSAGE_SIZE, "Size exceeds transmission limit!"); | ||
|
||
auto result = m_data->sendSemaphore().tryWait(); | ||
IOX_EXPECTS(!result.has_error()); | ||
|
||
if (*result) | ||
{ | ||
IOX_DISCARD_RESULT(m_data->messages.push(message)); | ||
IOX_EXPECTS(!m_data->receiveSemaphore().post().has_error()); | ||
return ok(); | ||
} | ||
return err(PosixIpcChannelError::TIMEOUT); | ||
} | ||
|
||
template <uint64_t N> | ||
expected<void, PosixIpcChannelError> NamedPipe::send(const iox::string<N>& message) const noexcept | ||
{ | ||
static_assert(N <= MAX_MESSAGE_SIZE, "Size exceeds transmission limit!"); | ||
|
||
IOX_EXPECTS(!m_data->sendSemaphore().wait().has_error()); | ||
IOX_DISCARD_RESULT(m_data->messages.push(message)); | ||
IOX_EXPECTS(!m_data->receiveSemaphore().post().has_error()); | ||
|
||
return ok(); | ||
} | ||
|
||
template <uint64_t N> | ||
expected<void, PosixIpcChannelError> NamedPipe::timedSend(const iox::string<N>& message, | ||
const units::Duration& timeout) const noexcept | ||
{ | ||
static_assert(N <= MAX_MESSAGE_SIZE, "Size exceeds transmission limit!"); | ||
|
||
auto result = m_data->sendSemaphore().timedWait(timeout); | ||
IOX_EXPECTS(!result.has_error()); | ||
|
||
if (*result == SemaphoreWaitState::NO_TIMEOUT) | ||
{ | ||
IOX_DISCARD_RESULT(m_data->messages.push(message)); | ||
IOX_EXPECTS(!m_data->receiveSemaphore().post().has_error()); | ||
return ok(); | ||
} | ||
return err(PosixIpcChannelError::TIMEOUT); | ||
} | ||
|
||
template <uint64_t N> | ||
expected<void, PosixIpcChannelError> NamedPipe::receive(iox::string<N>& message) const noexcept | ||
{ | ||
IOX_EXPECTS(!m_data->receiveSemaphore().wait().has_error()); | ||
auto msg = m_data->messages.pop(); | ||
if (msg.has_value()) | ||
{ | ||
IOX_EXPECTS(!m_data->sendSemaphore().post().has_error()); | ||
message = *msg; | ||
return ok(); | ||
} | ||
return err(PosixIpcChannelError::INTERNAL_LOGIC_ERROR); | ||
} | ||
|
||
template <uint64_t N> | ||
expected<void, PosixIpcChannelError> NamedPipe::tryReceive(iox::string<N>& message) const noexcept | ||
{ | ||
auto result = m_data->receiveSemaphore().tryWait(); | ||
IOX_EXPECTS(!result.has_error()); | ||
|
||
if (*result) | ||
{ | ||
auto msg = m_data->messages.pop(); | ||
if (msg.has_value()) | ||
{ | ||
IOX_EXPECTS(!m_data->sendSemaphore().post().has_error()); | ||
message = *msg; | ||
} | ||
return err(PosixIpcChannelError::INTERNAL_LOGIC_ERROR); | ||
} | ||
|
||
return err(PosixIpcChannelError::TIMEOUT); | ||
} | ||
|
||
template <uint64_t N> | ||
expected<void, PosixIpcChannelError> NamedPipe::timedReceive(iox::string<N>& message, | ||
const units::Duration& timeout) const noexcept | ||
{ | ||
auto result = m_data->receiveSemaphore().timedWait(timeout); | ||
IOX_EXPECTS(!result.has_error()); | ||
|
||
if (*result == SemaphoreWaitState::NO_TIMEOUT) | ||
{ | ||
auto msg = m_data->messages.pop(); | ||
if (msg.has_value()) | ||
{ | ||
IOX_EXPECTS(!m_data->sendSemaphore().post().has_error()); | ||
message = *msg; | ||
return ok(); | ||
} | ||
return err(PosixIpcChannelError::INTERNAL_LOGIC_ERROR); | ||
} | ||
return err(PosixIpcChannelError::TIMEOUT); | ||
} | ||
} // namespace iox | ||
|
||
#endif |
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