]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp03/tutorial/daytime7/server.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / asio / example / cpp03 / tutorial / daytime7 / server.cpp
1 //
2 // server.cpp
3 // ~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2017 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 <ctime>
12 #include <iostream>
13 #include <string>
14 #include <boost/array.hpp>
15 #include <boost/bind.hpp>
16 #include <boost/shared_ptr.hpp>
17 #include <boost/enable_shared_from_this.hpp>
18 #include <boost/asio.hpp>
19
20 using boost::asio::ip::tcp;
21 using boost::asio::ip::udp;
22
23 std::string make_daytime_string()
24 {
25 using namespace std; // For time_t, time and ctime;
26 time_t now = time(0);
27 return ctime(&now);
28 }
29
30 class tcp_connection
31 : public boost::enable_shared_from_this<tcp_connection>
32 {
33 public:
34 typedef boost::shared_ptr<tcp_connection> pointer;
35
36 static pointer create(boost::asio::io_context& io_context)
37 {
38 return pointer(new tcp_connection(io_context));
39 }
40
41 tcp::socket& socket()
42 {
43 return socket_;
44 }
45
46 void start()
47 {
48 message_ = make_daytime_string();
49
50 boost::asio::async_write(socket_, boost::asio::buffer(message_),
51 boost::bind(&tcp_connection::handle_write, shared_from_this()));
52 }
53
54 private:
55 tcp_connection(boost::asio::io_context& io_context)
56 : socket_(io_context)
57 {
58 }
59
60 void handle_write()
61 {
62 }
63
64 tcp::socket socket_;
65 std::string message_;
66 };
67
68 class tcp_server
69 {
70 public:
71 tcp_server(boost::asio::io_context& io_context)
72 : acceptor_(io_context, tcp::endpoint(tcp::v4(), 13))
73 {
74 start_accept();
75 }
76
77 private:
78 void start_accept()
79 {
80 tcp_connection::pointer new_connection =
81 tcp_connection::create(acceptor_.get_executor().context());
82
83 acceptor_.async_accept(new_connection->socket(),
84 boost::bind(&tcp_server::handle_accept, this, new_connection,
85 boost::asio::placeholders::error));
86 }
87
88 void handle_accept(tcp_connection::pointer new_connection,
89 const boost::system::error_code& error)
90 {
91 if (!error)
92 {
93 new_connection->start();
94 }
95
96 start_accept();
97 }
98
99 tcp::acceptor acceptor_;
100 };
101
102 class udp_server
103 {
104 public:
105 udp_server(boost::asio::io_context& io_context)
106 : socket_(io_context, udp::endpoint(udp::v4(), 13))
107 {
108 start_receive();
109 }
110
111 private:
112 void start_receive()
113 {
114 socket_.async_receive_from(
115 boost::asio::buffer(recv_buffer_), remote_endpoint_,
116 boost::bind(&udp_server::handle_receive, this,
117 boost::asio::placeholders::error));
118 }
119
120 void handle_receive(const boost::system::error_code& error)
121 {
122 if (!error)
123 {
124 boost::shared_ptr<std::string> message(
125 new std::string(make_daytime_string()));
126
127 socket_.async_send_to(boost::asio::buffer(*message), remote_endpoint_,
128 boost::bind(&udp_server::handle_send, this, message));
129
130 start_receive();
131 }
132 }
133
134 void handle_send(boost::shared_ptr<std::string> /*message*/)
135 {
136 }
137
138 udp::socket socket_;
139 udp::endpoint remote_endpoint_;
140 boost::array<char, 1> recv_buffer_;
141 };
142
143 int main()
144 {
145 try
146 {
147 boost::asio::io_context io_context;
148 tcp_server server1(io_context);
149 udp_server server2(io_context);
150 io_context.run();
151 }
152 catch (std::exception& e)
153 {
154 std::cerr << e.what() << std::endl;
155 }
156
157 return 0;
158 }