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