13 #include "libp2p/basic/readwriter.hpp" 14 #include "libp2p/host/host.hpp" 15 #include "libp2p/peer/peer_info.hpp" 16 #include "libp2p/peer/protocol.hpp" 26 template <
typename MessageReadWriterT>
38 template <
typename Request,
typename Response>
39 static void read(std::shared_ptr<libp2p::basic::ReadWriter> read_writer,
40 std::function<outcome::result<Response>(Request)> cb,
41 std::function<
void(outcome::result<void>)> error_cb) {
42 auto msg_read_writer =
43 std::make_shared<MessageReadWriterT>(std::move(read_writer));
44 msg_read_writer->template read<Request>(
45 [msg_read_writer, cb = std::move(cb), error_cb = std::move(error_cb)](
46 auto &&request_res)
mutable {
48 return error_cb(request_res.error());
51 auto response_res = cb(std::move(request_res.value()));
53 return error_cb(response_res.error());
56 msg_read_writer->template write<Response>(
58 [error_cb = std::move(error_cb)](
auto &&write_res) {
60 return error_cb(write_res.error());
73 template <
typename Request>
74 static void read(std::shared_ptr<libp2p::basic::ReadWriter> read_writer,
75 std::function<
void(outcome::result<Request>)> cb) {
76 auto msg_read_writer =
77 std::make_shared<MessageReadWriterT>(std::move(read_writer));
78 msg_read_writer->template read<Request>(
79 [msg_read_writer, cb = std::move(cb)](
auto &&msg_res)
mutable {
81 return cb(msg_res.error());
84 cb(std::move(msg_res.value()));
98 template <
typename Request,
typename Response>
103 std::function<
void(outcome::result<Response>)> cb) {
107 [request = std::move(request),
108 cb = std::move(cb)](
auto &&stream_res)
mutable {
110 return cb(stream_res.error());
113 auto stream_and_proto = std::move(stream_res.value());
114 auto &stream = stream_and_proto.stream;
118 "Sending blocks request to {}",
119 stream->remotePeerId().value().toBase58());
121 auto read_writer = std::make_shared<MessageReadWriterT>(stream);
122 read_writer->template write<Request>(
124 [read_writer, stream = std::move(stream), cb = std::move(cb)](
125 auto &&write_res)
mutable {
128 return cb(write_res.error());
133 "Request to {} sent successfully",
134 stream->remotePeerId().value().toBase58());
136 read_writer->template read<Response>(
137 [stream = std::move(stream),
138 cb = std::move(cb)](
auto &&msg_res) {
141 return cb(msg_res.error());
144 stream->close([](
auto &&) {});
145 return cb(std::move(msg_res.value()));
160 template <
typename Request>
165 std::function<
void(outcome::result<void>)> cb) {
166 host.newStream(peer_info.id,
168 [request = std::move(request),
169 cb = std::move(cb)](
auto &&stream_res)
mutable {
171 return cb(stream_res.error());
174 auto stream_and_proto = std::move(stream_res.value());
175 auto read_writer = std::make_shared<MessageReadWriterT>(
176 stream_and_proto.stream);
177 read_writer->template write<Request>(
179 [stream = std::move(stream_and_proto.stream),
180 cb = std::move(cb)](
auto &&write_res) {
183 return cb(write_res.error());
186 stream->close([](
auto &&) {});
187 return cb(outcome::success());
194 #endif // KAGOME_RPC_HPP static void read(std::shared_ptr< libp2p::basic::ReadWriter > read_writer, std::function< void(outcome::result< Request >)> cb)
libp2p::peer::PeerInfo PeerInfo
libp2p::peer::Protocol Protocol
static void read(std::shared_ptr< libp2p::basic::ReadWriter > read_writer, std::function< outcome::result< Response >(Request)> cb, std::function< void(outcome::result< void >)> error_cb)
static void write(libp2p::Host &host, const libp2p::peer::PeerInfo &peer_info, const libp2p::peer::Protocol &protocol, Request request, std::function< void(outcome::result< Response >)> cb)
Logger createLogger(const std::string &tag)
static void write(libp2p::Host &host, const libp2p::peer::PeerInfo &peer_info, const libp2p::peer::Protocol &protocol, Request request, std::function< void(outcome::result< void >)> cb)