cpp-msgpack-rpc 0.2.0
An RPC library implementing MessagePack RPC.
Loading...
Searching...
No Matches
single_thread_executor.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2023 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
20#include <exception>
21#include <memory>
22#include <utility>
23
28
29namespace msgpack_rpc::executors {
30
34class SingleThreadExecutor final : public ISingleThreadExecutor {
35public:
41 explicit SingleThreadExecutor(std::shared_ptr<logging::Logger> logger)
42 : logger_(std::move(logger)) {}
43
45 void run() override {
46 try {
47 MSGPACK_RPC_TRACE(logger_, "Start an executor.");
48 context_.run();
49 MSGPACK_RPC_TRACE(logger_, "Executor run stopped normally.");
50 } catch (const std::exception& e) {
52 logger_, "Executor stopped due to an exception: {}", e.what());
53 throw;
54 }
55 }
56
58 AsioContextType& context(OperationType type) noexcept override {
59 (void)type;
60 return context_;
61 }
62
63private:
66
68 std::shared_ptr<logging::Logger> logger_;
69};
70
71std::shared_ptr<ISingleThreadExecutor> create_single_thread_executor(
72 std::shared_ptr<logging::Logger> logger) {
73 return std::make_shared<SingleThreadExecutor>(std::move(logger));
74}
75
76} // namespace msgpack_rpc::executors
Definition of AsioContextType class.
void run() override
Run internal event loops to process asynchronous tasks.
std::shared_ptr< logging::Logger > logger_
Logger.
AsioContextType & context(OperationType type) noexcept override
Get the context in asio library.
SingleThreadExecutor(std::shared_ptr< logging::Logger > logger)
Constructor.
Definition of ISingleThreadExecutor class.
Definition of Logger class.
#define MSGPACK_RPC_CRITICAL(LOGGER_PTR,...)
Write a critical log.
Definition logger.h:234
#define MSGPACK_RPC_TRACE(LOGGER_PTR,...)
Write a trace log.
Definition logger.h:174
Namespace of executors to process asynchronous tasks.
OperationType
Enumeration of types of operations.
asio::io_context AsioContextType
Type of context in asio library.
std::shared_ptr< ISingleThreadExecutor > create_single_thread_executor(std::shared_ptr< logging::Logger > logger)
Create an executor runs in a single thread.
STL namespace.
Definition of OperationType enumeration.