cpp-msgpack-rpc 0.2.0
An RPC library implementing MessagePack RPC.
Loading...
Searching...
No Matches
connection_wrapper.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 <memory>
23#include <utility>
24
30
31namespace msgpack_rpc::transport {
32
37public:
46
52
61
67 explicit ConnectionWrapper(std::shared_ptr<IConnection> connection) noexcept
68 : connection_(std::move(connection)) {}
69
81 ConnectionClosedCallback on_closed) {
82 connection_->start(
83 std::move(on_received), std::move(on_sent), std::move(on_closed));
84 }
85
94 template <typename... Parameters>
96 messages::MessageID message_id, const Parameters&... parameters) {
98 method_name, message_id, parameters...));
99 }
100
108 template <typename T>
110 messages::MessageID request_id, const T& result) {
111 connection_->async_send(
113 request_id, result));
114 }
115
123 template <typename T>
124 void async_response_error(messages::MessageID request_id, const T& error) {
125 connection_->async_send(
127 request_id, error));
128 }
129
137 template <typename... Parameters>
139 messages::MethodNameView method_name, const Parameters&... parameters) {
140 connection_->async_send(
142 method_name, parameters...));
143 }
144
148 void async_close() { connection_->async_close(); }
149
155 [[nodiscard]] const addresses::IAddress& local_address() const noexcept {
156 return connection_->local_address();
157 }
158
164 [[nodiscard]] const addresses::IAddress& remote_address() const noexcept {
165 return connection_->remote_address();
166 }
167
173 [[nodiscard]] std::shared_ptr<IConnection> connection() const noexcept {
174 return connection_;
175 }
176
177private:
179 std::shared_ptr<IConnection> connection_;
180};
181
182} // namespace msgpack_rpc::transport
Interface of addresses.
Definition i_address.h:31
static SerializedMessage serialize_error_response(MessageID request_id, const T &error)
Serialize an error response.
static SerializedMessage serialize_notification(MethodNameView method_name, const Parameters &... parameters)
Serialize a notification.
static SerializedMessage serialize_successful_response(MessageID request_id, const T &result)
Serialize a successful response.
static SerializedMessage serialize_request(MethodNameView method_name, MessageID message_id, const Parameters &... parameters)
Serialize a request.
const addresses::IAddress & local_address() const noexcept
Get the address of the local endpoint.
IConnection::MessageReceivedCallback MessageReceivedCallback
Type of callback functions called when a message is received.
IConnection::ConnectionClosedCallback ConnectionClosedCallback
Type of callback functions called when a connection is closed.
void async_close()
Asynchronously close this connection.
std::shared_ptr< IConnection > connection() const noexcept
Get the IConnection object of this wrapper.
ConnectionWrapper(std::shared_ptr< IConnection > connection) noexcept
Constructor.
void async_request(messages::MethodNameView method_name, messages::MessageID message_id, const Parameters &... parameters)
Asynchronously send a request.
void async_response_error(messages::MessageID request_id, const T &error)
Asynchronously send a response with an error.
std::shared_ptr< IConnection > connection_
Connection.
void async_response_success(messages::MessageID request_id, const T &result)
Asynchronously send a response without an error.
void async_notify(messages::MethodNameView method_name, const Parameters &... parameters)
Asynchronously send a notification.
void start(MessageReceivedCallback on_received, MessageSentCallback on_sent, ConnectionClosedCallback on_closed)
Start process of this connection.
IConnection::MessageSentCallback MessageSentCallback
Type of callback functions called when a message is successfully sent.
const addresses::IAddress & remote_address() const noexcept
Get the address of the remote endpoint.
std::function< void(messages::ParsedMessage)> MessageReceivedCallback
Type of callback functions called when a message is received.
std::function< void(const Status &)> ConnectionClosedCallback
Type of callback functions called when a connection is closed.
std::function< void()> MessageSentCallback
Type of callback functions called when a message is successfully sent.
Definition of IAddress.
Definition of IConnection class.
Definition of MessageID type.
Definition of MessageSerializer class.
Definition of MethodNameView class.
std::uint32_t MessageID
Type of message IDs.
Definition message_id.h:27
Namespace of transport of messages.
Definition backends.h:31