• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>basic_socket::native_non_blocking (2 of 3 overloads)</title>
5<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="../../../../boost_asio.html" title="Boost.Asio">
8<link rel="up" href="../native_non_blocking.html" title="basic_socket::native_non_blocking">
9<link rel="prev" href="overload1.html" title="basic_socket::native_non_blocking (1 of 3 overloads)">
10<link rel="next" href="overload3.html" title="basic_socket::native_non_blocking (3 of 3 overloads)">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%"><tr>
14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
15<td align="center"><a href="../../../../../../index.html">Home</a></td>
16<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
20</tr></table>
21<hr>
22<div class="spirit-nav">
23<a accesskey="p" href="overload1.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../native_non_blocking.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../boost_asio.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="overload3.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h5 class="title">
27<a name="boost_asio.reference.basic_socket.native_non_blocking.overload2"></a><a class="link" href="overload2.html" title="basic_socket::native_non_blocking (2 of 3 overloads)">basic_socket::native_non_blocking
28          (2 of 3 overloads)</a>
29</h5></div></div></div>
30<p>
31            Sets the non-blocking mode of the native socket implementation.
32          </p>
33<pre class="programlisting">void native_non_blocking(
34    bool mode);
35</pre>
36<p>
37            This function is used to modify the non-blocking mode of the underlying
38            native socket. It has no effect on the behaviour of the socket object's
39            synchronous operations.
40          </p>
41<h6>
42<a name="boost_asio.reference.basic_socket.native_non_blocking.overload2.h0"></a>
43            <span class="phrase"><a name="boost_asio.reference.basic_socket.native_non_blocking.overload2.parameters"></a></span><a class="link" href="overload2.html#boost_asio.reference.basic_socket.native_non_blocking.overload2.parameters">Parameters</a>
44          </h6>
45<div class="variablelist">
46<p class="title"><b></b></p>
47<dl class="variablelist">
48<dt><span class="term">mode</span></dt>
49<dd><p>
50                  If <code class="computeroutput">true</code>, the underlying socket is put into non-blocking
51                  mode and direct system calls may fail with <code class="computeroutput">boost::asio::error::would_block</code>
52                  (or the equivalent system error).
53                </p></dd>
54</dl>
55</div>
56<h6>
57<a name="boost_asio.reference.basic_socket.native_non_blocking.overload2.h1"></a>
58            <span class="phrase"><a name="boost_asio.reference.basic_socket.native_non_blocking.overload2.exceptions"></a></span><a class="link" href="overload2.html#boost_asio.reference.basic_socket.native_non_blocking.overload2.exceptions">Exceptions</a>
59          </h6>
60<div class="variablelist">
61<p class="title"><b></b></p>
62<dl class="variablelist">
63<dt><span class="term">boost::system::system_error</span></dt>
64<dd><p>
65                  Thrown on failure. If the <code class="computeroutput">mode</code> is <code class="computeroutput">false</code>,
66                  but the current value of <code class="computeroutput">non_blocking()</code> is <code class="computeroutput">true</code>,
67                  this function fails with <code class="computeroutput">boost::asio::error::invalid_argument</code>,
68                  as the combination does not make sense.
69                </p></dd>
70</dl>
71</div>
72<h6>
73<a name="boost_asio.reference.basic_socket.native_non_blocking.overload2.h2"></a>
74            <span class="phrase"><a name="boost_asio.reference.basic_socket.native_non_blocking.overload2.example"></a></span><a class="link" href="overload2.html#boost_asio.reference.basic_socket.native_non_blocking.overload2.example">Example</a>
75          </h6>
76<p>
77            This function is intended to allow the encapsulation of arbitrary non-blocking
78            system calls as asynchronous operations, in a way that is transparent
79            to the user of the socket object. The following example illustrates how
80            Linux's <code class="computeroutput">sendfile</code> system call might be encapsulated:
81          </p>
82<pre class="programlisting">template &lt;typename Handler&gt;
83struct sendfile_op
84{
85  tcp::socket&amp; sock_;
86  int fd_;
87  Handler handler_;
88  off_t offset_;
89  std::size_t total_bytes_transferred_;
90
91  // Function call operator meeting WriteHandler requirements.
92  // Used as the handler for the async_write_some operation.
93  void operator()(boost::system::error_code ec, std::size_t)
94  {
95    // Put the underlying socket into non-blocking mode.
96    if (!ec)
97      if (!sock_.native_non_blocking())
98        sock_.native_non_blocking(true, ec);
99
100    if (!ec)
101    {
102      for (;;)
103      {
104        // Try the system call.
105        errno = 0;
106        int n = ::sendfile(sock_.native_handle(), fd_, &amp;offset_, 65536);
107        ec = boost::system::error_code(n &lt; 0 ? errno : 0,
108            boost::asio::error::get_system_category());
109        total_bytes_transferred_ += ec ? 0 : n;
110
111        // Retry operation immediately if interrupted by signal.
112        if (ec == boost::asio::error::interrupted)
113          continue;
114
115        // Check if we need to run the operation again.
116        if (ec == boost::asio::error::would_block
117            || ec == boost::asio::error::try_again)
118        {
119          // We have to wait for the socket to become ready again.
120          sock_.async_wait(tcp::socket::wait_write, *this);
121          return;
122        }
123
124        if (ec || n == 0)
125        {
126          // An error occurred, or we have reached the end of the file.
127          // Either way we must exit the loop so we can call the handler.
128          break;
129        }
130
131        // Loop around to try calling sendfile again.
132      }
133    }
134
135    // Pass result back to user's handler.
136    handler_(ec, total_bytes_transferred_);
137  }
138};
139
140template &lt;typename Handler&gt;
141void async_sendfile(tcp::socket&amp; sock, int fd, Handler h)
142{
143  sendfile_op&lt;Handler&gt; op = { sock, fd, h, 0, 0 };
144  sock.async_wait(tcp::socket::wait_write, op);
145}
146</pre>
147</div>
148<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
149<td align="left"></td>
150<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M.
151      Kohlhoff<p>
152        Distributed under the Boost Software License, Version 1.0. (See accompanying
153        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
154      </p>
155</div></td>
156</tr></table>
157<hr>
158<div class="spirit-nav">
159<a accesskey="p" href="overload1.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../native_non_blocking.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../boost_asio.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="overload3.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
160</div>
161</body>
162</html>
163