]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp03/chat/chat_client.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / asio / example / cpp03 / chat / chat_client.cpp
1 //
2 // chat_client.cpp
3 // ~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10
11 #include <cstdlib>
12 #include <deque>
13 #include <iostream>
14 #include <boost/bind.hpp>
15 #include <boost/asio.hpp>
16 #include <boost/thread/thread.hpp>
17 #include "chat_message.hpp"
18
19 using boost::asio::ip::tcp;
20
21 typedef std::deque<chat_message> chat_message_queue;
22
23 class chat_client
24 {
25 public:
26 chat_client(boost::asio::io_service& io_service,
27 tcp::resolver::iterator endpoint_iterator)
28 : io_service_(io_service),
29 socket_(io_service)
30 {
31 boost::asio::async_connect(socket_, endpoint_iterator,
32 boost::bind(&chat_client::handle_connect, this,
33 boost::asio::placeholders::error));
34 }
35
36 void write(const chat_message& msg)
37 {
38 io_service_.post(boost::bind(&chat_client::do_write, this, msg));
39 }
40
41 void close()
42 {
43 io_service_.post(boost::bind(&chat_client::do_close, this));
44 }
45
46 private:
47
48 void handle_connect(const boost::system::error_code& error)
49 {
50 if (!error)
51 {
52 boost::asio::async_read(socket_,
53 boost::asio::buffer(read_msg_.data(), chat_message::header_length),
54 boost::bind(&chat_client::handle_read_header, this,
55 boost::asio::placeholders::error));
56 }
57 }
58
59 void handle_read_header(const boost::system::error_code& error)
60 {
61 if (!error && read_msg_.decode_header())
62 {
63 boost::asio::async_read(socket_,
64 boost::asio::buffer(read_msg_.body(), read_msg_.body_length()),
65 boost::bind(&chat_client::handle_read_body, this,
66 boost::asio::placeholders::error));
67 }
68 else
69 {
70 do_close();
71 }
72 }
73
74 void handle_read_body(const boost::system::error_code& error)
75 {
76 if (!error)
77 {
78 std::cout.write(read_msg_.body(), read_msg_.body_length());
79 std::cout << "\n";
80 boost::asio::async_read(socket_,
81 boost::asio::buffer(read_msg_.data(), chat_message::header_length),
82 boost::bind(&chat_client::handle_read_header, this,
83 boost::asio::placeholders::error));
84 }
85 else
86 {
87 do_close();
88 }
89 }
90
91 void do_write(chat_message msg)
92 {
93 bool write_in_progress = !write_msgs_.empty();
94 write_msgs_.push_back(msg);
95 if (!write_in_progress)
96 {
97 boost::asio::async_write(socket_,
98 boost::asio::buffer(write_msgs_.front().data(),
99 write_msgs_.front().length()),
100 boost::bind(&chat_client::handle_write, this,
101 boost::asio::placeholders::error));
102 }
103 }
104
105 void handle_write(const boost::system::error_code& error)
106 {
107 if (!error)
108 {
109 write_msgs_.pop_front();
110 if (!write_msgs_.empty())
111 {
112 boost::asio::async_write(socket_,
113 boost::asio::buffer(write_msgs_.front().data(),
114 write_msgs_.front().length()),
115 boost::bind(&chat_client::handle_write, this,
116 boost::asio::placeholders::error));
117 }
118 }
119 else
120 {
121 do_close();
122 }
123 }
124
125 void do_close()
126 {
127 socket_.close();
128 }
129
130 private:
131 boost::asio::io_service& io_service_;
132 tcp::socket socket_;
133 chat_message read_msg_;
134 chat_message_queue write_msgs_;
135 };
136
137 int main(int argc, char* argv[])
138 {
139 try
140 {
141 if (argc != 3)
142 {
143 std::cerr << "Usage: chat_client <host> <port>\n";
144 return 1;
145 }
146
147 boost::asio::io_service io_service;
148
149 tcp::resolver resolver(io_service);
150 tcp::resolver::query query(argv[1], argv[2]);
151 tcp::resolver::iterator iterator = resolver.resolve(query);
152
153 chat_client c(io_service, iterator);
154
155 boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service));
156
157 char line[chat_message::max_body_length + 1];
158 while (std::cin.getline(line, chat_message::max_body_length + 1))
159 {
160 using namespace std; // For strlen and memcpy.
161 chat_message msg;
162 msg.body_length(strlen(line));
163 memcpy(msg.body(), line, msg.body_length());
164 msg.encode_header();
165 c.write(msg);
166 }
167
168 c.close();
169 t.join();
170 }
171 catch (std::exception& e)
172 {
173 std::cerr << "Exception: " << e.what() << "\n";
174 }
175
176 return 0;
177 }