cpp-msgpack-rpc 0.2.0
An RPC library implementing MessagePack RPC.
Loading...
Searching...
No Matches
stop_signal_handler.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 <csignal>
23#include <memory>
24#include <utility>
25
26#include <asio/error_code.hpp>
27#include <asio/io_context.hpp>
28#include <asio/signal_set.hpp>
29
31
32namespace msgpack_rpc::servers {
33
38public:
44 explicit StopSignalHandler(std::shared_ptr<logging::Logger> logger)
45 : context_(1),
46 signal_set_(context_, SIGINT, SIGTERM),
47 logger_(std::move(logger)) {}
48
52 void wait() {
53 signal_set_.async_wait(
54 [this](const asio::error_code& error, int signal_number) {
55 if (!error) {
57 logger_, "Received signal {}.", signal_number);
58 }
59 });
60 context_.run();
61 }
62
66 void stop() { context_.stop(); }
67
68private:
70 asio::io_context context_;
71
73 asio::signal_set signal_set_;
74
76 std::shared_ptr<logging::Logger> logger_;
77};
78
79} // namespace msgpack_rpc::servers
std::shared_ptr< logging::Logger > logger_
Logger.
asio::io_context context_
Context of asio library.
StopSignalHandler(std::shared_ptr< logging::Logger > logger)
Constructor.
void wait()
Wait for a signal SIGINT or SIGTERM.
asio::signal_set signal_set_
Object to wait signals.
Definition of Logger class.
#define MSGPACK_RPC_DEBUG(LOGGER_PTR,...)
Write a debug log.
Definition logger.h:186
Namespace of servers.
STL namespace.