]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/asio/example/cpp03/buffers/reference_counted.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / asio / example / cpp03 / buffers / reference_counted.cpp
CommitLineData
7c673cae
FG
1//
2// reference_counted.cpp
3// ~~~~~~~~~~~~~~~~~~~~~
4//
b32b8144 5// Copyright (c) 2003-2017 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 <boost/asio.hpp>
12#include <boost/bind.hpp>
13#include <boost/enable_shared_from_this.hpp>
14#include <boost/shared_ptr.hpp>
15#include <iostream>
16#include <vector>
17
18using boost::asio::ip::tcp;
19
20// A reference-counted non-modifiable buffer class.
21class shared_const_buffer
22{
23public:
24 // Construct from a std::string.
25 explicit shared_const_buffer(const std::string& data)
26 : data_(new std::vector<char>(data.begin(), data.end())),
27 buffer_(boost::asio::buffer(*data_))
28 {
29 }
30
31 // Implement the ConstBufferSequence requirements.
32 typedef boost::asio::const_buffer value_type;
33 typedef const boost::asio::const_buffer* const_iterator;
34 const boost::asio::const_buffer* begin() const { return &buffer_; }
35 const boost::asio::const_buffer* end() const { return &buffer_ + 1; }
36
37private:
38 boost::shared_ptr<std::vector<char> > data_;
39 boost::asio::const_buffer buffer_;
40};
41
42class session
43 : public boost::enable_shared_from_this<session>
44{
45public:
b32b8144
FG
46 session(boost::asio::io_context& io_context)
47 : socket_(io_context)
7c673cae
FG
48 {
49 }
50
51 tcp::socket& socket()
52 {
53 return socket_;
54 }
55
56 void start()
57 {
58 using namespace std; // For time_t, time and ctime.
59 time_t now = time(0);
60 shared_const_buffer buffer(ctime(&now));
61 boost::asio::async_write(socket_, buffer,
62 boost::bind(&session::handle_write, shared_from_this()));
63 }
64
65 void handle_write()
66 {
67 }
68
69private:
70 // The socket used to communicate with the client.
71 tcp::socket socket_;
72};
73
74typedef boost::shared_ptr<session> session_ptr;
75
76class server
77{
78public:
b32b8144
FG
79 server(boost::asio::io_context& io_context, short port)
80 : io_context_(io_context),
81 acceptor_(io_context, tcp::endpoint(tcp::v4(), port))
7c673cae 82 {
b32b8144 83 session_ptr new_session(new session(io_context_));
7c673cae
FG
84 acceptor_.async_accept(new_session->socket(),
85 boost::bind(&server::handle_accept, this, new_session,
86 boost::asio::placeholders::error));
87 }
88
89 void handle_accept(session_ptr new_session,
90 const boost::system::error_code& error)
91 {
92 if (!error)
93 {
94 new_session->start();
95 }
96
b32b8144 97 new_session.reset(new session(io_context_));
7c673cae
FG
98 acceptor_.async_accept(new_session->socket(),
99 boost::bind(&server::handle_accept, this, new_session,
100 boost::asio::placeholders::error));
101 }
102
103private:
b32b8144 104 boost::asio::io_context& io_context_;
7c673cae
FG
105 tcp::acceptor acceptor_;
106};
107
108int main(int argc, char* argv[])
109{
110 try
111 {
112 if (argc != 2)
113 {
114 std::cerr << "Usage: reference_counted <port>\n";
115 return 1;
116 }
117
b32b8144 118 boost::asio::io_context io_context;
7c673cae
FG
119
120 using namespace std; // For atoi.
b32b8144 121 server s(io_context, atoi(argv[1]));
7c673cae 122
b32b8144 123 io_context.run();
7c673cae
FG
124 }
125 catch (std::exception& e)
126 {
127 std::cerr << "Exception: " << e.what() << "\n";
128 }
129
130 return 0;
131}