]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/asio/example/cpp03/tutorial/daytime7/server.cpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / libs / asio / example / cpp03 / tutorial / daytime7 / server.cpp
CommitLineData
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/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
20using boost::asio::ip::tcp;
21using boost::asio::ip::udp;
22
23std::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
30class tcp_connection
31 : public boost::enable_shared_from_this<tcp_connection>
32{
33public:
34 typedef boost::shared_ptr<tcp_connection> pointer;
35
b32b8144 36 static pointer create(boost::asio::io_context& io_context)
7c673cae 37 {
b32b8144 38 return pointer(new tcp_connection(io_context));
7c673cae
FG
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
54private:
b32b8144
FG
55 tcp_connection(boost::asio::io_context& io_context)
56 : socket_(io_context)
7c673cae
FG
57 {
58 }
59
60 void handle_write()
61 {
62 }
63
64 tcp::socket socket_;
65 std::string message_;
66};
67
68class tcp_server
69{
70public:
b32b8144 71 tcp_server(boost::asio::io_context& io_context)
92f5a8d4
TL
72 : io_context_(io_context),
73 acceptor_(io_context, tcp::endpoint(tcp::v4(), 13))
7c673cae
FG
74 {
75 start_accept();
76 }
77
78private:
79 void start_accept()
80 {
81 tcp_connection::pointer new_connection =
92f5a8d4 82 tcp_connection::create(io_context_);
7c673cae
FG
83
84 acceptor_.async_accept(new_connection->socket(),
85 boost::bind(&tcp_server::handle_accept, this, new_connection,
86 boost::asio::placeholders::error));
87 }
88
89 void handle_accept(tcp_connection::pointer new_connection,
90 const boost::system::error_code& error)
91 {
92 if (!error)
93 {
94 new_connection->start();
95 }
96
97 start_accept();
98 }
99
92f5a8d4 100 boost::asio::io_context& io_context_;
7c673cae
FG
101 tcp::acceptor acceptor_;
102};
103
104class udp_server
105{
106public:
b32b8144
FG
107 udp_server(boost::asio::io_context& io_context)
108 : socket_(io_context, udp::endpoint(udp::v4(), 13))
7c673cae
FG
109 {
110 start_receive();
111 }
112
113private:
114 void start_receive()
115 {
116 socket_.async_receive_from(
117 boost::asio::buffer(recv_buffer_), remote_endpoint_,
118 boost::bind(&udp_server::handle_receive, this,
119 boost::asio::placeholders::error));
120 }
121
122 void handle_receive(const boost::system::error_code& error)
123 {
b32b8144 124 if (!error)
7c673cae
FG
125 {
126 boost::shared_ptr<std::string> message(
127 new std::string(make_daytime_string()));
128
129 socket_.async_send_to(boost::asio::buffer(*message), remote_endpoint_,
130 boost::bind(&udp_server::handle_send, this, message));
131
132 start_receive();
133 }
134 }
135
136 void handle_send(boost::shared_ptr<std::string> /*message*/)
137 {
138 }
139
140 udp::socket socket_;
141 udp::endpoint remote_endpoint_;
142 boost::array<char, 1> recv_buffer_;
143};
144
145int main()
146{
147 try
148 {
b32b8144
FG
149 boost::asio::io_context io_context;
150 tcp_server server1(io_context);
151 udp_server server2(io_context);
152 io_context.run();
7c673cae
FG
153 }
154 catch (std::exception& e)
155 {
156 std::cerr << e.what() << std::endl;
157 }
158
159 return 0;
160}