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
24
#include "
msgpack_rpc/executors/asio_context_type.h
"
25
#include "
msgpack_rpc/executors/i_single_thread_executor.h
"
26
#include "
msgpack_rpc/executors/operation_type.h
"
27
#include "
msgpack_rpc/logging/logger.h
"
28
29
namespace
msgpack_rpc::executors
{
30
34
class
SingleThreadExecutor
final :
public
ISingleThreadExecutor {
35
public
:
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) {
51
MSGPACK_RPC_CRITICAL
(
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
63
private
:
65
AsioContextType
context_
{1};
66
68
std::shared_ptr<logging::Logger>
logger_
;
69
};
70
71
std::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
asio_context_type.h
Definition of AsioContextType class.
msgpack_rpc::executors::SingleThreadExecutor::run
void run() override
Run internal event loops to process asynchronous tasks.
Definition
single_thread_executor.cpp:45
msgpack_rpc::executors::SingleThreadExecutor::logger_
std::shared_ptr< logging::Logger > logger_
Logger.
Definition
single_thread_executor.cpp:68
msgpack_rpc::executors::SingleThreadExecutor::context
AsioContextType & context(OperationType type) noexcept override
Get the context in asio library.
Definition
single_thread_executor.cpp:58
msgpack_rpc::executors::SingleThreadExecutor::context_
AsioContextType context_
Context.
Definition
single_thread_executor.cpp:65
msgpack_rpc::executors::SingleThreadExecutor::SingleThreadExecutor
SingleThreadExecutor(std::shared_ptr< logging::Logger > logger)
Constructor.
Definition
single_thread_executor.cpp:41
i_single_thread_executor.h
Definition of ISingleThreadExecutor class.
logger.h
Definition of Logger class.
MSGPACK_RPC_CRITICAL
#define MSGPACK_RPC_CRITICAL(LOGGER_PTR,...)
Write a critical log.
Definition
logger.h:234
MSGPACK_RPC_TRACE
#define MSGPACK_RPC_TRACE(LOGGER_PTR,...)
Write a trace log.
Definition
logger.h:174
msgpack_rpc::executors
Namespace of executors to process asynchronous tasks.
Definition
asio_context_type.h:24
msgpack_rpc::executors::OperationType
OperationType
Enumeration of types of operations.
Definition
operation_type.h:29
msgpack_rpc::executors::AsioContextType
asio::io_context AsioContextType
Type of context in asio library.
Definition
asio_context_type.h:29
msgpack_rpc::executors::create_single_thread_executor
std::shared_ptr< ISingleThreadExecutor > create_single_thread_executor(std::shared_ptr< logging::Logger > logger)
Create an executor runs in a single thread.
Definition
single_thread_executor.cpp:71
std
STL namespace.
operation_type.h
Definition of OperationType enumeration.
src
msgpack_rpc
executors
single_thread_executor.cpp
Generated on
for cpp-msgpack-rpc by
1.14.0