]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // server.cpp | |
3 | // ~~~~~~~~~~ | |
4 | // | |
92f5a8d4 | 5 | // Copyright (c) 2003-2019 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 | ||
11 | #include <ctime> | |
12 | #include <iostream> | |
13 | #include <string> | |
14 | #include <boost/bind.hpp> | |
15 | #include <boost/shared_ptr.hpp> | |
16 | #include <boost/enable_shared_from_this.hpp> | |
17 | #include <boost/asio.hpp> | |
18 | ||
19 | using boost::asio::ip::tcp; | |
20 | ||
21 | std::string make_daytime_string() | |
22 | { | |
23 | using namespace std; // For time_t, time and ctime; | |
24 | time_t now = time(0); | |
25 | return ctime(&now); | |
26 | } | |
27 | ||
28 | class tcp_connection | |
29 | : public boost::enable_shared_from_this<tcp_connection> | |
30 | { | |
31 | public: | |
32 | typedef boost::shared_ptr<tcp_connection> pointer; | |
33 | ||
b32b8144 | 34 | static pointer create(boost::asio::io_context& io_context) |
7c673cae | 35 | { |
b32b8144 | 36 | return pointer(new tcp_connection(io_context)); |
7c673cae FG |
37 | } |
38 | ||
39 | tcp::socket& socket() | |
40 | { | |
41 | return socket_; | |
42 | } | |
43 | ||
44 | void start() | |
45 | { | |
46 | message_ = make_daytime_string(); | |
47 | ||
48 | boost::asio::async_write(socket_, boost::asio::buffer(message_), | |
49 | boost::bind(&tcp_connection::handle_write, shared_from_this(), | |
50 | boost::asio::placeholders::error, | |
51 | boost::asio::placeholders::bytes_transferred)); | |
52 | } | |
53 | ||
54 | private: | |
b32b8144 FG |
55 | tcp_connection(boost::asio::io_context& io_context) |
56 | : socket_(io_context) | |
7c673cae FG |
57 | { |
58 | } | |
59 | ||
60 | void handle_write(const boost::system::error_code& /*error*/, | |
61 | size_t /*bytes_transferred*/) | |
62 | { | |
63 | } | |
64 | ||
65 | tcp::socket socket_; | |
66 | std::string message_; | |
67 | }; | |
68 | ||
69 | class tcp_server | |
70 | { | |
71 | public: | |
b32b8144 | 72 | tcp_server(boost::asio::io_context& io_context) |
92f5a8d4 TL |
73 | : io_context_(io_context), |
74 | acceptor_(io_context, tcp::endpoint(tcp::v4(), 13)) | |
7c673cae FG |
75 | { |
76 | start_accept(); | |
77 | } | |
78 | ||
79 | private: | |
80 | void start_accept() | |
81 | { | |
82 | tcp_connection::pointer new_connection = | |
92f5a8d4 | 83 | tcp_connection::create(io_context_); |
7c673cae FG |
84 | |
85 | acceptor_.async_accept(new_connection->socket(), | |
86 | boost::bind(&tcp_server::handle_accept, this, new_connection, | |
87 | boost::asio::placeholders::error)); | |
88 | } | |
89 | ||
90 | void handle_accept(tcp_connection::pointer new_connection, | |
91 | const boost::system::error_code& error) | |
92 | { | |
93 | if (!error) | |
94 | { | |
95 | new_connection->start(); | |
96 | } | |
97 | ||
98 | start_accept(); | |
99 | } | |
100 | ||
92f5a8d4 | 101 | boost::asio::io_context& io_context_; |
7c673cae FG |
102 | tcp::acceptor acceptor_; |
103 | }; | |
104 | ||
105 | int main() | |
106 | { | |
107 | try | |
108 | { | |
b32b8144 FG |
109 | boost::asio::io_context io_context; |
110 | tcp_server server(io_context); | |
111 | io_context.run(); | |
7c673cae FG |
112 | } |
113 | catch (std::exception& e) | |
114 | { | |
115 | std::cerr << e.what() << std::endl; | |
116 | } | |
117 | ||
118 | return 0; | |
119 | } |