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/local/connect_pair.cpp | 141 +++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 3rdparty/asio/src/examples/cpp03/local/connect_pair.cpp (limited to '3rdparty/asio/src/examples/cpp03/local/connect_pair.cpp') diff --git a/3rdparty/asio/src/examples/cpp03/local/connect_pair.cpp b/3rdparty/asio/src/examples/cpp03/local/connect_pair.cpp new file mode 100644 index 00000000000..92e0ae866d5 --- /dev/null +++ b/3rdparty/asio/src/examples/cpp03/local/connect_pair.cpp @@ -0,0 +1,141 @@ +// +// connect_pair.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 + +#if defined(ASIO_HAS_LOCAL_SOCKETS) + +using asio::local::stream_protocol; + +class uppercase_filter +{ +public: + uppercase_filter(asio::io_context& io_context) + : socket_(io_context) + { + } + + stream_protocol::socket& socket() + { + return socket_; + } + + void start() + { + // Wait for request. + socket_.async_read_some(asio::buffer(data_), + boost::bind(&uppercase_filter::handle_read, + this, asio::placeholders::error, + asio::placeholders::bytes_transferred)); + } + +private: + void handle_read(const asio::error_code& ec, std::size_t size) + { + if (!ec) + { + // Compute result. + for (std::size_t i = 0; i < size; ++i) + data_[i] = std::toupper(data_[i]); + + // Send result. + asio::async_write(socket_, asio::buffer(data_, size), + boost::bind(&uppercase_filter::handle_write, + this, asio::placeholders::error)); + } + else + { + throw asio::system_error(ec); + } + } + + void handle_write(const asio::error_code& ec) + { + if (!ec) + { + // Wait for request. + socket_.async_read_some(asio::buffer(data_), + boost::bind(&uppercase_filter::handle_read, + this, asio::placeholders::error, + asio::placeholders::bytes_transferred)); + } + else + { + throw asio::system_error(ec); + } + } + + stream_protocol::socket socket_; + boost::array data_; +}; + +void run(asio::io_context* io_context) +{ + try + { + io_context->run(); + } + catch (std::exception& e) + { + std::cerr << "Exception in thread: " << e.what() << "\n"; + std::exit(1); + } +} + +int main() +{ + try + { + asio::io_context io_context; + + // Create filter and establish a connection to it. + uppercase_filter filter(io_context); + stream_protocol::socket socket(io_context); + asio::local::connect_pair(socket, filter.socket()); + filter.start(); + + // The io_context runs in a background thread to perform filtering. + asio::thread thread(boost::bind(run, &io_context)); + + for (;;) + { + // Collect request from user. + std::cout << "Enter a string: "; + std::string request; + std::getline(std::cin, request); + + // Send request to filter. + asio::write(socket, asio::buffer(request)); + + // Wait for reply from filter. + std::vector reply(request.size()); + asio::read(socket, asio::buffer(reply)); + + // Show reply to user. + std::cout << "Result: "; + std::cout.write(&reply[0], request.size()); + std::cout << std::endl; + } + } + catch (std::exception& e) + { + std::cerr << "Exception: " << e.what() << "\n"; + std::exit(1); + } +} + +#else // defined(ASIO_HAS_LOCAL_SOCKETS) +# error Local sockets not available on this platform. +#endif // defined(ASIO_HAS_LOCAL_SOCKETS) -- cgit v1.2.3-70-g09d2