mirror of
https://github.com/Naios/continuable.git
synced 2025-12-07 09:16:46 +08:00
update example for changes
This commit is contained in:
parent
87bf8c0bd9
commit
6470c612b4
@ -28,13 +28,11 @@
|
|||||||
SOFTWARE.
|
SOFTWARE.
|
||||||
**/
|
**/
|
||||||
|
|
||||||
#define ASIO_HAS_RETURN_TYPE_DEDUCTION
|
#include <asio.hpp>
|
||||||
|
|
||||||
#include <continuable/continuable.hpp>
|
#include <continuable/continuable.hpp>
|
||||||
#include <continuable/support/asio.hpp>
|
#include <continuable/support/asio.hpp>
|
||||||
|
|
||||||
#include <asio.hpp>
|
|
||||||
|
|
||||||
// Queries the NIST daytime service and prints the current date and time
|
// Queries the NIST daytime service and prints the current date and time
|
||||||
void daytime_service();
|
void daytime_service();
|
||||||
|
|
||||||
@ -42,6 +40,12 @@ void daytime_service();
|
|||||||
// `asio::error::operation_aborted`
|
// `asio::error::operation_aborted`
|
||||||
void cancelled_async_wait();
|
void cancelled_async_wait();
|
||||||
|
|
||||||
|
// Indicates fatal error due to an unexpected failure in the continuation chain.
|
||||||
|
void unexpected_error(cti::exception_t);
|
||||||
|
|
||||||
|
// Check that the failure was an aborted operation, as expected.
|
||||||
|
void check_aborted_operation(cti::exception_t);
|
||||||
|
|
||||||
int main(int, char**) {
|
int main(int, char**) {
|
||||||
daytime_service();
|
daytime_service();
|
||||||
cancelled_async_wait();
|
cancelled_async_wait();
|
||||||
@ -57,16 +61,16 @@ void daytime_service() {
|
|||||||
tcp::socket socket(ioc);
|
tcp::socket socket(ioc);
|
||||||
std::string buf;
|
std::string buf;
|
||||||
|
|
||||||
resolver.async_resolve("time.nist.gov", "daytime", asio::use_cti)
|
resolver.async_resolve("time.nist.gov", "daytime", cti::asio_token)
|
||||||
.then([&socket](tcp::resolver::results_type endpoints) {
|
.then([&socket](tcp::resolver::results_type endpoints) {
|
||||||
return asio::async_connect(socket, endpoints, asio::use_cti);
|
return asio::async_connect(socket, endpoints, cti::asio_token);
|
||||||
})
|
})
|
||||||
.then([&socket, &buf] {
|
.then([&socket, &buf] {
|
||||||
return asio::async_read_until(socket, asio::dynamic_buffer(buf), '\n',
|
return asio::async_read_until(socket, asio::dynamic_buffer(buf), '\n',
|
||||||
asio::use_cti);
|
cti::asio_token);
|
||||||
})
|
})
|
||||||
.then([&buf](std::size_t) { puts(buf.data()); })
|
.then([&buf](std::size_t) { puts(buf.data()); })
|
||||||
.fail([](cti::exception_t e) { std::rethrow_exception(e); });
|
.fail(&unexpected_error);
|
||||||
|
|
||||||
ioc.run();
|
ioc.run();
|
||||||
}
|
}
|
||||||
@ -77,20 +81,49 @@ void cancelled_async_wait() {
|
|||||||
|
|
||||||
t.expires_after(std::chrono::seconds(999));
|
t.expires_after(std::chrono::seconds(999));
|
||||||
|
|
||||||
t.async_wait(asio::use_cti)
|
t.async_wait(cti::asio_token)
|
||||||
.then([] { throw std::logic_error("Unexpected continuation call"); })
|
.then([] {
|
||||||
.fail([](cti::exception_t ep) {
|
puts("This should never be called");
|
||||||
try {
|
std::terminate();
|
||||||
std::rethrow_exception(ep);
|
})
|
||||||
} catch (asio::system_error const& ex) {
|
.fail(&check_aborted_operation);
|
||||||
if (ex.code() == asio::error::operation_aborted) {
|
|
||||||
puts("async_wait failed with expected cancellation error");
|
|
||||||
} else {
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
t.cancel_one();
|
t.cancel_one();
|
||||||
ioc.run();
|
ioc.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void unexpected_error(cti::exception_t e) {
|
||||||
|
#if defined(CONTINUABLE_HAS_EXCEPTIONS)
|
||||||
|
std::rethrow_exception(e);
|
||||||
|
#else
|
||||||
|
puts("Continuation failed with unexpected error");
|
||||||
|
puts(e.message().data());
|
||||||
|
std::terminate();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void check_aborted_operation(cti::exception_t ex) {
|
||||||
|
auto is_expected_error = [](auto err_val) {
|
||||||
|
if (err_val == asio::error_code(asio::error::operation_aborted)) {
|
||||||
|
puts("Continuation failed due to aborted async operation, as expected.");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
#if defined(CONTINUABLE_HAS_EXCEPTIONS)
|
||||||
|
try {
|
||||||
|
std::rethrow_exception(ex);
|
||||||
|
} catch (asio::system_error const& err) {
|
||||||
|
if (is_expected_error(err.code())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
if (is_expected_error(ex)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
unexpected_error(ex);
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user