Skip to content

Commit

Permalink
read at, write at
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed Dec 4, 2023
1 parent 95c4f33 commit e8c9cf8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 31 deletions.
13 changes: 7 additions & 6 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 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

0 comments on commit e8c9cf8

Please sign in to comment.