• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // blocking_udp_client.cpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2021 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 <boost/asio/buffer.hpp>
12 #include <boost/asio/io_context.hpp>
13 #include <boost/asio/ip/udp.hpp>
14 #include <cstdlib>
15 #include <boost/bind/bind.hpp>
16 #include <iostream>
17 
18 using boost::asio::ip::udp;
19 
20 //----------------------------------------------------------------------
21 
22 //
23 // This class manages socket timeouts by running the io_context using the timed
24 // io_context::run_for() member function. Each asynchronous operation is given
25 // a timeout within which it must complete. The socket operations themselves
26 // use boost::bind to specify the completion handler:
27 //
28 //   +---------------+
29 //   |               |
30 //   |    receive    |
31 //   |               |
32 //   +---------------+
33 //           |
34 //  async_-  |    +----------------+
35 // receive() |    |                |
36 //           +--->| handle_receive |
37 //                |                |
38 //                +----------------+
39 //
40 // For a given socket operation, the client object runs the io_context to block
41 // thread execution until the operation completes or the timeout is reached. If
42 // the io_context::run_for() function times out, the socket is closed and the
43 // outstanding asynchronous operation is cancelled.
44 //
45 class client
46 {
47 public:
client(const udp::endpoint & listen_endpoint)48   client(const udp::endpoint& listen_endpoint)
49     : socket_(io_context_, listen_endpoint)
50   {
51   }
52 
receive(const boost::asio::mutable_buffer & buffer,boost::asio::chrono::steady_clock::duration timeout,boost::system::error_code & ec)53   std::size_t receive(const boost::asio::mutable_buffer& buffer,
54       boost::asio::chrono::steady_clock::duration timeout,
55       boost::system::error_code& ec)
56   {
57     // Start the asynchronous operation. The handle_receive function used as a
58     // callback will update the ec and length variables.
59     std::size_t length = 0;
60     socket_.async_receive(boost::asio::buffer(buffer),
61         boost::bind(&client::handle_receive,
62           boost::placeholders::_1, boost::placeholders::_2, &ec, &length));
63 
64     // Run the operation until it completes, or until the timeout.
65     run(timeout);
66 
67     return length;
68   }
69 
70 private:
run(boost::asio::chrono::steady_clock::duration timeout)71   void run(boost::asio::chrono::steady_clock::duration timeout)
72   {
73     // Restart the io_context, as it may have been left in the "stopped" state
74     // by a previous operation.
75     io_context_.restart();
76 
77     // Block until the asynchronous operation has completed, or timed out. If
78     // the pending asynchronous operation is a composed operation, the deadline
79     // applies to the entire operation, rather than individual operations on
80     // the socket.
81     io_context_.run_for(timeout);
82 
83     // If the asynchronous operation completed successfully then the io_context
84     // would have been stopped due to running out of work. If it was not
85     // stopped, then the io_context::run_for call must have timed out.
86     if (!io_context_.stopped())
87     {
88       // Cancel the outstanding asynchronous operation.
89       socket_.cancel();
90 
91       // Run the io_context again until the operation completes.
92       io_context_.run();
93     }
94   }
95 
handle_receive(const boost::system::error_code & ec,std::size_t length,boost::system::error_code * out_ec,std::size_t * out_length)96   static void handle_receive(
97       const boost::system::error_code& ec, std::size_t length,
98       boost::system::error_code* out_ec, std::size_t* out_length)
99   {
100     *out_ec = ec;
101     *out_length = length;
102   }
103 
104 private:
105   boost::asio::io_context io_context_;
106   udp::socket socket_;
107 };
108 
109 //----------------------------------------------------------------------
110 
main(int argc,char * argv[])111 int main(int argc, char* argv[])
112 {
113   try
114   {
115     using namespace std; // For atoi.
116 
117     if (argc != 3)
118     {
119       std::cerr << "Usage: blocking_udp_client <listen_addr> <listen_port>\n";
120       return 1;
121     }
122 
123     udp::endpoint listen_endpoint(
124         boost::asio::ip::make_address(argv[1]),
125         std::atoi(argv[2]));
126 
127     client c(listen_endpoint);
128 
129     for (;;)
130     {
131       char data[1024];
132       boost::system::error_code ec;
133       std::size_t n = c.receive(boost::asio::buffer(data),
134           boost::asio::chrono::seconds(10), ec);
135 
136       if (ec)
137       {
138         std::cout << "Receive error: " << ec.message() << "\n";
139       }
140       else
141       {
142         std::cout << "Received: ";
143         std::cout.write(data, n);
144         std::cout << "\n";
145       }
146     }
147   }
148   catch (std::exception& e)
149   {
150     std::cerr << "Exception: " << e.what() << "\n";
151   }
152 
153   return 0;
154 }
155