5 // Copyright (c) 2003-2016 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)
18 #include <boost/asio.hpp>
19 #include "chat_message.hpp"
21 using boost::asio::ip::tcp
;
23 //----------------------------------------------------------------------
25 typedef std::deque
<chat_message
> chat_message_queue
;
27 //----------------------------------------------------------------------
29 class chat_participant
32 virtual ~chat_participant() {}
33 virtual void deliver(const chat_message
& msg
) = 0;
36 typedef std::shared_ptr
<chat_participant
> chat_participant_ptr
;
38 //----------------------------------------------------------------------
43 void join(chat_participant_ptr participant
)
45 participants_
.insert(participant
);
46 for (auto msg
: recent_msgs_
)
47 participant
->deliver(msg
);
50 void leave(chat_participant_ptr participant
)
52 participants_
.erase(participant
);
55 void deliver(const chat_message
& msg
)
57 recent_msgs_
.push_back(msg
);
58 while (recent_msgs_
.size() > max_recent_msgs
)
59 recent_msgs_
.pop_front();
61 for (auto participant
: participants_
)
62 participant
->deliver(msg
);
66 std::set
<chat_participant_ptr
> participants_
;
67 enum { max_recent_msgs
= 100 };
68 chat_message_queue recent_msgs_
;
71 //----------------------------------------------------------------------
74 : public chat_participant
,
75 public std::enable_shared_from_this
<chat_session
>
78 chat_session(tcp::socket socket
, chat_room
& room
)
79 : socket_(std::move(socket
)),
86 room_
.join(shared_from_this());
90 void deliver(const chat_message
& msg
)
92 bool write_in_progress
= !write_msgs_
.empty();
93 write_msgs_
.push_back(msg
);
94 if (!write_in_progress
)
101 void do_read_header()
103 auto self(shared_from_this());
104 boost::asio::async_read(socket_
,
105 boost::asio::buffer(read_msg_
.data(), chat_message::header_length
),
106 [this, self
](boost::system::error_code ec
, std::size_t /*length*/)
108 if (!ec
&& read_msg_
.decode_header())
114 room_
.leave(shared_from_this());
121 auto self(shared_from_this());
122 boost::asio::async_read(socket_
,
123 boost::asio::buffer(read_msg_
.body(), read_msg_
.body_length()),
124 [this, self
](boost::system::error_code ec
, std::size_t /*length*/)
128 room_
.deliver(read_msg_
);
133 room_
.leave(shared_from_this());
140 auto self(shared_from_this());
141 boost::asio::async_write(socket_
,
142 boost::asio::buffer(write_msgs_
.front().data(),
143 write_msgs_
.front().length()),
144 [this, self
](boost::system::error_code ec
, std::size_t /*length*/)
148 write_msgs_
.pop_front();
149 if (!write_msgs_
.empty())
156 room_
.leave(shared_from_this());
163 chat_message read_msg_
;
164 chat_message_queue write_msgs_
;
167 //----------------------------------------------------------------------
172 chat_server(boost::asio::io_service
& io_service
,
173 const tcp::endpoint
& endpoint
)
174 : acceptor_(io_service
, endpoint
),
183 acceptor_
.async_accept(socket_
,
184 [this](boost::system::error_code ec
)
188 std::make_shared
<chat_session
>(std::move(socket_
), room_
)->start();
195 tcp::acceptor acceptor_
;
200 //----------------------------------------------------------------------
202 int main(int argc
, char* argv
[])
208 std::cerr
<< "Usage: chat_server <port> [<port> ...]\n";
212 boost::asio::io_service io_service
;
214 std::list
<chat_server
> servers
;
215 for (int i
= 1; i
< argc
; ++i
)
217 tcp::endpoint
endpoint(tcp::v4(), std::atoi(argv
[i
]));
218 servers
.emplace_back(io_service
, endpoint
);
223 catch (std::exception
& e
)
225 std::cerr
<< "Exception: " << e
.what() << "\n";