Module compress_encrypt

Source
Available on crate feature compress_encryption only.
Expand description

Compression and encryption protocol.

Recommended to use this protocol.

§Example

use anyhow::Result;
use bytes::{Buf, BufMut, BytesMut};
use tcp_handler::protocols::compress_encrypt::*;
use tokio::net::{TcpListener, TcpStream};
use variable_len_reader::{VariableReader, VariableWriter};

#[tokio::main]
async fn main() -> Result<()> {
    let server = TcpListener::bind("localhost:0").await?;
    let mut client = TcpStream::connect(server.local_addr()?).await?;
    let (mut server, _) = server.accept().await?;

    let c_init = client_init(&mut client, "test", "0").await;
    let s_init = server_init(&mut server, "test", |v| v == "0").await;
    let (s_cipher, protocol_version, client_version) = server_start(&mut server, "test", "0", s_init).await?;
    let c_cipher = client_start(&mut client, c_init).await?;

    let mut writer = BytesMut::new().writer();
    writer.write_string("hello server.")?;
    let mut bytes = writer.into_inner();
    send(&mut client, &mut bytes, &c_cipher).await?;

    let mut reader = recv(&mut server, &s_cipher).await?.reader();
    let message = reader.read_string()?;
    assert_eq!("hello server.", message);

    let mut writer = BytesMut::new().writer();
    writer.write_string("hello client.")?;
    let mut bytes = writer.into_inner();
    send(&mut server, &mut bytes, &s_cipher).await?;

    let mut reader = recv(&mut client, &c_cipher).await?.reader();
    let message = reader.read_string()?;
    assert_eq!("hello client.", message);

    Ok(())
}

The send protocol:

        ┌────┬────────┬────────────┐ (It may not be in contiguous memory.)
in  --> │ ** │ ****** │ ********** │
        └────┴────────┴────────────┘
          └─────┐
         +Nonce │
          │     │─ Chain
          v     v
        ┌─────┬────┬────────┬────────────┐ (Zero copy. Not in contiguous memory.)
        │ *** │ ** │ ****** │ ********** │
        └─────┴────┴────────┴────────────┘
          │
          │─ DeflateEncoder
          v
        ┌─────────────────────┐ (Compressed bytes. In contiguous memory.)
        │ ******************* │
        └─────────────────────┘
          │
          │─ Encrypt in-place
          v
        ┌─────────────────────┐ (Compressed and encrypted bytes.)
out <-- │ ******************* │
        └─────────────────────┘

The recv process:

        ┌─────────────────────┐ (Packet data.)
in  --> │ ******************* │
        └─────────────────────┘
          │
          │─ Decrypt in-place
          v
        ┌─────────────────────┐ (Decrypted bytes.)
        │ ******************* │
        └─────────────────────┘
          │
          │─ DeflateEncoder
          v
        ┌─────┬──────────────────┐ (Decrypted and decompressed bytes.)
        │ *** │ **************** │
        └─────┴──────────────────┘
          │     │
         -Nonce │
out <--  ───────┘

Functions§

client_init
Init the client side in tcp-handler compress_encrypt protocol.
client_start
Make sure the client side is ready to use in tcp-handler compress_encrypt protocol.
recv
Recv the message in tcp-handler compress_encrypt protocol.
send
Send the message in tcp-handler compress_encrypt protocol.
server_init
Init the server side in tcp-handler compress_encrypt protocol.
server_start
Make sure the server side is ready to use in tcp-handler compress_encrypt protocol.