cbsk is a TCP data callback tool that allows you to focus on your business processing without having to worry about TCP data read, write, and split
internal protocol
cbsk has a custom TCP data verification protocol internally, and the protocol logic is as follows:
-
Verify if the data uses ['c ',' b ','s',' k '] as the header frame. If not, the data will be discarded. Of course, you can customize the data frame header
-
Obtain the first byte after the header frame, which represents the length description of the data length
-
Obtain the true data length based on the length description of the data length
-
Read the real data, if there is packet occupancy, split it and call a callback. If the length is insufficient, wait for the next TCP data to be obtained until the length is consistent
-
Repeat the above steps and start from the first one again
example
Cargo.toml:
= { = "1.2.0", = false, = ["once_cell"] }
= { = "1.2.0", = ["server_tokio"] }
main.rs:
use ;
use Arc;
use Duration;
use CbskWriteTrait;
use CbskClientCallBack;
use CbskClient;
use CbskServerCallBack;
use CbskServer;
use CbskServerClient;
use tokio;
use Lazy;
static addr: = new;
static cbsk_client: = new;
async
// ------------------------- you server business ---------------------
// ------------------------- you client business ---------------------
features explain
- default is
client
andclient_tokio
client_rayon
, tcp client by rayon thread runtimeserver_rayon
, tcp server by rayon thread runtimeclient_tokio
, tcp client by tokio runtimeserver_tokio
, tcp server by tokio runtime