Skip to content

Commit

Permalink
Add missing header, clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
Thalhammer committed Jul 12, 2024
1 parent a824a6d commit 332ec04
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 50 deletions.
15 changes: 6 additions & 9 deletions include/asyncpp/io/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ namespace asyncpp::io {
detail::io_engine::completion_data m_completion;

public:
file_read_awaitable(io_engine* engine, io_engine::file_handle_t fd, void* buf, size_t len,
uint64_t offset, std::error_code* ec) noexcept
file_read_awaitable(io_engine* engine, io_engine::file_handle_t fd, void* buf, size_t len, uint64_t offset,
std::error_code* ec) noexcept
: m_engine(engine), m_fd(fd), m_buf(buf), m_len(len), m_offset(offset), m_ec(ec), m_completion{} {}
bool await_ready() const noexcept { return false; }
bool await_suspend(coroutine_handle<> hdl) {
Expand All @@ -113,8 +113,7 @@ namespace asyncpp::io {
}
size_t await_resume() {
if (!m_completion.result) return m_completion.result_size;
if (m_ec == nullptr)
throw std::system_error(m_completion.result);
if (m_ec == nullptr) throw std::system_error(m_completion.result);
*m_ec = m_completion.result;
return 0;
}
Expand All @@ -141,7 +140,7 @@ namespace asyncpp::io {

public:
file_write_awaitable(io_engine* engine, io_engine::file_handle_t fd, const void* buf, size_t len,
uint64_t offset, std::error_code* ec) noexcept
uint64_t offset, std::error_code* ec) noexcept
: m_engine(engine), m_fd(fd), m_buf(buf), m_len(len), m_offset(offset), m_ec(ec), m_completion{} {}
bool await_ready() const noexcept { return false; }
bool await_suspend(coroutine_handle<> hdl) {
Expand All @@ -151,8 +150,7 @@ namespace asyncpp::io {
}
size_t await_resume() {
if (!m_completion.result) return m_completion.result_size;
if (m_ec == nullptr)
throw std::system_error(m_completion.result);
if (m_ec == nullptr) throw std::system_error(m_completion.result);
*m_ec = m_completion.result;
return 0;
}
Expand Down Expand Up @@ -185,8 +183,7 @@ namespace asyncpp::io {
}
void await_resume() {
if (!m_completion.result) return;
if (m_ec == nullptr)
throw std::system_error(m_completion.result);
if (m_ec == nullptr) throw std::system_error(m_completion.result);
*m_ec = m_completion.result;
}
};
Expand Down
53 changes: 22 additions & 31 deletions include/asyncpp/io/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ namespace asyncpp::io {

[[nodiscard]] detail::io_engine::socket_handle_t native_handle() const noexcept { return m_fd; }
[[nodiscard]] detail::io_engine::socket_handle_t release() noexcept {
if(m_io != nullptr && m_fd != detail::io_engine::invalid_socket_handle)
if (m_io != nullptr && m_fd != detail::io_engine::invalid_socket_handle)
m_io->engine()->socket_release(m_fd);
m_io = nullptr;
m_remote_ep = {};
Expand All @@ -115,21 +115,19 @@ namespace asyncpp::io {
[[nodiscard]] socket_accept_awaitable accept() noexcept;
[[nodiscard]] socket_accept_error_code_awaitable accept(std::error_code& ec) noexcept;
[[nodiscard]] socket_send_awaitable send(const void* buffer, std::size_t size) noexcept;
[[nodiscard]] socket_send_awaitable send(const void* buffer, std::size_t size,
std::error_code& ec) noexcept;
[[nodiscard]] socket_send_awaitable send(const void* buffer, std::size_t size, std::error_code& ec) noexcept;
[[nodiscard]] socket_recv_awaitable recv(void* buffer, std::size_t size) noexcept;
[[nodiscard]] socket_recv_awaitable recv(void* buffer, std::size_t size,
std::error_code& ec) noexcept;
[[nodiscard]] socket_recv_awaitable recv(void* buffer, std::size_t size, std::error_code& ec) noexcept;
[[nodiscard]] socket_recv_exact_awaitable recv_exact(void* buffer, std::size_t size) noexcept;
[[nodiscard]] socket_recv_exact_awaitable recv_exact(void* buffer, std::size_t size,
std::error_code& ec) noexcept;
[[nodiscard]] socket_send_to_awaitable send_to(const void* buffer, std::size_t size,
const endpoint& dst_ep) noexcept;
std::error_code& ec) noexcept;
[[nodiscard]] socket_send_to_awaitable send_to(const void* buffer, std::size_t size,
const endpoint& dst_ep, std::error_code& ec) noexcept;
const endpoint& dst_ep) noexcept;
[[nodiscard]] socket_send_to_awaitable send_to(const void* buffer, std::size_t size, const endpoint& dst_ep,
std::error_code& ec) noexcept;
[[nodiscard]] socket_recv_from_awaitable recv_from(void* buffer, std::size_t size) noexcept;
[[nodiscard]] socket_recv_from_awaitable recv_from(void* buffer, std::size_t size,
std::error_code& ec) noexcept;
std::error_code& ec) noexcept;

[[nodiscard]] socket_connect_cancellable_awaitable connect(const endpoint& ep, asyncpp::stop_token st) noexcept;
[[nodiscard]] socket_connect_cancellable_awaitable connect(const endpoint& ep, asyncpp::stop_token st,
Expand Down Expand Up @@ -249,7 +247,7 @@ namespace asyncpp::io {

public:
socket_send_awaitable(socket& sock, const void* buffer, std::size_t size,
std::error_code* ec = nullptr) noexcept
std::error_code* ec = nullptr) noexcept
: socket_awaitable_base{sock}, m_buffer{buffer}, m_size{size}, m_ec{ec} {}
bool await_suspend(coroutine_handle<> hdl);
void await_resume();
Expand All @@ -261,8 +259,7 @@ namespace asyncpp::io {
std::error_code* const m_ec;

public:
socket_recv_awaitable(socket& sock, void* buffer, std::size_t size,
std::error_code* ec = nullptr) noexcept
socket_recv_awaitable(socket& sock, void* buffer, std::size_t size, std::error_code* ec = nullptr) noexcept
: socket_awaitable_base{sock}, m_buffer{buffer}, m_size{size}, m_ec{ec} {}
bool await_suspend(coroutine_handle<> hdl);
size_t await_resume();
Expand All @@ -277,7 +274,7 @@ namespace asyncpp::io {

public:
socket_recv_exact_awaitable(asyncpp::io::socket& sock, void* buffer, std::size_t size,
std::error_code* ec = nullptr) noexcept
std::error_code* ec = nullptr) noexcept
: socket_awaitable_base{sock}, m_buffer{static_cast<unsigned char*>(buffer)}, m_size{size},
m_remaining{size}, m_ec{ec} {}
bool await_suspend(asyncpp::coroutine_handle<> hdl);
Expand Down Expand Up @@ -309,7 +306,7 @@ namespace asyncpp::io {

public:
socket_send_to_awaitable(socket& sock, const void* buffer, std::size_t size, endpoint dst,
std::error_code* ec = nullptr) noexcept
std::error_code* ec = nullptr) noexcept
: socket_awaitable_base{sock}, m_buffer{buffer}, m_size{size}, m_destination{dst}, m_ec{ec} {}
bool await_suspend(coroutine_handle<> hdl);
size_t await_resume();
Expand All @@ -322,8 +319,7 @@ namespace asyncpp::io {
std::error_code* const m_ec;

public:
socket_recv_from_awaitable(socket& sock, void* buffer, std::size_t size,
std::error_code* ec = nullptr) noexcept
socket_recv_from_awaitable(socket& sock, void* buffer, std::size_t size, std::error_code* ec = nullptr) noexcept
: socket_awaitable_base{sock}, m_buffer{buffer}, m_size{size}, m_ec{ec} {}
bool await_suspend(coroutine_handle<> hdl);
std::pair<size_t, endpoint> await_resume();
Expand All @@ -333,14 +329,11 @@ namespace asyncpp::io {
return socket_connect_awaitable(*this, ep);
}

[[nodiscard]] inline socket_connect_awaitable socket::connect(const endpoint& ep,
std::error_code& ec) noexcept {
[[nodiscard]] inline socket_connect_awaitable socket::connect(const endpoint& ep, std::error_code& ec) noexcept {
return socket_connect_awaitable(*this, ep, &ec);
}

[[nodiscard]] inline socket_accept_awaitable socket::accept() noexcept {
return socket_accept_awaitable(*this);
}
[[nodiscard]] inline socket_accept_awaitable socket::accept() noexcept { return socket_accept_awaitable(*this); }

[[nodiscard]] inline socket_accept_error_code_awaitable socket::accept(std::error_code& ec) noexcept {
return socket_accept_error_code_awaitable(*this, ec);
Expand All @@ -351,7 +344,7 @@ namespace asyncpp::io {
}

[[nodiscard]] inline socket_send_awaitable socket::send(const void* buffer, std::size_t size,
std::error_code& ec) noexcept {
std::error_code& ec) noexcept {
return socket_send_awaitable(*this, buffer, size, &ec);
}

Expand All @@ -360,22 +353,21 @@ namespace asyncpp::io {
}

[[nodiscard]] inline socket_recv_awaitable socket::recv(void* buffer, std::size_t size,
std::error_code& ec) noexcept {
std::error_code& ec) noexcept {
return socket_recv_awaitable(*this, buffer, size, &ec);
}

[[nodiscard]] inline socket_recv_exact_awaitable socket::recv_exact(void* buffer,
std::size_t size) noexcept {
[[nodiscard]] inline socket_recv_exact_awaitable socket::recv_exact(void* buffer, std::size_t size) noexcept {
return socket_recv_exact_awaitable(*this, buffer, size);
}

[[nodiscard]] inline socket_recv_exact_awaitable socket::recv_exact(void* buffer, std::size_t size,
std::error_code& ec) noexcept {
std::error_code& ec) noexcept {
return socket_recv_exact_awaitable(*this, buffer, size, &ec);
}

[[nodiscard]] inline socket_send_to_awaitable socket::send_to(const void* buffer, std::size_t size,
const endpoint& dst_ep) noexcept {
const endpoint& dst_ep) noexcept {
return socket_send_to_awaitable(*this, buffer, size, dst_ep);
}

Expand All @@ -384,13 +376,12 @@ namespace asyncpp::io {
return socket_send_to_awaitable(*this, buffer, size, dst_ep, &ec);
}

[[nodiscard]] inline socket_recv_from_awaitable socket::recv_from(void* buffer,
std::size_t size) noexcept {
[[nodiscard]] inline socket_recv_from_awaitable socket::recv_from(void* buffer, std::size_t size) noexcept {
return socket_recv_from_awaitable(*this, buffer, size);
}

[[nodiscard]] inline socket_recv_from_awaitable socket::recv_from(void* buffer, std::size_t size,
std::error_code& ec) noexcept {
std::error_code& ec) noexcept {
return socket_recv_from_awaitable(*this, buffer, size, &ec);
}

Expand Down
6 changes: 3 additions & 3 deletions src/io_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ namespace asyncpp::io::detail {
else if (!engine.empty())
throw std::runtime_error("unknown io engine " + std::string(engine));
}
#ifdef _WIN32
#ifdef _WIN32
return create_io_engine_win32cq();
#else
#else
if (auto uring = create_io_engine_uring(); uring != nullptr) return uring;
return create_io_engine_select();
#endif
#endif
}
} // namespace asyncpp::io::detail
6 changes: 4 additions & 2 deletions src/io_engine_select.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ namespace asyncpp::io::detail {
void file_close(file_handle_t fd) override;
uint64_t file_size(file_handle_t fd) override;
bool enqueue_readv(file_handle_t fd, void* buf, size_t len, uint64_t offset, completion_data* cd) override;
bool enqueue_writev(file_handle_t fd, const void* buf, size_t len, uint64_t offset, completion_data* cd) override;
bool enqueue_writev(file_handle_t fd, const void* buf, size_t len, uint64_t offset,
completion_data* cd) override;
bool enqueue_fsync(file_handle_t fd, fsync_flags flags, completion_data* cd) override;

bool cancel(completion_data* cd) override;
Expand Down Expand Up @@ -581,7 +582,8 @@ namespace asyncpp::io::detail {
#endif
}

bool io_engine_select::enqueue_readv(file_handle_t fd, void* buf, size_t len, uint64_t offset, completion_data* cd) {
bool io_engine_select::enqueue_readv(file_handle_t fd, void* buf, size_t len, uint64_t offset,
completion_data* cd) {
// There is no way to do async file io on linux without uring, so just do the read inline
auto res = pread(fd, buf, len, offset);
if (res >= 0) {
Expand Down
8 changes: 5 additions & 3 deletions src/io_engine_uring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ namespace asyncpp::io::detail {
} // namespace asyncpp::io::detail
#else

#include <asm/unistd_64.h>
#include <cstring>
#include <liburing.h>
#include <mutex>

#include <asm/unistd_64.h>
#include <liburing.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/time.h>
Expand Down Expand Up @@ -54,7 +55,8 @@ namespace asyncpp::io::detail {
void file_close(file_handle_t fd) override;
uint64_t file_size(file_handle_t fd) override;
bool enqueue_readv(file_handle_t fd, void* buf, size_t len, uint64_t offset, completion_data* cd) override;
bool enqueue_writev(file_handle_t fd, const void* buf, size_t len, uint64_t offset, completion_data* cd) override;
bool enqueue_writev(file_handle_t fd, const void* buf, size_t len, uint64_t offset,
completion_data* cd) override;
bool enqueue_fsync(file_handle_t fd, fsync_flags flags, completion_data* cd) override;

bool cancel(completion_data* cd) override;
Expand Down
4 changes: 2 additions & 2 deletions test/tls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ TEST(ASYNCPP_IO, TLSRoundtrip) {
std::cout.sync_with_stdio(true);
// Generate cert if missing
if (!std::filesystem::exists("ssl.crt") || !std::filesystem::exists("ssl.key")) {
#ifdef _WIN32
#ifdef _WIN32
GTEST_SKIP() << "Can not generate certs on windows";
#endif
#endif
std::cout << "Generating temporary cert..." << std::endl;
system("openssl req -x509 -newkey rsa:2048 -keyout ssl.key -out ssl.crt -sha256 -days 2 -nodes -subj "
"\"/C=XX/ST=StateName/L=SomeCity/O=ASYNCPP/OU=ASYNCPP-TEST/CN=server1\"");
Expand Down

0 comments on commit 332ec04

Please sign in to comment.