mirror of
https://github.com/Naios/continuable.git
synced 2025-12-06 08:46:44 +08:00
Add a small asio example
This commit is contained in:
parent
76f3fb0380
commit
c75baaec90
@ -1 +1 @@
|
||||
add_subdirectory(asio-banking)
|
||||
add_subdirectory(example-asio)
|
||||
|
||||
@ -1,11 +0,0 @@
|
||||
add_executable(example-asio-banking
|
||||
${CMAKE_CURRENT_LIST_DIR}/example-asio-banking.cpp)
|
||||
|
||||
target_include_directories(example-asio-banking
|
||||
PRIVATE
|
||||
${CMAKE_CURRENT_LIST_DIR})
|
||||
|
||||
target_link_libraries(example-asio-banking
|
||||
PRIVATE
|
||||
asio
|
||||
continuable)
|
||||
15
examples/example-asio/CMakeLists.txt
Normal file
15
examples/example-asio/CMakeLists.txt
Normal file
@ -0,0 +1,15 @@
|
||||
add_executable(example-asio
|
||||
${CMAKE_CURRENT_LIST_DIR}/example-asio.cpp)
|
||||
|
||||
target_include_directories(example-asio
|
||||
PRIVATE
|
||||
${CMAKE_CURRENT_LIST_DIR})
|
||||
|
||||
target_link_libraries(example-asio
|
||||
PRIVATE
|
||||
asio
|
||||
continuable)
|
||||
|
||||
target_compile_definitions(example-asio
|
||||
PUBLIC
|
||||
-DCONTINUABLE_WITH_NO_EXCEPTIONS)
|
||||
@ -28,7 +28,11 @@
|
||||
SOFTWARE.
|
||||
**/
|
||||
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <system_error>
|
||||
|
||||
#include <asio.hpp>
|
||||
|
||||
@ -37,35 +41,68 @@
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
struct functional_io_service : asio::io_service {
|
||||
asio::ip::udp::resolver resolver_;
|
||||
|
||||
functional_io_service() : resolver_(*this) {
|
||||
}
|
||||
|
||||
auto post() const noexcept {
|
||||
return [this](auto&& work) {
|
||||
asio::io_service::post(std::forward<decltype(work)>(work));
|
||||
};
|
||||
}
|
||||
|
||||
auto wait_async(std::chrono::milliseconds time) {
|
||||
return cti::make_continuable<void>([](auto&& promise) {
|
||||
// ...
|
||||
promise.set_value();
|
||||
});
|
||||
}
|
||||
|
||||
template <typename Protocol>
|
||||
auto async_resolve(Protocol&& protocol, std::string host,
|
||||
std::string service) {
|
||||
using asio::ip::udp;
|
||||
|
||||
return cti::make_continuable<udp::resolver::iterator>([
|
||||
this, protocol = std::forward<Protocol>(protocol), host = std::move(host),
|
||||
service = std::move(service)
|
||||
](auto&& promise) mutable {
|
||||
resolver_.async_resolve(
|
||||
host, service,
|
||||
[promise = std::forward<decltype(promise)>(promise)](
|
||||
std::error_code const& error,
|
||||
udp::resolver::iterator iterator) mutable {
|
||||
|
||||
if (error) {
|
||||
promise.set_exception(
|
||||
std::error_condition(error.value(), error.category()));
|
||||
} else {
|
||||
promise.set_value(std::move(iterator));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
static functional_io_service service;
|
||||
|
||||
auto wait_async(std::chrono::milliseconds time) {
|
||||
return cti::make_continuable<void>([](auto&& promise) {
|
||||
// ...
|
||||
promise.set_value();
|
||||
});
|
||||
}
|
||||
|
||||
int main(int, char**) {
|
||||
wait_async(10s)
|
||||
.then([] {
|
||||
// ...
|
||||
using asio::ip::udp;
|
||||
|
||||
return wait_async(2s);
|
||||
functional_io_service service;
|
||||
|
||||
service.async_resolve(udp::v4(), "127.0.0.1", "daytime")
|
||||
.then([](udp::resolver::iterator iterator) {
|
||||
// ...
|
||||
return *iterator;
|
||||
})
|
||||
.then([] {
|
||||
.then([](udp::endpoint endpoint) {
|
||||
// auto socket = std::make_shared<udp::socket>(service);
|
||||
// socket->async_send_to()
|
||||
})
|
||||
.fail([](cti::error_type error) {
|
||||
// ...
|
||||
});
|
||||
|
||||
asio::tim
|
||||
|
||||
service.run();
|
||||
return 0;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user