#tcp-socket #callback #sockets #tcp #websocket #ws #websocket-server

cbsk_socket

cbsk_socket is a socket callback tool

35 releases (14 stable)

2.0.6 Nov 15, 2024
2.0.0 Aug 21, 2024
1.3.11 Aug 19, 2024
1.3.10 Jul 26, 2024
0.2.2 Dec 29, 2023

#306 in Data structures

Download history 315/week @ 2024-08-18 39/week @ 2024-08-25 301/week @ 2024-09-01 27/week @ 2024-09-08 16/week @ 2024-09-15 93/week @ 2024-09-22 43/week @ 2024-09-29 8/week @ 2024-10-06 17/week @ 2024-10-13 3/week @ 2024-10-20 56/week @ 2024-11-03 118/week @ 2024-11-10 29/week @ 2024-11-17 15/week @ 2024-11-24 10/week @ 2024-12-01

172 downloads per month
Used in 4 crates (2 directly)

MIT/Apache

17KB
213 lines

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.80.0

now supported sockets

  • tcp client √
  • tcp server √
  • ws client √
  • ws server √

tcp server example

tcp server example

Cargo.toml file:

fast_log = "1.7.5"
cbsk_base = "2.0.6"
cbsk_socket_tokio = { version = "2.0.6", default-features = false, features = ["tcp_server"] }

main.rs file:

use std::net::{IpAddr, SocketAddr};
use std::sync::Arc;
use cbsk_base::{log, tokio};
use cbsk_base::async_trait::async_trait;
use cbsk_base::log::LevelFilter;
use cbsk_socket_tokio::cbsk_socket::tcp::server::config::TcpServerConfig;
use cbsk_socket_tokio::tcp::common::tcp_write_trait::TcpWriteTrait;
use cbsk_socket_tokio::tcp::server::callback::TcpServerCallBack;
use cbsk_socket_tokio::tcp::server::client::TcpServerClient;
use cbsk_socket_tokio::tcp::server::TcpServer;

#[tokio::main]
pub async fn main() {
    fast_log::init(fast_log::config::Config::default().level(LevelFilter::Info).console()).unwrap();
    let addr = SocketAddr::new(IpAddr::from([127, 0, 0, 1]), 8080);
    let conf = TcpServerConfig::new("".into(), addr, false);
    let tcp_server = TcpServer::new(conf.into(), Cb {});
    tcp_server.start().await;
}

struct Cb {}

#[async_trait]
impl TcpServerCallBack for Cb {
    async fn recv(&self, bytes: Vec<u8>, client: Arc<TcpServerClient>) -> Vec<u8> {
        log::info!("recv is {bytes:?}");
        client.send_bytes(b"hello client").await;
        Vec::with_capacity(1)
    }
}

tcp client example

tcp client example

Cargo.toml file:

fast_log = "1.7.5"
cbsk_base = "2.0.6"
cbsk_socket_tokio = "2.0.6" 

main.rs file:

use std::net::{IpAddr, SocketAddr};
use std::sync::LazyLock;
use std::time::Duration;
use cbsk_base::{log, tokio};
use cbsk_base::async_trait::async_trait;
use cbsk_base::log::LevelFilter;
use cbsk_socket_tokio::cbsk_socket::config::re_conn::SocketReConn;
use cbsk_socket_tokio::cbsk_socket::tcp::client::config::TcpClientConfig;
use cbsk_socket_tokio::tcp::client::callback::TcpClientCallBack;
use cbsk_socket_tokio::tcp::client::TcpClient;
use cbsk_socket_tokio::tcp::common::tcp_write_trait::TcpWriteTrait;

#[allow(non_upper_case_globals)]
static addr: LazyLock<SocketAddr> = LazyLock::new(|| { SocketAddr::new(IpAddr::from([127, 0, 0, 1]), 8080) });

#[allow(non_upper_case_globals)]
static tcp_client: LazyLock<TcpClient> = LazyLock::new(|| {
    let conf = TcpClientConfig::new("tcp client".into(), *addr, SocketReConn::enable(Duration::from_secs(3)));
    TcpClient::new(conf.into(), Cb {})
});

#[tokio::main]
pub async fn main() {
    fast_log::init(fast_log::config::Config::default().level(LevelFilter::Info).console()).unwrap();
    tcp_client.start().await;
}

struct Cb {}

#[async_trait]
impl TcpClientCallBack for Cb {
    async fn conn(&self) {
        tcp_client.send_bytes(b"hello server").await;
    }

    async fn recv(&self, bytes: Vec<u8>) -> Vec<u8> {
        log::info!("bytes is {bytes:?}");
        tcp_client.send_bytes(b"hello server").await;
        Vec::with_capacity(1)
    }
}

Dependencies

~0–7MB
~43K SLoC