cpp-msgpack-rpc
0.2.0
An RPC library implementing MessagePack RPC.
Toggle main menu visibility
Loading...
Searching...
No Matches
background_task_state_machine.h
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
#pragma once
21
22
#include <atomic>
23
24
#include "
msgpack_rpc/common/msgpack_rpc_exception.h
"
25
#include "
msgpack_rpc/common/status_code.h
"
26
27
namespace
msgpack_rpc::transport
{
28
33
class
BackgroundTaskStateMachine
{
34
public
:
36
enum class
State
{
38
INIT,
39
41
STARTING,
42
44
PROCESSING,
45
47
STOPPED
48
};
49
51
BackgroundTaskStateMachine
() =
default
;
52
56
void
handle_start_request
() {
57
auto
expected_state =
State::INIT
;
58
const
auto
next_state =
State::STARTING
;
59
if
(!
state_
.compare_exchange_strong(
60
expected_state, next_state, std::memory_order_relaxed)) {
61
throw
MsgpackRPCException
(
62
StatusCode::PRECONDITION_NOT_MET,
"Already started."
);
63
}
64
}
65
69
void
handle_processing_started
() {
70
state_
.store(
State::PROCESSING
, std::memory_order_release);
71
}
72
79
bool
handle_stop_requested
() {
80
return
state_
.exchange(
State::STOPPED
, std::memory_order_relaxed) !=
81
State::STOPPED
;
82
}
83
87
void
handle_processing_stopped
() {
88
state_
.store(
State::STOPPED
, std::memory_order_relaxed);
89
}
90
97
[[nodiscard]]
bool
is_processing
() const noexcept {
98
return
state_
.load(std::memory_order_acquire) ==
State::PROCESSING
;
99
}
100
101
private
:
103
std::atomic<State>
state_
{
State::INIT
};
104
};
105
106
}
// namespace msgpack_rpc::transport
msgpack_rpc::MsgpackRPCException
Class of exceptions in cpp-msgpack-rpc library.
Definition
msgpack_rpc_exception.h:35
msgpack_rpc::transport::BackgroundTaskStateMachine::State
State
Enumeration of states.
Definition
background_task_state_machine.h:36
msgpack_rpc::transport::BackgroundTaskStateMachine::State::STARTING
@ STARTING
Starting.
Definition
background_task_state_machine.h:41
msgpack_rpc::transport::BackgroundTaskStateMachine::State::STOPPED
@ STOPPED
Stopped.
Definition
background_task_state_machine.h:47
msgpack_rpc::transport::BackgroundTaskStateMachine::State::PROCESSING
@ PROCESSING
Processing.
Definition
background_task_state_machine.h:44
msgpack_rpc::transport::BackgroundTaskStateMachine::State::INIT
@ INIT
Initial state.
Definition
background_task_state_machine.h:38
msgpack_rpc::transport::BackgroundTaskStateMachine::handle_processing_stopped
void handle_processing_stopped()
Handle the condition that processing has been stopped.
Definition
background_task_state_machine.h:87
msgpack_rpc::transport::BackgroundTaskStateMachine::state_
std::atomic< State > state_
Whether the process of this connection is started.
Definition
background_task_state_machine.h:103
msgpack_rpc::transport::BackgroundTaskStateMachine::BackgroundTaskStateMachine
BackgroundTaskStateMachine()=default
Constructor.
msgpack_rpc::transport::BackgroundTaskStateMachine::is_processing
bool is_processing() const noexcept
Check whether processing.
Definition
background_task_state_machine.h:97
msgpack_rpc::transport::BackgroundTaskStateMachine::handle_start_request
void handle_start_request()
Handle the condition that starting processing is requested.
Definition
background_task_state_machine.h:56
msgpack_rpc::transport::BackgroundTaskStateMachine::handle_processing_started
void handle_processing_started()
Handle the condition that processing is started.
Definition
background_task_state_machine.h:69
msgpack_rpc::transport::BackgroundTaskStateMachine::handle_stop_requested
bool handle_stop_requested()
Handle the condition that stopping processing is requested.
Definition
background_task_state_machine.h:79
msgpack_rpc_exception.h
Definition of MsgpackRPCException class.
msgpack_rpc::transport
Namespace of transport of messages.
Definition
backends.h:31
status_code.h
Definition of StatusCode enumeration.
src
msgpack_rpc
transport
background_task_state_machine.h
Generated on
for cpp-msgpack-rpc by
1.18.0