cpp-msgpack-rpc 0.2.0
An RPC library implementing MessagePack RPC.
Loading...
Searching...
No Matches
async_connect.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 <functional>
23#include <memory>
24#include <utility>
25#include <vector>
26
35
36namespace msgpack_rpc::transport {
37
38namespace impl {
39
47public:
57 const std::vector<addresses::URI>& uris,
58 std::function<void(const Status&, std::shared_ptr<IConnection>)>
59 on_connection)
60 : backends_(&backends),
61 uris_(&uris),
62 on_connection_(std::move(on_connection)),
63 current_uri_(uris_->cbegin()) {}
64
68 void start() { async_connect(); }
69
77 const Status& status, std::shared_ptr<IConnection> connection) {
78 if (status.code() == StatusCode::SUCCESS) {
79 on_connection_(status, std::move(connection));
80 return;
81 }
83 if (current_uri_ == uris_->end()) {
84 on_connection_(Status(StatusCode::CONNECTION_FAILURE,
85 "Failed to connect to all the URIs."),
86 std::move(connection));
87 return;
88 }
90 }
91
92private:
98 backends_->find(current_uri_->scheme())->create_connector();
99 try {
100 connector_->async_connect(*current_uri_, *this);
101 } catch (const MsgpackRPCException& e) {
102 (*this)(e.status(), nullptr);
103 }
104 }
105
108
110 const std::vector<addresses::URI>* uris_;
111
113 std::function<void(Status, std::shared_ptr<IConnection>)> on_connection_;
114
116 std::vector<addresses::URI>::const_iterator current_uri_;
117
119 std::shared_ptr<IConnector> connector_{};
120};
121
122} // namespace impl
123
134inline void async_connect(const BackendList& backends,
135 const std::vector<addresses::URI>& uris,
136 std::function<void(const Status&, std::shared_ptr<IConnection>)>
137 on_connection) {
138 impl::MultiBackendConnector(backends, uris, std::move(on_connection))
139 .start();
140}
141
142} // namespace msgpack_rpc::transport
Definition of BackendList class.
Class of exceptions in cpp-msgpack-rpc library.
const Status & status() const noexcept
Get the status.
Class of statuses.
Definition status.h:34
StatusCode code() const noexcept
Get the status code.
Definition status.cpp:69
Class of lists of backends of protocols.
Class to establish a connection using multiple backends of protocols.
std::shared_ptr< IConnector > connector_
Current connector.
void start()
Start to connect to an endpoint.
void async_connect()
Asynchronously connect to an endpoint.
std::function< void(Status, std::shared_ptr< IConnection >)> on_connection_
Callback function called when a connection is established or error occurred in all URIs.
const std::vector< addresses::URI > * uris_
URIs.
void operator()(const Status &status, std::shared_ptr< IConnection > connection)
Handle the result to connect to an endpoint.
std::vector< addresses::URI >::const_iterator current_uri_
Current URI.
MultiBackendConnector(const BackendList &backends, const std::vector< addresses::URI > &uris, std::function< void(const Status &, std::shared_ptr< IConnection >)> on_connection)
Constructor.
Definition of IBackend class.
Definition of IConnection class.
Definition of IConnector class.
Definition of MsgpackRPCException class.
Namespace of internal implementations.
Namespace of transport of messages.
Definition backends.h:31
void async_connect(const BackendList &backends, const std::vector< addresses::URI > &uris, std::function< void(const Status &, std::shared_ptr< IConnection >)> on_connection)
Asynchronously connect to an endpoint.
STL namespace.
Definition of Status class.
Definition of StatusCode enumeration.
Definition of URI class.