diff options
Diffstat (limited to '3rdparty/asio/src/examples/cpp03/local')
5 files changed, 418 insertions, 0 deletions
diff --git a/3rdparty/asio/src/examples/cpp03/local/.gitignore b/3rdparty/asio/src/examples/cpp03/local/.gitignore new file mode 100644 index 00000000000..688277b48d0 --- /dev/null +++ b/3rdparty/asio/src/examples/cpp03/local/.gitignore @@ -0,0 +1,13 @@ +.deps +.dirstamp +*.o +*.obj +*.exe +connect_pair +stream_server +stream_client +iostream_client +*.ilk +*.manifest +*.pdb +*.tds 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 <iostream> +#include <string> +#include <cctype> +#include <asio.hpp> +#include <boost/array.hpp> +#include <boost/bind.hpp> + +#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<char, 512> 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<char> 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) diff --git a/3rdparty/asio/src/examples/cpp03/local/iostream_client.cpp b/3rdparty/asio/src/examples/cpp03/local/iostream_client.cpp new file mode 100644 index 00000000000..d28d29e88c1 --- /dev/null +++ b/3rdparty/asio/src/examples/cpp03/local/iostream_client.cpp @@ -0,0 +1,62 @@ +// +// stream_client.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 <cstring> +#include <iostream> +#include "asio.hpp" + +#if defined(ASIO_HAS_LOCAL_SOCKETS) + +using asio::local::stream_protocol; + +enum { max_length = 1024 }; + +int main(int argc, char* argv[]) +{ + try + { + if (argc != 2) + { + std::cerr << "Usage: iostream_client <file>\n"; + return 1; + } + + stream_protocol::endpoint ep(argv[1]); + stream_protocol::iostream s(ep); + if (!s) + { + std::cerr << "Unable to connect: " << s.error().message() << std::endl; + return 1; + } + + using namespace std; // For strlen. + std::cout << "Enter message: "; + char request[max_length]; + std::cin.getline(request, max_length); + size_t length = strlen(request); + s << request; + + char reply[max_length]; + s.read(reply, length); + std::cout << "Reply is: "; + std::cout.write(reply, length); + std::cout << "\n"; + } + catch (std::exception& e) + { + std::cerr << "Exception: " << e.what() << "\n"; + } + + return 0; +} + +#else // defined(ASIO_HAS_LOCAL_SOCKETS) +# error Local sockets not available on this platform. +#endif // defined(ASIO_HAS_LOCAL_SOCKETS) diff --git a/3rdparty/asio/src/examples/cpp03/local/stream_client.cpp b/3rdparty/asio/src/examples/cpp03/local/stream_client.cpp new file mode 100644 index 00000000000..6374f8fb534 --- /dev/null +++ b/3rdparty/asio/src/examples/cpp03/local/stream_client.cpp @@ -0,0 +1,61 @@ +// +// stream_client.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 <cstdlib> +#include <cstring> +#include <iostream> +#include "asio.hpp" + +#if defined(ASIO_HAS_LOCAL_SOCKETS) + +using asio::local::stream_protocol; + +enum { max_length = 1024 }; + +int main(int argc, char* argv[]) +{ + try + { + if (argc != 2) + { + std::cerr << "Usage: stream_client <file>\n"; + return 1; + } + + asio::io_context io_context; + + stream_protocol::socket s(io_context); + s.connect(stream_protocol::endpoint(argv[1])); + + using namespace std; // For strlen. + std::cout << "Enter message: "; + char request[max_length]; + std::cin.getline(request, max_length); + size_t request_length = strlen(request); + asio::write(s, asio::buffer(request, request_length)); + + char reply[max_length]; + size_t reply_length = asio::read(s, + asio::buffer(reply, request_length)); + std::cout << "Reply is: "; + std::cout.write(reply, reply_length); + std::cout << "\n"; + } + catch (std::exception& e) + { + std::cerr << "Exception: " << e.what() << "\n"; + } + + return 0; +} + +#else // defined(ASIO_HAS_LOCAL_SOCKETS) +# error Local sockets not available on this platform. +#endif // defined(ASIO_HAS_LOCAL_SOCKETS) diff --git a/3rdparty/asio/src/examples/cpp03/local/stream_server.cpp b/3rdparty/asio/src/examples/cpp03/local/stream_server.cpp new file mode 100644 index 00000000000..0bfe2788cae --- /dev/null +++ b/3rdparty/asio/src/examples/cpp03/local/stream_server.cpp @@ -0,0 +1,141 @@ +// +// stream_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 <cstdio> +#include <iostream> +#include <boost/array.hpp> +#include <boost/bind.hpp> +#include <boost/enable_shared_from_this.hpp> +#include <boost/shared_ptr.hpp> +#include "asio.hpp" + +#if defined(ASIO_HAS_LOCAL_SOCKETS) + +using asio::local::stream_protocol; + +class session + : public boost::enable_shared_from_this<session> +{ +public: + session(asio::io_context& io_context) + : socket_(io_context) + { + } + + stream_protocol::socket& socket() + { + return socket_; + } + + void start() + { + socket_.async_read_some(asio::buffer(data_), + boost::bind(&session::handle_read, + shared_from_this(), + asio::placeholders::error, + asio::placeholders::bytes_transferred)); + } + + void handle_read(const asio::error_code& error, + size_t bytes_transferred) + { + if (!error) + { + asio::async_write(socket_, + asio::buffer(data_, bytes_transferred), + boost::bind(&session::handle_write, + shared_from_this(), + asio::placeholders::error)); + } + } + + void handle_write(const asio::error_code& error) + { + if (!error) + { + socket_.async_read_some(asio::buffer(data_), + boost::bind(&session::handle_read, + shared_from_this(), + asio::placeholders::error, + asio::placeholders::bytes_transferred)); + } + } + +private: + // The socket used to communicate with the client. + stream_protocol::socket socket_; + + // Buffer used to store data received from the client. + boost::array<char, 1024> data_; +}; + +typedef boost::shared_ptr<session> session_ptr; + +class server +{ +public: + server(asio::io_context& io_context, const std::string& file) + : io_context_(io_context), + acceptor_(io_context, stream_protocol::endpoint(file)) + { + session_ptr new_session(new session(io_context_)); + acceptor_.async_accept(new_session->socket(), + boost::bind(&server::handle_accept, this, new_session, + asio::placeholders::error)); + } + + void handle_accept(session_ptr new_session, + const asio::error_code& error) + { + if (!error) + { + new_session->start(); + } + + new_session.reset(new session(io_context_)); + acceptor_.async_accept(new_session->socket(), + boost::bind(&server::handle_accept, this, new_session, + asio::placeholders::error)); + } + +private: + asio::io_context& io_context_; + stream_protocol::acceptor acceptor_; +}; + +int main(int argc, char* argv[]) +{ + try + { + if (argc != 2) + { + std::cerr << "Usage: stream_server <file>\n"; + std::cerr << "*** WARNING: existing file is removed ***\n"; + return 1; + } + + asio::io_context io_context; + + std::remove(argv[1]); + server s(io_context, argv[1]); + + io_context.run(); + } + catch (std::exception& e) + { + std::cerr << "Exception: " << e.what() << "\n"; + } + + return 0; +} + +#else // defined(ASIO_HAS_LOCAL_SOCKETS) +# error Local sockets not available on this platform. +#endif // defined(ASIO_HAS_LOCAL_SOCKETS) |