From ff01b716711b97c2fcaa709ea97f7650f106aa10 Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Fri, 7 Oct 2016 14:13:19 +0200 Subject: Added ASIO networking library (nw) --- .../asio/src/examples/cpp03/spawn/echo_server.cpp | 122 +++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 3rdparty/asio/src/examples/cpp03/spawn/echo_server.cpp (limited to '3rdparty/asio/src/examples/cpp03/spawn/echo_server.cpp') diff --git a/3rdparty/asio/src/examples/cpp03/spawn/echo_server.cpp b/3rdparty/asio/src/examples/cpp03/spawn/echo_server.cpp new file mode 100644 index 00000000000..415f6da8f33 --- /dev/null +++ b/3rdparty/asio/src/examples/cpp03/spawn/echo_server.cpp @@ -0,0 +1,122 @@ +// +// 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 +#include +#include +#include +#include +#include +#include +#include +#include + +using asio::ip::tcp; + +class session : public boost::enable_shared_from_this +{ +public: + explicit session(asio::io_context& io_context) + : 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(), _1)); + asio::spawn(strand_, + boost::bind(&session::timeout, + shared_from_this(), _1)); + } + +private: + void echo(asio::yield_context yield) + { + try + { + char data[128]; + for (;;) + { + timer_.expires_from_now(boost::posix_time::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_.expires_from_now() <= boost::posix_time::seconds(0)) + socket_.close(); + } + } + + asio::io_context::strand strand_; + tcp::socket socket_; + asio::deadline_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 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 \n"; + return 1; + } + + asio::io_context io_context; + + asio::spawn(io_context, + boost::bind(do_accept, + boost::ref(io_context), atoi(argv[1]), _1)); + + io_context.run(); + } + catch (std::exception& e) + { + std::cerr << "Exception: " << e.what() << "\n"; + } + + return 0; +} -- cgit v1.2.3-70-g09d2