summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/asio/src/examples/cpp11/spawn
diff options
context:
space:
mode:
Diffstat (limited to '3rdparty/asio/src/examples/cpp11/spawn')
-rw-r--r--3rdparty/asio/src/examples/cpp11/spawn/.gitignore12
-rw-r--r--3rdparty/asio/src/examples/cpp11/spawn/echo_server.cpp108
-rw-r--r--3rdparty/asio/src/examples/cpp11/spawn/parallel_grep.cpp84
3 files changed, 204 insertions, 0 deletions
diff --git a/3rdparty/asio/src/examples/cpp11/spawn/.gitignore b/3rdparty/asio/src/examples/cpp11/spawn/.gitignore
new file mode 100644
index 00000000000..6b8a121e099
--- /dev/null
+++ b/3rdparty/asio/src/examples/cpp11/spawn/.gitignore
@@ -0,0 +1,12 @@
+.deps
+.dirstamp
+parallel_grep
+*.o
+*.obj
+*.exe
+*_server
+*_client
+*.ilk
+*.manifest
+*.pdb
+*.tds
diff --git a/3rdparty/asio/src/examples/cpp11/spawn/echo_server.cpp b/3rdparty/asio/src/examples/cpp11/spawn/echo_server.cpp
new file mode 100644
index 00000000000..5397a4efe22
--- /dev/null
+++ b/3rdparty/asio/src/examples/cpp11/spawn/echo_server.cpp
@@ -0,0 +1,108 @@
+//
+// echo_server.cpp
+// ~~~~~~~~~~~~~~~
+//
+// Copyright (c) 2003-2016 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 <iostream>
+#include <memory>
+
+using asio::ip::tcp;
+
+class session : public std::enable_shared_from_this<session>
+{
+public:
+ explicit session(tcp::socket socket)
+ : socket_(std::move(socket)),
+ timer_(socket_.get_io_context()),
+ strand_(socket_.get_io_context())
+ {
+ }
+
+ void go()
+ {
+ auto self(shared_from_this());
+ asio::spawn(strand_,
+ [this, self](asio::yield_context yield)
+ {
+ try
+ {
+ char data[128];
+ for (;;)
+ {
+ timer_.expires_from_now(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);
+ }
+ }
+ catch (std::exception& e)
+ {
+ socket_.close();
+ timer_.cancel();
+ }
+ });
+
+ asio::spawn(strand_,
+ [this, self](asio::yield_context yield)
+ {
+ while (socket_.is_open())
+ {
+ asio::error_code ignored_ec;
+ timer_.async_wait(yield[ignored_ec]);
+ if (timer_.expires_from_now() <= std::chrono::seconds(0))
+ socket_.close();
+ }
+ });
+ }
+
+private:
+ tcp::socket socket_;
+ asio::steady_timer timer_;
+ asio::io_context::strand strand_;
+};
+
+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,
+ [&](asio::yield_context yield)
+ {
+ tcp::acceptor acceptor(io_context,
+ tcp::endpoint(tcp::v4(), std::atoi(argv[1])));
+
+ for (;;)
+ {
+ asio::error_code ec;
+ tcp::socket socket(io_context);
+ acceptor.async_accept(socket, yield[ec]);
+ if (!ec) std::make_shared<session>(std::move(socket))->go();
+ }
+ });
+
+ io_context.run();
+ }
+ catch (std::exception& e)
+ {
+ std::cerr << "Exception: " << e.what() << "\n";
+ }
+
+ return 0;
+}
diff --git a/3rdparty/asio/src/examples/cpp11/spawn/parallel_grep.cpp b/3rdparty/asio/src/examples/cpp11/spawn/parallel_grep.cpp
new file mode 100644
index 00000000000..954d9472bbe
--- /dev/null
+++ b/3rdparty/asio/src/examples/cpp11/spawn/parallel_grep.cpp
@@ -0,0 +1,84 @@
+//
+// parallel_grep.cpp
+// ~~~~~~~~~~~~~~~~~
+//
+// Copyright (c) 2003-2016 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/dispatch.hpp>
+#include <asio/post.hpp>
+#include <asio/spawn.hpp>
+#include <asio/strand.hpp>
+#include <asio/thread_pool.hpp>
+#include <fstream>
+#include <iostream>
+#include <string>
+
+using asio::dispatch;
+using asio::spawn;
+using asio::strand;
+using asio::thread_pool;
+using asio::yield_context;
+
+int main(int argc, char* argv[])
+{
+ try
+ {
+ if (argc < 2)
+ {
+ std::cerr << "Usage: parallel_grep <string> <files...>\n";
+ return 1;
+ }
+
+ // We use a fixed size pool of threads for reading the input files. The
+ // number of threads is automatically determined based on the number of
+ // CPUs available in the system.
+ thread_pool pool;
+
+ // To prevent the output from being garbled, we use a strand to synchronise
+ // printing.
+ strand<thread_pool::executor_type> output_strand(pool.get_executor());
+
+ // Spawn a new coroutine for each file specified on the command line.
+ std::string search_string = argv[1];
+ for (int argn = 2; argn < argc; ++argn)
+ {
+ std::string input_file = argv[argn];
+ spawn(pool,
+ [=](yield_context yield)
+ {
+ std::ifstream is(input_file.c_str());
+ std::string line;
+ std::size_t line_num = 0;
+ while (std::getline(is, line))
+ {
+ // If we find a match, send a message to the output.
+ if (line.find(search_string) != std::string::npos)
+ {
+ dispatch(output_strand,
+ [=]
+ {
+ std::cout << input_file << ':' << line << std::endl;
+ });
+ }
+
+ // Every so often we yield control to another coroutine.
+ if (++line_num % 10 == 0)
+ post(yield);
+ }
+ });
+ }
+
+ // Join the thread pool to wait for all the spawned tasks to complete.
+ pool.join();
+ }
+ catch (std::exception& e)
+ {
+ std::cerr << "Exception: " << e.what() << "\n";
+ }
+
+ return 0;
+}