lynn_tcp 1.1.16

Lightweight asynchronous TCP framework
Documentation

Lynn_tcp

Crates.io MIT licensed doc Downloads

English|简体中文

Lynn_tcp is a lightweight TCP server framework


Keywords

  • Lightweight: concise code that is easier to learn and use

  • Concurrent and Performance: Based on Tokio's excellent asynchronous performance, it is easy to achieve concurrent processing capabilities for multi-user links

  • Lower latency: Design with read and write separation to achieve lower latency

  • Single Server: Focused on providing TCP service development for single server

  • Security: Code written with strong typing and memory safety in Rust

    tips: Lynn_tcp is mainly used for message forwarding and long link TCP game servers

    Quickly develop suitable business scenarios based on frameworks

    Different message parsing, communication data encryption, and other operations can be achieved through custom settings

Simple Use

Dependencies

Make sure you activated the features which you need of the lynn_tcp on Cargo.toml:

full features

Use cargo add lynn_tcp or:

[dependencies]
lynn_tcp = "=1.1.16"

server feature

[dependencies]
lynn_tcp = { version = "=1.1.16" , features = "server" }

client feature

[dependencies]
lynn_tcp = { version = "=1.1.16" , features = "client" }

Server

use lynn_tcp::{lynn_server::*, lynn_tcp_dependents::*};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize tracing or use app.logserver()
    tracing_subscriber::fmt::init();
    let _ = LynnServer::new()
        .await
        .add_router(1, my_service)
        .add_router(2, my_service_with_buf)
        .add_router(3, my_service_with_clients)
        .start()
        .await;
    Ok(())
}

pub async fn my_service() -> HandlerResult {
    HandlerResult::new_without_send()
}
pub async fn my_service_with_buf(input_buf_vo: InputBufVO) -> HandlerResult {
    println!(
        "service read from :{}",
        input_buf_vo.get_input_addr().unwrap()
    );
    HandlerResult::new_without_send()
}
pub async fn my_service_with_clients(clients_context: ClientsContext) -> HandlerResult {
    HandlerResult::new_with_send(1, "hello lynn".into(), clients_context.get_all_clients_addrs().await)
}

Server with config

use lynn_tcp::{lynn_server::*, lynn_tcp_dependents::*};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize tracing or use app.logserver()
    tracing_subscriber::fmt::init();
    let _ = LynnServer::new_with_config(
        LynnServerConfigBuilder::new()
            .with_addr("0.0.0.0:9177")
            .with_server_max_connections(Some(&200))
            // Suggestion 256-512
            .with_server_max_taskpool_size(&512)
            // ...more
            .build(),
    )
    .await
    .add_router(1, my_service)
    .add_router(2, my_service_with_buf)
    .add_router(3, my_service_with_clients)
    .start()
    .await;
    Ok(())
}

pub async fn my_service() -> HandlerResult {
    HandlerResult::new_without_send()
}
pub async fn my_service_with_buf(input_buf_vo: InputBufVO) -> HandlerResult {
    println!(
        "service read from :{}",
        input_buf_vo.get_input_addr().unwrap()
    );
    HandlerResult::new_without_send()
}
pub async fn my_service_with_clients(clients_context: ClientsContext) -> HandlerResult {
    HandlerResult::new_with_send(
        1,
        "hello lynn".into(),
        clients_context.get_all_clients_addrs().await,
    )
}

Client

use lynn_tcp::{
    lynn_client::LynnClient,
    lynn_tcp_dependents::*,
};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize tracing or use app.logserver()
    tracing_subscriber::fmt::init();
    let client = LynnClient::new_with_addr("127.0.0.1:9177")
            .await
            .start()
            .await;
    let _ = client.send_data(HandlerResult::new_with_send_to_server(1, "hello".into())).await;
    let input_buf_vo = client.get_receive_data().await.unwrap();
    Ok(())
}

Features

  • server: Provide customizable TCP services that can easily achieve multi-user long connections and concurrent processing capabilities, with services for different routes
  • client: Provides a custom TCP client that sends and receives messages to and from a TCP server

Road maps

Basic functions

✅ Tcp server

✅ Tcp client

✅ Custom message parsing

✅ Automatically clean sockets

✅ Routing service for asynchronous tasks

Note:

All Basic functions support on v1.0.0 and above

Extended functions

  • Scheduled tasks
  • Middleware
  • Global database handle
  • Communication data encryption
  • Disconnecting reconnection mechanism

Note:

If you haven't seen clear instructions, it's currently unavailable and still under development

Flow chart

FlowChart.png

FlowChart

Release note

version.md

Test results

platform:mac-m1pro-16G

client concurrency Total time Requests Maximum time Minimum time Average time Requests/Seconds
256 14.95s 2259754 29.15ms 33µs 1ms 151128
512 14.91s 2290097 77.93ms 43µs 3ms 153591
1024 14.89s 2330169 101.87ms 43µs 6ms 156537
2048 14.71s 2339629 147.11ms 51µs 12ms 159065
4096 14.47s 2072039 411.51ms 163µs 28ms 143095

License

This project is licensed under the MIT license.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in Lynn_tcp by you, shall be licensed as MIT, without any additional terms or conditions.