uvw/index.html
2021-08-23 15:35:37 +02:00

343 lines
33 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.9.1"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>uvw: Main Page</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">uvw
&#160;<span id="projectnumber">2.10.0</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.1 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search','.html');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
initMenu('',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
</div><!-- top -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="PageDoc"><div class="header">
<div class="headertitle">
<div class="title">uvw Documentation</div> </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><a class="anchor" id="md_README"></a> <img src="https://user-images.githubusercontent.com/1812216/46069406-c977a600-c17b-11e8-9a47-9bba6f412c57.png" alt="uvw - libuv wrapper in modern C++" class="inline"/></p>
<h1><a class="anchor" id="autotoc_md0"></a>
Introduction</h1>
<p><code>uvw</code> started as a header-only, event based, tiny and easy to use wrapper for <a href="https://github.com/libuv/libuv"><code>libuv</code></a> written in modern C++.<br />
Now it's finally available also as a compilable static library.</p>
<p>The basic idea is to hide completely the <em>C-ish</em> interface of <code>libuv</code> behind a graceful C++ API. Currently, no <code>uv_*_t</code> data structure is actually exposed by the library.<br />
Note that <code>uvw</code> stays true to the API of <code>libuv</code> and it doesn't add anything to its interface. For the same reasons, users of the library must follow the same rules which are used with <code>libuv</code>.<br />
As an example, a <em>handle</em> should be initialized before any other operation and closed once it is no longer in use.</p>
<h2><a class="anchor" id="autotoc_md1"></a>
Code Example</h2>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;uvw.hpp&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;memory&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> listen(<a class="code" href="classuvw_1_1Loop.html">uvw::Loop</a> &amp;loop) {</div>
<div class="line"> std::shared_ptr&lt;uvw::TCPHandle&gt; tcp = loop.<a class="code" href="classuvw_1_1Loop.html#a7044486c90abcaa665b67e5d9f284dcd">resource</a>&lt;<a class="code" href="classuvw_1_1TCPHandle.html">uvw::TCPHandle</a>&gt;();</div>
<div class="line"> </div>
<div class="line"> tcp-&gt;<a class="code" href="classuvw_1_1Emitter.html#a79e4ed59b5bb5cdc5ee4b0dd71a38472">once</a>&lt;<a class="code" href="structuvw_1_1ListenEvent.html">uvw::ListenEvent</a>&gt;([](<span class="keyword">const</span> <a class="code" href="structuvw_1_1ListenEvent.html">uvw::ListenEvent</a> &amp;, <a class="code" href="classuvw_1_1TCPHandle.html">uvw::TCPHandle</a> &amp;srv) {</div>
<div class="line"> std::shared_ptr&lt;uvw::TCPHandle&gt; client = srv.loop().resource&lt;<a class="code" href="classuvw_1_1TCPHandle.html">uvw::TCPHandle</a>&gt;();</div>
<div class="line"> </div>
<div class="line"> client-&gt;<a class="code" href="classuvw_1_1Emitter.html#af6ba3eecb542efbef8d62ae905d8a8cd">on</a>&lt;<a class="code" href="structuvw_1_1CloseEvent.html">uvw::CloseEvent</a>&gt;([ptr = srv.shared_from_this()](<span class="keyword">const</span> <a class="code" href="structuvw_1_1CloseEvent.html">uvw::CloseEvent</a> &amp;, <a class="code" href="classuvw_1_1TCPHandle.html">uvw::TCPHandle</a> &amp;) { ptr-&gt;close(); });</div>
<div class="line"> client-&gt;on&lt;<a class="code" href="structuvw_1_1EndEvent.html">uvw::EndEvent</a>&gt;([](<span class="keyword">const</span> <a class="code" href="structuvw_1_1EndEvent.html">uvw::EndEvent</a> &amp;, <a class="code" href="classuvw_1_1TCPHandle.html">uvw::TCPHandle</a> &amp;client) { client.close(); });</div>
<div class="line"> </div>
<div class="line"> srv.accept(*client);</div>
<div class="line"> client-&gt;read();</div>
<div class="line"> });</div>
<div class="line"> </div>
<div class="line"> tcp-&gt;bind(<span class="stringliteral">&quot;127.0.0.1&quot;</span>, 4242);</div>
<div class="line"> tcp-&gt;listen();</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> conn(<a class="code" href="classuvw_1_1Loop.html">uvw::Loop</a> &amp;loop) {</div>
<div class="line"> <span class="keyword">auto</span> tcp = loop.<a class="code" href="classuvw_1_1Loop.html#a7044486c90abcaa665b67e5d9f284dcd">resource</a>&lt;<a class="code" href="classuvw_1_1TCPHandle.html">uvw::TCPHandle</a>&gt;();</div>
<div class="line"> </div>
<div class="line"> tcp-&gt;<a class="code" href="classuvw_1_1Emitter.html#af6ba3eecb542efbef8d62ae905d8a8cd">on</a>&lt;<a class="code" href="structuvw_1_1ErrorEvent.html">uvw::ErrorEvent</a>&gt;([](<span class="keyword">const</span> <a class="code" href="structuvw_1_1ErrorEvent.html">uvw::ErrorEvent</a> &amp;, <a class="code" href="classuvw_1_1TCPHandle.html">uvw::TCPHandle</a> &amp;) { <span class="comment">/* handle errors */</span> });</div>
<div class="line"> </div>
<div class="line"> tcp-&gt;once&lt;<a class="code" href="structuvw_1_1ConnectEvent.html">uvw::ConnectEvent</a>&gt;([](<span class="keyword">const</span> <a class="code" href="structuvw_1_1ConnectEvent.html">uvw::ConnectEvent</a> &amp;, <a class="code" href="classuvw_1_1TCPHandle.html">uvw::TCPHandle</a> &amp;tcp) {</div>
<div class="line"> <span class="keyword">auto</span> dataWrite = std::unique_ptr&lt;char[]&gt;(<span class="keyword">new</span> <span class="keywordtype">char</span>[2]{ <span class="charliteral">&#39;b&#39;</span>, <span class="charliteral">&#39;c&#39;</span> });</div>
<div class="line"> tcp.write(std::move(dataWrite), 2);</div>
<div class="line"> tcp.close();</div>
<div class="line"> });</div>
<div class="line"> </div>
<div class="line"> tcp-&gt;connect(std::string{<span class="stringliteral">&quot;127.0.0.1&quot;</span>}, 4242);</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main() {</div>
<div class="line"> <span class="keyword">auto</span> loop = <a class="code" href="classuvw_1_1Loop.html#a284450b3ef37c62bd7a984c73552d7b8">uvw::Loop::getDefault</a>();</div>
<div class="line"> listen(*loop);</div>
<div class="line"> conn(*loop);</div>
<div class="line"> loop-&gt;<a class="code" href="classuvw_1_1Loop.html#aa86d06ae9f91d8b48d1d5358040f2547">run</a>();</div>
<div class="line">}</div>
<div class="ttc" id="aclassuvw_1_1Emitter_html_a79e4ed59b5bb5cdc5ee4b0dd71a38472"><div class="ttname"><a href="classuvw_1_1Emitter.html#a79e4ed59b5bb5cdc5ee4b0dd71a38472">uvw::Emitter::once</a></div><div class="ttdeci">Connection&lt; E &gt; once(Listener&lt; E &gt; f)</div><div class="ttdoc">Registers a short-lived listener with the event emitter.</div><div class="ttdef"><b>Definition:</b> <a href="emitter_8h_source.html#l00245">emitter.h:245</a></div></div>
<div class="ttc" id="aclassuvw_1_1Emitter_html_af6ba3eecb542efbef8d62ae905d8a8cd"><div class="ttname"><a href="classuvw_1_1Emitter.html#af6ba3eecb542efbef8d62ae905d8a8cd">uvw::Emitter::on</a></div><div class="ttdeci">Connection&lt; E &gt; on(Listener&lt; E &gt; f)</div><div class="ttdoc">Registers a long-lived listener with the event emitter.</div><div class="ttdef"><b>Definition:</b> <a href="emitter_8h_source.html#l00225">emitter.h:225</a></div></div>
<div class="ttc" id="aclassuvw_1_1Loop_html"><div class="ttname"><a href="classuvw_1_1Loop.html">uvw::Loop</a></div><div class="ttdoc">The Loop class.</div><div class="ttdef"><b>Definition:</b> <a href="loop_8h_source.html#l00065">loop.h:65</a></div></div>
<div class="ttc" id="aclassuvw_1_1Loop_html_a284450b3ef37c62bd7a984c73552d7b8"><div class="ttname"><a href="classuvw_1_1Loop.html#a284450b3ef37c62bd7a984c73552d7b8">uvw::Loop::getDefault</a></div><div class="ttdeci">static std::shared_ptr&lt; Loop &gt; getDefault()</div><div class="ttdoc">Gets the initialized default loop.</div></div>
<div class="ttc" id="aclassuvw_1_1Loop_html_a7044486c90abcaa665b67e5d9f284dcd"><div class="ttname"><a href="classuvw_1_1Loop.html#a7044486c90abcaa665b67e5d9f284dcd">uvw::Loop::resource</a></div><div class="ttdeci">std::shared_ptr&lt; R &gt; resource(Args &amp;&amp;... args)</div><div class="ttdoc">Creates resources of any type.</div><div class="ttdef"><b>Definition:</b> <a href="loop_8h_source.html#l00166">loop.h:166</a></div></div>
<div class="ttc" id="aclassuvw_1_1Loop_html_aa86d06ae9f91d8b48d1d5358040f2547"><div class="ttname"><a href="classuvw_1_1Loop.html#aa86d06ae9f91d8b48d1d5358040f2547">uvw::Loop::run</a></div><div class="ttdeci">bool run() noexcept</div><div class="ttdoc">Runs the event loop.</div></div>
<div class="ttc" id="aclassuvw_1_1TCPHandle_html"><div class="ttname"><a href="classuvw_1_1TCPHandle.html">uvw::TCPHandle</a></div><div class="ttdoc">The TCPHandle handle.</div><div class="ttdef"><b>Definition:</b> <a href="tcp_8h_source.html#l00046">tcp.h:46</a></div></div>
<div class="ttc" id="astructuvw_1_1CloseEvent_html"><div class="ttname"><a href="structuvw_1_1CloseEvent.html">uvw::CloseEvent</a></div><div class="ttdoc">CloseEvent event.</div><div class="ttdef"><b>Definition:</b> <a href="handle_8hpp_source.html#l00021">handle.hpp:21</a></div></div>
<div class="ttc" id="astructuvw_1_1ConnectEvent_html"><div class="ttname"><a href="structuvw_1_1ConnectEvent.html">uvw::ConnectEvent</a></div><div class="ttdoc">ConnectEvent event.</div><div class="ttdef"><b>Definition:</b> <a href="stream_8h_source.html#l00024">stream.h:24</a></div></div>
<div class="ttc" id="astructuvw_1_1EndEvent_html"><div class="ttname"><a href="structuvw_1_1EndEvent.html">uvw::EndEvent</a></div><div class="ttdoc">EndEvent event.</div><div class="ttdef"><b>Definition:</b> <a href="stream_8h_source.html#l00032">stream.h:32</a></div></div>
<div class="ttc" id="astructuvw_1_1ErrorEvent_html"><div class="ttname"><a href="structuvw_1_1ErrorEvent.html">uvw::ErrorEvent</a></div><div class="ttdoc">The ErrorEvent event.</div><div class="ttdef"><b>Definition:</b> <a href="emitter_8h_source.html#l00025">emitter.h:25</a></div></div>
<div class="ttc" id="astructuvw_1_1ListenEvent_html"><div class="ttname"><a href="structuvw_1_1ListenEvent.html">uvw::ListenEvent</a></div><div class="ttdoc">ListenEvent event.</div><div class="ttdef"><b>Definition:</b> <a href="stream_8h_source.html#l00040">stream.h:40</a></div></div>
</div><!-- fragment --><h2><a class="anchor" id="autotoc_md2"></a>
Motivation</h2>
<p>The main reason for which <code>uvw</code> has been written is the fact that there does not exist a valid <code>libuv</code> wrapper in C++. That's all.</p>
<h1><a class="anchor" id="autotoc_md3"></a>
Build Instructions</h1>
<h2><a class="anchor" id="autotoc_md4"></a>
Requirements</h2>
<p>To be able to use <code>uvw</code>, users must provide the following system-wide tools:</p>
<ul>
<li>A full-featured compiler that supports at least C++17.</li>
<li><code>libuv</code> (which version depends on the tag of <code>uvw</code> in use).</li>
</ul>
<p>The requirements below are mandatory to compile the tests and to extract the documentation:</p>
<ul>
<li>CMake version 3.13 or later.</li>
<li>Doxygen version 1.8 or later.</li>
</ul>
<p>Note that <code>libuv</code> is part of the dependencies of the project and may be cloned by <code>CMake</code> in some cases (see below for further details).<br />
Because of that, users don't have to install it to run the tests or when <code>uvw</code> libraries are compiled through <code>CMake</code>.</p>
<h2><a class="anchor" id="autotoc_md5"></a>
Library</h2>
<p><code>uvw</code> is a dual-mode library. It can be used in its header-only form or as a compiled static library.<br />
The following sections describe what to do in both cases to get <code>uvw</code> up and runningin your own project.</p>
<h3><a class="anchor" id="autotoc_md6"></a>
Header-only</h3>
<p>To use <code>uvw</code> as a header-only library, all is needed is to include the <code><a class="el" href="uvw_8hpp_source.html">uvw.hpp</a></code> header or one of the other <code>uvw/*.hpp</code> files.<br />
It's a matter of adding the following line at the top of a file:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;uvw.hpp&gt;</span></div>
</div><!-- fragment --><p>Then pass the proper <code>-I</code> argument to the compiler to add the <code>src</code> directory to the include paths.<br />
Note that users are required to correctly setup the include directories and libraries search paths for <code>libuv</code> in this case.</p>
<p>When used through <code>CMake</code>, the <code>uvw::uvw</code> target is exported for convenience.</p>
<h3><a class="anchor" id="autotoc_md7"></a>
Static</h3>
<p>To use <code>uvw</code> as a compiled library, set the <code>BUILD_UVW_LIBS</code> options in cmake before including the project.<br />
This option triggers the generation of a targets named <code>uvw::uvw-static</code>. The matching version of <code>libuv</code> is also compiled and exported as <code>uv::uv-static</code> for convenience.</p>
<p>In case you don't use or don't want to use <code>CMake</code>, you can still compile all <code>.cpp</code> files and include all <code>.h</code> files to get the job done. In this case, users are required to correctly setup the include directories and libraries search paths for <code>libuv</code>.</p>
<h2><a class="anchor" id="autotoc_md8"></a>
Versioning</h2>
<p>Starting with tag <em>v1.12.0</em> of <code>libuv</code>, <code>uvw</code> follows the <a href="http://semver.org/">semantic versioning</a> scheme.<br />
The problem is that any version of <code>uvw</code> also requires to track explicitly the version of <code>libuv</code> to which it is bound.<br />
Because of that, the latter wil be appended to the version of <code>uvw</code>. As an example: </p><pre class="fragment">vU.V.W_libuv-vX.Y
</pre><p> In particular, the following applies:</p>
<ul>
<li><em>U.V.W</em> are major, minor and patch versions of <code>uvw</code>.</li>
<li><em>X.Y</em> is the version of <code>libuv</code> to which to refer (where any patch version is valid).</li>
</ul>
<p>In other terms, tags will look like this from now on: </p><pre class="fragment">v1.0.0_libuv-v1.12
</pre><p> Branch <code>master</code> of <code>uvw</code> will be a work in progress branch that follows branch <em>v1.x</em> of <code>libuv</code> (at least as long as it remains their <em>master</em> branch).<br />
</p>
<h2><a class="anchor" id="autotoc_md9"></a>
Documentation</h2>
<p>The documentation is based on <a href="http://www.stack.nl/~dimitri/doxygen/"><code>doxygen</code></a>. To build it:</p>
<ul>
<li><code>$ cd build</code></li>
<li><code>$ cmake ..</code></li>
<li><code>$ make docs</code></li>
</ul>
<p>The API reference will be created in HTML format within the directory <code>build/docs/html</code>.<br />
To navigate it with your favorite browser:</p>
<ul>
<li><code>$ cd build</code></li>
<li><code>$ your_favorite_browser docs/html/index.html</code></li>
</ul>
<p>The same version is also available <a href="https://skypjack.github.io/uvw/">online</a> for the latest release, that is the last stable tag. If you are looking for something more pleasing to the eye, consider reading the nice-looking version available on <a href="https://uvw.docsforge.com/">docsforge</a>: same documentation, much more pleasant to read.</p>
<h3><a class="anchor" id="autotoc_md10"></a>
Note</h3>
<p>The documentation is mostly inspired by the official <a href="http://docs.libuv.org/en/v1.x/">libuv API documentation</a> for obvious reasons.</p>
<h2><a class="anchor" id="autotoc_md11"></a>
Tests</h2>
<p>To compile and run the tests, <code>uvw</code> requires <code>libuv</code> and <code>googletest</code>.<br />
<code>CMake</code> will download and compile both the libraries before compiling anything else.</p>
<p>To build the tests:</p>
<ul>
<li><code>$ cd build</code></li>
<li><code>$ cmake .. -DBUILD_TESTING=ON</code></li>
<li><code>$ make</code></li>
<li><code>$ ctest -j4 -R uvw</code></li>
</ul>
<p>Omit <code>-R uvw</code> if you also want to test <code>libuv</code> and other dependencies.</p>
<h1><a class="anchor" id="autotoc_md12"></a>
Crash Course</h1>
<h2><a class="anchor" id="autotoc_md13"></a>
Vademecum</h2>
<p>There is only one rule when using <code>uvw</code>: always initialize the resources and terminate them.</p>
<p>Resources belong mainly to two families: <em>handles</em> and <em>requests</em>.<br />
Handles represent long-lived objects capable of performing certain operations while active.<br />
Requests represent (typically) short-lived operations performed either over a handle or standalone.</p>
<p>The following sections will explain in short what it means to initialize and terminate these kinds of resources.<br />
For more details, please refer to the <a href="https://skypjack.github.io/uvw/">online documentation</a>.</p>
<h2><a class="anchor" id="autotoc_md14"></a>
Handles</h2>
<p>Initialization is usually performed under the hood and can be even passed over, as far as handles are created using the <code>Loop::resource</code> member function.<br />
On the other side, handles keep themselves alive until one explicitly closes them. Because of that, memory usage will grow if users simply forget about a handle.<br />
Therefore the rule quickly becomes <em>always close your handles</em>. It's as simple as calling the <code>close</code> member function on them.</p>
<h2><a class="anchor" id="autotoc_md15"></a>
Requests</h2>
<p>Usually initializing a request object is not required. Anyway, the recommended way to create a request is still through the <code>Loop::resource</code> member function.<br />
Requests will keep themselves alive as long as they are bound to unfinished underlying activities. This means that users don't have to discard a request explicitly .<br />
Therefore the rule quickly becomes <em>feel free to make a request and forget about it</em>. It's as simple as calling a member function on them.</p>
<h2><a class="anchor" id="autotoc_md16"></a>
The Loop and the Resource</h2>
<p>The first thing to do to use <code>uvw</code> is to create a loop. In case the default one is enough, it's easy as doing this:</p>
<div class="fragment"><div class="line"><span class="keyword">auto</span> loop = <a class="code" href="classuvw_1_1Loop.html#a284450b3ef37c62bd7a984c73552d7b8">uvw::Loop::getDefault</a>();</div>
</div><!-- fragment --><p>Note that loop objects don't require being closed explicitly, even if they offer the <code>close</code> member function in case a user wants to do that.<br />
Loops can be started using the <code>run</code> member function. The two calls below are equivalent:</p>
<div class="fragment"><div class="line">loop-&gt;<a class="code" href="classuvw_1_1Loop.html#aa86d06ae9f91d8b48d1d5358040f2547">run</a>();</div>
<div class="line">loop-&gt;<a class="code" href="classuvw_1_1Loop.html#aa86d06ae9f91d8b48d1d5358040f2547">run</a>&lt;uvw::Loop::Mode::DEFAULT&gt;();</div>
</div><!-- fragment --><p>Available modes are: <code>DEFAULT</code>, <code>ONCE</code>, <code>NOWAIT</code>. Please refer to the documentation of <code>libuv</code> for further details.</p>
<p>In order to create a resource and to bind it to the given loop, just do the following:</p>
<div class="fragment"><div class="line"><span class="keyword">auto</span> tcp = loop-&gt;<a class="code" href="classuvw_1_1Loop.html#a7044486c90abcaa665b67e5d9f284dcd">resource</a>&lt;<a class="code" href="classuvw_1_1TCPHandle.html">uvw::TCPHandle</a>&gt;();</div>
</div><!-- fragment --><p>The line above will create and initialize a tcp handle, then a shared pointer to that resource will be returned.<br />
Users should check if pointers have been correctly initialized: in case of errors, they won't be.<br />
Another way to create a resource is:</p>
<div class="fragment"><div class="line"><span class="keyword">auto</span> tcp = TCPHandle::create(loop);</div>
<div class="line">tcp-&gt;init();</div>
</div><!-- fragment --><p>Pretty annoying indeed. Using a loop is the recommended approach.</p>
<p>The resources also accept arbitrary user-data that won't be touched in any case.<br />
Users can set and get them through the <code>data</code> member function as it follows:</p>
<div class="fragment"><div class="line">resource-&gt;data(std::make_shared&lt;int&gt;(42));</div>
<div class="line">std::shared_ptr&lt;void&gt; data = resource-&gt;data();</div>
</div><!-- fragment --><p>Resources expect a <code>std::shared_pointer&lt;void&gt;</code> and return it, therefore any kind of data is welcome.<br />
Users can explicitly specify a type other than <code>void</code> when calling the <code>data</code> member function:</p>
<div class="fragment"><div class="line">std::shared_ptr&lt;int&gt; data = resource-&gt;data&lt;<span class="keywordtype">int</span>&gt;();</div>
</div><!-- fragment --><p>Remember from the previous section that a handle will keep itself alive until one invokes the <code>close</code> member function on it.<br />
To know what are the handles that are still alive and bound to a given loop, there exists the <code>walk</code> member function. It returns handles with their types. Therefore, the use of <code>Overloaded</code> is recommended to be able to intercept all types of interest:</p>
<div class="fragment"><div class="line">handle.loop().walk(<a class="code" href="structuvw_1_1Overloaded.html">uvw::Overloaded</a>{</div>
<div class="line"> [](<a class="code" href="classuvw_1_1TimerHandle.html">uvw::TimerHandle</a> &amp;h){ <span class="comment">/* application code for timers here */</span> },</div>
<div class="line"> [](<span class="keyword">auto</span> &amp;&amp;){ <span class="comment">/* ignore all other types */</span> }</div>
<div class="line">});</div>
<div class="ttc" id="aclassuvw_1_1TimerHandle_html"><div class="ttname"><a href="classuvw_1_1TimerHandle.html">uvw::TimerHandle</a></div><div class="ttdoc">The TimerHandle handle.</div><div class="ttdef"><b>Definition:</b> <a href="timer_8h_source.html#l00029">timer.h:29</a></div></div>
<div class="ttc" id="astructuvw_1_1Overloaded_html"><div class="ttname"><a href="structuvw_1_1Overloaded.html">uvw::Overloaded</a></div><div class="ttdoc">Helper type for visitors.</div><div class="ttdef"><b>Definition:</b> <a href="util_8h_source.html#l00829">util.h:829</a></div></div>
</div><!-- fragment --><p>This function can also be used for a completely generic approach. For example, all the pending handles can be closed easily as it follows:</p>
<div class="fragment"><div class="line">loop-&gt;<a class="code" href="classuvw_1_1Loop.html#a87d300f049faa82fe1ec92448499431e">walk</a>([](<span class="keyword">auto</span> &amp;&amp;h){ h.close(); });</div>
<div class="ttc" id="aclassuvw_1_1Loop_html_a87d300f049faa82fe1ec92448499431e"><div class="ttname"><a href="classuvw_1_1Loop.html#a87d300f049faa82fe1ec92448499431e">uvw::Loop::walk</a></div><div class="ttdeci">void walk(Func callback)</div><div class="ttdoc">Walks the list of handles.</div><div class="ttdef"><b>Definition:</b> <a href="loop_8h_source.html#l00276">loop.h:276</a></div></div>
</div><!-- fragment --><p>No need to keep track of them.</p>
<h2><a class="anchor" id="autotoc_md17"></a>
The event-based approach</h2>
<p><code>uvw</code> offers an event-based approach, so resources are small event emitters to which listeners can be attached.<br />
Attaching a listener to a resource is the recommended way to be notified about changes.<br />
Listeners must be callable objects of type <code>void(EventType &amp;, ResourceType &amp;)</code>, where:</p>
<ul>
<li><code>EventType</code> is the type of the event for which they have been designed.</li>
<li><code>ResourceType</code> is the type of the resource that has originated the event.</li>
</ul>
<p>It means that the following function types are all valid:</p>
<ul>
<li><code>void(EventType &amp;, ResourceType &amp;)</code></li>
<li><code>void(const EventType &amp;, ResourceType &amp;)</code></li>
<li><code>void(EventType &amp;, const ResourceType &amp;)</code></li>
<li><code>void(const EventType &amp;, const ResourceType &amp;)</code></li>
</ul>
<p>Please note that there is no need to keep around references to the resources: they will pass themselves as an argument whenever an event is published.</p>
<p>There exist two methods to attach a listener to a resource:</p>
<ul>
<li><code>resource.once&lt;EventType&gt;(listener)</code>: the listener will be automatically removed after the first event of the given type.</li>
<li><code>resource.on&lt;EventType&gt;(listener)</code>: to be used for long-running listeners.</li>
</ul>
<p>Both of them return an object of type <code>ResourceType::Connection</code> (as an example, <code>TCPHandle::Connection</code>).<br />
A connection object can be used later as an argument to the <code>erase</code> member function of the resource to remove the listener.<br />
There exists also the <code>clear</code> member function to drop all the listeners at once. Note that <code>clear</code> should only be invoked on non-active handles. The handles exploit the same event mechanism made available to users to satisfy pending requests. Invoking <code>clear</code> on an active handle, for example with requests still in progress, risks leading to memory leaks or unexpected behavior.</p>
<p>Almost all the resources emit <code>ErrorEvent</code> in case of errors.<br />
All the other events are specific for the given resource and documented in the API reference.</p>
<p>The code below shows how to create a simple tcp server using <code>uvw</code>:</p>
<div class="fragment"><div class="line"><span class="keyword">auto</span> loop = <a class="code" href="classuvw_1_1Loop.html#a284450b3ef37c62bd7a984c73552d7b8">uvw::Loop::getDefault</a>();</div>
<div class="line"><span class="keyword">auto</span> tcp = loop-&gt;<a class="code" href="classuvw_1_1Loop.html#a7044486c90abcaa665b67e5d9f284dcd">resource</a>&lt;<a class="code" href="classuvw_1_1TCPHandle.html">uvw::TCPHandle</a>&gt;();</div>
<div class="line"> </div>
<div class="line">tcp-&gt;<a class="code" href="classuvw_1_1Emitter.html#af6ba3eecb542efbef8d62ae905d8a8cd">on</a>&lt;<a class="code" href="structuvw_1_1ErrorEvent.html">uvw::ErrorEvent</a>&gt;([](<span class="keyword">const</span> <a class="code" href="structuvw_1_1ErrorEvent.html">uvw::ErrorEvent</a> &amp;, <a class="code" href="classuvw_1_1TCPHandle.html">uvw::TCPHandle</a> &amp;) { <span class="comment">/* something went wrong */</span> });</div>
<div class="line"> </div>
<div class="line">tcp-&gt;on&lt;<a class="code" href="structuvw_1_1ListenEvent.html">uvw::ListenEvent</a>&gt;([](<span class="keyword">const</span> <a class="code" href="structuvw_1_1ListenEvent.html">uvw::ListenEvent</a> &amp;, <a class="code" href="classuvw_1_1TCPHandle.html">uvw::TCPHandle</a> &amp;srv) {</div>
<div class="line"> std::shared_ptr&lt;uvw::TCPHandle&gt; client = srv.loop().resource&lt;<a class="code" href="classuvw_1_1TCPHandle.html">uvw::TCPHandle</a>&gt;();</div>
<div class="line"> client-&gt;<a class="code" href="classuvw_1_1Emitter.html#a79e4ed59b5bb5cdc5ee4b0dd71a38472">once</a>&lt;<a class="code" href="structuvw_1_1EndEvent.html">uvw::EndEvent</a>&gt;([](<span class="keyword">const</span> <a class="code" href="structuvw_1_1EndEvent.html">uvw::EndEvent</a> &amp;, <a class="code" href="classuvw_1_1TCPHandle.html">uvw::TCPHandle</a> &amp;client) { client.close(); });</div>
<div class="line"> client-&gt;on&lt;<a class="code" href="structuvw_1_1DataEvent.html">uvw::DataEvent</a>&gt;([](<span class="keyword">const</span> <a class="code" href="structuvw_1_1DataEvent.html">uvw::DataEvent</a> &amp;, <a class="code" href="classuvw_1_1TCPHandle.html">uvw::TCPHandle</a> &amp;) { <span class="comment">/* data received */</span> });</div>
<div class="line"> srv.accept(*client);</div>
<div class="line"> client-&gt;read();</div>
<div class="line">});</div>
<div class="line"> </div>
<div class="line">tcp-&gt;bind(<span class="stringliteral">&quot;127.0.0.1&quot;</span>, 4242);</div>
<div class="line">tcp-&gt;listen();</div>
<div class="ttc" id="astructuvw_1_1DataEvent_html"><div class="ttname"><a href="structuvw_1_1DataEvent.html">uvw::DataEvent</a></div><div class="ttdoc">DataEvent event.</div><div class="ttdef"><b>Definition:</b> <a href="stream_8h_source.html#l00064">stream.h:64</a></div></div>
</div><!-- fragment --><p>Note also that <code><a class="el" href="classuvw_1_1TCPHandle.html" title="The TCPHandle handle.">uvw::TCPHandle</a></code> already supports <em>IPv6</em> out-of-the-box. The statement above is equivalent to <code>tcp-&gt;bind&lt;<a class="el" href="structuvw_1_1IPv4.html" title="The IPv4 tag.">uvw::IPv4</a>&gt;("127.0.0.1", 4242)</code>.<br />
It's sufficient to explicitly specify <code><a class="el" href="structuvw_1_1IPv6.html" title="The IPv6 tag.">uvw::IPv6</a></code> as the underlying protocol to use it.</p>
<p>The API reference is the recommended documentation for further details about resources and their methods.</p>
<h2><a class="anchor" id="autotoc_md18"></a>
Going raw</h2>
<p>In case users need to use functionalities not wrapped yet by <code>uvw</code> or if they want to get the underlying data structures as defined by <code>libuv</code> for some other reasons, almost all the classes in <code>uvw</code> give direct access to them.<br />
Please, note that this functions should not be used directly unless users know exactly what they are doing and what are the risks. Going raw is dangerous, mainly because the lifetime management of a loop, a handle or a request is completely controlled by the library and working around it could quickly break things.</p>
<p>That being said, <em>going raw</em> is a matter of using the <code>raw</code> member functions:</p>
<div class="fragment"><div class="line"><span class="keyword">auto</span> loop = <a class="code" href="classuvw_1_1Loop.html#a284450b3ef37c62bd7a984c73552d7b8">uvw::Loop::getDefault</a>();</div>
<div class="line"><span class="keyword">auto</span> tcp = loop-&gt;<a class="code" href="classuvw_1_1Loop.html#a7044486c90abcaa665b67e5d9f284dcd">resource</a>&lt;<a class="code" href="classuvw_1_1TCPHandle.html">uvw::TCPHandle</a>&gt;();</div>
<div class="line"> </div>
<div class="line">uv_loop_t *raw = loop-&gt;<a class="code" href="classuvw_1_1Loop.html#ac33b64667ba24e94456ed4ec172372c6">raw</a>();</div>
<div class="line">uv_tcp_t *handle = tcp-&gt;raw();</div>
<div class="ttc" id="aclassuvw_1_1Loop_html_ac33b64667ba24e94456ed4ec172372c6"><div class="ttname"><a href="classuvw_1_1Loop.html#ac33b64667ba24e94456ed4ec172372c6">uvw::Loop::raw</a></div><div class="ttdeci">const uv_loop_t * raw() const noexcept</div><div class="ttdoc">Gets the underlying raw data structure.</div></div>
</div><!-- fragment --><p>Go the raw way at your own risk, but do not expect any support in case of bugs.</p>
<h1><a class="anchor" id="autotoc_md19"></a>
Contributors</h1>
<p>If you want to contribute, please send patches as pull requests against the branch master.<br />
Check the <a href="https://github.com/skypjack/uvw/blob/master/AUTHORS">contributors list</a> to see who has partecipated so far.</p>
<h1><a class="anchor" id="autotoc_md20"></a>
License</h1>
<p>Code and documentation Copyright (c) 2016-2021 Michele Caini.<br />
Logo Copyright (c) 2018-2021 Richard Caseres.</p>
<p>Code released under <a href="https://github.com/skypjack/uvw/blob/master/LICENSE">the MIT license</a>. Documentation released under <a href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a>.<br />
Logo released under <a href="https://creativecommons.org/licenses/by-sa/4.0/">CC BY-SA 4.0</a>. </p>
</div></div><!-- PageDoc -->
</div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by&#160;<a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.1
</small></address>
</body>
</html>