cbsk_socket is a socket callback tool
you can use cbsk_socket create TCP/WebSocket server or client, you don't need to focus on TCP/WebSocket read and write,
just focus on
business processing
minimum supported Rust version
Rust 1.75.0
now supported sockets
- tcp client √
- tcp server √
- ws client √
- ws server √
tcp server example
Cargo.toml file:
fast_log = "1.6.16"
cbsk_base = { version = "0.1.8" }
cbsk_socket = { version = "0.5.1", features = ["tcp_server"] }
main.rs file:
use std::net::{IpAddr, SocketAddr};
use std::sync::Arc;
use cbsk_base::{log, tokio};
use cbsk_socket::tcp::server::callback::TcpServerCallBack;
use cbsk_socket::tcp::server::client::TcpServerClient;
use cbsk_socket::tcp::server::config::TcpServerConfig;
use cbsk_socket::tcp::server::TcpServer;
use cbsk_socket::tcp::write_trait::WriteTrait;
#[tokio::main]
async fn main() {
let fast_config = fast_log::config::Config::default().console();
fast_log::init(fast_config).unwrap();
let addr = SocketAddr::new(IpAddr::from([0, 0, 0, 0]), 8080);
let tcp_config = TcpServerConfig::new("test".into(), addr, false);
let tcp_server = TcpServer::new(tcp_config.into(), TcpServerBusiness {}.into());
let handle = tcp_server.start::<1024>();
handle.await.unwrap();
log::logger().flush();
}
pub struct TcpServerBusiness {}
impl TcpServerCallBack for TcpServerBusiness {
async fn recv(&self, bytes: Vec<u8>, client: Arc<TcpServerClient>) -> Vec<u8> {
println!("{} read bytes [{bytes:?}]", client.log_head);
client.send_bytes(b"hello world").await;
client.send_text("hello world").await;
client.send_json(&"hello world".to_string()).await;
Vec::new()
}
}
tcp client example
Cargo.toml file:
fast_log = "1.6.16"
cbsk_base = { version = "0.1.8" }
cbsk_socket = { version = "0.5.1", features = ["tcp_client"] }
main.rs file:
use std::net::{IpAddr, SocketAddr};
use std::time::Duration;
use cbsk_base::{log, tokio};
use cbsk_socket::config::re_conn::SocketReConn;
use cbsk_socket::tcp::client::callback::TcpClientCallBack;
use cbsk_socket::tcp::client::config::TcpClientConfig;
use cbsk_socket::tcp::client::TcpClient;
use cbsk_socket::tcp::write_trait::WriteTrait;
#[tokio::main]
async fn main() {
let fast_config = fast_log::config::Config::default().console();
fast_log::init(fast_config).unwrap();
let addr = SocketAddr::new(IpAddr::from([127, 0, 0, 1]), 8080);
let tcp_config = TcpClientConfig::new("test".into(), addr, SocketReConn::enable(Duration::from_secs(3)));
let tcp_client = TcpClient::new(tcp_config.into(), TcpClientBusiness {}.into());
let read_handle = tcp_client.start::<1024>();
let write_handle = tokio::spawn(async move {
loop {
if tcp_client.is_connected() {
tcp_client.send_bytes(b"hello world").await;
tcp_client.send_text("hello world").await;
tcp_client.send_json(&"hello world".to_string()).await;
}
tokio::time::sleep(Duration::from_secs(3)).await;
}
});
read_handle.await.unwrap();
write_handle.await.unwrap();
log::logger().flush();
}
pub struct TcpClientBusiness {}
impl TcpClientCallBack for TcpClientBusiness {
async fn recv(&self, bytes: Vec<u8>) -> Vec<u8> {
println!("read bytes [{bytes:?}]");
Vec::new()
}
}
features explain
the following features are only valid for tcp_server
or tcp_client
- default is
tokio_tcp
, use tokio runtime and tokio tcp
tokio_tcp
, use tokio runtime and tokio tcp
system_tcp
, use tokio runtime and system tcp
tcp_runtime_thread
use thread and system tcp
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 default tokio_tcp
-
websocket tls coming soon
if y want to use tls, y can
use tokio-tungstenite(github)