cpp-msgpack-rpc 0.2.0
An RPC library implementing MessagePack RPC.
Loading...
Searching...
No Matches
client_impl.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#include <exception>
24#include <memory>
25#include <optional>
26#include <utility>
27
42
44
48class ClientImpl final : public IClientImpl {
49public:
58 ClientImpl(std::shared_ptr<ClientConnector> connector,
59 std::shared_ptr<CallList> call_list,
60 std::shared_ptr<executors::IAsyncExecutor> executor,
61 std::shared_ptr<logging::Logger> logger)
62 : executor_(std::move(executor)),
63 connector_(std::move(connector)),
64 call_list_(std::move(call_list)),
65 logger_(std::move(logger)),
66 sender_(std::make_shared<MessageSender>(connector_, logger_)) {}
67
71 ~ClientImpl() override {
72 try {
73 stop();
74 } catch (const std::exception& e) {
76 "An exception was thrown when destructing a client but "
77 "ignored: {}",
78 e.what());
79 }
80 }
81
82 ClientImpl(const ClientImpl&) = delete;
83 ClientImpl(ClientImpl&&) = delete;
84 ClientImpl& operator=(const ClientImpl&) = delete;
85 ClientImpl& operator=(ClientImpl&&) = delete;
86
90 void start() {
91 if (is_started_.exchange(true)) {
92 throw MsgpackRPCException(StatusCode::PRECONDITION_NOT_MET,
93 "This client has already been started.");
94 }
95 connector_->start(
96 // on_connection
97 [sender = sender_] { sender->send_next(); },
98 // on_received
100 // on_sent
101 [sender = sender_] {
102 sender->handle_sent_message();
103 sender->send_next();
104 },
105 // on_closed
106 [sender = sender_] { sender->handle_disconnection(); });
107 executor_->start();
108 }
109
111 void stop() override {
112 if (!is_started_.load()) {
113 return;
114 }
115 if (is_stopped_.exchange(true)) {
116 return;
117 }
118 connector_->stop();
119 connector_.reset();
120 call_list_.reset();
121 sender_.reset();
122 executor_->stop();
123 executor_.reset();
124 }
125
127 [[nodiscard]] std::shared_ptr<ICallFutureImpl> async_call(
128 messages::MethodNameView method_name,
129 const IParametersSerializer& parameters) override {
131
132 const auto [request_id, serialized_request, future] =
133 call_list_->create(method_name, parameters);
134
135 sender_->send(serialized_request, request_id);
136
138 logger_, "Send request {} (id: {})", method_name, request_id);
139
140 return future;
141 }
142
145 const IParametersSerializer& parameters) override {
147
148 const auto serialized_notification =
149 parameters.create_serialized_notification(method_name);
150
151 sender_->send(serialized_notification);
152
153 MSGPACK_RPC_DEBUG(logger_, "Send notification {}", method_name);
154 }
155
157 [[nodiscard]] std::shared_ptr<executors::IExecutor> executor() override {
158 return executor_;
159 }
160
161private:
166 if (!executor_ || !executor_->is_running()) {
167 throw MsgpackRPCException(StatusCode::PRECONDITION_NOT_MET,
168 "This client has been stopped.");
169 }
170 }
171
173 std::shared_ptr<executors::IAsyncExecutor> executor_;
174
176 std::shared_ptr<ClientConnector> connector_;
177
179 std::shared_ptr<CallList> call_list_;
180
182 std::shared_ptr<logging::Logger> logger_;
183
185 std::shared_ptr<MessageSender> sender_;
186
188 std::atomic<bool> is_started_{false};
189
191 std::atomic<bool> is_stopped_{false};
192};
193
194} // namespace msgpack_rpc::clients::impl
Definition of CallList class.
Class of internal implementation of clients.
Definition client_impl.h:48
std::shared_ptr< executors::IAsyncExecutor > executor_
Executor.
std::shared_ptr< ICallFutureImpl > async_call(messages::MethodNameView method_name, const IParametersSerializer &parameters) override
Asynchronously call a method.
std::shared_ptr< ClientConnector > connector_
Connector.
ClientImpl(std::shared_ptr< ClientConnector > connector, std::shared_ptr< CallList > call_list, std::shared_ptr< executors::IAsyncExecutor > executor, std::shared_ptr< logging::Logger > logger)
Constructor.
Definition client_impl.h:58
std::shared_ptr< CallList > call_list_
List of RPCs.
std::shared_ptr< executors::IExecutor > executor() override
Get the executor.
std::shared_ptr< MessageSender > sender_
Sender of messages.
void stop() override
Stop processing of this client.
std::atomic< bool > is_stopped_
Whether this client has been stopped.
std::shared_ptr< logging::Logger > logger_
Logger.
void check_executor_state()
Check whether the executor is running.
void notify(messages::MethodNameView method_name, const IParametersSerializer &parameters) override
Notify to a method.
std::atomic< bool > is_started_
Whether this client has been started.
void start()
Start processing of this client.
Definition client_impl.h:90
virtual messages::SerializedMessage create_serialized_notification(messages::MethodNameView method_name) const =0
Create a serialized notification data.
Class to send messages in clients.
Class of exceptions in cpp-msgpack-rpc library.
Definition of ClientConnector class.
Definition of IAsyncExecutor class.
Definition of ICallFutureImpl class.
Definition of IClientImpl class.
Definition of IExecutor class.
Definition of Logger class.
#define MSGPACK_RPC_CRITICAL(LOGGER_PTR,...)
Write a critical log.
Definition logger.h:234
#define MSGPACK_RPC_DEBUG(LOGGER_PTR,...)
Write a debug log.
Definition logger.h:186
Definition of MessageSender class.
Definition of MethodNameView class.
Definition of MsgpackRPCException class.
Namespace of internal implementations.
STL namespace.
Definition of ParametersSerializer class.
Definition of ReceivedMessageProcessor class.
Definition of SerializedMessage class.
Definition of StatusCode enumeration.