5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
17 #include <boost/bind.hpp>
18 #include <boost/shared_ptr.hpp>
19 #include <boost/enable_shared_from_this.hpp>
20 #include <boost/asio.hpp>
21 #include "chat_message.hpp"
23 using boost::asio::ip::tcp;
25 //----------------------------------------------------------------------
27 typedef std::deque<chat_message> chat_message_queue;
29 //----------------------------------------------------------------------
31 class chat_participant
34 virtual ~chat_participant() {}
35 virtual void deliver(const chat_message& msg) = 0;
38 typedef boost::shared_ptr<chat_participant> chat_participant_ptr;
40 //----------------------------------------------------------------------
45 void join(chat_participant_ptr participant)
47 participants_.insert(participant);
48 std::for_each(recent_msgs_.begin(), recent_msgs_.end(),
49 boost::bind(&chat_participant::deliver, participant, _1));
52 void leave(chat_participant_ptr participant)
54 participants_.erase(participant);
57 void deliver(const chat_message& msg)
59 recent_msgs_.push_back(msg);
60 while (recent_msgs_.size() > max_recent_msgs)
61 recent_msgs_.pop_front();
63 std::for_each(participants_.begin(), participants_.end(),
64 boost::bind(&chat_participant::deliver, _1, boost::ref(msg)));
68 std::set<chat_participant_ptr> participants_;
69 enum { max_recent_msgs = 100 };
70 chat_message_queue recent_msgs_;
73 //----------------------------------------------------------------------
76 : public chat_participant,
77 public boost::enable_shared_from_this<chat_session>
80 chat_session(boost::asio::io_service& io_service, chat_room& room)
81 : socket_(io_service),
93 room_.join(shared_from_this());
94 boost::asio::async_read(socket_,
95 boost::asio::buffer(read_msg_.data(), chat_message::header_length),
97 &chat_session::handle_read_header, shared_from_this(),
98 boost::asio::placeholders::error));
101 void deliver(const chat_message& msg)
103 bool write_in_progress = !write_msgs_.empty();
104 write_msgs_.push_back(msg);
105 if (!write_in_progress)
107 boost::asio::async_write(socket_,
108 boost::asio::buffer(write_msgs_.front().data(),
109 write_msgs_.front().length()),
110 boost::bind(&chat_session::handle_write, shared_from_this(),
111 boost::asio::placeholders::error));
115 void handle_read_header(const boost::system::error_code& error)
117 if (!error && read_msg_.decode_header())
119 boost::asio::async_read(socket_,
120 boost::asio::buffer(read_msg_.body(), read_msg_.body_length()),
121 boost::bind(&chat_session::handle_read_body, shared_from_this(),
122 boost::asio::placeholders::error));
126 room_.leave(shared_from_this());
130 void handle_read_body(const boost::system::error_code& error)
134 room_.deliver(read_msg_);
135 boost::asio::async_read(socket_,
136 boost::asio::buffer(read_msg_.data(), chat_message::header_length),
137 boost::bind(&chat_session::handle_read_header, shared_from_this(),
138 boost::asio::placeholders::error));
142 room_.leave(shared_from_this());
146 void handle_write(const boost::system::error_code& error)
150 write_msgs_.pop_front();
151 if (!write_msgs_.empty())
153 boost::asio::async_write(socket_,
154 boost::asio::buffer(write_msgs_.front().data(),
155 write_msgs_.front().length()),
156 boost::bind(&chat_session::handle_write, shared_from_this(),
157 boost::asio::placeholders::error));
162 room_.leave(shared_from_this());
169 chat_message read_msg_;
170 chat_message_queue write_msgs_;
173 typedef boost::shared_ptr<chat_session> chat_session_ptr;
175 //----------------------------------------------------------------------
180 chat_server(boost::asio::io_service& io_service,
181 const tcp::endpoint& endpoint)
182 : io_service_(io_service),
183 acceptor_(io_service, endpoint)
190 chat_session_ptr new_session(new chat_session(io_service_, room_));
191 acceptor_.async_accept(new_session->socket(),
192 boost::bind(&chat_server::handle_accept, this, new_session,
193 boost::asio::placeholders::error));
196 void handle_accept(chat_session_ptr session,
197 const boost::system::error_code& error)
208 boost::asio::io_service& io_service_;
209 tcp::acceptor acceptor_;
213 typedef boost::shared_ptr<chat_server> chat_server_ptr;
214 typedef std::list<chat_server_ptr> chat_server_list;
216 //----------------------------------------------------------------------
218 int main(int argc, char* argv[])
224 std::cerr << "Usage: chat_server <port> [<port> ...]\n";
228 boost::asio::io_service io_service;
230 chat_server_list servers;
231 for (int i = 1; i < argc; ++i)
233 using namespace std; // For atoi.
234 tcp::endpoint endpoint(tcp::v4(), atoi(argv[i]));
235 chat_server_ptr server(new chat_server(io_service, endpoint));
236 servers.push_back(server);
241 catch (std::exception& e)
243 std::cerr << "Exception: " << e.what() << "\n";