-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: ik <[email protected]>
- Loading branch information
Showing
21 changed files
with
691 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// | ||
// Copyright 2024 Huawei Cloud Computing Technologies Co., Ltd. | ||
// | ||
// 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. | ||
// | ||
|
||
/// @file CompletionToken.hpp | ||
#ifndef OPENGEMINI_COMPLETIONTOKEN_HPP | ||
#define OPENGEMINI_COMPLETIONTOKEN_HPP | ||
|
||
#include <boost/asio/deferred.hpp> | ||
#include <boost/asio/detached.hpp> | ||
#include <boost/asio/use_awaitable.hpp> | ||
#include <boost/asio/use_future.hpp> | ||
|
||
namespace opengemini::token { | ||
|
||
class Sync { }; | ||
|
||
/// | ||
/// \~English | ||
/// @brief A completion token that causes an operation blocking until the | ||
/// task completes or an exception is thrown. | ||
/// | ||
/// \~Chinese | ||
/// @brief 该完成令牌使操作阻塞直到任务完成或抛出异常。 | ||
/// | ||
constexpr Sync sync{}; | ||
|
||
/// | ||
/// \~English | ||
/// @brief A completion token that causes an operation to return a future. | ||
/// | ||
/// \~Chinese | ||
/// @brief 该完成令牌使操作返回future对象。 | ||
/// | ||
constexpr auto future = boost::asio::use_future; | ||
|
||
/// | ||
/// \~English | ||
/// @brief A completion token that indicate an operation is detached, that is, | ||
/// there is no handler waiting for ther operation's result. | ||
/// | ||
/// \~Chinese | ||
/// @brief | ||
/// 该完成令牌指示操作应当独立运行,既不感知也不处理任务的结果。 | ||
/// | ||
constexpr auto detached = boost::asio::detached; | ||
|
||
/// | ||
/// \~English | ||
/// @brief A completion token that specify an operation should return a function | ||
/// object to lazily launch the operation, instead of starting the asynchronous | ||
/// task immediately. | ||
/// | ||
/// \~Chinese | ||
/// @brief | ||
/// 该完成令牌指示操作不要立即启动异步任务,而是返回一个函数对象用于懒启动。 | ||
/// | ||
constexpr auto deferred = boost::asio::deferred; | ||
|
||
#if __cplusplus >= 202002L | ||
|
||
/// | ||
/// \~English | ||
/// @brief A completion token that represent the currently executing coroutine. | ||
/// | ||
/// \~Chinese | ||
/// @brief | ||
/// 该完成令牌用于表示当前正在执行的协程。 | ||
/// | ||
constexpr auto coro = boost::asio::use_awaitable; | ||
|
||
#endif // (__cplusplus < 202002L) | ||
|
||
} // namespace opengemini::token | ||
|
||
#include "opengemini/impl/CompletionToken.tpp" | ||
|
||
#endif // !OPENGEMINI_COMPLETIONTOKEN_HPP |
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// | ||
// Copyright 2024 Huawei Cloud Computing Technologies Co., Ltd. | ||
// | ||
// 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. | ||
// | ||
|
||
#include "opengemini/impl/ClientImpl.hpp" | ||
|
||
#include <boost/exception/diagnostic_information.hpp> | ||
|
||
#include "opengemini/impl/cli/Functor.hpp" | ||
#include "opengemini/impl/comm/CompletionSignature.hpp" | ||
#include "opengemini/impl/util/ErrorHandling.hpp" | ||
#include "opengemini/impl/util/TypeTraits.hpp" | ||
|
||
namespace opengemini::impl { | ||
|
||
template<typename COMPLETION_TOKEN> | ||
auto ClientImpl::Ping(std::size_t index, COMPLETION_TOKEN&& token) | ||
{ | ||
using Signature = sig::Ping; | ||
return boost::asio::async_initiate<COMPLETION_TOKEN, Signature>( | ||
[this](auto&& token, std::size_t index) { | ||
static_assert(util::IsInvocable_v<decltype(token), Signature>, | ||
"Completion signature of Ping must be: " | ||
"void(std::exception_ptr, std::string)"); | ||
|
||
Spawn<Signature>(Functor::RunPing{ this, index }, | ||
OPENGEMINI_PF(token)); | ||
}, | ||
token, | ||
index); | ||
} | ||
|
||
template<typename COMPLETION_SIGNATURE, | ||
typename COMPLETION_TOKEN, | ||
typename FUNCTION, | ||
typename> | ||
void ClientImpl::Spawn(FUNCTION&& func, COMPLETION_TOKEN&& token) | ||
{ | ||
boost::asio::spawn(ctx_(), | ||
std::forward<FUNCTION>(func), | ||
[_token = std::forward<COMPLETION_TOKEN>(token)]( | ||
std::exception_ptr ex) mutable { | ||
_token(util::ConvertException(ex)); | ||
}); | ||
} | ||
|
||
template<typename COMPLETION_SIGNATURE, | ||
typename COMPLETION_TOKEN, | ||
typename FUNCTION, | ||
typename EXTRA_ARGS, | ||
typename> | ||
void ClientImpl::Spawn(FUNCTION&& func, COMPLETION_TOKEN&& token) | ||
{ | ||
boost::asio::spawn( | ||
ctx_(), | ||
std::forward<FUNCTION>(func), | ||
[_token = std::forward<COMPLETION_TOKEN>( | ||
token)](std::exception_ptr ex, EXTRA_ARGS args) mutable { | ||
std::apply( | ||
_token, | ||
std::tuple_cat(std::make_tuple(util::ConvertException(ex)), | ||
std::move(args))); | ||
}); | ||
} | ||
|
||
} // namespace opengemini::impl |
Oops, something went wrong.