1 //
2 // echo_server.cpp
3 // ~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2019 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 <asio/io_context.hpp>
12 #include <asio/ip/tcp.hpp>
13 #include <asio/spawn.hpp>
14 #include <asio/steady_timer.hpp>
15 #include <asio/write.hpp>
16 #include <boost/bind.hpp>
17 #include <boost/shared_ptr.hpp>
18 #include <boost/enable_shared_from_this.hpp>
19 #include <iostream>
20
21 using asio::ip::tcp;
22
23 class session : public boost::enable_shared_from_this<session>
24 {
25 public:
session(asio::io_context & io_context)26 explicit session(asio::io_context& io_context)
27 : strand_(asio::make_strand(io_context)),
28 socket_(io_context),
29 timer_(io_context)
30 {
31 }
32
socket()33 tcp::socket& socket()
34 {
35 return socket_;
36 }
37
go()38 void go()
39 {
40 asio::spawn(strand_,
41 boost::bind(&session::echo,
42 shared_from_this(), _1));
43 asio::spawn(strand_,
44 boost::bind(&session::timeout,
45 shared_from_this(), _1));
46 }
47
48 private:
echo(asio::yield_context yield)49 void echo(asio::yield_context yield)
50 {
51 try
52 {
53 char data[128];
54 for (;;)
55 {
56 timer_.expires_after(asio::chrono::seconds(10));
57 std::size_t n = socket_.async_read_some(asio::buffer(data), yield);
58 asio::async_write(socket_, asio::buffer(data, n), yield);
59 }
60 }
61 catch (std::exception& e)
62 {
63 socket_.close();
64 timer_.cancel();
65 }
66 }
67
timeout(asio::yield_context yield)68 void timeout(asio::yield_context yield)
69 {
70 while (socket_.is_open())
71 {
72 asio::error_code ignored_ec;
73 timer_.async_wait(yield[ignored_ec]);
74 if (timer_.expiry() <= asio::steady_timer::clock_type::now())
75 socket_.close();
76 }
77 }
78
79 asio::strand<asio::io_context::executor_type> strand_;
80 tcp::socket socket_;
81 asio::steady_timer timer_;
82 };
83
do_accept(asio::io_context & io_context,unsigned short port,asio::yield_context yield)84 void do_accept(asio::io_context& io_context,
85 unsigned short port, asio::yield_context yield)
86 {
87 tcp::acceptor acceptor(io_context, tcp::endpoint(tcp::v4(), port));
88
89 for (;;)
90 {
91 asio::error_code ec;
92 boost::shared_ptr<session> new_session(new session(io_context));
93 acceptor.async_accept(new_session->socket(), yield[ec]);
94 if (!ec) new_session->go();
95 }
96 }
97
main(int argc,char * argv[])98 int main(int argc, char* argv[])
99 {
100 try
101 {
102 if (argc != 2)
103 {
104 std::cerr << "Usage: echo_server <port>\n";
105 return 1;
106 }
107
108 asio::io_context io_context;
109
110 asio::spawn(io_context,
111 boost::bind(do_accept,
112 boost::ref(io_context), atoi(argv[1]), _1));
113
114 io_context.run();
115 }
116 catch (std::exception& e)
117 {
118 std::cerr << "Exception: " << e.what() << "\n";
119 }
120
121 return 0;
122 }
123