]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp11/buffers/reference_counted.cpp
2 // reference_counted.cpp
3 // ~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #include <boost/asio.hpp>
17 using boost::asio::ip::tcp
;
19 // A reference-counted non-modifiable buffer class.
20 class shared_const_buffer
23 // Construct from a std::string.
24 explicit shared_const_buffer(const std::string
& data
)
25 : data_(new std::vector
<char>(data
.begin(), data
.end())),
26 buffer_(boost::asio::buffer(*data_
))
30 // Implement the ConstBufferSequence requirements.
31 typedef boost::asio::const_buffer value_type
;
32 typedef const boost::asio::const_buffer
* const_iterator
;
33 const boost::asio::const_buffer
* begin() const { return &buffer_
; }
34 const boost::asio::const_buffer
* end() const { return &buffer_
+ 1; }
37 std::shared_ptr
<std::vector
<char> > data_
;
38 boost::asio::const_buffer buffer_
;
42 : public std::enable_shared_from_this
<session
>
45 session(tcp::socket socket
)
46 : socket_(std::move(socket
))
58 std::time_t now
= std::time(0);
59 shared_const_buffer
buffer(std::ctime(&now
));
61 auto self(shared_from_this());
62 boost::asio::async_write(socket_
, buffer
,
63 [this, self
](boost::system::error_code
/*ec*/, std::size_t /*length*/)
68 // The socket used to communicate with the client.
75 server(boost::asio::io_context
& io_context
, short port
)
76 : acceptor_(io_context
, tcp::endpoint(tcp::v4(), port
))
84 acceptor_
.async_accept(
85 [this](boost::system::error_code ec
, tcp::socket socket
)
89 std::make_shared
<session
>(std::move(socket
))->start();
96 tcp::acceptor acceptor_
;
99 int main(int argc
, char* argv
[])
105 std::cerr
<< "Usage: reference_counted <port>\n";
109 boost::asio::io_context io_context
;
111 server
s(io_context
, std::atoi(argv
[1]));
115 catch (std::exception
& e
)
117 std::cerr
<< "Exception: " << e
.what() << "\n";