6 #ifndef KAGOME_PROTOBUF_MESSAGE_READ_WRITER_HPP 7 #define KAGOME_PROTOBUF_MESSAGE_READ_WRITER_HPP 12 #include <libp2p/basic/message_read_writer_uvarint.hpp> 13 #include <libp2p/basic/protobuf_message_read_writer.hpp> 19 #include "scale/scale.hpp" 26 :
public std::enable_shared_from_this<ProtobufMessageReadWriter> {
27 template <
typename MsgType>
28 using ReadCallback = std::function<void(outcome::result<MsgType>)>;
34 const std::shared_ptr<libp2p::basic::ReadWriter> &read_writer)
36 std::make_shared<
libp2p::basic::MessageReadWriterUvarint>(
44 template <
typename MsgType>
47 [
self{shared_from_this()}, cb = std::move(cb)](
auto &&read_res) {
49 return cb(read_res.error());
56 if (read_res.value()) {
57 if (
auto msg_res = ProtobufRW::read(
58 msg, *read_res.value(), read_res.value()->begin());
60 return cb(msg_res.error());
63 return cb(std::move(msg));
73 template <
typename MsgType>
75 libp2p::basic::Writer::WriteCallbackFunc &&cb)
const {
80 std::vector<uint8_t> out;
81 auto it = ProtobufRW::write(msg, out);
83 gsl::span<uint8_t> data(it.base(),
84 out.size() - std::distance(out.begin(), it));
86 read_writer_->write(data,
87 [
self{shared_from_this()},
89 cb = std::move(cb)](
auto &&write_res) {
91 return cb(write_res.error());
93 cb(outcome::success());
100 #endif // KAGOME_PROTOBUF_MESSAGE_READ_WRITER_HPP
std::shared_ptr< libp2p::basic::MessageReadWriter > read_writer_
std::function< void(outcome::result< MsgType >)> ReadCallback
void read(ReadCallback< MsgType > &&cb) const
void write(const MsgType &msg, libp2p::basic::Writer::WriteCallbackFunc &&cb) const
ProtobufMessageReadWriter(const std::shared_ptr< libp2p::basic::ReadWriter > &read_writer)