5 // Copyright (c) 2003-2022 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)
19 #include <boost/asio/awaitable.hpp>
20 #include <boost/asio/detached.hpp>
21 #include <boost/asio/co_spawn.hpp>
22 #include <boost/asio/io_context.hpp>
23 #include <boost/asio/ip/tcp.hpp>
24 #include <boost/asio/read_until.hpp>
25 #include <boost/asio/redirect_error.hpp>
26 #include <boost/asio/signal_set.hpp>
27 #include <boost/asio/steady_timer.hpp>
28 #include <boost/asio/use_awaitable.hpp>
29 #include <boost/asio/write.hpp>
31 using boost::asio::ip::tcp
;
32 using boost::asio::awaitable
;
33 using boost::asio::co_spawn
;
34 using boost::asio::detached
;
35 using boost::asio::redirect_error
;
36 using boost::asio::use_awaitable
;
38 //----------------------------------------------------------------------
40 class chat_participant
43 virtual ~chat_participant() {}
44 virtual void deliver(const std::string
& msg
) = 0;
47 typedef std::shared_ptr
<chat_participant
> chat_participant_ptr
;
49 //----------------------------------------------------------------------
54 void join(chat_participant_ptr participant
)
56 participants_
.insert(participant
);
57 for (auto msg
: recent_msgs_
)
58 participant
->deliver(msg
);
61 void leave(chat_participant_ptr participant
)
63 participants_
.erase(participant
);
66 void deliver(const std::string
& msg
)
68 recent_msgs_
.push_back(msg
);
69 while (recent_msgs_
.size() > max_recent_msgs
)
70 recent_msgs_
.pop_front();
72 for (auto participant
: participants_
)
73 participant
->deliver(msg
);
77 std::set
<chat_participant_ptr
> participants_
;
78 enum { max_recent_msgs
= 100 };
79 std::deque
<std::string
> recent_msgs_
;
82 //----------------------------------------------------------------------
85 : public chat_participant
,
86 public std::enable_shared_from_this
<chat_session
>
89 chat_session(tcp::socket socket
, chat_room
& room
)
90 : socket_(std::move(socket
)),
91 timer_(socket_
.get_executor()),
94 timer_
.expires_at(std::chrono::steady_clock::time_point::max());
99 room_
.join(shared_from_this());
101 co_spawn(socket_
.get_executor(),
102 [self
= shared_from_this()]{ return self
->reader(); },
105 co_spawn(socket_
.get_executor(),
106 [self
= shared_from_this()]{ return self
->writer(); },
110 void deliver(const std::string
& msg
)
112 write_msgs_
.push_back(msg
);
117 awaitable
<void> reader()
121 for (std::string read_msg
;;)
123 std::size_t n
= co_await
boost::asio::async_read_until(socket_
,
124 boost::asio::dynamic_buffer(read_msg
, 1024), "\n", use_awaitable
);
126 room_
.deliver(read_msg
.substr(0, n
));
127 read_msg
.erase(0, n
);
130 catch (std::exception
&)
136 awaitable
<void> writer()
140 while (socket_
.is_open())
142 if (write_msgs_
.empty())
144 boost::system::error_code ec
;
145 co_await timer_
.async_wait(redirect_error(use_awaitable
, ec
));
149 co_await
boost::asio::async_write(socket_
,
150 boost::asio::buffer(write_msgs_
.front()), use_awaitable
);
151 write_msgs_
.pop_front();
155 catch (std::exception
&)
163 room_
.leave(shared_from_this());
169 boost::asio::steady_timer timer_
;
171 std::deque
<std::string
> write_msgs_
;
174 //----------------------------------------------------------------------
176 awaitable
<void> listener(tcp::acceptor acceptor
)
182 std::make_shared
<chat_session
>(
183 co_await acceptor
.async_accept(use_awaitable
),
189 //----------------------------------------------------------------------
191 int main(int argc
, char* argv
[])
197 std::cerr
<< "Usage: chat_server <port> [<port> ...]\n";
201 boost::asio::io_context
io_context(1);
203 for (int i
= 1; i
< argc
; ++i
)
205 unsigned short port
= std::atoi(argv
[i
]);
207 listener(tcp::acceptor(io_context
, {tcp::v4(), port
})),
211 boost::asio::signal_set
signals(io_context
, SIGINT
, SIGTERM
);
212 signals
.async_wait([&](auto, auto){ io_context
.stop(); });
216 catch (std::exception
& e
)
218 std::cerr
<< "Exception: " << e
.what() << "\n";