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/chat | |
parent | 2a138159c30457aecd9e9679be5159704db0f954 (diff) |
Added ASIO networking library (nw)
Diffstat (limited to '3rdparty/asio/src/examples/cpp03/chat')
-rw-r--r-- | 3rdparty/asio/src/examples/cpp03/chat/.gitignore | 11 | ||||
-rw-r--r-- | 3rdparty/asio/src/examples/cpp03/chat/chat_client.cpp | 177 | ||||
-rw-r--r-- | 3rdparty/asio/src/examples/cpp03/chat/chat_message.hpp | 93 | ||||
-rw-r--r-- | 3rdparty/asio/src/examples/cpp03/chat/chat_server.cpp | 247 | ||||
-rw-r--r-- | 3rdparty/asio/src/examples/cpp03/chat/posix_chat_client.cpp | 204 |
5 files changed, 732 insertions, 0 deletions
diff --git a/3rdparty/asio/src/examples/cpp03/chat/.gitignore b/3rdparty/asio/src/examples/cpp03/chat/.gitignore new file mode 100644 index 00000000000..0882fa6ced3 --- /dev/null +++ b/3rdparty/asio/src/examples/cpp03/chat/.gitignore @@ -0,0 +1,11 @@ +.deps +.dirstamp +*.o +*.obj +*.exe +*_server +*_client +*.ilk +*.manifest +*.pdb +*.tds diff --git a/3rdparty/asio/src/examples/cpp03/chat/chat_client.cpp b/3rdparty/asio/src/examples/cpp03/chat/chat_client.cpp new file mode 100644 index 00000000000..9dd16145b81 --- /dev/null +++ b/3rdparty/asio/src/examples/cpp03/chat/chat_client.cpp @@ -0,0 +1,177 @@ +// +// chat_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 <deque> +#include <iostream> +#include <boost/bind.hpp> +#include "asio.hpp" +#include "chat_message.hpp" + +using asio::ip::tcp; + +typedef std::deque<chat_message> chat_message_queue; + +class chat_client +{ +public: + chat_client(asio::io_context& io_context, + const tcp::resolver::results_type& endpoints) + : io_context_(io_context), + socket_(io_context) + { + asio::async_connect(socket_, endpoints, + boost::bind(&chat_client::handle_connect, this, + asio::placeholders::error)); + } + + void write(const chat_message& msg) + { + asio::post(io_context_, + boost::bind(&chat_client::do_write, this, msg)); + } + + void close() + { + asio::post(io_context_, + boost::bind(&chat_client::do_close, this)); + } + +private: + + void handle_connect(const asio::error_code& error) + { + if (!error) + { + asio::async_read(socket_, + asio::buffer(read_msg_.data(), chat_message::header_length), + boost::bind(&chat_client::handle_read_header, this, + asio::placeholders::error)); + } + } + + void handle_read_header(const asio::error_code& error) + { + if (!error && read_msg_.decode_header()) + { + asio::async_read(socket_, + asio::buffer(read_msg_.body(), read_msg_.body_length()), + boost::bind(&chat_client::handle_read_body, this, + asio::placeholders::error)); + } + else + { + do_close(); + } + } + + void handle_read_body(const asio::error_code& error) + { + if (!error) + { + std::cout.write(read_msg_.body(), read_msg_.body_length()); + std::cout << "\n"; + asio::async_read(socket_, + asio::buffer(read_msg_.data(), chat_message::header_length), + boost::bind(&chat_client::handle_read_header, this, + asio::placeholders::error)); + } + else + { + do_close(); + } + } + + void do_write(chat_message msg) + { + bool write_in_progress = !write_msgs_.empty(); + write_msgs_.push_back(msg); + if (!write_in_progress) + { + asio::async_write(socket_, + asio::buffer(write_msgs_.front().data(), + write_msgs_.front().length()), + boost::bind(&chat_client::handle_write, this, + asio::placeholders::error)); + } + } + + void handle_write(const asio::error_code& error) + { + if (!error) + { + write_msgs_.pop_front(); + if (!write_msgs_.empty()) + { + asio::async_write(socket_, + asio::buffer(write_msgs_.front().data(), + write_msgs_.front().length()), + boost::bind(&chat_client::handle_write, this, + asio::placeholders::error)); + } + } + else + { + do_close(); + } + } + + void do_close() + { + socket_.close(); + } + +private: + asio::io_context& io_context_; + tcp::socket socket_; + chat_message read_msg_; + chat_message_queue write_msgs_; +}; + +int main(int argc, char* argv[]) +{ + try + { + if (argc != 3) + { + std::cerr << "Usage: chat_client <host> <port>\n"; + return 1; + } + + asio::io_context io_context; + + tcp::resolver resolver(io_context); + tcp::resolver::results_type endpoints = resolver.resolve(argv[1], argv[2]); + + chat_client c(io_context, endpoints); + + asio::thread t(boost::bind(&asio::io_context::run, &io_context)); + + char line[chat_message::max_body_length + 1]; + while (std::cin.getline(line, chat_message::max_body_length + 1)) + { + using namespace std; // For strlen and memcpy. + chat_message msg; + msg.body_length(strlen(line)); + memcpy(msg.body(), line, msg.body_length()); + msg.encode_header(); + c.write(msg); + } + + c.close(); + t.join(); + } + catch (std::exception& e) + { + std::cerr << "Exception: " << e.what() << "\n"; + } + + return 0; +} diff --git a/3rdparty/asio/src/examples/cpp03/chat/chat_message.hpp b/3rdparty/asio/src/examples/cpp03/chat/chat_message.hpp new file mode 100644 index 00000000000..ceeef2f03f8 --- /dev/null +++ b/3rdparty/asio/src/examples/cpp03/chat/chat_message.hpp @@ -0,0 +1,93 @@ +// +// chat_message.hpp +// ~~~~~~~~~~~~~~~~ +// +// 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) +// + +#ifndef CHAT_MESSAGE_HPP +#define CHAT_MESSAGE_HPP + +#include <cstdio> +#include <cstdlib> +#include <cstring> + +class chat_message +{ +public: + enum { header_length = 4 }; + enum { max_body_length = 512 }; + + chat_message() + : body_length_(0) + { + } + + const char* data() const + { + return data_; + } + + char* data() + { + return data_; + } + + size_t length() const + { + return header_length + body_length_; + } + + const char* body() const + { + return data_ + header_length; + } + + char* body() + { + return data_ + header_length; + } + + size_t body_length() const + { + return body_length_; + } + + void body_length(size_t new_length) + { + body_length_ = new_length; + if (body_length_ > max_body_length) + body_length_ = max_body_length; + } + + bool decode_header() + { + using namespace std; // For strncat and atoi. + char header[header_length + 1] = ""; + strncat(header, data_, header_length); + body_length_ = atoi(header); + if (body_length_ > max_body_length) + { + body_length_ = 0; + return false; + } + return true; + } + + void encode_header() + { + using namespace std; // For sprintf and memcpy. + char header[header_length + 1] = ""; + sprintf(header, "%4d", static_cast<int>(body_length_)); + memcpy(data_, header, header_length); + } + +private: + char data_[header_length + max_body_length]; + size_t body_length_; +}; + +#endif // CHAT_MESSAGE_HPP diff --git a/3rdparty/asio/src/examples/cpp03/chat/chat_server.cpp b/3rdparty/asio/src/examples/cpp03/chat/chat_server.cpp new file mode 100644 index 00000000000..902515b3b54 --- /dev/null +++ b/3rdparty/asio/src/examples/cpp03/chat/chat_server.cpp @@ -0,0 +1,247 @@ +// +// chat_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 <algorithm> +#include <cstdlib> +#include <deque> +#include <iostream> +#include <list> +#include <set> +#include <boost/bind.hpp> +#include <boost/shared_ptr.hpp> +#include <boost/enable_shared_from_this.hpp> +#include "asio.hpp" +#include "chat_message.hpp" + +using asio::ip::tcp; + +//---------------------------------------------------------------------- + +typedef std::deque<chat_message> chat_message_queue; + +//---------------------------------------------------------------------- + +class chat_participant +{ +public: + virtual ~chat_participant() {} + virtual void deliver(const chat_message& msg) = 0; +}; + +typedef boost::shared_ptr<chat_participant> chat_participant_ptr; + +//---------------------------------------------------------------------- + +class chat_room +{ +public: + void join(chat_participant_ptr participant) + { + participants_.insert(participant); + std::for_each(recent_msgs_.begin(), recent_msgs_.end(), + boost::bind(&chat_participant::deliver, participant, _1)); + } + + void leave(chat_participant_ptr participant) + { + participants_.erase(participant); + } + + void deliver(const chat_message& msg) + { + recent_msgs_.push_back(msg); + while (recent_msgs_.size() > max_recent_msgs) + recent_msgs_.pop_front(); + + std::for_each(participants_.begin(), participants_.end(), + boost::bind(&chat_participant::deliver, _1, boost::ref(msg))); + } + +private: + std::set<chat_participant_ptr> participants_; + enum { max_recent_msgs = 100 }; + chat_message_queue recent_msgs_; +}; + +//---------------------------------------------------------------------- + +class chat_session + : public chat_participant, + public boost::enable_shared_from_this<chat_session> +{ +public: + chat_session(asio::io_context& io_context, chat_room& room) + : socket_(io_context), + room_(room) + { + } + + tcp::socket& socket() + { + return socket_; + } + + void start() + { + room_.join(shared_from_this()); + asio::async_read(socket_, + asio::buffer(read_msg_.data(), chat_message::header_length), + boost::bind( + &chat_session::handle_read_header, shared_from_this(), + asio::placeholders::error)); + } + + void deliver(const chat_message& msg) + { + bool write_in_progress = !write_msgs_.empty(); + write_msgs_.push_back(msg); + if (!write_in_progress) + { + asio::async_write(socket_, + asio::buffer(write_msgs_.front().data(), + write_msgs_.front().length()), + boost::bind(&chat_session::handle_write, shared_from_this(), + asio::placeholders::error)); + } + } + + void handle_read_header(const asio::error_code& error) + { + if (!error && read_msg_.decode_header()) + { + asio::async_read(socket_, + asio::buffer(read_msg_.body(), read_msg_.body_length()), + boost::bind(&chat_session::handle_read_body, shared_from_this(), + asio::placeholders::error)); + } + else + { + room_.leave(shared_from_this()); + } + } + + void handle_read_body(const asio::error_code& error) + { + if (!error) + { + room_.deliver(read_msg_); + asio::async_read(socket_, + asio::buffer(read_msg_.data(), chat_message::header_length), + boost::bind(&chat_session::handle_read_header, shared_from_this(), + asio::placeholders::error)); + } + else + { + room_.leave(shared_from_this()); + } + } + + void handle_write(const asio::error_code& error) + { + if (!error) + { + write_msgs_.pop_front(); + if (!write_msgs_.empty()) + { + asio::async_write(socket_, + asio::buffer(write_msgs_.front().data(), + write_msgs_.front().length()), + boost::bind(&chat_session::handle_write, shared_from_this(), + asio::placeholders::error)); + } + } + else + { + room_.leave(shared_from_this()); + } + } + +private: + tcp::socket socket_; + chat_room& room_; + chat_message read_msg_; + chat_message_queue write_msgs_; +}; + +typedef boost::shared_ptr<chat_session> chat_session_ptr; + +//---------------------------------------------------------------------- + +class chat_server +{ +public: + chat_server(asio::io_context& io_context, + const tcp::endpoint& endpoint) + : io_context_(io_context), + acceptor_(io_context, endpoint) + { + start_accept(); + } + + void start_accept() + { + chat_session_ptr new_session(new chat_session(io_context_, room_)); + acceptor_.async_accept(new_session->socket(), + boost::bind(&chat_server::handle_accept, this, new_session, + asio::placeholders::error)); + } + + void handle_accept(chat_session_ptr session, + const asio::error_code& error) + { + if (!error) + { + session->start(); + } + + start_accept(); + } + +private: + asio::io_context& io_context_; + tcp::acceptor acceptor_; + chat_room room_; +}; + +typedef boost::shared_ptr<chat_server> chat_server_ptr; +typedef std::list<chat_server_ptr> chat_server_list; + +//---------------------------------------------------------------------- + +int main(int argc, char* argv[]) +{ + try + { + if (argc < 2) + { + std::cerr << "Usage: chat_server <port> [<port> ...]\n"; + return 1; + } + + asio::io_context io_context; + + chat_server_list servers; + for (int i = 1; i < argc; ++i) + { + using namespace std; // For atoi. + tcp::endpoint endpoint(tcp::v4(), atoi(argv[i])); + chat_server_ptr server(new chat_server(io_context, endpoint)); + servers.push_back(server); + } + + io_context.run(); + } + catch (std::exception& e) + { + std::cerr << "Exception: " << e.what() << "\n"; + } + + return 0; +} diff --git a/3rdparty/asio/src/examples/cpp03/chat/posix_chat_client.cpp b/3rdparty/asio/src/examples/cpp03/chat/posix_chat_client.cpp new file mode 100644 index 00000000000..91428825aad --- /dev/null +++ b/3rdparty/asio/src/examples/cpp03/chat/posix_chat_client.cpp @@ -0,0 +1,204 @@ +// +// posix_chat_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 <boost/array.hpp> +#include <boost/bind.hpp> +#include "asio.hpp" +#include "chat_message.hpp" + +#if defined(ASIO_HAS_POSIX_STREAM_DESCRIPTOR) + +using asio::ip::tcp; +namespace posix = asio::posix; + +class posix_chat_client +{ +public: + posix_chat_client(asio::io_context& io_context, + const tcp::resolver::results_type& endpoints) + : socket_(io_context), + input_(io_context, ::dup(STDIN_FILENO)), + output_(io_context, ::dup(STDOUT_FILENO)), + input_buffer_(chat_message::max_body_length) + { + asio::async_connect(socket_, endpoints, + boost::bind(&posix_chat_client::handle_connect, this, + asio::placeholders::error)); + } + +private: + + void handle_connect(const asio::error_code& error) + { + if (!error) + { + // Read the fixed-length header of the next message from the server. + asio::async_read(socket_, + asio::buffer(read_msg_.data(), chat_message::header_length), + boost::bind(&posix_chat_client::handle_read_header, this, + asio::placeholders::error)); + + // Read a line of input entered by the user. + asio::async_read_until(input_, input_buffer_, '\n', + boost::bind(&posix_chat_client::handle_read_input, this, + asio::placeholders::error, + asio::placeholders::bytes_transferred)); + } + } + + void handle_read_header(const asio::error_code& error) + { + if (!error && read_msg_.decode_header()) + { + // Read the variable-length body of the message from the server. + asio::async_read(socket_, + asio::buffer(read_msg_.body(), read_msg_.body_length()), + boost::bind(&posix_chat_client::handle_read_body, this, + asio::placeholders::error)); + } + else + { + close(); + } + } + + void handle_read_body(const asio::error_code& error) + { + if (!error) + { + // Write out the message we just received, terminated by a newline. + static char eol[] = { '\n' }; + boost::array<asio::const_buffer, 2> buffers = {{ + asio::buffer(read_msg_.body(), read_msg_.body_length()), + asio::buffer(eol) }}; + asio::async_write(output_, buffers, + boost::bind(&posix_chat_client::handle_write_output, this, + asio::placeholders::error)); + } + else + { + close(); + } + } + + void handle_write_output(const asio::error_code& error) + { + if (!error) + { + // Read the fixed-length header of the next message from the server. + asio::async_read(socket_, + asio::buffer(read_msg_.data(), chat_message::header_length), + boost::bind(&posix_chat_client::handle_read_header, this, + asio::placeholders::error)); + } + else + { + close(); + } + } + + void handle_read_input(const asio::error_code& error, + std::size_t length) + { + if (!error) + { + // Write the message (minus the newline) to the server. + write_msg_.body_length(length - 1); + input_buffer_.sgetn(write_msg_.body(), length - 1); + input_buffer_.consume(1); // Remove newline from input. + write_msg_.encode_header(); + asio::async_write(socket_, + asio::buffer(write_msg_.data(), write_msg_.length()), + boost::bind(&posix_chat_client::handle_write, this, + asio::placeholders::error)); + } + else if (error == asio::error::not_found) + { + // Didn't get a newline. Send whatever we have. + write_msg_.body_length(input_buffer_.size()); + input_buffer_.sgetn(write_msg_.body(), input_buffer_.size()); + write_msg_.encode_header(); + asio::async_write(socket_, + asio::buffer(write_msg_.data(), write_msg_.length()), + boost::bind(&posix_chat_client::handle_write, this, + asio::placeholders::error)); + } + else + { + close(); + } + } + + void handle_write(const asio::error_code& error) + { + if (!error) + { + // Read a line of input entered by the user. + asio::async_read_until(input_, input_buffer_, '\n', + boost::bind(&posix_chat_client::handle_read_input, this, + asio::placeholders::error, + asio::placeholders::bytes_transferred)); + } + else + { + close(); + } + } + + void close() + { + // Cancel all outstanding asynchronous operations. + socket_.close(); + input_.close(); + output_.close(); + } + +private: + tcp::socket socket_; + posix::stream_descriptor input_; + posix::stream_descriptor output_; + chat_message read_msg_; + chat_message write_msg_; + asio::streambuf input_buffer_; +}; + +int main(int argc, char* argv[]) +{ + try + { + if (argc != 3) + { + std::cerr << "Usage: posix_chat_client <host> <port>\n"; + return 1; + } + + asio::io_context io_context; + + tcp::resolver resolver(io_context); + tcp::resolver::results_type endpoints = resolver.resolve(argv[1], argv[2]); + + posix_chat_client c(io_context, endpoints); + + io_context.run(); + } + catch (std::exception& e) + { + std::cerr << "Exception: " << e.what() << "\n"; + } + + return 0; +} + +#else // defined(ASIO_HAS_POSIX_STREAM_DESCRIPTOR) +int main() {} +#endif // defined(ASIO_HAS_POSIX_STREAM_DESCRIPTOR) |