cpp-msgpack-rpc
0.2.0
An RPC library implementing MessagePack RPC.
Toggle main menu visibility
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
27
#include "
msgpack_rpc/addresses/uri.h
"
28
#include "
msgpack_rpc/common/msgpack_rpc_exception.h
"
29
#include "
msgpack_rpc/common/status.h
"
30
#include "
msgpack_rpc/common/status_code.h
"
31
#include "
msgpack_rpc/transport/backend_list.h
"
32
#include "
msgpack_rpc/transport/i_backend.h
"
33
#include "
msgpack_rpc/transport/i_connection.h
"
34
#include "
msgpack_rpc/transport/i_connector.h
"
35
36
namespace
msgpack_rpc::transport
{
37
38
namespace
impl
{
39
46
class
MultiBackendConnector
{
47
public
:
56
MultiBackendConnector
(
const
BackendList
& backends,
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
76
void
operator()
(
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
}
82
++
current_uri_
;
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
}
89
async_connect
();
90
}
91
92
private
:
96
void
async_connect
() {
97
connector_
=
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
107
const
BackendList
*
backends_
;
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
134
inline
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
backend_list.h
Definition of BackendList class.
msgpack_rpc::MsgpackRPCException
Class of exceptions in cpp-msgpack-rpc library.
Definition
msgpack_rpc_exception.h:35
msgpack_rpc::common::MsgpackRPCException::status
const Status & status() const noexcept
Get the status.
Definition
msgpack_rpc_exception.cpp:34
msgpack_rpc::Status
Class of statuses.
Definition
status.h:34
msgpack_rpc::common::Status::code
StatusCode code() const noexcept
Get the status code.
Definition
status.cpp:69
msgpack_rpc::transport::BackendList
Class of lists of backends of protocols.
Definition
backend_list.h:39
msgpack_rpc::transport::BackendList::find
std::shared_ptr< transport::IBackend > find(std::string_view scheme) const
Find a backend. Throw an exception when not found.
Definition
backend_list.h:66
msgpack_rpc::transport::impl::MultiBackendConnector
Class to establish a connection using multiple backends of protocols.
Definition
async_connect.h:46
msgpack_rpc::transport::impl::MultiBackendConnector::connector_
std::shared_ptr< IConnector > connector_
Current connector.
Definition
async_connect.h:119
msgpack_rpc::transport::impl::MultiBackendConnector::start
void start()
Start to connect to an endpoint.
Definition
async_connect.h:68
msgpack_rpc::transport::impl::MultiBackendConnector::async_connect
void async_connect()
Asynchronously connect to an endpoint.
Definition
async_connect.h:96
msgpack_rpc::transport::impl::MultiBackendConnector::on_connection_
std::function< void(Status, std::shared_ptr< IConnection >)> on_connection_
Callback function called when a connection is established or error occurred in all URIs.
Definition
async_connect.h:113
msgpack_rpc::transport::impl::MultiBackendConnector::uris_
const std::vector< addresses::URI > * uris_
URIs.
Definition
async_connect.h:110
msgpack_rpc::transport::impl::MultiBackendConnector::operator()
void operator()(const Status &status, std::shared_ptr< IConnection > connection)
Handle the result to connect to an endpoint.
Definition
async_connect.h:76
msgpack_rpc::transport::impl::MultiBackendConnector::current_uri_
std::vector< addresses::URI >::const_iterator current_uri_
Current URI.
Definition
async_connect.h:116
msgpack_rpc::transport::impl::MultiBackendConnector::MultiBackendConnector
MultiBackendConnector(const BackendList &backends, const std::vector< addresses::URI > &uris, std::function< void(const Status &, std::shared_ptr< IConnection >)> on_connection)
Constructor.
Definition
async_connect.h:56
msgpack_rpc::transport::impl::MultiBackendConnector::backends_
const BackendList * backends_
Backends.
Definition
async_connect.h:107
i_backend.h
Definition of IBackend class.
i_connection.h
Definition of IConnection class.
i_connector.h
Definition of IConnector class.
msgpack_rpc_exception.h
Definition of MsgpackRPCException class.
msgpack_rpc::transport::impl
Namespace of internal implementations.
Definition
async_connect.h:38
msgpack_rpc::transport
Namespace of transport of messages.
Definition
backends.h:31
msgpack_rpc::transport::async_connect
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.
Definition
async_connect.h:134
std
STL namespace.
status.h
Definition of Status class.
status_code.h
Definition of StatusCode enumeration.
uri.h
Definition of URI class.
src
msgpack_rpc
transport
async_connect.h
Generated on
for cpp-msgpack-rpc by
1.18.0