Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(2) Quinn Introduction Chapter, Connection Setup Page #921

Merged
merged 11 commits into from
Jan 28, 2021
4 changes: 3 additions & 1 deletion docs/book/src/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Summary

- [Quinn introduction](quinn.md)
- [QUINN Introduction](quinn.md)
- [Certificate Configuration](quinn/certificate.md)
- [Connection Setup](quinn/set-up-connection.md)
- [The QUIC protocol](quic.md)
89 changes: 89 additions & 0 deletions docs/book/src/quinn/set-up-connection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Connection Setup

In the [previous chapter](certificate.md) we looked at how to configure a certificate.
This aspect is omitted in this chapter to prevent duplication.
But **remember** that is is required to get your [Endpoint][Endpoint] up and running.
This chapter explains how to set up a connection and prepare it for data transfer.

It all starts with the [Endpoint][Endpoint] struct, this is the entry of the library.
TimonPost marked this conversation as resolved.
Show resolved Hide resolved

## Example

Let's start by defining some constants.

```rust
static SERVER_NAME: &str = "localhost";

fn client_addr() -> SocketAddr {
"127.0.0.1:5000".parse::<SocketAddr>().unwrap()
}

fn server_addr() -> SocketAddr {
"127.0.0.1:5001".parse::<SocketAddr>().unwrap()
}
```

For both the server and the client we use the [EndpointBuilder][EndpointBuilder].
The [EndpointBuilder][EndpointBuilder] has a method [bind(address)][bind] with which you link an address to the endpoint.
TimonPost marked this conversation as resolved.
Show resolved Hide resolved
This method initializes a UDP-socket that is used by quinn.
TimonPost marked this conversation as resolved.
Show resolved Hide resolved
If you need more control over the socket creation, it is also possible to initialize a quinn endpoint with an existing UDP socket with [with_socket][with_socket].
TimonPost marked this conversation as resolved.
Show resolved Hide resolved

**Server**

Just like a TCP Listener, you have to listen to incoming connections.
TimonPost marked this conversation as resolved.
Show resolved Hide resolved
Before you can listen to connections you need to configure the [EndpointBuilder][EndpointBuilder] as a server.
Note that the configuration itself does not perform any listening logic, instead use the `Incomming` type returned by [bind()][bind].
TimonPost marked this conversation as resolved.
Show resolved Hide resolved

```rust
async fn server() -> anyhow::Result<()> {
let mut endpoint_builder = Endpoint::builder();
// Configure this endpoint as a server by passing in `ServerConfig`.
endpoint_builder.listen(ServerConfig::default());

// Bind this endpoint to a UDP socket on the given server address.
let (endpoint, mut incoming) = endpoint_builder.bind(&server_addr())?;

// Start iterating over incoming connections.
while let Some(conn) = incoming.next().await {
let mut connection: NewConnection = conn.await?;

// Save connection somewhere, start transferring, receiving data, see DataTransfer tutorial.
}

Ok(())
}
```

**Client**

Just like a TCP client, you need to connect to a listening endpoint (the server).
In quinn you can do this with the method [connect()][connect].
TimonPost marked this conversation as resolved.
Show resolved Hide resolved
The [connect()][connect] method has an argument 'server name' which has to be the name that is in the configured certificate.
TimonPost marked this conversation as resolved.
Show resolved Hide resolved

```rust
async fn client() -> anyhow::Result<()> {
let mut endpoint_builder = Endpoint::builder();

// Bind this endpoint to a UDP socket on the given client address.
let (endpoint, _) = endpoint_builder.bind(&client_addr())?;

// Connect to the server passing in the server name which is supposed to be in the server certificate.
let connection: NewConnection = endpoint
.connect(&server_addr(), SERVER_NAME)?
.await?;
TimonPost marked this conversation as resolved.
Show resolved Hide resolved

// Start transferring, receiving data, see data transfer page.

Ok(())
}
```
<br><hr>

[Nextup](set-up-connection.md), lets have a look at sending data over this connection.
TimonPost marked this conversation as resolved.
Show resolved Hide resolved


[Endpoint]: https://docs.rs/quinn/latest/quinn/generic/struct.Endpoint.html
[EndpointBuilder]: https://docs.rs/quinn/latest/quinn/generic/struct.EndpointBuilder.html
[bind]: https://docs.rs/quinn/latest/quinn/generic/struct.EndpointBuilder.html#method.bind
[connect]: https://docs.rs/quinn/latest/quinn/generic/struct.Endpoint.html#method.connect
[with_socket]: https://docs.rs/quinn/latest/quinn/generic/struct.EndpointBuilder.html#method.with_socket