generated from Warchant/cmake-hunter-seed
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: turuslan <turuslan.devbox@gmail.com>
- Loading branch information
Showing
7 changed files
with
117 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef LIBP2P_BASIC_READ_HPP | ||
#define LIBP2P_BASIC_READ_HPP | ||
|
||
#include <libp2p/basic/reader.hpp> | ||
#include <libp2p/common/span_size.hpp> | ||
#include <memory> | ||
|
||
namespace libp2p { | ||
/// Read exactly `out.size()` bytes | ||
inline void read(const std::shared_ptr<basic::Reader> &reader, | ||
gsl::span<uint8_t> out, | ||
std::function<void(outcome::result<void>)> cb) { | ||
auto post_cb = [](decltype(reader) reader, decltype(cb) &&cb, | ||
outcome::result<size_t> r) { | ||
reader->deferReadCallback(r, | ||
[cb{std::move(cb)}](outcome::result<size_t> r) { | ||
if (r.has_error()) { | ||
cb(r.error()); | ||
} else { | ||
cb(outcome::success()); | ||
} | ||
}); | ||
}; | ||
if (out.empty()) { | ||
return post_cb(reader, std::move(cb), outcome::success()); | ||
} | ||
// read some bytes | ||
reader->readSome( | ||
out, spanSize(out), | ||
[weak{std::weak_ptr{reader}}, out, cb{std::move(cb)}, | ||
post_cb](outcome::result<size_t> n_res) mutable { | ||
auto reader = weak.lock(); | ||
if (not reader) { | ||
return; | ||
} | ||
if (n_res.has_error()) { | ||
return post_cb(reader, std::move(cb), n_res.error()); | ||
} | ||
auto n = n_res.value(); | ||
if (n == 0) { | ||
throw std::logic_error{"libp2p::read zero bytes read"}; | ||
} | ||
if (n > spanSize(out)) { | ||
throw std::logic_error{"libp2p::read too much bytes read"}; | ||
} | ||
if (n == spanSize(out)) { | ||
// successfully read last bytes | ||
return post_cb(reader, std::move(cb), outcome::success()); | ||
} | ||
// read remaining bytes | ||
read(reader, out.subspan(n), std::move(cb)); | ||
}); | ||
} | ||
} // namespace libp2p | ||
|
||
#endif // LIBP2P_BASIC_READ_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef LIBP2P_BASIC_READ_RETURN_SIZE_HPP | ||
#define LIBP2P_BASIC_READ_RETURN_SIZE_HPP | ||
|
||
#include <libp2p/basic/read.hpp> | ||
|
||
namespace libp2p { | ||
/// Read exactly `out.size()` bytes | ||
inline void readReturnSize(const std::shared_ptr<basic::Reader> &reader, | ||
gsl::span<uint8_t> out, | ||
basic::Reader::ReadCallbackFunc cb) { | ||
read(reader, out, | ||
[n{spanSize(out)}, cb{std::move(cb)}](outcome::result<void> r) { | ||
if (r.has_error()) { | ||
cb(r.error()); | ||
} else { | ||
cb(n); | ||
} | ||
}); | ||
} | ||
} // namespace libp2p | ||
|
||
#endif // LIBP2P_BASIC_READ_RETURN_SIZE_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters