summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/asio/src/examples/cpp11/spawn/echo_server.cpp
diff options
context:
space:
mode:
Diffstat (limited to '3rdparty/asio/src/examples/cpp11/spawn/echo_server.cpp')
-rw-r--r--3rdparty/asio/src/examples/cpp11/spawn/echo_server.cpp33
1 files changed, 21 insertions, 12 deletions
diff --git a/3rdparty/asio/src/examples/cpp11/spawn/echo_server.cpp b/3rdparty/asio/src/examples/cpp11/spawn/echo_server.cpp
index 5397a4efe22..ae949d60bef 100644
--- a/3rdparty/asio/src/examples/cpp11/spawn/echo_server.cpp
+++ b/3rdparty/asio/src/examples/cpp11/spawn/echo_server.cpp
@@ -2,12 +2,13 @@
// echo_server.cpp
// ~~~~~~~~~~~~~~~
//
-// Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
+#include <asio/detached.hpp>
#include <asio/io_context.hpp>
#include <asio/ip/tcp.hpp>
#include <asio/spawn.hpp>
@@ -21,10 +22,10 @@ using asio::ip::tcp;
class session : public std::enable_shared_from_this<session>
{
public:
- explicit session(tcp::socket socket)
+ explicit session(asio::io_context& io_context, tcp::socket socket)
: socket_(std::move(socket)),
- timer_(socket_.get_io_context()),
- strand_(socket_.get_io_context())
+ timer_(io_context),
+ strand_(io_context.get_executor())
{
}
@@ -39,7 +40,7 @@ public:
char data[128];
for (;;)
{
- timer_.expires_from_now(std::chrono::seconds(10));
+ timer_.expires_after(std::chrono::seconds(10));
std::size_t n = socket_.async_read_some(asio::buffer(data), yield);
asio::async_write(socket_, asio::buffer(data, n), yield);
}
@@ -49,25 +50,25 @@ public:
socket_.close();
timer_.cancel();
}
- });
+ }, asio::detached);
asio::spawn(strand_,
[this, self](asio::yield_context yield)
{
while (socket_.is_open())
{
- asio::error_code ignored_ec;
+ std::error_code ignored_ec;
timer_.async_wait(yield[ignored_ec]);
- if (timer_.expires_from_now() <= std::chrono::seconds(0))
+ if (timer_.expiry() <= asio::steady_timer::clock_type::now())
socket_.close();
}
- });
+ }, asio::detached);
}
private:
tcp::socket socket_;
asio::steady_timer timer_;
- asio::io_context::strand strand_;
+ asio::strand<asio::io_context::executor_type> strand_;
};
int main(int argc, char* argv[])
@@ -90,11 +91,19 @@ int main(int argc, char* argv[])
for (;;)
{
- asio::error_code ec;
+ std::error_code ec;
tcp::socket socket(io_context);
acceptor.async_accept(socket, yield[ec]);
- if (!ec) std::make_shared<session>(std::move(socket))->go();
+ if (!ec)
+ {
+ std::make_shared<session>(io_context, std::move(socket))->go();
+ }
}
+ },
+ [](std::exception_ptr e)
+ {
+ if (e)
+ std::rethrow_exception(e);
});
io_context.run();