summaryrefslogtreecommitdiff
path: root/ref/api
diff options
context:
space:
mode:
authorgdamore <gdamore@users.noreply.github.com>2025-01-02 03:46:39 +0000
committergdamore <gdamore@users.noreply.github.com>2025-01-02 03:46:39 +0000
commitc4b4b89e95f3c6bc701a32aba9884ceb3aa118b2 (patch)
treec8e33e0d07ec79d55e1142abc4bcb373059fe878 /ref/api
parentbb67a9767078bd98add53ead0e0e2f1390cfb539 (diff)
downloadnng-c4b4b89e95f3c6bc701a32aba9884ceb3aa118b2.tar.gz
nng-c4b4b89e95f3c6bc701a32aba9884ceb3aa118b2.tar.bz2
nng-c4b4b89e95f3c6bc701a32aba9884ceb3aa118b2.zip
deploy: f293e96b03b22c6a91140b118c4e15059e285543
Diffstat (limited to 'ref/api')
-rw-r--r--ref/api/ctx.html507
-rw-r--r--ref/api/index.html1
-rw-r--r--ref/api/memory.html4
-rw-r--r--ref/api/sock.html8
4 files changed, 514 insertions, 6 deletions
diff --git a/ref/api/ctx.html b/ref/api/ctx.html
new file mode 100644
index 00000000..790efc27
--- /dev/null
+++ b/ref/api/ctx.html
@@ -0,0 +1,507 @@
+<!DOCTYPE HTML>
+<html lang="en" class="light sidebar-visible" dir="ltr">
+ <head>
+ <!-- Book generated using mdBook -->
+ <meta charset="UTF-8">
+ <title>Contexts - 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">
+
+
+ <!-- Provide site root to javascript -->
+ <script>
+ var path_to_root = "../";
+ var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
+ </script>
+ <!-- Start loading toc.js asap -->
+ <script src="../toc.js"></script>
+ </head>
+ <body>
+ <div id="body-container">
+ <!-- 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; }
+ const html = document.documentElement;
+ html.classList.remove('light')
+ html.classList.add(theme);
+ html.classList.add("js");
+ </script>
+
+ <input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
+
+ <!-- Hide / unhide sidebar before it is displayed -->
+ <script>
+ 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';
+ html.classList.remove('sidebar-visible');
+ html.classList.add("sidebar-" + sidebar);
+ </script>
+
+ <nav id="sidebar" class="sidebar" aria-label="Table of contents">
+ <!-- populated by js -->
+ <mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
+ <noscript>
+ <iframe class="sidebar-iframe-outer" src="../toc.html"></iframe>
+ </noscript>
+ <div id="sidebar-resize-handle" class="sidebar-resize-handle">
+ <div class="sidebar-resize-indicator"></div>
+ </div>
+ </nav>
+
+ <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="contexts"><a class="header" href="#contexts">Contexts</a></h1>
+<p>Contexts <a name="a001"></a> in Scalability Protocols provide for isolation of protocol-specific
+state machines and associated data, allowing multiple <a name="a002"></a>concurrent <a name="a003"></a>state machines (or transactions)
+to coexist on a single <a href="/TODO.html">socket</a>.</p>
+<p>For example, a <a href="/proto/rep.html">REP</a> server may wish to allow many requests to be serviced concurrently,
+even though some jobs may take significant time to process. Contexts provide for this ability.</p>
+<p>Not all protocols have contexts, because many protocols simply have no state to manage.
+The following protocols support contexts:</p>
+<ul>
+<li><a href="/proto/rep.html">REP</a></li>
+<li><a href="/proto/req.html">REQ</a></li>
+<li><a href="/proto/respondent.html">RESPONDENT</a></li>
+<li><a href="/proto/surveyor.html">SURVEYOR</a></li>
+<li><a href="/proto/sub.html">SUB</a></li>
+</ul>
+<p>For these protocols, the socket will also have a single, default, context that is used when
+performing send or receive operations on the socket directly.</p>
+<p>Other protocols are stateless, at least with respect to message processing, and have no use for
+contexts. For the same reason, <a href="/api/sock.html#raw-mode-sockets">raw</a> mode sockets do not support contexts.</p>
+<div class="mdbook-alerts mdbook-alerts-tip">
+<p class="mdbook-alerts-title">
+ <span class="mdbook-alerts-icon"></span>
+ tip
+</p>
+<p>Developers with experience with [libnanomsg] may be used to using <a href="/api/sock.html#raw-mode-sockets">raw</a> sockets for concurrency.
+Contexts provide a superior solution, as they are much easier to use, less error prone, and allow
+for easy control of the amount of concurrency used on any given socket.</p>
+</div>
+<p>One drawback of contexts is that they cannot be used with file descriptor polling using
+<a href="/api/sock.html#polling-socket-events"><code>nng_socket_get_recv_poll_fd</code></a> or <a href="/api/sock.html#polling-socket-events"><code>nng_socket_get_send_poll_fd</code></a>.</p>
+<h2 id="context-structure"><a class="header" href="#context-structure">Context Structure</a></h2>
+<pre><code class="language-c">#define NNG_CTX_INITIALIZER // opaque value
+
+typedef struct nng_ctx_s nng_ctx;
+</code></pre>
+<p>The <a name="a004"></a><code>nng_ctx</code> structure represents context. This is a handle, and
+the members of it are opaque. However, unlike a pointer, it is passed by value.</p>
+<p>A context may be initialized statically with the <a name="a005"></a><code>NNG_CTX_INITIALIZER</code> macro,
+to ensure that it cannot be confused with a valid open context.</p>
+<h2 id="creating-a-context"><a class="header" href="#creating-a-context">Creating a Context</a></h2>
+<pre><code class="language-c">int nng_ctx_open(nng_ctx *ctxp, nng_socket s);
+</code></pre>
+<p>The <a name="a006"></a><code>nng_ctx_open</code> function creates a separate context to be used with the <a href="/TODO.html">socket</a> <em>s</em>,
+and returns it at the location pointed by <em>ctxp</em>.</p>
+<h2 id="context-identity"><a class="header" href="#context-identity">Context Identity</a></h2>
+<pre><code class="language-c">int nng_ctx_id(nng_ctx c);
+</code></pre>
+<p>The <a name="a007"></a><code>nng_ctx_id</code> function returns a positive identifier for the context <em>c</em> if it is valid.
+Otherwise it returns <code>-1</code>.</p>
+<p>A context is considered valid if it was ever opened with <a href="/api/ctx.html#creating-a-context"><code>nng_ctx_open</code></a> function.
+Contexts that are allocated on the stack or statically should be initialized with the macro <a href="/api/ctx.html#context-structure"><code>NNG_CTX_INITIALIZER</code></a>
+to ensure that they cannot be confused with a valid context before they are opened.</p>
+<h2 id="closing-a-context"><a class="header" href="#closing-a-context">Closing a Context</a></h2>
+<pre><code class="language-c">int nng_ctx_close(nng_ctx ctx);
+</code></pre>
+<p>The <a name="a008"></a><code>nng_ctx_close</code> function closes the context <em>ctx</em>.
+Messages that have been submitted for sending may be flushed or delivered,
+depending upon the transport.</p>
+<p>Further attempts to use the context after this call returns will result in <code>NNG_ECLOSED</code>.
+Threads waiting for operations on the context when this
+call is executed may also return with an <code>NNG_ECLOSED</code> result.</p>
+<div class="mdbook-alerts mdbook-alerts-note">
+<p class="mdbook-alerts-title">
+ <span class="mdbook-alerts-icon"></span>
+ note
+</p>
+<p>Closing the socket associated with <em>ctx</em> using <code>nng_socket_close</code>] also closes this context.</p>
+</div>
+<h2 id="sending-messages"><a class="header" href="#sending-messages">Sending Messages</a></h2>
+<pre><code class="language-c">int nng_ctx_sendmsg(nng_ctx ctx, nng_msg *msg, int flags);
+void nng_ctx_send(nng_ctx ctx, nng_aio *aio);
+</code></pre>
+<p>These functions (<a name="a009"></a><code>nng_ctx_sendmsg</code> and <a name="a010"></a><code>nng_ctx_send</code>) send
+messages over the socket <em>s</em>. The differences in their behaviors are as follows.</p>
+<div class="mdbook-alerts mdbook-alerts-note">
+<p class="mdbook-alerts-title">
+ <span class="mdbook-alerts-icon"></span>
+ note
+</p>
+<p>The semantics of what sending a message means varies from protocol to
+protocol, so examination of the protocol documentation is encouraged.
+Additionally, some protocols may not support sending at all or may require other pre-conditions first.
+(For example, <a href="/proto/rep.html">REP</a> sockets cannot normally send data until they have first received a request,
+while <a href="/proto/sub.html">SUB</a> sockets can only receive data and never send it.)</p>
+</div>
+<h3 id="nng_ctx_sendmsg"><a class="header" href="#nng_ctx_sendmsg">nng_ctx_sendmsg</a></h3>
+<p>The <code>nng_ctx_sendmsg</code> function sends the <em>msg</em> over the context <em>ctx</em>.</p>
+<p>If this function returns zero, then the socket will dispose of <em>msg</em> when the transmission is complete.
+If the function returns a non-zero status, then the call retains the responsibility for disposing of <em>msg</em>.</p>
+<p>The <em>flags</em> can contain the value <a href="/TODO.html"><code>NNG_FLAG_NONBLOCK</code></a>, indicating that the function should not wait if the socket
+cannot accept more data for sending. In such a case, it will return <a href="/api/errors.html#NNG_EAGAIN"><code>NNG_EAGAIN</code></a>.</p>
+<h3 id="nng_ctx_send"><a class="header" href="#nng_ctx_send">nng_ctx_send</a></h3>
+<p>The <code>nng_ctx_send</code> function sends a message asynchronously, using the <a href="/api/aio.html#asynchronous-io-handle"><code>nng_aio</code></a> <em>aio</em>, over the context <em>ctx</em>.
+The message to send must have been set on <em>aio</em> using the <a href="/api/aio.html#messages"><code>nng_aio_set_msg</code></a> function.</p>
+<p>If the operation completes successfully, then the context will have disposed of the message.
+However, if it fails, then callback of <em>aio</em> should arrange for a final disposition of the message.
+(The message can be retrieved from <em>aio</em> with <a href="/api/aio.html#messages"><code>nng_aio_get_msg</code></a>.)</p>
+<p>Note that callback associated with <em>aio</em> may be called <em>before</em> the message is finally delivered to the recipient.
+For example, the message may be sitting in queue, or located in TCP buffers, or even in flight.</p>
+<div class="mdbook-alerts mdbook-alerts-tip">
+<p class="mdbook-alerts-title">
+ <span class="mdbook-alerts-icon"></span>
+ tip
+</p>
+<p>This is the preferred function to use for sending data on a context. While it does require a few extra
+steps on the part of the application, the lowest latencies and highest performance will be achieved by using
+this function instead of <a href="/api/ctx.html#nng_ctx_sendmsg"><code>nng_ctx_sendmsg</code></a>.</p>
+</div>
+<h2 id="receiving-messages"><a class="header" href="#receiving-messages">Receiving Messages</a></h2>
+<pre><code class="language-c">int nng_ctx_recvmsg(nng_ctx ctx, nng_msg **msgp, int flags);
+void nng_ctx_recv(nng_ctx ctx, nng_aio *aio);
+</code></pre>
+<p>These functions (, <a name="a011"></a><code>nng_ctx_recvmsg</code> and <a name="a012"></a><code>nng_ctx_recv</code>) receive
+messages over the context <em>ctx</em>. The differences in their behaviors are as follows.</p>
+<div class="mdbook-alerts mdbook-alerts-note">
+<p class="mdbook-alerts-title">
+ <span class="mdbook-alerts-icon"></span>
+ note
+</p>
+<p>The semantics of what receving a message means varies from protocol to
+protocol, so examination of the protocol documentation is encouraged.
+Additionally, some protocols may not support receiving at all or may require other pre-conditions first.
+(For example, <a href="/proto/req.html">REQ</a> sockets cannot normally receive data until they have first sent a request.)</p>
+</div>
+<h3 id="nng_recvmsg"><a class="header" href="#nng_recvmsg">nng_recvmsg</a></h3>
+<p>The <code>nng_ctx_recvmsg</code> function receives a message and stores a pointer to the <a href="/api/msg.html#message-structure"><code>nng_msg</code></a> for that message in <em>msgp</em>.</p>
+<p>The <em>flags</em> can contain the value <a href="/TODO.html"><code>NNG_FLAG_NONBLOCK</code></a>, indicating that the function should not wait if the socket
+has no messages available to receive. In such a case, it will return <a href="/api/errors.html#NNG_EAGAIN"><code>NNG_EAGAIN</code></a>.</p>
+<h3 id="nng_recv_aio"><a class="header" href="#nng_recv_aio">nng_recv_aio</a></h3>
+<p>The <code>nng_ctx_send</code> function receives a message asynchronously, using the <a href="/api/aio.html#asynchronous-io-handle"><code>nng_aio</code></a> <em>aio</em>, over the context <em>ctx</em>.
+On success, the received message can be retrieved from the <em>aio</em> using the <a href="/api/aio.html#messages"><code>nng_aio_get_msg</code></a> function.</p>
+<div class="mdbook-alerts mdbook-alerts-note">
+<p class="mdbook-alerts-title">
+ <span class="mdbook-alerts-icon"></span>
+ note
+</p>
+<p>It is important that the application retrieves the message, and disposes of it accordingly.
+Failure to do so will leak the memory.</p>
+</div>
+<div class="mdbook-alerts mdbook-alerts-tip">
+<p class="mdbook-alerts-title">
+ <span class="mdbook-alerts-icon"></span>
+ tip
+</p>
+<p>This is the preferred function to use for receiving data on a context. While it does require a few extra
+steps on the part of the application, the lowest latencies and highest performance will be achieved by using
+this function instead of <a href="/api/ctx.html#nng_ctx_recvmsg"><code>nng_ctx_recvmsg</code></a>.</p>
+</div>
+<h2 id="context-options"><a class="header" href="#context-options">Context Options</a></h2>
+<pre><code class="language-c">int nng_ctx_get_bool(nng_ctx ctx, const char *opt, bool *valp);
+int nng_ctx_get_int(nng_ctx ctx, const char *opt, int *valp);
+int nng_ctx_get_ms(nng_ctx ctx, const char *opt, nng_duration *valp);
+int nng_ctx_get_size(nng_ctx ctx, const char *opt, size_t *valp);
+int nng_ctx_get_uint64(nng_ctx ctx, const char *opt, uint64_t *valp);
+
+int nng_ctx_set_bool(nng_ctx ctx, const char *opt, int val);
+int nng_ctx_set_int(nng_ctx ctx, const char *opt, int val);
+int nng_ctx_set_ms(nng_ctx ctx, const char *opt, nng_duration val);
+int nng_ctx_set_size(nng_ctx ctx, const char *opt, size_t val);
+int nng_ctx_set_uint64(nng_ctx ctx, const char *opt, uint64_t val);
+</code></pre>
+<p>These functions are used to retrieve or change the value of an option named <em>opt</em> from the context <em>ctx</em>.
+The <code>nng_ctx_get_</code> functions retrieve the value, and store it in the location <em>valp</em> references.
+The <code>nng_ctx_set_</code> functions change the value for the <em>ctx</em>, taking it from <em>val</em>.</p>
+<p>These functions access an option as a specific type. The protocol will have details about which options
+are available for contexts, and which type they may be accessed using.</p>
+<h2 id="examples"><a class="header" href="#examples">Examples</a></h2>
+<p>These examples show building blocks for a concurrent service based on contexts.</p>
+<h3 id="example-1-context-echo-server"><a class="header" href="#example-1-context-echo-server">Example 1: Context Echo Server</a></h3>
+<p>The following program fragment demonstrates the use of contexts to implement
+a concurrent <a href="/proto/rep.html">REP</a> service that simply echos messages back
+to the sender.</p>
+<pre><code class="language-c">struct echo_context {
+ nng_ctx ctx;
+ nng_aio *aio;
+ enum { INIT, RECV, SEND } state;
+};
+
+void
+echo(void *arg)
+{
+ struct echo_context *ec = arg;
+
+ switch (ec-&gt;state) {
+ case INIT:
+ ec-&gt;state = RECV;
+ nng_ctx_recv(ec-&gt;ctx, ec-&gt;aio);
+ return;
+ case RECV:
+ if (nng_aio_result(ec-&gt;aio) != 0) {
+ // ... handle error
+ }
+ // We reuse the message on the ec-&gt;aio
+ ec-&gt;state = SEND;
+ nng_ctx_send(ec-&gt;ctx, ec-&gt;aio);
+ return;
+ case SEND:
+ if (nng_aio_result(ec-&gt;aio) != 0) {
+ // ... handle error
+ }
+ ec-&gt;state = RECV;
+ nng_ctx_recv(ec-&gt;ctx, ec-&gt;aio);
+ return;
+ }
+}
+</code></pre>
+<h3 id="example-2-starting-the-echo-service"><a class="header" href="#example-2-starting-the-echo-service">Example 2: Starting the Echo Service</a></h3>
+<p>Given the above fragment, the following example shows setting up the service.
+It assumes that the <a href="/TODO.html">socket</a> has already been
+created and any transports set up as well with functions such as <a href="/TODO.html"><code>nng_dial</code></a> or <a href="/TODO.html"><code>nng_listen</code></a>.</p>
+<pre><code class="language-c">
+#define CONCURRENCY 1024
+
+static struct echo_context ecs[CONCURRENCY];
+
+void
+start_echo_service(nng_socket rep_socket)
+{
+ for (int i = 0; i &lt; CONCURRENCY; i++) {
+ // error checks elided for clarity
+ nng_ctx_open(&amp;ecs[i].ctx, rep_socket);
+ nng_aio_alloc(&amp;ecs[i].aio, echo, ecs+i);
+ ecs[i].state = INIT;
+ echo(ecs+i); // start it running
+ }
+}
+</code></pre>
+<!-- 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/sock.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/memory.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/sock.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/memory.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>
diff --git a/ref/api/index.html b/ref/api/index.html
index db3f30c3..5372ced4 100644
--- a/ref/api/index.html
+++ b/ref/api/index.html
@@ -233,6 +233,7 @@ include the <code>nng/nng.h</code> header file like so:</p>
<li><a href="init.html">Initialization</a></li>
<li><a href="msg.html">Messages</a></li>
<li><a href="sock.html">Sockets</a></li>
+<li><a href="ctx.html">Contexts</a></li>
<li><a href="memory.html">Memory</a></li>
<li><a href="time.html">Time</a></li>
<li><a href="url.html">URLs</a></li>
diff --git a/ref/api/memory.html b/ref/api/memory.html
index 32319564..ddacea19 100644
--- a/ref/api/memory.html
+++ b/ref/api/memory.html
@@ -278,7 +278,7 @@ can be used to deallocate strings allocated with <a href="/api/memory.html#dupli
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
- <a rel="prev" href="../api/sock.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
+ <a rel="prev" href="../api/ctx.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
@@ -292,7 +292,7 @@ can be used to deallocate strings allocated with <a href="/api/memory.html#dupli
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
- <a rel="prev" href="../api/sock.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
+ <a rel="prev" href="../api/ctx.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
diff --git a/ref/api/sock.html b/ref/api/sock.html
index 215726e0..cdb4b824 100644
--- a/ref/api/sock.html
+++ b/ref/api/sock.html
@@ -485,7 +485,7 @@ Failure to do so will leak the memory.</p>
</p>
<p>This is the preferred function to use for receiving data on a socket. While it does require a few extra
steps on the part of the application, the lowest latencies and highest performance will be achieved by using
-this function instead of <a href="/TODO.html"><code>nng_recv</code></a> or <a href="/TODO.html"><code>nng_recvmsg</code></a>.</p>
+this function instead of <a href="/TODO.html"><code>nng_recv</code></a> or <a href="/api/sock.html#nng_recvmsg"><code>nng_recvmsg</code></a>.</p>
</div>
<h2 id="polling-socket-events"><a class="header" href="#polling-socket-events">Polling Socket Events</a></h2>
<pre><code class="language-c">int nng_socket_get_recv_poll_fd(nng_socket s, int *fdp);
@@ -586,7 +586,7 @@ int main(int argc, char **argv) {
}
</code></pre>
<h3 id="example-3-watching-a-periodic-timestamp"><a class="header" href="#example-3-watching-a-periodic-timestamp">Example 3: Watching a Periodic Timestamp</a></h3>
-<p>This example demonstrates the use of <a href="/api/aio.html#asynchronous-io-handle"><code>nng_aio</code></a>, [<code>nng_recv_aio</code>], to build a client to
+<p>This example demonstrates the use of <a href="/api/aio.html#asynchronous-io-handle"><code>nng_aio</code></a>, <a href="/api/sock.html#nng_recv_aio"><code>nng_recv_aio</code></a>, to build a client to
watch for messages received from the service created in Example 2.
Error handling is elided for the sake of clarity.</p>
<pre><code class="language-c">#include &lt;stdlib.h&gt;
@@ -645,7 +645,7 @@ int main(int argc, char **argv) {
<i class="fa fa-angle-left"></i>
</a>
- <a rel="next prefetch" href="../api/memory.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
+ <a rel="next prefetch" href="../api/ctx.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
@@ -659,7 +659,7 @@ int main(int argc, char **argv) {
<i class="fa fa-angle-left"></i>
</a>
- <a rel="next prefetch" href="../api/memory.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
+ <a rel="next prefetch" href="../api/ctx.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>