diff options
author | 2016-10-07 14:13:19 +0200 | |
---|---|---|
committer | 2016-10-07 14:13:19 +0200 | |
commit | ff01b716711b97c2fcaa709ea97f7650f106aa10 (patch) | |
tree | 50dd4d687f38f50c4e136af030c02c267c769f3a /3rdparty/asio/src/examples/cpp03/echo | |
parent | 2a138159c30457aecd9e9679be5159704db0f954 (diff) |
Added ASIO networking library (nw)
Diffstat (limited to '3rdparty/asio/src/examples/cpp03/echo')
7 files changed, 490 insertions, 0 deletions
diff --git a/3rdparty/asio/src/examples/cpp03/echo/.gitignore b/3rdparty/asio/src/examples/cpp03/echo/.gitignore new file mode 100644 index 00000000000..0882fa6ced3 --- /dev/null +++ b/3rdparty/asio/src/examples/cpp03/echo/.gitignore @@ -0,0 +1,11 @@ +.deps +.dirstamp +*.o +*.obj +*.exe +*_server +*_client +*.ilk +*.manifest +*.pdb +*.tds diff --git a/3rdparty/asio/src/examples/cpp03/echo/async_tcp_echo_server.cpp b/3rdparty/asio/src/examples/cpp03/echo/async_tcp_echo_server.cpp new file mode 100644 index 00000000000..c26136e89b5 --- /dev/null +++ b/3rdparty/asio/src/examples/cpp03/echo/async_tcp_echo_server.cpp @@ -0,0 +1,137 @@ +// +// async_tcp_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 <cstdlib> +#include <iostream> +#include <boost/bind.hpp> +#include "asio.hpp" + +using asio::ip::tcp; + +class session +{ +public: + session(asio::io_context& io_context) + : socket_(io_context) + { + } + + tcp::socket& socket() + { + return socket_; + } + + void start() + { + socket_.async_read_some(asio::buffer(data_, max_length), + boost::bind(&session::handle_read, this, + asio::placeholders::error, + asio::placeholders::bytes_transferred)); + } + +private: + 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, this, + asio::placeholders::error)); + } + else + { + delete this; + } + } + + void handle_write(const asio::error_code& error) + { + if (!error) + { + socket_.async_read_some(asio::buffer(data_, max_length), + boost::bind(&session::handle_read, this, + asio::placeholders::error, + asio::placeholders::bytes_transferred)); + } + else + { + delete this; + } + } + + tcp::socket socket_; + enum { max_length = 1024 }; + char data_[max_length]; +}; + +class server +{ +public: + server(asio::io_context& io_context, short port) + : io_context_(io_context), + acceptor_(io_context, tcp::endpoint(tcp::v4(), port)) + { + start_accept(); + } + +private: + void start_accept() + { + session* 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* new_session, + const asio::error_code& error) + { + if (!error) + { + new_session->start(); + } + else + { + delete new_session; + } + + start_accept(); + } + + asio::io_context& io_context_; + tcp::acceptor acceptor_; +}; + +int main(int argc, char* argv[]) +{ + try + { + if (argc != 2) + { + std::cerr << "Usage: async_tcp_echo_server <port>\n"; + return 1; + } + + asio::io_context io_context; + + using namespace std; // For atoi. + server s(io_context, atoi(argv[1])); + + io_context.run(); + } + catch (std::exception& e) + { + std::cerr << "Exception: " << e.what() << "\n"; + } + + return 0; +} diff --git a/3rdparty/asio/src/examples/cpp03/echo/async_udp_echo_server.cpp b/3rdparty/asio/src/examples/cpp03/echo/async_udp_echo_server.cpp new file mode 100644 index 00000000000..851c46b0803 --- /dev/null +++ b/3rdparty/asio/src/examples/cpp03/echo/async_udp_echo_server.cpp @@ -0,0 +1,92 @@ +// +// async_udp_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 <cstdlib> +#include <iostream> +#include <boost/bind.hpp> +#include "asio.hpp" + +using asio::ip::udp; + +class server +{ +public: + server(asio::io_context& io_context, short port) + : socket_(io_context, udp::endpoint(udp::v4(), port)) + { + socket_.async_receive_from( + asio::buffer(data_, max_length), sender_endpoint_, + boost::bind(&server::handle_receive_from, this, + asio::placeholders::error, + asio::placeholders::bytes_transferred)); + } + + void handle_receive_from(const asio::error_code& error, + size_t bytes_recvd) + { + if (!error && bytes_recvd > 0) + { + socket_.async_send_to( + asio::buffer(data_, bytes_recvd), sender_endpoint_, + boost::bind(&server::handle_send_to, this, + asio::placeholders::error, + asio::placeholders::bytes_transferred)); + } + else + { + socket_.async_receive_from( + asio::buffer(data_, max_length), sender_endpoint_, + boost::bind(&server::handle_receive_from, this, + asio::placeholders::error, + asio::placeholders::bytes_transferred)); + } + } + + void handle_send_to(const asio::error_code& /*error*/, + size_t /*bytes_sent*/) + { + socket_.async_receive_from( + asio::buffer(data_, max_length), sender_endpoint_, + boost::bind(&server::handle_receive_from, this, + asio::placeholders::error, + asio::placeholders::bytes_transferred)); + } + +private: + udp::socket socket_; + udp::endpoint sender_endpoint_; + enum { max_length = 1024 }; + char data_[max_length]; +}; + +int main(int argc, char* argv[]) +{ + try + { + if (argc != 2) + { + std::cerr << "Usage: async_udp_echo_server <port>\n"; + return 1; + } + + asio::io_context io_context; + + using namespace std; // For atoi. + server s(io_context, atoi(argv[1])); + + io_context.run(); + } + catch (std::exception& e) + { + std::cerr << "Exception: " << e.what() << "\n"; + } + + return 0; +} diff --git a/3rdparty/asio/src/examples/cpp03/echo/blocking_tcp_echo_client.cpp b/3rdparty/asio/src/examples/cpp03/echo/blocking_tcp_echo_client.cpp new file mode 100644 index 00000000000..224b9dbb1b2 --- /dev/null +++ b/3rdparty/asio/src/examples/cpp03/echo/blocking_tcp_echo_client.cpp @@ -0,0 +1,59 @@ +// +// blocking_tcp_echo_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" + +using asio::ip::tcp; + +enum { max_length = 1024 }; + +int main(int argc, char* argv[]) +{ + try + { + if (argc != 3) + { + std::cerr << "Usage: blocking_tcp_echo_client <host> <port>\n"; + return 1; + } + + asio::io_context io_context; + + tcp::resolver resolver(io_context); + tcp::resolver::results_type endpoints = + resolver.resolve(tcp::v4(), argv[1], argv[2]); + + tcp::socket s(io_context); + asio::connect(s, endpoints); + + 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; +} diff --git a/3rdparty/asio/src/examples/cpp03/echo/blocking_tcp_echo_server.cpp b/3rdparty/asio/src/examples/cpp03/echo/blocking_tcp_echo_server.cpp new file mode 100644 index 00000000000..163dc2a392c --- /dev/null +++ b/3rdparty/asio/src/examples/cpp03/echo/blocking_tcp_echo_server.cpp @@ -0,0 +1,79 @@ +// +// blocking_tcp_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 <cstdlib> +#include <iostream> +#include <boost/bind.hpp> +#include <boost/smart_ptr.hpp> +#include "asio.hpp" + +using asio::ip::tcp; + +const int max_length = 1024; + +typedef boost::shared_ptr<tcp::socket> socket_ptr; + +void session(socket_ptr sock) +{ + try + { + for (;;) + { + char data[max_length]; + + asio::error_code error; + size_t length = sock->read_some(asio::buffer(data), error); + if (error == asio::error::eof) + break; // Connection closed cleanly by peer. + else if (error) + throw asio::system_error(error); // Some other error. + + asio::write(*sock, asio::buffer(data, length)); + } + } + catch (std::exception& e) + { + std::cerr << "Exception in thread: " << e.what() << "\n"; + } +} + +void server(asio::io_context& io_context, unsigned short port) +{ + tcp::acceptor a(io_context, tcp::endpoint(tcp::v4(), port)); + for (;;) + { + socket_ptr sock(new tcp::socket(io_context)); + a.accept(*sock); + asio::thread t(boost::bind(session, sock)); + } +} + +int main(int argc, char* argv[]) +{ + try + { + if (argc != 2) + { + std::cerr << "Usage: blocking_tcp_echo_server <port>\n"; + return 1; + } + + asio::io_context io_context; + + using namespace std; // For atoi. + server(io_context, atoi(argv[1])); + } + catch (std::exception& e) + { + std::cerr << "Exception: " << e.what() << "\n"; + } + + return 0; +} diff --git a/3rdparty/asio/src/examples/cpp03/echo/blocking_udp_echo_client.cpp b/3rdparty/asio/src/examples/cpp03/echo/blocking_udp_echo_client.cpp new file mode 100644 index 00000000000..ee90081b7ea --- /dev/null +++ b/3rdparty/asio/src/examples/cpp03/echo/blocking_udp_echo_client.cpp @@ -0,0 +1,59 @@ +// +// blocking_udp_echo_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" + +using asio::ip::udp; + +enum { max_length = 1024 }; + +int main(int argc, char* argv[]) +{ + try + { + if (argc != 3) + { + std::cerr << "Usage: blocking_udp_echo_client <host> <port>\n"; + return 1; + } + + asio::io_context io_context; + + udp::socket s(io_context, udp::endpoint(udp::v4(), 0)); + + udp::resolver resolver(io_context); + udp::resolver::results_type endpoints = + resolver.resolve(udp::v4(), argv[1], argv[2]); + + 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); + s.send_to(asio::buffer(request, request_length), *endpoints.begin()); + + char reply[max_length]; + udp::endpoint sender_endpoint; + size_t reply_length = s.receive_from( + asio::buffer(reply, max_length), sender_endpoint); + 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; +} diff --git a/3rdparty/asio/src/examples/cpp03/echo/blocking_udp_echo_server.cpp b/3rdparty/asio/src/examples/cpp03/echo/blocking_udp_echo_server.cpp new file mode 100644 index 00000000000..3cc364c20b5 --- /dev/null +++ b/3rdparty/asio/src/examples/cpp03/echo/blocking_udp_echo_server.cpp @@ -0,0 +1,53 @@ +// +// blocking_udp_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 <cstdlib> +#include <iostream> +#include "asio.hpp" + +using asio::ip::udp; + +enum { max_length = 1024 }; + +void server(asio::io_context& io_context, unsigned short port) +{ + udp::socket sock(io_context, udp::endpoint(udp::v4(), port)); + for (;;) + { + char data[max_length]; + udp::endpoint sender_endpoint; + size_t length = sock.receive_from( + asio::buffer(data, max_length), sender_endpoint); + sock.send_to(asio::buffer(data, length), sender_endpoint); + } +} + +int main(int argc, char* argv[]) +{ + try + { + if (argc != 2) + { + std::cerr << "Usage: blocking_udp_echo_server <port>\n"; + return 1; + } + + asio::io_context io_context; + + using namespace std; // For atoi. + server(io_context, atoi(argv[1])); + } + catch (std::exception& e) + { + std::cerr << "Exception: " << e.what() << "\n"; + } + + return 0; +} |