Skip to content

Commit

Permalink
Merge pull request #136 from redboltz/refine_cpp20coro_stub_socket
Browse files Browse the repository at this point in the history
Refined cpp20coro_stub_socket.
  • Loading branch information
redboltz authored Feb 20, 2024
2 parents 38633d3 + 27dc24d commit 1bfc8f6
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions test/unit/cpp20coro_stub_socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,21 @@ namespace async_mqtt {

namespace as = boost::asio;

struct cpp20coro_stub_socket {
template <std::size_t PacketIdBytes>
struct cpp20coro_basic_stub_socket {
using executor_type = as::any_io_executor;
using packet_iterator_t = packet_iterator<std::vector, as::const_buffer>;
using packet_range = std::pair<packet_iterator_t, packet_iterator_t>;

cpp20coro_stub_socket(
cpp20coro_basic_stub_socket(
protocol_version version,
as::any_io_executor exe
)
:version_{version},
exe_{force_move(exe)}
{}

cpp20coro_stub_socket(
cpp20coro_basic_stub_socket(
protocol_version version,
as::io_context& ioc
)
Expand All @@ -41,7 +42,7 @@ struct cpp20coro_stub_socket {

template <typename CompletionToken>
auto emulate_recv(
packet_variant pv,
basic_packet_variant<PacketIdBytes> pv,
CompletionToken&& token
) {
return as::async_initiate<
Expand All @@ -50,7 +51,7 @@ struct cpp20coro_stub_socket {
>(
[this](
auto completion_handler,
packet_variant pv
basic_packet_variant<PacketIdBytes> pv
) {
ch_recv_.async_send(
pv,
Expand All @@ -77,14 +78,14 @@ struct cpp20coro_stub_socket {
auto wait_response(CompletionToken&& token) {
return as::async_initiate<
CompletionToken,
void(packet_variant)
void(basic_packet_variant<PacketIdBytes>)
>(
[this](
auto completion_handler
) {
ch_send_.async_receive(
[completion_handler = force_move(completion_handler)]
(packet_variant pv) mutable {
(basic_packet_variant<PacketIdBytes> pv) mutable {
force_move(completion_handler)(force_move(pv));
}
);
Expand Down Expand Up @@ -113,7 +114,7 @@ struct cpp20coro_stub_socket {
void close(error_code&) {
open_ = false;
ch_send_.async_send(
packet_variant{errc::make_error_code(errc::connection_reset)},
basic_packet_variant<PacketIdBytes>{errc::make_error_code(errc::connection_reset)},
[](auto) {
}
);
Expand All @@ -135,7 +136,7 @@ struct cpp20coro_stub_socket {
auto it = as::buffers_iterator<ConstBufferSequence>::begin(buffers);
auto end = as::buffers_iterator<ConstBufferSequence>::end(buffers);
auto buf = allocate_buffer(it, end);
auto pv = buffer_to_packet_variant(buf, version_);
auto pv = buffer_to_basic_packet_variant<PacketIdBytes>(buf, version_);
as::post(
as::bind_executor(
exe_,
Expand Down Expand Up @@ -220,15 +221,15 @@ struct cpp20coro_stub_socket {
token = std::forward<CompletionToken>(token),
partial_read
]
(packet_variant pv) mutable {
(basic_packet_variant<PacketIdBytes> pv) mutable {
if (!pv) {
auto exe = as::get_associated_executor(token);
as::post(
as::bind_executor(
exe,
[
token = force_move(token),
code = pv.get<system_error>().code()
code = pv.template get<system_error>().code()
] () mutable {
token(code, 0);
}
Expand All @@ -245,17 +246,19 @@ struct cpp20coro_stub_socket {
}

private:
using channel_t = as::experimental::channel<void(packet_variant)>;
using channel_t = as::experimental::channel<void(basic_packet_variant<PacketIdBytes>)>;
protocol_version version_;
as::any_io_executor exe_;
packet_variant pv_;
basic_packet_variant<PacketIdBytes> pv_;
std::vector<as::const_buffer> cbs_;
optional<packet_range> pv_r_;
bool open_ = true;
channel_t ch_recv_{exe_, 1};
channel_t ch_send_{exe_, 1};
};

using cpp20coro_stub_socket = cpp20coro_basic_stub_socket<2>;

template <typename MutableBufferSequence, typename CompletionToken>
void async_read(
cpp20coro_stub_socket& socket,
Expand All @@ -265,6 +268,15 @@ void async_read(
socket.async_read_some(mb, std::forward<CompletionToken>(token));
}

template <typename MutableBufferSequence, typename CompletionToken>
void async_read(
cpp20coro_basic_stub_socket<4>& socket,
MutableBufferSequence const& mb,
CompletionToken&& token
) {
socket.async_read_some(mb, std::forward<CompletionToken>(token));
}

inline bool is_close(packet_variant const& pv) {
if (pv) {
BOOST_TEST_MESSAGE("close expected but receive packet: " << pv);
Expand All @@ -273,6 +285,14 @@ inline bool is_close(packet_variant const& pv) {
return pv.get<system_error>().code() == errc::make_error_code(errc::connection_reset);
}

inline bool is_close(basic_packet_variant<4> const& pv) {
if (pv) {
BOOST_TEST_MESSAGE("close expected but receive packet: " << pv);
return false; // pv is packet
}
return pv.get<system_error>().code() == errc::make_error_code(errc::connection_reset);
}

} // namespace async_mqtt

#endif // ASYNC_MQTT_CPP20CORO_STUB_SOCKET_HPP

0 comments on commit 1bfc8f6

Please sign in to comment.