Module tcp_handler::raw
source · Expand description
Raw protocol. Without encryption and compression.
Not recommend. It’s unsafe and slow.
§Examples
use anyhow::Result;
use bytes::{Buf, BufMut, BytesMut};
use tcp_handler::raw::*;
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;
server_start(&mut server, s_init).await?;
client_start(&mut client, c_init).await?;
let mut writer = BytesMut::new().writer();
writer.write_string("hello server.")?;
send(&mut client, &mut writer.into_inner()).await?;
let mut reader = recv(&mut server).await?.reader();
let message = reader.read_string()?;
assert_eq!("hello server.", message);
let mut writer = BytesMut::new().writer();
writer.write_string("hello client.")?;
send(&mut server, &mut writer.into_inner()).await?;
let mut reader = recv(&mut client).await?.reader();
let message = reader.read_string()?;
assert_eq!("hello client.", message);
Ok(())
}
This protocol is like this:
┌────┬────────┬────────────┐ (It may not be in contiguous memory.)
in --> │ ** │ ****** │ ********** │
└────┴────────┴────────────┘
│
│─ Directly send
out <-- ─┘
Functions§
- Init the client side in tcp-handler raw protocol.
- Make sure the client side is ready to use in tcp-handler raw protocol.
- Recv message in raw tcp-handler protocol.
- Send message in raw tcp-handler protocol.
- Init the server side in tcp-handler raw protocol.
- Make sure the server side is ready to use in tcp-handler raw protocol.