]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp03/local/stream_server.cpp
5 // Copyright (c) 2003-2016 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)
13 #include <boost/array.hpp>
14 #include <boost/bind.hpp>
15 #include <boost/enable_shared_from_this.hpp>
16 #include <boost/shared_ptr.hpp>
17 #include <boost/asio.hpp>
19 #if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
21 using boost::asio::local::stream_protocol
;
24 : public boost::enable_shared_from_this
<session
>
27 session(boost::asio::io_service
& io_service
)
32 stream_protocol::socket
& socket()
39 socket_
.async_read_some(boost::asio::buffer(data_
),
40 boost::bind(&session::handle_read
,
42 boost::asio::placeholders::error
,
43 boost::asio::placeholders::bytes_transferred
));
46 void handle_read(const boost::system::error_code
& error
,
47 size_t bytes_transferred
)
51 boost::asio::async_write(socket_
,
52 boost::asio::buffer(data_
, bytes_transferred
),
53 boost::bind(&session::handle_write
,
55 boost::asio::placeholders::error
));
59 void handle_write(const boost::system::error_code
& error
)
63 socket_
.async_read_some(boost::asio::buffer(data_
),
64 boost::bind(&session::handle_read
,
66 boost::asio::placeholders::error
,
67 boost::asio::placeholders::bytes_transferred
));
72 // The socket used to communicate with the client.
73 stream_protocol::socket socket_
;
75 // Buffer used to store data received from the client.
76 boost::array
<char, 1024> data_
;
79 typedef boost::shared_ptr
<session
> session_ptr
;
84 server(boost::asio::io_service
& io_service
, const std::string
& file
)
85 : io_service_(io_service
),
86 acceptor_(io_service
, stream_protocol::endpoint(file
))
88 session_ptr
new_session(new session(io_service_
));
89 acceptor_
.async_accept(new_session
->socket(),
90 boost::bind(&server::handle_accept
, this, new_session
,
91 boost::asio::placeholders::error
));
94 void handle_accept(session_ptr new_session
,
95 const boost::system::error_code
& error
)
102 new_session
.reset(new session(io_service_
));
103 acceptor_
.async_accept(new_session
->socket(),
104 boost::bind(&server::handle_accept
, this, new_session
,
105 boost::asio::placeholders::error
));
109 boost::asio::io_service
& io_service_
;
110 stream_protocol::acceptor acceptor_
;
113 int main(int argc
, char* argv
[])
119 std::cerr
<< "Usage: stream_server <file>\n";
120 std::cerr
<< "*** WARNING: existing file is removed ***\n";
124 boost::asio::io_service io_service
;
126 std::remove(argv
[1]);
127 server
s(io_service
, argv
[1]);
131 catch (std::exception
& e
)
133 std::cerr
<< "Exception: " << e
.what() << "\n";
139 #else // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
140 # error Local sockets not available on this platform.
141 #endif // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)