Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[coro_file][fix]read at, write at #513

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions include/ylt/coro_io/coro_file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,11 @@ class coro_file {
return true;
}

async_simple::coro::Lazy<std::pair<std::error_code, size_t>>
async_read_some_at(uint64_t offset, char* data, size_t size) {
async_simple::coro::Lazy<std::pair<std::error_code, size_t>> async_read_at(
uint64_t offset, char* data, size_t size) {
assert(stream_file_);

auto [ec, read_size] = co_await coro_io::async_read_some_at(
auto [ec, read_size] = co_await coro_io::async_read_at(
offset,
*reinterpret_cast<asio::random_access_file*>(stream_file_.get()),
asio::buffer(data, size));
Expand All @@ -284,11 +284,12 @@ class coro_file {
co_return std::make_pair(std::error_code{}, read_size);
}

async_simple::coro::Lazy<std::error_code> async_write_some_at(
uint64_t offset, const char* data, size_t size) {
async_simple::coro::Lazy<std::error_code> async_write_at(uint64_t offset,
const char* data,
size_t size) {
assert(stream_file_);

auto [ec, write_size] = co_await coro_io::async_write_some_at(
auto [ec, write_size] = co_await coro_io::async_write_at(
offset,
*reinterpret_cast<asio::random_access_file*>(stream_file_.get()),
asio::buffer(data, size));
Expand All @@ -299,7 +300,7 @@ class coro_file {
char* data, size_t size) {
assert(stream_file_);

auto [ec, read_size] = co_await coro_io::async_read_some(
auto [ec, read_size] = co_await coro_io::async_read(
*reinterpret_cast<asio::stream_file*>(stream_file_.get()),
asio::buffer(data, size));
if (ec == asio::error::eof) {
Expand All @@ -314,7 +315,7 @@ class coro_file {
size_t size) {
assert(stream_file_);

auto [ec, write_size] = co_await coro_io::async_write_some(
auto [ec, write_size] = co_await coro_io::async_write(
*reinterpret_cast<asio::stream_file*>(stream_file_.get()),
asio::buffer(data, size));

Expand Down
24 changes: 12 additions & 12 deletions include/ylt/coro_io/coro_io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
#include <asio/dispatch.hpp>
#include <asio/ip/tcp.hpp>
#include <asio/read.hpp>
#include <asio/read_at.hpp>
#include <asio/read_until.hpp>
#include <asio/write.hpp>
#include <asio/write_at.hpp>
#include <chrono>
#include <deque>

Expand Down Expand Up @@ -137,14 +139,13 @@ async_read_some(Socket &socket, AsioBuffer &&buffer) noexcept {

template <typename Socket, typename AsioBuffer>
inline async_simple::coro::Lazy<std::pair<std::error_code, size_t>>
async_read_some_at(uint64_t offset, Socket &socket,
AsioBuffer &&buffer) noexcept {
async_read_at(uint64_t offset, Socket &socket, AsioBuffer &&buffer) noexcept {
callback_awaitor<std::pair<std::error_code, size_t>> awaitor;
co_return co_await awaitor.await_resume([&](auto handler) {
socket.async_read_some_at(offset, buffer,
[&, handler](const auto &ec, auto size) {
handler.set_value_then_resume(ec, size);
});
asio::async_read_at(socket, offset, buffer,
[&, handler](const auto &ec, auto size) {
handler.set_value_then_resume(ec, size);
});
});
}

Expand Down Expand Up @@ -208,14 +209,13 @@ async_write_some(Socket &socket, AsioBuffer &&buffer) noexcept {

template <typename Socket, typename AsioBuffer>
inline async_simple::coro::Lazy<std::pair<std::error_code, size_t>>
async_write_some_at(uint64_t offset, Socket &socket,
AsioBuffer &&buffer) noexcept {
async_write_at(uint64_t offset, Socket &socket, AsioBuffer &&buffer) noexcept {
callback_awaitor<std::pair<std::error_code, size_t>> awaitor;
co_return co_await awaitor.await_resume([&](auto handler) {
socket.async_write_some_at(offset, buffer,
[&, handler](const auto &ec, auto size) {
handler.set_value_then_resume(ec, size);
});
asio::async_write_at(socket, offset, buffer,
[&, handler](const auto &ec, auto size) {
handler.set_value_then_resume(ec, size);
});
});
}

Expand Down
21 changes: 8 additions & 13 deletions src/coro_io/tests/test_corofile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,19 @@ TEST_CASE("coro_file pread and pwrite basic test") {
CHECK(file.is_open());

char buf[100];
auto pair =
async_simple::coro::syncAwait(file.async_read_some_at(0, buf, 10));
auto pair = async_simple::coro::syncAwait(file.async_read_at(0, buf, 10));
CHECK(std::string_view(buf, pair.second) == "AAAAAAAAAA");
CHECK(!file.eof());

pair = async_simple::coro::syncAwait(file.async_read_some_at(10, buf, 100));
pair = async_simple::coro::syncAwait(file.async_read_at(10, buf, 100));
CHECK(!file.eof());
CHECK(pair.second == 100);

pair =
async_simple::coro::syncAwait(file.async_read_some_at(110, buf, 100));
CHECK(!file.eof());
pair = async_simple::coro::syncAwait(file.async_read_at(110, buf, 100));
CHECK(pair.second == 80);

// only read size equal 0 is eof.
pair =
async_simple::coro::syncAwait(file.async_read_some_at(200, buf, 100));
pair = async_simple::coro::syncAwait(file.async_read_at(200, buf, 100));
CHECK(file.eof());
CHECK(pair.second == 0);
}
Expand All @@ -137,21 +133,20 @@ TEST_CASE("coro_file pread and pwrite basic test") {

std::string buf = "cccccccccc";
auto ec = async_simple::coro::syncAwait(
file.async_write_some_at(0, buf.data(), buf.size()));
file.async_write_at(0, buf.data(), buf.size()));
CHECK(!ec);

std::string buf1 = "dddddddddd";
ec = async_simple::coro::syncAwait(
file.async_write_some_at(10, buf1.data(), buf1.size()));
file.async_write_at(10, buf1.data(), buf1.size()));
CHECK(!ec);

char buf2[100];
auto pair =
async_simple::coro::syncAwait(file.async_read_some_at(0, buf2, 10));
auto pair = async_simple::coro::syncAwait(file.async_read_at(0, buf2, 10));
CHECK(!file.eof());
CHECK(std::string_view(buf2, pair.second) == "cccccccccc");

pair = async_simple::coro::syncAwait(file.async_read_some_at(10, buf2, 10));
pair = async_simple::coro::syncAwait(file.async_read_at(10, buf2, 10));
CHECK(!file.eof());
CHECK(std::string_view(buf2, pair.second) == "dddddddddd");
}
Expand Down
Loading