]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // stream_server.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 <cstdio> | |
12 | #include <iostream> | |
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> | |
18 | ||
19 | #if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS) | |
20 | ||
21 | using boost::asio::local::stream_protocol; | |
22 | ||
23 | class session | |
24 | : public boost::enable_shared_from_this<session> | |
25 | { | |
26 | public: | |
b32b8144 FG |
27 | session(boost::asio::io_context& io_context) |
28 | : socket_(io_context) | |
7c673cae FG |
29 | { |
30 | } | |
31 | ||
32 | stream_protocol::socket& socket() | |
33 | { | |
34 | return socket_; | |
35 | } | |
36 | ||
37 | void start() | |
38 | { | |
39 | socket_.async_read_some(boost::asio::buffer(data_), | |
40 | boost::bind(&session::handle_read, | |
41 | shared_from_this(), | |
42 | boost::asio::placeholders::error, | |
43 | boost::asio::placeholders::bytes_transferred)); | |
44 | } | |
45 | ||
46 | void handle_read(const boost::system::error_code& error, | |
47 | size_t bytes_transferred) | |
48 | { | |
49 | if (!error) | |
50 | { | |
51 | boost::asio::async_write(socket_, | |
52 | boost::asio::buffer(data_, bytes_transferred), | |
53 | boost::bind(&session::handle_write, | |
54 | shared_from_this(), | |
55 | boost::asio::placeholders::error)); | |
56 | } | |
57 | } | |
58 | ||
59 | void handle_write(const boost::system::error_code& error) | |
60 | { | |
61 | if (!error) | |
62 | { | |
63 | socket_.async_read_some(boost::asio::buffer(data_), | |
64 | boost::bind(&session::handle_read, | |
65 | shared_from_this(), | |
66 | boost::asio::placeholders::error, | |
67 | boost::asio::placeholders::bytes_transferred)); | |
68 | } | |
69 | } | |
70 | ||
71 | private: | |
72 | // The socket used to communicate with the client. | |
73 | stream_protocol::socket socket_; | |
74 | ||
75 | // Buffer used to store data received from the client. | |
76 | boost::array<char, 1024> data_; | |
77 | }; | |
78 | ||
79 | typedef boost::shared_ptr<session> session_ptr; | |
80 | ||
81 | class server | |
82 | { | |
83 | public: | |
b32b8144 FG |
84 | server(boost::asio::io_context& io_context, const std::string& file) |
85 | : io_context_(io_context), | |
86 | acceptor_(io_context, stream_protocol::endpoint(file)) | |
7c673cae | 87 | { |
b32b8144 | 88 | session_ptr new_session(new session(io_context_)); |
7c673cae FG |
89 | acceptor_.async_accept(new_session->socket(), |
90 | boost::bind(&server::handle_accept, this, new_session, | |
91 | boost::asio::placeholders::error)); | |
92 | } | |
93 | ||
94 | void handle_accept(session_ptr new_session, | |
95 | const boost::system::error_code& error) | |
96 | { | |
97 | if (!error) | |
98 | { | |
99 | new_session->start(); | |
100 | } | |
101 | ||
b32b8144 | 102 | new_session.reset(new session(io_context_)); |
7c673cae FG |
103 | acceptor_.async_accept(new_session->socket(), |
104 | boost::bind(&server::handle_accept, this, new_session, | |
105 | boost::asio::placeholders::error)); | |
106 | } | |
107 | ||
108 | private: | |
b32b8144 | 109 | boost::asio::io_context& io_context_; |
7c673cae FG |
110 | stream_protocol::acceptor acceptor_; |
111 | }; | |
112 | ||
113 | int main(int argc, char* argv[]) | |
114 | { | |
115 | try | |
116 | { | |
117 | if (argc != 2) | |
118 | { | |
119 | std::cerr << "Usage: stream_server <file>\n"; | |
120 | std::cerr << "*** WARNING: existing file is removed ***\n"; | |
121 | return 1; | |
122 | } | |
123 | ||
b32b8144 | 124 | boost::asio::io_context io_context; |
7c673cae FG |
125 | |
126 | std::remove(argv[1]); | |
b32b8144 | 127 | server s(io_context, argv[1]); |
7c673cae | 128 | |
b32b8144 | 129 | io_context.run(); |
7c673cae FG |
130 | } |
131 | catch (std::exception& e) | |
132 | { | |
133 | std::cerr << "Exception: " << e.what() << "\n"; | |
134 | } | |
135 | ||
136 | return 0; | |
137 | } | |
138 | ||
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) |