summaryrefslogtreecommitdiff
path: root/ref/api/aio.html
diff options
context:
space:
mode:
authorgdamore <gdamore@users.noreply.github.com>2024-10-27 18:55:51 +0000
committergdamore <gdamore@users.noreply.github.com>2024-10-27 18:55:51 +0000
commit45ac4fa56b6e5c31a28fd08eaad14a09bf3934f6 (patch)
treee0ed7b8907d4f6ac5651fb6c9d38556ec46b231a /ref/api/aio.html
parentb83fdb3ff1327e4376acd0ca24479b16b24f7e0b (diff)
downloadnng-45ac4fa56b6e5c31a28fd08eaad14a09bf3934f6.tar.gz
nng-45ac4fa56b6e5c31a28fd08eaad14a09bf3934f6.tar.bz2
nng-45ac4fa56b6e5c31a28fd08eaad14a09bf3934f6.zip
deploy: ffeb31c64ea72c4eb287f75b641ca2a707df90b0
Diffstat (limited to 'ref/api/aio.html')
-rw-r--r--ref/api/aio.html558
1 files changed, 558 insertions, 0 deletions
diff --git a/ref/api/aio.html b/ref/api/aio.html
new file mode 100644
index 00000000..fa90d946
--- /dev/null
+++ b/ref/api/aio.html
@@ -0,0 +1,558 @@
+<!DOCTYPE HTML>
+<html lang="en" class="light" dir="ltr">
+ <head>
+ <!-- Book generated using mdBook -->
+ <meta charset="UTF-8">
+ <title>Asynchronous I/O - NNG Reference Manual (DRAFT)</title>
+
+
+ <!-- Custom HTML head -->
+
+ <meta name="description" content="">
+ <meta name="viewport" content="width=device-width, initial-scale=1">
+ <meta name="theme-color" content="#ffffff">
+
+ <link rel="icon" href="../favicon.svg">
+ <link rel="shortcut icon" href="../favicon.png">
+ <link rel="stylesheet" href="../css/variables.css">
+ <link rel="stylesheet" href="../css/general.css">
+ <link rel="stylesheet" href="../css/chrome.css">
+ <link rel="stylesheet" href="../css/print.css" media="print">
+
+ <!-- Fonts -->
+ <link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
+ <link rel="stylesheet" href="../fonts/fonts.css">
+
+ <!-- Highlight.js Stylesheets -->
+ <link rel="stylesheet" href="../highlight.css">
+ <link rel="stylesheet" href="../tomorrow-night.css">
+ <link rel="stylesheet" href="../ayu-highlight.css">
+
+ <!-- Custom theme stylesheets -->
+ <link rel="stylesheet" href="../theme/pagetoc.css">
+
+ </head>
+ <body class="sidebar-visible no-js">
+ <div id="body-container">
+ <!-- Provide site root to javascript -->
+ <script>
+ var path_to_root = "../";
+ var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
+ </script>
+
+ <!-- Work around some values being stored in localStorage wrapped in quotes -->
+ <script>
+ try {
+ var theme = localStorage.getItem('mdbook-theme');
+ var sidebar = localStorage.getItem('mdbook-sidebar');
+
+ if (theme.startsWith('"') && theme.endsWith('"')) {
+ localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
+ }
+
+ if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
+ localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
+ }
+ } catch (e) { }
+ </script>
+
+ <!-- Set the theme before any content is loaded, prevents flash -->
+ <script>
+ var theme;
+ try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
+ if (theme === null || theme === undefined) { theme = default_theme; }
+ var html = document.querySelector('html');
+ html.classList.remove('light')
+ html.classList.add(theme);
+ var body = document.querySelector('body');
+ body.classList.remove('no-js')
+ body.classList.add('js');
+ </script>
+
+ <input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
+
+ <!-- Hide / unhide sidebar before it is displayed -->
+ <script>
+ var body = document.querySelector('body');
+ var sidebar = null;
+ var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
+ if (document.body.clientWidth >= 1080) {
+ try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
+ sidebar = sidebar || 'visible';
+ } else {
+ sidebar = 'hidden';
+ }
+ sidebar_toggle.checked = sidebar === 'visible';
+ body.classList.remove('sidebar-visible');
+ body.classList.add("sidebar-" + sidebar);
+ </script>
+
+ <nav id="sidebar" class="sidebar" aria-label="Table of contents">
+ <div class="sidebar-scrollbox">
+ <ol class="chapter"><li class="chapter-item expanded affix "><a href="../copyright.html">Copyright</a></li><li class="chapter-item expanded affix "><a href="../dedication.html">Dedication</a></li><li class="chapter-item expanded affix "><a href="../preface.html">Preface</a></li><li class="chapter-item expanded "><a href="../api/index.html"><strong aria-hidden="true">1.</strong> API Reference</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../api/msg.html"><strong aria-hidden="true">1.1.</strong> Messages</a></li><li class="chapter-item expanded "><a href="../api/memory.html"><strong aria-hidden="true">1.2.</strong> Memory</a></li><li class="chapter-item expanded "><a href="../api/time.html"><strong aria-hidden="true">1.3.</strong> Time</a></li><li class="chapter-item expanded "><a href="../api/url.html"><strong aria-hidden="true">1.4.</strong> URLs</a></li><li class="chapter-item expanded "><a href="../api/aio.html" class="active"><strong aria-hidden="true">1.5.</strong> Asynchronous I/O</a></li><li class="chapter-item expanded "><a href="../api/synch.html"><strong aria-hidden="true">1.6.</strong> Synchronization</a></li><li class="chapter-item expanded "><a href="../api/thread.html"><strong aria-hidden="true">1.7.</strong> Threads</a></li><li class="chapter-item expanded "><a href="../api/logging.html"><strong aria-hidden="true">1.8.</strong> Logging</a></li><li class="chapter-item expanded "><a href="../api/stats.html"><strong aria-hidden="true">1.9.</strong> Statistics</a></li><li class="chapter-item expanded "><a href="../api/errors.html"><strong aria-hidden="true">1.10.</strong> Errors</a></li><li class="chapter-item expanded "><a href="../api/misc.html"><strong aria-hidden="true">1.11.</strong> Miscellaneous</a></li><li class="chapter-item expanded "><a href="../api/id_map.html"><strong aria-hidden="true">1.12.</strong> ID Map</a></li><li class="chapter-item expanded "><a href="../api/cmd_opts.html"><strong aria-hidden="true">1.13.</strong> Command Options</a></li></ol></li><li class="chapter-item expanded "><a href="../proto/index.html"><strong aria-hidden="true">2.</strong> Protocols</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../proto/bus.html"><strong aria-hidden="true">2.1.</strong> BUS Protocol</a></li><li class="chapter-item expanded "><a href="../proto/pair.html"><strong aria-hidden="true">2.2.</strong> PAIR Protocol</a></li><li class="chapter-item expanded "><a href="../proto/pub.html"><strong aria-hidden="true">2.3.</strong> PUB Protocol</a></li><li class="chapter-item expanded "><a href="../proto/pull.html"><strong aria-hidden="true">2.4.</strong> PULL Protocol</a></li><li class="chapter-item expanded "><a href="../proto/push.html"><strong aria-hidden="true">2.5.</strong> PUSH Protocol</a></li><li class="chapter-item expanded "><a href="../proto/rep.html"><strong aria-hidden="true">2.6.</strong> REP Protocol</a></li><li class="chapter-item expanded "><a href="../proto/req.html"><strong aria-hidden="true">2.7.</strong> REQ Protocol</a></li><li class="chapter-item expanded "><a href="../proto/respondent.html"><strong aria-hidden="true">2.8.</strong> RESPONDENT Protocol</a></li><li class="chapter-item expanded "><a href="../proto/sub.html"><strong aria-hidden="true">2.9.</strong> SUB Protocol</a></li><li class="chapter-item expanded "><a href="../proto/surveyor.html"><strong aria-hidden="true">2.10.</strong> SURVEYOR Protocol</a></li></ol></li><li class="chapter-item expanded "><a href="../tran/index.html"><strong aria-hidden="true">3.</strong> Transports</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../tran/inproc.html"><strong aria-hidden="true">3.1.</strong> Intra-Process Transport</a></li><li class="chapter-item expanded "><a href="../tran/ipc.html"><strong aria-hidden="true">3.2.</strong> Inter-Process Transport</a></li><li class="chapter-item expanded "><a href="../tran/socket.html"><strong aria-hidden="true">3.3.</strong> BSD Socket (Experimental)</a></li><li class="chapter-item expanded "><a href="../tran/udp.html"><strong aria-hidden="true">3.4.</strong> UDP Transport (Experimental)</a></li></ol></li><li class="chapter-item expanded "><a href="../migrate/index.html"><strong aria-hidden="true">4.</strong> Migration Guides</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../migrate/nng1.html"><strong aria-hidden="true">4.1.</strong> Migrating from NNG 1.x</a></li><li class="chapter-item expanded "><a href="../migrate/nanomsg.html"><strong aria-hidden="true">4.2.</strong> Migrating from libnanomsg</a></li></ol></li><li class="chapter-item expanded "><a href="../indexing.html">Index</a></li></ol>
+ </div>
+ <div id="sidebar-resize-handle" class="sidebar-resize-handle">
+ <div class="sidebar-resize-indicator"></div>
+ </div>
+ </nav>
+
+ <!-- Track and set sidebar scroll position -->
+ <script>
+ var sidebarScrollbox = document.querySelector('#sidebar .sidebar-scrollbox');
+ sidebarScrollbox.addEventListener('click', function(e) {
+ if (e.target.tagName === 'A') {
+ sessionStorage.setItem('sidebar-scroll', sidebarScrollbox.scrollTop);
+ }
+ }, { passive: true });
+ var sidebarScrollTop = sessionStorage.getItem('sidebar-scroll');
+ sessionStorage.removeItem('sidebar-scroll');
+ if (sidebarScrollTop) {
+ // preserve sidebar scroll position when navigating via links within sidebar
+ sidebarScrollbox.scrollTop = sidebarScrollTop;
+ } else {
+ // scroll sidebar to current active section when navigating via "next/previous chapter" buttons
+ var activeSection = document.querySelector('#sidebar .active');
+ if (activeSection) {
+ activeSection.scrollIntoView({ block: 'center' });
+ }
+ }
+ </script>
+
+ <div id="page-wrapper" class="page-wrapper">
+
+ <div class="page">
+ <div id="menu-bar-hover-placeholder"></div>
+ <div id="menu-bar" class="menu-bar sticky">
+ <div class="left-buttons">
+ <label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
+ <i class="fa fa-bars"></i>
+ </label>
+ <button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
+ <i class="fa fa-paint-brush"></i>
+ </button>
+ <ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
+ <li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
+ <li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
+ <li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
+ <li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
+ <li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
+ </ul>
+ <button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
+ <i class="fa fa-search"></i>
+ </button>
+ </div>
+
+ <h1 class="menu-title">NNG Reference Manual (DRAFT)</h1>
+
+ <div class="right-buttons">
+ <a href="../print.html" title="Print this book" aria-label="Print this book">
+ <i id="print-button" class="fa fa-print"></i>
+ </a>
+
+ </div>
+ </div>
+
+ <div id="search-wrapper" class="hidden">
+ <form id="searchbar-outer" class="searchbar-outer">
+ <input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
+ </form>
+ <div id="searchresults-outer" class="searchresults-outer hidden">
+ <div id="searchresults-header" class="searchresults-header"></div>
+ <ul id="searchresults">
+ </ul>
+ </div>
+ </div>
+
+ <!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
+ <script>
+ document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
+ document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
+ Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
+ link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
+ });
+ </script>
+
+ <div id="content" class="content">
+ <main>
+ <style>
+.mdbook-alerts {
+ padding: 8px 16px;
+ margin-bottom: 16px;
+ border-left: 0.25em solid var(--mdbook-alerts-color);
+}
+
+.mdbook-alerts > *:first-child {
+ margin-top: 0;
+}
+
+.mdbook-alerts > *:last-child {
+ margin-bottom: 0;
+}
+
+.mdbook-alerts-title {
+ display: flex;
+ font-weight: 600;
+ align-items: center;
+ line-height: 1;
+ color: var(--mdbook-alerts-color);
+ text-transform: capitalize;
+}
+
+.mdbook-alerts-icon {
+ display: inline-block;
+ width: 1em;
+ height: 1em;
+ margin-right: 0.2em;
+ background-color: currentColor;
+ -webkit-mask: no-repeat center / 100%;
+ mask: no-repeat center / 100%;
+ -webkit-mask-image: var(--mdbook-alerts-icon);
+ mask-image: var(--mdbook-alerts-icon);
+}
+
+.mdbook-alerts-note {
+ --mdbook-alerts-color: rgb(9, 105, 218);
+ /* https://icon-sets.iconify.design/material-symbols/info-outline-rounded/ */
+ --mdbook-alerts-icon: url('data:image/svg+xml,%3Csvg xmlns="http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg" width="24" height="24" viewBox="0 0 24 24"%3E%3Cpath fill="currentColor" d="M12 17q.425 0 .713-.288T13 16v-4q0-.425-.288-.712T12 11q-.425 0-.712.288T11 12v4q0 .425.288.713T12 17m0-8q.425 0 .713-.288T13 8q0-.425-.288-.712T12 7q-.425 0-.712.288T11 8q0 .425.288.713T12 9m0 13q-2.075 0-3.9-.788t-3.175-2.137q-1.35-1.35-2.137-3.175T2 12q0-2.075.788-3.9t2.137-3.175q1.35-1.35 3.175-2.137T12 2q2.075 0 3.9.788t3.175 2.137q1.35 1.35 2.138 3.175T22 12q0 2.075-.788 3.9t-2.137 3.175q-1.35 1.35-3.175 2.138T12 22m0-2q3.35 0 5.675-2.325T20 12q0-3.35-2.325-5.675T12 4Q8.65 4 6.325 6.325T4 12q0 3.35 2.325 5.675T12 20m0-8"%2F%3E%3C%2Fsvg%3E');
+}
+
+.mdbook-alerts-tip {
+ --mdbook-alerts-color: rgb(26, 127, 55);
+ /* https://icon-sets.iconify.design/material-symbols/lightbulb-outline-rounded/ */
+ --mdbook-alerts-icon: url('data:image/svg+xml,%3Csvg xmlns="http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg" width="24" height="24" viewBox="0 0 24 24"%3E%3Cpath fill="currentColor" d="M12 22q-.825 0-1.412-.587T10 20h4q0 .825-.587 1.413T12 22m-3-3q-.425 0-.712-.288T8 18q0-.425.288-.712T9 17h6q.425 0 .713.288T16 18q0 .425-.288.713T15 19zm-.75-3q-1.725-1.025-2.738-2.75T4.5 9.5q0-3.125 2.188-5.312T12 2q3.125 0 5.313 2.188T19.5 9.5q0 2.025-1.012 3.75T15.75 16zm.6-2h6.3q1.125-.8 1.738-1.975T17.5 9.5q0-2.3-1.6-3.9T12 4Q9.7 4 8.1 5.6T6.5 9.5q0 1.35.613 2.525T8.85 14M12 14"%2F%3E%3C%2Fsvg%3E');
+}
+
+.mdbook-alerts-important {
+ --mdbook-alerts-color: rgb(130, 80, 223);
+ /* https://icon-sets.iconify.design/material-symbols/chat-info-outline-rounded/ */
+ --mdbook-alerts-icon: url('data:image/svg+xml,%3Csvg xmlns="http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg" width="24" height="24" viewBox="0 0 24 24"%3E%3Cpath fill="currentColor" d="M12 7q.425 0 .713-.288T13 6q0-.425-.288-.712T12 5q-.425 0-.712.288T11 6q0 .425.288.713T12 7m0 8q.425 0 .713-.288T13 14v-4q0-.425-.288-.712T12 9q-.425 0-.712.288T11 10v4q0 .425.288.713T12 15m-6 3l-2.3 2.3q-.475.475-1.088.213T2 19.575V4q0-.825.588-1.412T4 2h16q.825 0 1.413.588T22 4v12q0 .825-.587 1.413T20 18zm-.85-2H20V4H4v13.125zM4 16V4z"%2F%3E%3C%2Fsvg%3E');
+}
+
+.mdbook-alerts-warning {
+ --mdbook-alerts-color: rgb(154, 103, 0);
+ /* https://icon-sets.iconify.design/material-symbols/warning-outline-rounded/ */
+ --mdbook-alerts-icon: url('data:image/svg+xml,%3Csvg xmlns="http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg" width="24" height="24" viewBox="0 0 24 24"%3E%3Cpath fill="currentColor" d="M2.725 21q-.275 0-.5-.137t-.35-.363q-.125-.225-.137-.488t.137-.512l9.25-16q.15-.25.388-.375T12 3q.25 0 .488.125t.387.375l9.25 16q.15.25.138.513t-.138.487q-.125.225-.35.363t-.5.137zm1.725-2h15.1L12 6zM12 18q.425 0 .713-.288T13 17q0-.425-.288-.712T12 16q-.425 0-.712.288T11 17q0 .425.288.713T12 18m0-3q.425 0 .713-.288T13 14v-3q0-.425-.288-.712T12 10q-.425 0-.712.288T11 11v3q0 .425.288.713T12 15m0-2.5"%2F%3E%3C%2Fsvg%3E');
+}
+
+.mdbook-alerts-caution {
+ --mdbook-alerts-color: rgb(207, 34, 46);
+ /* https://icon-sets.iconify.design/material-symbols/brightness-alert-outline-rounded/ */
+ --mdbook-alerts-icon: url('data:image/svg+xml,%3Csvg xmlns="http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg" width="24" height="24" viewBox="0 0 24 24"%3E%3Cpath fill="currentColor" d="M12 17q.425 0 .713-.288T13 16q0-.425-.288-.712T12 15q-.425 0-.712.288T11 16q0 .425.288.713T12 17m0-4q.425 0 .713-.288T13 12V8q0-.425-.288-.712T12 7q-.425 0-.712.288T11 8v4q0 .425.288.713T12 13m-3.35 7H6q-.825 0-1.412-.587T4 18v-2.65L2.075 13.4q-.275-.3-.425-.662T1.5 12q0-.375.15-.737t.425-.663L4 8.65V6q0-.825.588-1.412T6 4h2.65l1.95-1.925q.3-.275.663-.425T12 1.5q.375 0 .738.15t.662.425L15.35 4H18q.825 0 1.413.588T20 6v2.65l1.925 1.95q.275.3.425.663t.15.737q0 .375-.15.738t-.425.662L20 15.35V18q0 .825-.587 1.413T18 20h-2.65l-1.95 1.925q-.3.275-.662.425T12 22.5q-.375 0-.737-.15t-.663-.425zm.85-2l2.5 2.5l2.5-2.5H18v-3.5l2.5-2.5L18 9.5V6h-3.5L12 3.5L9.5 6H6v3.5L3.5 12L6 14.5V18zm2.5-6"%2F%3E%3C%2Fsvg%3E');
+}
+
+</style>
+<h1 id="asynchronous-operations"><a class="header" href="#asynchronous-operations">Asynchronous Operations</a></h1>
+<p>In order to obtain significant scalability, with low-latency, and minimal
+overheads, <em>NNG</em> supports performing operations asynchronously.</p>
+<p>One way that applications can perform work asynchronously and concurrently
+is by using <a href="/api/thread.html">threads</a>, but threads carry significant resource overheads
+and frequently there are limits on the number that can easily be created.</p>
+<p>Additionally, with most network applications, the flow of execution will spend
+the bulk of its time waiting for traffic from a peer.</p>
+<p>For these kinds of applications, it is far more efficient to use asynchronous operations
+using the mechanisms described in this chapter.</p>
+<div class="mdbook-alerts mdbook-alerts-tip">
+<p class="mdbook-alerts-title">
+ <span class="mdbook-alerts-icon"></span>
+ tip
+</p>
+<p>To get the highest performance with the least overhead, applications should use
+asynchronous operations described in this chapter whenever possible.</p>
+</div>
+<h2 id="asynchronous-io-handle"><a class="header" href="#asynchronous-io-handle">Asynchronous I/O Handle</a></h2>
+<pre><code class="language-c">typedef struct nng_aio nng_aio;
+</code></pre>
+<p>An <a name="a001"></a><code>nng_aio</code><a name="a002"></a> is an opaque structure used in conjunction with
+<a name="a003"></a>asynchronous I/O operations.
+Every asynchronous operation uses one of these structures, each of which
+can only be used with a single operation at a time.</p>
+<p>Asynchronous operations are performed without blocking calling application threads.
+Instead the application registers a callback function to be executed
+when the operation is complete (whether successfully or not).
+This callback will be executed exactly once.</p>
+<p>The asynchronous I/O framework also supports <a href="/api/aio.html#cancellation">cancellation</a> of
+operations that are already in progress as well setting a maximum
+<a href="/TODO.html">timeout</a> for them to complete.</p>
+<p>It is also possible to initiate an asynchronous operation, and <a href="/api/api.html#wait-for-completion">wait</a> for it to
+complete, creating a synchronous flow from an asynchronous one.</p>
+<h2 id="create-handle"><a class="header" href="#create-handle">Create Handle</a></h2>
+<pre><code class="language-c">int nng_aio_alloc(nng_aio **aiop, void (*callb)(void *), void *arg);
+</code></pre>
+<p>The <a name="a004"></a><code>nng_aio_alloc</code> function creates an <a href="/TODO.html"><code>nng_aio</code></a> object, with the
+<a name="a005"></a>callback <em>callb</em> taking the argument <em>arg</em>, and returns it in <em>aiop</em>.</p>
+<p>If this succeeds, the function returns zero. Otherwise it may return <a href="/api/errors.html#NNG_ENOMEM"><code>NNG_ENOMEM</code></a>.</p>
+<div class="mdbook-alerts mdbook-alerts-tip">
+<p class="mdbook-alerts-title">
+ <span class="mdbook-alerts-icon"></span>
+ tip
+</p>
+<p>The <em>arg</em> should normally be a structure that contains a pointer to the <em>aiop</em>,
+or from which it can be located. This allows <em>callb</em> to check the handle for
+success using <a href="/api/api.html#result-of-operation"><code>nng_aio_result</code></a>, as well access other properties of <em>aiop</em>.</p>
+</div>
+<div class="mdbook-alerts mdbook-alerts-tip">
+<p class="mdbook-alerts-title">
+ <span class="mdbook-alerts-icon"></span>
+ tip
+</p>
+<p>The <em>callb</em> may be executed on another <a href="/api/thread.html">thread</a>, so it may be necessary to use
+<a href="/api/synch.html">synchronization</a> methods in <em>callb</em> to avoid data races.</p>
+</div>
+<h2 id="destroy-handle"><a class="header" href="#destroy-handle">Destroy Handle</a></h2>
+<pre><code class="language-c">void nng_aio_free(nng_aio *aio);
+void nng_aio_reap(nng_aio *aio);
+</code></pre>
+<p>The <a name="a006"></a><code>nng_aio_free</code> handle destroys the handle <em>aio</em>, waiting for any operations
+and associated callbacks to complete before doing so.</p>
+<p>The <a name="a007"></a><code>nng_aio_reap</code> handle destroys the handle <em>aio</em> asynchronously, using a <em>reaper</em>
+<a href="/api/thread.html">thread</a> to do so. It does not wait for the object to be destroyed. Thus this function
+is safe to call from <em>aio</em>’s own callback.</p>
+<div class="mdbook-alerts mdbook-alerts-note">
+<p class="mdbook-alerts-title">
+ <span class="mdbook-alerts-icon"></span>
+ note
+</p>
+<p>The <code>nng_aio_free</code> function must never be called from an <em>aio</em> callback.
+Use <code>nng_aio_reap</code> instead if an object must be destroyed from a callback.</p>
+</div>
+<h2 id="cancellation"><a class="header" href="#cancellation">Cancellation</a></h2>
+<pre><code class="language-c">void nng_aio_abort(nng_aio *aio, int err);
+void nng_aio_cancel(nng_aio *aio);
+void nng_aio_stop(nng_aio *aio);
+</code></pre>
+<p>These functions are used to stop a previously submitted asynchronous
+I/O operation. The operation may be canceled, or may continue to
+completion. If no operation is in progress (perhaps because it has
+already completed), then these operations have no effect.
+If the operation is successfully canceled or aborted, then the callback
+will still be called.</p>
+<p>The <a name="a008"></a><code>nng_aio_abort</code> function aborts the operation associated with <em>aio</em>
+and returns immediately without waiting. If cancellation was successful,
+then <a href="/api/api.html#result-of-operation"><code>nng_aio_result</code></a> will return <em>err</em>.</p>
+<p>The <a name="a009"></a><code>nng_aio_cancel</code> function acts like <code>nng_aio_abort</code>, but uses the error code
+<a href="/api/errors.html#NNG_ECANCELED"><code>NNG_ECANCELED</code></a><a name="a010"></a>.</p>
+<p>The <a name="a011"></a><code>nng_aio_stop</code> function aborts the <em>aio</em> operation with <a href="/api/errors.html#NNG_ECANCELED"><code>NNG_ECANCELED</code></a>,
+and then waits the operation and any associated callback to complete.
+This function also marks <em>aio</em> itself permanently stopped, so that any
+new operations scheduled by I/O providers using <a href="/TODO.html"><code>nng_aio_begin</code></a>
+return false. Thus this function should be used to teardown operations.</p>
+<div class="mdbook-alerts mdbook-alerts-tip">
+<p class="mdbook-alerts-title">
+ <span class="mdbook-alerts-icon"></span>
+ tip
+</p>
+<p>When multiple asynchronous I/O handles are in use and need to be
+deallocated, it is safest to stop all of them using <code>nng_aio_stop</code>,
+before deallocating any of them with <a href="/api/aio.html#destroy-handle"><code>nng_aio_free</code></a>,
+particularly if the callbacks might attempt to reschedule further operations.</p>
+</div>
+<h2 id="set-timeout"><a class="header" href="#set-timeout">Set Timeout</a></h2>
+<pre><code class="language-c">void nng_aio_set_timeout(nng_aio *aio, nng_duration timeout);
+void nng_aio_set_expire(nng_aio *aio, nng_time expiration);
+</code></pre>
+<p>The <code>nng_aio_set_timeout</code> function sets a <a name="a012"></a><em>timeout</em>
+for the asynchronous operation associated with <em>aio</em>.
+This causes a timer to be started when the operation is actually started.
+If the timer expires before the operation is completed, then it is
+<a href="/api/aio.html#cancellation">aborted</a> with an error of <code>NNG_ETIMEDOUT</code>.
+The <em>timeout</em> <a href="/api/time.html">duration</a> is specified as a relative number of milliseconds.</p>
+<p>If the timeout is <a href="/api/time.html#duration-type"><code>NNG_DURATION_INFINITE</code></a>, then no timeout is used.
+If the timeout is <a href="/api/time.html#duration-type"><code>NNG_DURATION_DEFAULT</code></a>, then a “default” or socket-specific
+timeout is used.
+(This is frequently the same as <a href="/api/time.html#duration-type"><code>NNG_DURATION_INFINITE</code></a>.)</p>
+<p>The <a name="a013"></a><code>nng_aio_set_expire</code> function is similar to <code>nng_aio_set_timeout</code>, but sets
+an expiration time based on the system clock. The <em>expiration</em>
+<a href="/api/time.html">time</a> is a clock timestamp, such as would be returned by <a href="/api/time.html#get-the-current-time"><code>nng_clock</code></a>.</p>
+<h2 id="wait-for-completion"><a class="header" href="#wait-for-completion">Wait for Completion</a></h2>
+<pre><code class="language-c">void nng_aio_wait(nng_aio *aio);
+</code></pre>
+<p>The <a name="a014"></a><code>nng_aio_wait</code> function waits for an asynchronous I/O operation to complete.
+If the operation has not been started, or has already completed, then it returns immediately.</p>
+<p>If a callback was set with <em>aio</em> when it was allocated, then this
+function will not be called until the callback has completed.</p>
+<div class="mdbook-alerts mdbook-alerts-important">
+<p class="mdbook-alerts-title">
+ <span class="mdbook-alerts-icon"></span>
+ important
+</p>
+<p>The <code>nng_aio_wait</code> function should never be called from a function that itself
+is a callback of an <a href="/TODO.html"><code>nng_aio</code></a>, either this one or any other.
+Doing so may result in a deadlock.</p>
+</div>
+<h2 id="test-for-completion"><a class="header" href="#test-for-completion">Test for Completion</a></h2>
+<pre><code class="language-c">bool nng_aio_busy(nng_aio *aio);
+</code></pre>
+<p>The <a name="a015"></a><code>nng_aio_busy</code> function returns <code>true</code> if the <em>aio</em> is currently busy performing an
+operation or is executing a completion callback. Otherwise it return <code>false</code>.
+This is the same test used internally by <a href="/api/api.html#wait-for-completion"><code>nng_aio_wait</code></a>.</p>
+<div class="mdbook-alerts mdbook-alerts-important">
+<p class="mdbook-alerts-title">
+ <span class="mdbook-alerts-icon"></span>
+ important
+</p>
+<p>The caller is responsible for coordinating any use of this with any reuse of the <em>aio</em>.
+Because the <em>aio</em> can be reused use of this function can be racy.</p>
+</div>
+<h2 id="result-of-operation"><a class="header" href="#result-of-operation">Result of Operation</a></h2>
+<pre><code class="language-c">int nng_aio_result(nng_aio *aio);
+size_t nng_aio_count(nng_aio *aio);
+</code></pre>
+<p>The <a name="a016"></a><code>nng_aio_result</code> function returns the result of the operation associated
+with the handle <em>aio</em>.
+If the operation was successful, then 0 is returned.
+Otherwise a non-zero <a href="/api/errors.html">error</a> code, such as <a href="/api/errors.html#NNG_ECANCELED"><code>NNG_ECANCELED</code></a> or <a href="/api/errors.html#NNG_ETIMEDOUT"><code>NNG_ETIMEDOUT</code></a>, is returned.</p>
+<p>For operations that transfer data, <a name="a017"></a><code>nng_aio_count</code> returns the
+number of bytes transferred by the operation associated with the handle <em>aio</em>.
+Operations that do not transfer data, or do not keep a count, may return zero for this function.</p>
+<div class="mdbook-alerts mdbook-alerts-note">
+<p class="mdbook-alerts-title">
+ <span class="mdbook-alerts-icon"></span>
+ note
+</p>
+<p>The return value from these functions is undefined if the operation has not completed yet.
+Either call these from the handle’s completion callback, or after waiting for the
+operation to complete with <a href="/api/api.html#wait-for-completion"><code>nng_aio_wait</code></a>.</p>
+</div>
+<h2 id="messages"><a class="header" href="#messages">Messages</a></h2>
+<pre><code class="language-c">nng_msg *nng_aio_get_msg(nng_aio *aio);
+void nng_aio_set_msg(nng_aio *aio, nng_msg *msg);
+</code></pre>
+<p>The <a name="a018"></a><code>nng_aio_get_msg</code> and <a name="a019"></a><code>nng_aio_set_msg</code> functions retrieve and store a <a href="/api/msg.html">message</a>
+in <em>aio</em>.
+For example, if a function to receive data is called, that function can generally be expected
+to store a message on the asssociated <em>aio</em>, for the application to retrieve with
+<code>nng_aio_get_msg</code>.
+Conversely an application desiring to send a message <em>msg</em> will store it in the <em>aio</em> using
+<code>nng_aio_set_msg</code>. The function implementing the send operation will retrieve the message
+and arrange for it to be sent.</p>
+<h3 id="message-ownership"><a class="header" href="#message-ownership">Message Ownership</a></h3>
+<p>For send or transmit operations, the rule of thumb is that implementation of the operation
+is responsible for taking ownership of the message (and releasing resources when it is complete),
+if it will return success. If the operation will end in error, then the message will be
+retained and it is the consuming application’s responsibility to dispose of the message.
+This allows an application the opportunity to reuse the message to try again, if it so desires.</p>
+<p>For receive operations, the implementation of the operation will set the message on the <em>aio</em>
+on success, and the consuming application hasa responsibility to retrieve and dispose of the
+message. Failure to do so will leak the message. If the operation does not complete successfully,
+then no message is stored on the <em>aio</em>.</p>
+<h2 id="io-vector"><a class="header" href="#io-vector">I/O Vector</a></h2>
+<pre><code class="language-c">typedef struct nng_iov {
+ void * iov_buf;
+ size_t iov_len;
+};
+
+int nng_aio_set_iov(nng_aio *aio, unsigned int niov, nng_iov *iov);
+</code></pre>
+<p>For some operations, the unit of data transferred is not a <a href="/api/msg.html">message</a>, but
+rather a stream of bytes.</p>
+<p>For these operations, an array of <em>niov</em> <a name="a020"></a><code>nng_iov</code> structures can be passed to
+the <a name="a021"></a><code>nng_aio_set_iov</code> function to provide a scatter/gather array of
+elements describing the location (<code>iov_buf</code>) and length (<code>iov_len</code>) of data,
+to transfer.</p>
+<p>The <em>iov</em> vector is copied into storage in the <em>aio</em> itself, so that callers may use stack allocated <code>nng_iov</code> structures.
+The values pointed to by the <code>iov_buf</code> members are <em>not</em> copied by this function though.</p>
+<p>A maximum of four (4) <code>nng_iov</code> members may be supplied.</p>
+<div class="mdbook-alerts mdbook-alerts-tip">
+<p class="mdbook-alerts-title">
+ <span class="mdbook-alerts-icon"></span>
+ tip
+</p>
+<p>Most functions using <code>nng_iov</code> do not guarantee to transfer all of the data that they
+are requested to. To be sure that correct amount of data is transferred, as well as to
+start an attempt to complete any partial transfer, check the amount of data transferred by
+calling <a href="/api/api.html#result-of-operation"><code>nng_aio_count</code></a>.</p>
+</div>
+<h2 id="inputs-and-outputs"><a class="header" href="#inputs-and-outputs">Inputs and Outputs</a></h2>
+<pre><code class="language-c">void nng_aio_set_input(nng_aio *aio, unsigned int index, void *param);
+void *nng_aio_get_output(nng_aio *aio, unsigned int index);
+</code></pre>
+<p>Asynchronous operations can take additional input parameters, and
+provide additional result outputs besides the <a href="/api/api.html#result-of-operation">result</a> code.</p>
+<p>The <code>nng_aio_set_input</code> function sets the input parameter at <em>index</em>
+to <em>param</em> for the operation associated with <em>aio</em>.</p>
+<p>The <code>nng_aio_get_output</code> function returns the output result at <em>index</em>
+for the operation associated with <em>aio</em>.</p>
+<p>The type and semantics of input parameters and output results are determined by specific
+operations. The documentation for the operation should provide details.</p>
+<p>The valid values of <em>index</em> range from zero (0) to three (3), as no operation
+currently defined can accept more than four parameters or return more than four additional
+results.</p>
+<div class="mdbook-alerts mdbook-alerts-note">
+<p class="mdbook-alerts-title">
+ <span class="mdbook-alerts-icon"></span>
+ note
+</p>
+<p>If the <em>index</em> does not correspond to a defined input for the operation,
+then <code>nng_aio_set_input</code> will have no effect, and <code>nng_aio_get_output</code> will
+return <code>NULL</code>.</p>
+</div>
+<div class="mdbook-alerts mdbook-alerts-important">
+<p class="mdbook-alerts-title">
+ <span class="mdbook-alerts-icon"></span>
+ important
+</p>
+<p>It is an error to call this function while the <em>aio</em> is currently
+in use by an active asynchronous operation.</p>
+</div>
+<h2 id="see-also"><a class="header" href="#see-also">See Also</a></h2>
+<p><a href="/api/synch.html">Synchronization</a>,
+<a href="/api/thread.html">Threads</a>,
+<a href="/api/time.html">Time</a></p>
+<!-- Symbol cross reference -->
+<!-- Macros -->
+<!-- Protocols -->
+<!-- Transports -->
+<!-- Concept index -->
+
+ </main>
+
+ <nav class="nav-wrapper" aria-label="Page navigation">
+ <!-- Mobile navigation buttons -->
+ <a rel="prev" href="../api/url.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
+ <i class="fa fa-angle-left"></i>
+ </a>
+
+ <a rel="next prefetch" href="../api/synch.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
+ <i class="fa fa-angle-right"></i>
+ </a>
+
+ <div style="clear: both"></div>
+ </nav>
+ </div>
+ </div>
+
+ <nav class="nav-wide-wrapper" aria-label="Page navigation">
+ <a rel="prev" href="../api/url.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
+ <i class="fa fa-angle-left"></i>
+ </a>
+
+ <a rel="next prefetch" href="../api/synch.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
+ <i class="fa fa-angle-right"></i>
+ </a>
+ </nav>
+
+ </div>
+
+
+
+
+ <script>
+ window.playground_copyable = true;
+ </script>
+
+
+ <script src="../elasticlunr.min.js"></script>
+ <script src="../mark.min.js"></script>
+ <script src="../searcher.js"></script>
+
+ <script src="../clipboard.min.js"></script>
+ <script src="../highlight.js"></script>
+ <script src="../book.js"></script>
+
+ <!-- Custom JS scripts -->
+ <script src="../theme/pagetoc.js"></script>
+
+
+ </div>
+ </body>
+</html>