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:
= { = "0.1.7", = false, = ["once_cell"] }
= { = "0.2.0", = ["server"] }
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
andtokio_tcp
system_tcp
, use system tcp and tokio runtimetokio_tcp
, use tokio tcp and tokio runtimetcp_runtime_thread
, use system tcp and system thread runtime
other issues
- The reason for adding system tcp and thread runtime is that during some Linux testing, there was a deadlock issue
with
tokio::net::TcpStream
+tokio runtime
, causing tokio to not run. As this Linux is customized, we are currently unable to provide testable issues to Tokio. If you are using Windows, Windows Server 2012, macos, ubuntu, etc., this cargo crate is normal and you can use the defaulttokio_tcp