diff --git a/iceoryx_posh/include/iceoryx_posh/internal/popo/client_impl.hpp b/iceoryx_posh/include/iceoryx_posh/internal/popo/client_impl.hpp new file mode 100644 index 00000000000..6f8c40353e5 --- /dev/null +++ b/iceoryx_posh/include/iceoryx_posh/internal/popo/client_impl.hpp @@ -0,0 +1,63 @@ +// Copyright (c) 2022 by 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_POSH_POPO_CLIENT_IMPL_HPP +#define IOX_POSH_POPO_CLIENT_IMPL_HPP + +#include "iceoryx_posh/capro/service_description.hpp" +#include "iceoryx_posh/internal/popo/base_client.hpp" +#include "iceoryx_posh/internal/popo/request_deleter.hpp" +#include "iceoryx_posh/internal/popo/response_deleter.hpp" +#include "iceoryx_posh/internal/popo/rpc_interface.hpp" +#include "iceoryx_posh/popo/client_options.hpp" +#include "iceoryx_posh/popo/request.hpp" +#include "iceoryx_posh/popo/response.hpp" +#include "iceoryx_posh/popo/trigger_handle.hpp" +#include "iceoryx_posh/runtime/posh_runtime.hpp" + +namespace iox +{ +namespace popo +{ +template > +class ClientImpl : public BaseClientT, public RpcInterface> +{ + public: + explicit ClientImpl(const capro::ServiceDescription& service, const ClientOptions& clientOptions = {}) noexcept; + + template + cxx::expected, AllocationError> loan(Args&&... args) noexcept; + + void send(Request&& request) noexcept override; + + cxx::expected, ChunkReceiveResult> take() noexcept; + + private: + using BaseClientT::port; + + cxx::expected, AllocationError> loanUninitialized() noexcept; + + using RequestSampleDeleter = RequestDeleter; + RequestSampleDeleter m_requestDeleter{port()}; + using ResponseSampleDeleter = ResponseDeleter; + ResponseSampleDeleter m_responseDeleter{port()}; +}; +} // namespace popo +} // namespace iox + +#include "iceoryx_posh/internal/popo/client_impl.inl" + +#endif // IOX_POSH_POPO_CLIENT_IMPL_HPP diff --git a/iceoryx_posh/include/iceoryx_posh/internal/popo/client_impl.inl b/iceoryx_posh/include/iceoryx_posh/internal/popo/client_impl.inl new file mode 100644 index 00000000000..d8aff2a8d2a --- /dev/null +++ b/iceoryx_posh/include/iceoryx_posh/internal/popo/client_impl.inl @@ -0,0 +1,83 @@ +// Copyright (c) 2021 by 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_POSH_POPO_CLIENT_IMPL_INL +#define IOX_POSH_POPO_CLIENT_IMPL_INL + +#include "iceoryx_posh/internal/popo/client_impl.hpp" + +namespace iox +{ +namespace popo +{ +template +ClientImpl::ClientImpl(const capro::ServiceDescription& service, + const ClientOptions& clientOptions) noexcept + : BaseClientT(service, clientOptions) +{ +} + +template +cxx::expected, AllocationError> ClientImpl::loanUninitialized() noexcept +{ + auto result = port().allocateRequest(sizeof(Req), alignof(Req)); + if (result.has_error()) + { + return cxx::error(result.get_error()); + } + else + { + auto requestHeader = result.value(); + auto payload = mepoo::ChunkHeader::fromUserHeader(requestHeader)->userPayload(); + auto request = cxx::unique_ptr(reinterpret_cast(payload), m_requestDeleter); + return cxx::success>(Request{std::move(request), *this}); + } +} + +template +template +cxx::expected, AllocationError> ClientImpl::loan(Args&&... args) noexcept +{ + return std::move( + loanUninitialized().and_then([&](auto& request) { new (request.get()) Req(std::forward(args)...); })); +} + +template +void ClientImpl::send(Request&& request) noexcept +{ + auto payload = request.release(); // release the Request ownership of the chunk before publishing + auto* requestHeader = static_cast(mepoo::ChunkHeader::fromUserPayload(payload)->userHeader()); + port().sendRequest(requestHeader); +} + +template +cxx::expected, ChunkReceiveResult> ClientImpl::take() noexcept +{ + auto result = port().getResponse(); + if (result.has_error()) + { + return cxx::error(result.get_error()); + } + auto responseHeader = result.value(); + auto payload = mepoo::ChunkHeader::fromUserHeader(responseHeader)->userPayload(); + auto response = cxx::unique_ptr(static_cast(payload), m_responseDeleter); + return cxx::success>(Response{std::move(response)}); +} + +} // namespace popo +} // namespace iox + +#endif // IOX_POSH_POPO_CLIENT_IMPL_INL diff --git a/iceoryx_posh/include/iceoryx_posh/internal/popo/server_impl.hpp b/iceoryx_posh/include/iceoryx_posh/internal/popo/server_impl.hpp new file mode 100644 index 00000000000..14587ef7b49 --- /dev/null +++ b/iceoryx_posh/include/iceoryx_posh/internal/popo/server_impl.hpp @@ -0,0 +1,63 @@ +// Copyright (c) 2022 by 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_POSH_POPO_SERVER_IMPL_HPP +#define IOX_POSH_POPO_SERVER_IMPL_HPP + +#include "iceoryx_posh/capro/service_description.hpp" +#include "iceoryx_posh/internal/popo/base_server.hpp" +#include "iceoryx_posh/internal/popo/request_deleter.hpp" +#include "iceoryx_posh/internal/popo/response_deleter.hpp" +#include "iceoryx_posh/internal/popo/rpc_interface.hpp" +#include "iceoryx_posh/popo/request.hpp" +#include "iceoryx_posh/popo/response.hpp" +#include "iceoryx_posh/popo/server_options.hpp" +#include "iceoryx_posh/popo/trigger_handle.hpp" +#include "iceoryx_posh/runtime/posh_runtime.hpp" + +namespace iox +{ +namespace popo +{ +template > +class ServerImpl : public BaseServerT, public RpcInterface> +{ + public: + explicit ServerImpl(const capro::ServiceDescription& service, const ServerOptions& serverOptions = {}) noexcept; + + cxx::expected, ServerRequestResult> take() noexcept; + + template + cxx::expected, AllocationError> loan(const Request& request, Args&&... args) noexcept; + + void send(Response&& response) noexcept override; + + private: + using BaseServerT::port; + + cxx::expected, AllocationError> loanUninitialized(const Request& request) noexcept; + + using RequestSampleDeleter = RequestDeleter; + RequestSampleDeleter m_requestDeleter{port()}; + using ResponseSampleDeleter = ResponseDeleter; + ResponseSampleDeleter m_responseDeleter{port()}; +}; +} // namespace popo +} // namespace iox + +#include "iceoryx_posh/internal/popo/server_impl.inl" + +#endif // IOX_POSH_POPO_SERVER_IMPL_HPP diff --git a/iceoryx_posh/include/iceoryx_posh/internal/popo/server_impl.inl b/iceoryx_posh/include/iceoryx_posh/internal/popo/server_impl.inl new file mode 100644 index 00000000000..606946f3da9 --- /dev/null +++ b/iceoryx_posh/include/iceoryx_posh/internal/popo/server_impl.inl @@ -0,0 +1,86 @@ +// Copyright (c) 2022 by 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_POSH_POPO_SERVER_IMPL_INL +#define IOX_POSH_POPO_SERVER_IMPL_INL + +#include "iceoryx_posh/internal/popo/server_impl.hpp" + +namespace iox +{ +namespace popo +{ +template +inline ServerImpl::ServerImpl(const capro::ServiceDescription& service, + const ServerOptions& serverOptions) noexcept + : BaseServerT(service, serverOptions) +{ +} + +template +cxx::expected, ServerRequestResult> ServerImpl::take() noexcept +{ + auto result = port().getRequest(); + if (result.has_error()) + { + return cxx::error(result.get_error()); + } + auto requestHeader = result.value(); + auto payload = mepoo::ChunkHeader::fromUserHeader(requestHeader)->userPayload(); + auto request = cxx::unique_ptr(static_cast(payload), m_requestDeleter); + return cxx::success>(Request{std::move(request)}); +} + +template +cxx::expected, AllocationError> +ServerImpl::loanUninitialized(const Request& request) noexcept +{ + const auto* requestHeader = &request.getRequestHeader(); + auto result = port().allocateResponse(requestHeader, sizeof(Req), alignof(Req)); + if (result.has_error()) + { + return cxx::error(result.get_error()); + } + else + { + auto responseHeader = result.value(); + auto payload = mepoo::ChunkHeader::fromUserHeader(responseHeader)->userPayload(); + auto response = cxx::unique_ptr(reinterpret_cast(payload), m_responseDeleter); + return cxx::success>(Response{std::move(response), *this}); + } +} + +template +template +cxx::expected, AllocationError> ServerImpl::loan(const Request& request, + Args&&... args) noexcept +{ + return std::move(loanUninitialized(request).and_then( + [&](auto& response) { new (response.get()) Res(std::forward(args)...); })); +} + +template +void ServerImpl::send(Response&& response) noexcept +{ + auto payload = response.release(); // release the Request ownership of the chunk before publishing + auto* responseHeader = static_cast(mepoo::ChunkHeader::fromUserPayload(payload)->userHeader()); + port().sendResponse(responseHeader); +} + +} // namespace popo +} // namespace iox + +#endif // IOX_POSH_POPO_SERVER_IMPL_INL diff --git a/iceoryx_posh/include/iceoryx_posh/popo/client.hpp b/iceoryx_posh/include/iceoryx_posh/popo/client.hpp new file mode 100644 index 00000000000..c323c5a00a2 --- /dev/null +++ b/iceoryx_posh/include/iceoryx_posh/popo/client.hpp @@ -0,0 +1,39 @@ +// Copyright (c) 2022 by 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_POSH_POPO_CLIENT_HPP +#define IOX_POSH_POPO_CLIENT_HPP + +#include "iceoryx_posh/capro/service_description.hpp" +#include "iceoryx_posh/internal/popo/client_impl.hpp" +#include "iceoryx_posh/popo/client_options.hpp" +#include "iceoryx_posh/popo/trigger_handle.hpp" +#include "iceoryx_posh/runtime/posh_runtime.hpp" + +namespace iox +{ +namespace popo +{ +template +class Client : public ClientImpl +{ + public: + using ClientImpl::ClientImpl; +}; +} // namespace popo +} // namespace iox + +#endif // IOX_POSH_POPO_CLIENT_HPP diff --git a/iceoryx_posh/include/iceoryx_posh/popo/server.hpp b/iceoryx_posh/include/iceoryx_posh/popo/server.hpp new file mode 100644 index 00000000000..0d686cca1eb --- /dev/null +++ b/iceoryx_posh/include/iceoryx_posh/popo/server.hpp @@ -0,0 +1,35 @@ +// Copyright (c) 2022 by 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_POSH_POPO_SERVER_HPP +#define IOX_POSH_POPO_SERVER_HPP + +#include "iceoryx_posh/internal/popo/server_impl.hpp" + +namespace iox +{ +namespace popo +{ +template +class Server : public ServerImpl +{ + public: + using ServerImpl::ServerImpl; +}; +} // namespace popo +} // namespace iox + +#endif // IOX_POSH_POPO_SERVER_HPP