• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // connect_pair.cpp
3 // ~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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 <array>
12 #include <iostream>
13 #include <string>
14 #include <cctype>
15 #include <boost/asio.hpp>
16 #include <boost/thread/thread.hpp>
17 
18 #if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
19 
20 using boost::asio::local::stream_protocol;
21 
22 class uppercase_filter
23 {
24 public:
uppercase_filter(stream_protocol::socket sock)25   uppercase_filter(stream_protocol::socket sock)
26     : socket_(std::move(sock))
27   {
28     read();
29   }
30 
31 private:
read()32   void read()
33   {
34     socket_.async_read_some(boost::asio::buffer(data_),
35         [this](boost::system::error_code ec, std::size_t size)
36         {
37           if (!ec)
38           {
39             // Compute result.
40             for (std::size_t i = 0; i < size; ++i)
41               data_[i] = std::toupper(data_[i]);
42 
43             // Send result.
44             write(size);
45           }
46           else
47           {
48             throw boost::system::system_error(ec);
49           }
50         });
51   }
52 
write(std::size_t size)53   void write(std::size_t size)
54   {
55     boost::asio::async_write(socket_, boost::asio::buffer(data_, size),
56         [this](boost::system::error_code ec, std::size_t /*size*/)
57         {
58           if (!ec)
59           {
60             // Wait for request.
61             read();
62           }
63           else
64           {
65             throw boost::system::system_error(ec);
66           }
67         });
68   }
69 
70   stream_protocol::socket socket_;
71   std::array<char, 512> data_;
72 };
73 
main()74 int main()
75 {
76   try
77   {
78     boost::asio::io_context io_context;
79 
80     // Create a connected pair and pass one end to a filter.
81     stream_protocol::socket socket(io_context);
82     stream_protocol::socket filter_socket(io_context);
83     boost::asio::local::connect_pair(socket, filter_socket);
84     uppercase_filter filter(std::move(filter_socket));
85 
86     // The io_context runs in a background thread to perform filtering.
87     boost::thread thread(
88         [&io_context]()
89         {
90           try
91           {
92             io_context.run();
93           }
94           catch (std::exception& e)
95           {
96             std::cerr << "Exception in thread: " << e.what() << "\n";
97             std::exit(1);
98           }
99         });
100 
101     for (;;)
102     {
103       // Collect request from user.
104       std::cout << "Enter a string: ";
105       std::string request;
106       std::getline(std::cin, request);
107 
108       // Send request to filter.
109       boost::asio::write(socket, boost::asio::buffer(request));
110 
111       // Wait for reply from filter.
112       std::vector<char> reply(request.size());
113       boost::asio::read(socket, boost::asio::buffer(reply));
114 
115       // Show reply to user.
116       std::cout << "Result: ";
117       std::cout.write(&reply[0], request.size());
118       std::cout << std::endl;
119     }
120   }
121   catch (std::exception& e)
122   {
123     std::cerr << "Exception: " << e.what() << "\n";
124     std::exit(1);
125   }
126 }
127 
128 #else // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
129 # error Local sockets not available on this platform.
130 #endif // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
131