6 #ifndef KAGOME_SCALE_MESSAGE_READ_WRITER_HPP 7 #define KAGOME_SCALE_MESSAGE_READ_WRITER_HPP 12 #include <libp2p/basic/message_read_writer_uvarint.hpp> 15 #include "scale/scale.hpp" 22 :
public std::enable_shared_from_this<ScaleMessageReadWriter> {
23 template <
typename MsgType>
24 using ReadCallback = std::function<void(outcome::result<MsgType>)>;
28 std::shared_ptr<libp2p::basic::MessageReadWriter> read_writer);
30 const std::shared_ptr<libp2p::basic::ReadWriter> &read_writer);
37 template <
typename MsgType>
40 [
self{shared_from_this()}, cb = std::move(cb)](
auto &&read_res) {
42 return cb(read_res.error());
45 if (read_res.value()) {
46 auto msg_res = scale::decode<MsgType>(*read_res.value());
48 return cb(msg_res.error());
50 return cb(std::move(msg_res.value()));
62 template <
typename MsgType>
64 libp2p::basic::Writer::WriteCallbackFunc cb)
const {
65 auto encoded_msg_res = scale::encode(msg);
66 if (!encoded_msg_res) {
67 return cb(encoded_msg_res.error());
69 auto msg_ptr = std::make_shared<std::vector<uint8_t>>(
70 std::move(encoded_msg_res.value()));
73 [
self{shared_from_this()},
75 cb = std::move(cb)](
auto &&write_res) {
77 return cb(write_res.error());
79 cb(outcome::success());
88 #endif // KAGOME_SCALE_MESSAGE_READ_WRITER_HPP std::function< void(outcome::result< MsgType >)> ReadCallback
ScaleMessageReadWriter(std::shared_ptr< libp2p::basic::MessageReadWriter > read_writer)
void read(ReadCallback< MsgType > cb) const
void write(const MsgType &msg, libp2p::basic::Writer::WriteCallbackFunc cb) const
std::shared_ptr< libp2p::basic::MessageReadWriter > read_writer_