summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/asio/src/examples/cpp03/spawn/echo_server.cpp
diff options
context:
space:
mode:
author Vas Crabb <vas@vastheman.com>2024-04-22 08:04:58 +1000
committer Vas Crabb <vas@vastheman.com>2024-04-22 08:04:58 +1000
commit184292b730f4236bd4840e780fdad97ee060ec84 (patch)
tree67902161a465fe92aec78ca502de07092b8cde0f /3rdparty/asio/src/examples/cpp03/spawn/echo_server.cpp
parent24154bc1f00790f344120b3a85175d6f616c5ad0 (diff)
3rdparty/asio: Updated to 1.30.2
Diffstat (limited to '3rdparty/asio/src/examples/cpp03/spawn/echo_server.cpp')
-rw-r--r--3rdparty/asio/src/examples/cpp03/spawn/echo_server.cpp122
1 files changed, 0 insertions, 122 deletions
diff --git a/3rdparty/asio/src/examples/cpp03/spawn/echo_server.cpp b/3rdparty/asio/src/examples/cpp03/spawn/echo_server.cpp
deleted file mode 100644
index 4549b446245..00000000000
--- a/3rdparty/asio/src/examples/cpp03/spawn/echo_server.cpp
+++ /dev/null
@@ -1,122 +0,0 @@
-//
-// echo_server.cpp
-// ~~~~~~~~~~~~~~~
-//
-// Copyright (c) 2003-2021 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/io_context.hpp>
-#include <asio/ip/tcp.hpp>
-#include <asio/spawn.hpp>
-#include <asio/steady_timer.hpp>
-#include <asio/write.hpp>
-#include <boost/bind/bind.hpp>
-#include <boost/shared_ptr.hpp>
-#include <boost/enable_shared_from_this.hpp>
-#include <iostream>
-
-using asio::ip::tcp;
-
-class session : public boost::enable_shared_from_this<session>
-{
-public:
- explicit session(asio::io_context& io_context)
- : strand_(asio::make_strand(io_context)),
- socket_(io_context),
- timer_(io_context)
- {
- }
-
- tcp::socket& socket()
- {
- return socket_;
- }
-
- void go()
- {
- asio::spawn(strand_,
- boost::bind(&session::echo,
- shared_from_this(), boost::placeholders::_1));
- asio::spawn(strand_,
- boost::bind(&session::timeout,
- shared_from_this(), boost::placeholders::_1));
- }
-
-private:
- void echo(asio::yield_context yield)
- {
- try
- {
- char data[128];
- for (;;)
- {
- timer_.expires_after(asio::chrono::seconds(10));
- std::size_t n = socket_.async_read_some(asio::buffer(data), yield);
- asio::async_write(socket_, asio::buffer(data, n), yield);
- }
- }
- catch (std::exception& e)
- {
- socket_.close();
- timer_.cancel();
- }
- }
-
- void timeout(asio::yield_context yield)
- {
- while (socket_.is_open())
- {
- asio::error_code ignored_ec;
- timer_.async_wait(yield[ignored_ec]);
- if (timer_.expiry() <= asio::steady_timer::clock_type::now())
- socket_.close();
- }
- }
-
- asio::strand<asio::io_context::executor_type> strand_;
- tcp::socket socket_;
- asio::steady_timer timer_;
-};
-
-void do_accept(asio::io_context& io_context,
- unsigned short port, asio::yield_context yield)
-{
- tcp::acceptor acceptor(io_context, tcp::endpoint(tcp::v4(), port));
-
- for (;;)
- {
- asio::error_code ec;
- boost::shared_ptr<session> new_session(new session(io_context));
- acceptor.async_accept(new_session->socket(), yield[ec]);
- if (!ec) new_session->go();
- }
-}
-
-int main(int argc, char* argv[])
-{
- try
- {
- if (argc != 2)
- {
- std::cerr << "Usage: echo_server <port>\n";
- return 1;
- }
-
- asio::io_context io_context;
-
- asio::spawn(io_context,
- boost::bind(do_accept,
- boost::ref(io_context), atoi(argv[1]), boost::placeholders::_1));
-
- io_context.run();
- }
- catch (std::exception& e)
- {
- std::cerr << "Exception: " << e.what() << "\n";
- }
-
- return 0;
-}