Function tcp_handler::encrypt::send

source ·
pub async fn send<W: AsyncWriteExt + Unpin + Send, B: Buf>(
    stream: &mut W,
    message: &mut B,
    cipher: AesCipher
) -> Result<AesCipher, PacketError>
Available on crate feature encrypt only.
Expand description

Send message in encrypt tcp-handler protocol.

You may use some crate to read and write data, such as serde, postcard and variable-len-reader.

§Arguments

  • stream - The tcp stream or WriteHalf.
  • message - The message to send.

§Example

use anyhow::Result;
use bytes::{BufMut, BytesMut};
use tcp_handler::encrypt::{client_init, client_start, send};
use tokio::net::TcpStream;
use variable_len_reader::VariableWriter;

#[tokio::main]
async fn main() -> Result<()> {
    let mut client = TcpStream::connect("localhost:25564").await?;
    let c_init = client_init(&mut client, &"test", &"0").await;
    let mut cipher = client_start(&mut client, c_init).await?;

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

    Ok(())
}