Function server_start

Source
pub async fn server_start<W: AsyncWrite + Unpin>(
    stream: &mut W,
    identifier: &str,
    version: &str,
    last: Result<((u16, String), RsaPublicKey), StarterError>,
) -> Result<(Cipher, u16, String), StarterError>
Available on crate feature encryption only.
Expand description

Make sure the server side is ready to use in tcp-handler encrypt protocol.

Must be used in conjunction with server_init.

§Runtime

Due to call block_in_place internally, this function cannot be called in a current_thread runtime.

§Arguments

  • stream - The tcp stream or WriteHalf.
  • identifier - The returned application identifier. (Should be same with the para in server_init.)
  • version - The returned recommended application version. (Should be passed the prediction in server_init.)
  • last - The return value of server_init.

§Example

use anyhow::Result;
use tcp_handler::protocols::encrypt::{server_init, server_start};
use tokio::net::TcpListener;

#[tokio::main]
async fn main() -> Result<()> {
    let server = TcpListener::bind("localhost:25564").await?;
    let (mut server, _) = server.accept().await?;
    let s_init = server_init(&mut server, "test", |v| v == "0").await;
    let (cipher, protocol_version, client_version) = server_start(&mut server, "test", "0", s_init).await?;
    // Now the server is ready to use.
    Ok(())
}