Files
noq/quinn/data-transfer.html
T
2021-02-23 08:53:04 +00:00

355 lines
17 KiB
HTML

<!DOCTYPE HTML>
<html lang="en" class="sidebar-visible no-js light">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>Data Transfer - Quinn</title>
<!-- Custom HTML head -->
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<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 -->
</head>
<body>
<!-- Provide site root to javascript -->
<script type="text/javascript">
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 type="text/javascript">
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 type="text/javascript">
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('no-js')
html.classList.remove('light')
html.classList.add(theme);
html.classList.add('js');
</script>
<!-- Hide / unhide sidebar before it is displayed -->
<script type="text/javascript">
var html = document.querySelector('html');
var sidebar = 'hidden';
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
}
html.classList.remove('sidebar-visible');
html.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 "><a href="../networking-introduction.html"><strong aria-hidden="true">1.</strong> Networking introduction</a></li><li class="chapter-item expanded "><a href="../quinn.html"><strong aria-hidden="true">2.</strong> Quinn introduction</a></li><li class="chapter-item expanded "><a href="../quinn.html"><strong aria-hidden="true">3.</strong> QUINN Introduction</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../quinn/certificate.html"><strong aria-hidden="true">3.1.</strong> Certificate Configuration</a></li><li class="chapter-item expanded "><a href="../quinn/set-up-connection.html"><strong aria-hidden="true">3.2.</strong> Connection Setup</a></li><li class="chapter-item expanded "><a href="../quinn/data-transfer.html" class="active"><strong aria-hidden="true">3.3.</strong> Data Transfer</a></li></ol></li><li class="chapter-item expanded "><a href="../quic.html"><strong aria-hidden="true">4.</strong> The QUIC protocol</a></li></ol>
</div>
<div id="sidebar-resize-handle" class="sidebar-resize-handle"></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 bordered">
<div class="left-buttons">
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</button>
<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 (default)</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">Quinn</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" name="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 type="text/javascript">
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>
<h1 id="data-transfer"><a class="header" href="#data-transfer">Data Transfer</a></h1>
<p>The <a href="set-up-connection.html">previous chapter</a> explained how to set up an <a href="https://docs.rs/quinn/latest/quinn/generic/struct.Endpoint.html">Endpoint</a>
and then get access to a <a href="https://docs.rs/quinn/latest/quinn/generic/struct.NewConnection.html">NewConnection</a> instance.
This chapter continues with the subject of sending data over this connection.</p>
<h2 id="multiplexing"><a class="header" href="#multiplexing">Multiplexing</a></h2>
<p>Multiplexing is the act of combining data from multiple streams into a single stream.
This can have a significant positive effect on the performance of the application.
With QUIC, the programmer is in full control over the stream allocation.</p>
<h2 id="stream-types"><a class="header" href="#stream-types">Stream Types</a></h2>
<p>QUIC provides support for both stream and message-based communication.
Streams and messages can be initiated both on the client and server.</p>
<table><thead><tr><th align="left">Type</th><th align="left">Description</th><th align="left">Reference</th></tr></thead><tbody>
<tr><td align="left"><strong>Bidirectional Stream</strong></td><td align="left">two way stream communication.</td><td align="left">see <a href="https://docs.rs/quinn/latest/quinn/generic/struct.Connection.html#method.open_bi">open_bi</a></td></tr>
<tr><td align="left"><strong>Unidirectional Stream</strong></td><td align="left">one way stream communication.</td><td align="left">see <a href="https://docs.rs/quinn/latest/quinn/generic/struct.Connection.html#method.open_uni">open_uni</a></td></tr>
<tr><td align="left"><strong>Unreliable Messaging (extension)</strong></td><td align="left">message based unreliable communication.</td><td align="left">see <a href="https://docs.rs/quinn/latest/quinn/generic/struct.Connection.html#method.send_datagram">send_datagram</a></td></tr>
</tbody></table>
<h2 id="how-to-use"><a class="header" href="#how-to-use">How to Use</a></h2>
<p>New streams can be created with <a href="https://docs.rs/quinn/latest/quinn/generic/struct.Connection.html#method.open_bi">open_bi()</a>, <a href="https://docs.rs/quinn/latest/quinn/generic/struct.Connection.html#method.open_uni">open_uni()</a> from type <a href="https://docs.rs/quinn/latest/quinn/generic/struct.NewConnection.html#structfield.connection">Connection</a>.
An instance of this type, together with existing streams, can be found in the <a href="https://docs.rs/quinn/latest/quinn/generic/struct.NewConnection.html#structfield.connection">connection</a> field of <a href="https://docs.rs/quinn/latest/quinn/generic/struct.NewConnection.html">NewConnection</a>.</p>
<h2 id="bidirectional-streams"><a class="header" href="#bidirectional-streams">Bidirectional Streams</a></h2>
<p>With bidirectional streams, data can be sent in both directions.
For example, from the connection initiator to the peer and the other way around.</p>
<p><em>open bidirectional stream</em></p>
<pre><pre class="playground"><code class="language-rust">
<span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>async fn open_bidirectional_stream(connection: Connection) -&gt; anyhow::Result&lt;()&gt; {
let (mut send, recv) = connection.
open_bi()
.await?;
send.write_all(b&quot;test&quot;).await?;
send.finish().await?;
let received = recv.read_to_end(10).await?;
Ok(())
}
<span class="boring">}
</span></code></pre></pre>
<p><em>iterate incoming bidirectional stream(s)</em></p>
<pre><pre class="playground"><code class="language-rust">
<span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>async fn receive_bidirectional_stream(mut connection: NewConnection) -&gt; anyhow::Result&lt;()&gt; {
while let Some(Ok((send, recv))) = connection.bi_streams.next().await {
// Because it is a bidirectional stream, we can both send and recieve.
println!(&quot;request: {:?}&quot;, recv.read_to_end(50).await?);
send.write_all(b&quot;response&quot;).await?;
send.finish().await?;
}
Ok(())
}
<span class="boring">}
</span></code></pre></pre>
<h2 id="unidirectional-streams"><a class="header" href="#unidirectional-streams">Unidirectional Streams</a></h2>
<p>With unidirectional streams, you can carry data only in one direction: from the initiator of the stream to its peer.
It is possible to get reliability without ordering (so no head-of-line blocking) by opening a new stream for each packet.</p>
<p><em>open unidirectional stream</em></p>
<pre><pre class="playground"><code class="language-rust">
<span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>async fn open_unidirectional_stream(connection: Connection)-&gt; anyhow::Result&lt;()&gt; {
let mut send = connection.
open_uni()
.await?;
send.write_all(b&quot;test&quot;).await.unwrap();
send.finish().await?;
Ok(())
}
<span class="boring">}
</span></code></pre></pre>
<p><em>iterating incoming unidirectional stream(s)</em></p>
<pre><pre class="playground"><code class="language-rust">
<span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>async fn receive_unidirectional_stream(mut connection: NewConnection) -&gt; anyhow::Result&lt;()&gt; {
while let Some(Ok(recv)) = connection.uni_streams.next().await {
// Because it is a unidirectional stream, we can only receive not send back.
println!(&quot;{:?}&quot;, recv.read_to_end(50).await?);
}
Ok(())
}
<span class="boring">}
</span></code></pre></pre>
<h2 id="unreliable-messaging"><a class="header" href="#unreliable-messaging">Unreliable Messaging</a></h2>
<p>With unreliable messaging, you can transfer data without reliability.
This could be useful if data arrival isn't essential or when high throughput is important.</p>
<p><em>send datagram</em></p>
<pre><pre class="playground"><code class="language-rust">
<span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>async fn send_unreliable(connection: Connection)-&gt; anyhow::Result&lt;()&gt; {
connection
.send_datagram(b&quot;test&quot;.into())
.await?;
Ok(())
}
<span class="boring">}
</span></code></pre></pre>
<p><em>iterating datagram stream(s)</em></p>
<pre><pre class="playground"><code class="language-rust">
<span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>async fn receive_datagram(mut connection: NewConnection) -&gt; anyhow::Result&lt;()&gt; {
while let Some(Ok(received_bytes)) = connection.datagrams.next().await {
// Because it is a unidirectional stream, we can only receive not send back.
println!(&quot;request: {:?}&quot;, received);
}
Ok(())
}
<span class="boring">}
</span></code></pre></pre>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../quinn/set-up-connection.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" href="../quic.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="../quinn/set-up-connection.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" href="../quic.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 type="text/javascript">
window.playground_copyable = true;
</script>
<script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
<script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
<script src="../book.js" type="text/javascript" charset="utf-8"></script>
<!-- Custom JS scripts -->
</body>
</html>