]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp11/local/stream_server.cpp
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)
15 #include <boost/asio.hpp>
17 #if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
19 using boost::asio::local::stream_protocol
;
22 : public std::enable_shared_from_this
<session
>
25 session(stream_protocol::socket sock
)
26 : socket_(std::move(sock
))
38 auto self(shared_from_this());
39 socket_
.async_read_some(boost::asio::buffer(data_
),
40 [this, self
](boost::system::error_code ec
, std::size_t length
)
47 void do_write(std::size_t length
)
49 auto self(shared_from_this());
50 boost::asio::async_write(socket_
,
51 boost::asio::buffer(data_
, length
),
52 [this, self
](boost::system::error_code ec
, std::size_t /*length*/)
59 // The socket used to communicate with the client.
60 stream_protocol::socket socket_
;
62 // Buffer used to store data received from the client.
63 std::array
<char, 1024> data_
;
69 server(boost::asio::io_context
& io_context
, const std::string
& file
)
70 : acceptor_(io_context
, stream_protocol::endpoint(file
))
78 acceptor_
.async_accept(
79 [this](boost::system::error_code ec
, stream_protocol::socket socket
)
83 std::make_shared
<session
>(std::move(socket
))->start();
90 stream_protocol::acceptor acceptor_
;
93 int main(int argc
, char* argv
[])
99 std::cerr
<< "Usage: stream_server <file>\n";
100 std::cerr
<< "*** WARNING: existing file is removed ***\n";
104 boost::asio::io_context io_context
;
106 std::remove(argv
[1]);
107 server
s(io_context
, argv
[1]);
111 catch (std::exception
& e
)
113 std::cerr
<< "Exception: " << e
.what() << "\n";
119 #else // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
120 # error Local sockets not available on this platform.
121 #endif // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)