Skip to content

Commit

Permalink
Merge pull request #1 from cherish-ltt/v1.0.0-rc.2-dev
Browse files Browse the repository at this point in the history
V1.0.0 rc.2 dev
  • Loading branch information
cherish-ltt authored Jan 19, 2025
2 parents 7f1b715 + 535ea62 commit ba8a3d8
Show file tree
Hide file tree
Showing 15 changed files with 217 additions and 125 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "lynn_tcp"
version = "1.0.0"
edition = "1.83.0"
version = "1.0.0-rc.2"
edition = "2021"
license = "MIT"

[dependencies]
Expand Down
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@

`Lynn_tcp` is a TCP service based on `Tokio`, and this project library is designed for easy use in different projects based on lynn_tcp. It may not be suitable for your own project.Anyway, you can use it for free, provided that you have a clear understanding of some of the customized attributes inside.

`Lynn_tcp`是一个基于`Tokio`的tcp服务,这个项目库是为了在基于`Lynn_tcp`的不同项目中易于使用而设计的。它可能不适合你自己的项目。不管怎样,您就可以免费使用它,前提是您要清楚地了解其中的一些定制化属性(如一些最大链接数等)。
`Lynn_tcp`是一个基于`Tokio`的tcp服务,这个项目库是为了在基于`Lynn_tcp`的不同项目中易于使用而设计的。它可能不适合你自己的项目。不管怎样,您可以免费使用它,前提是您要清楚地了解其中的一些定制化属性(如一些最大链接数等)。

### Simple Use|如何使用

#### Dependencies|依赖

```rust
[dependencies]
lynn_tcp = { git = "https://github.com/cherish-ltt/lynn_tcp.git", branch = "main" }
```

#### Server|服务

```rust
use lynn_tcp::server::{HandlerResult, InputBufVO, LynnConfigBuilder, LynnServer};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let _ = LynnServer::new().await.add_router(1, my_service).start().await;
Expand All @@ -24,3 +30,30 @@ pub fn my_service(input_buf_vo: &mut InputBufVO) -> HandlerResult {
}
```

#### Server with config|带自定义配置的服务

```rust
use lynn_tcp::server::{HandlerResult, InputBufVO, LynnConfigBuilder, LynnServer};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let _ = LynnServer::new_with_config(
LynnConfigBuilder::new()
.with_server_ipv4("0.0.0.0:9177")
.with_server_max_connections(Some(&200))
.with_server_max_threadpool_size(&10)
.build(),
)
.await
.add_router(1, my_service)
.start()
.await;
Ok(())
}

pub fn my_service(input_buf_vo: &mut InputBufVO) -> HandlerResult {
println!("service read from :{}", input_buf_vo.get_input_addr());
HandlerResult::new_without_send()
}
```

124 changes: 68 additions & 56 deletions src/app/lynn_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,30 @@ use crate::const_config::{
///
/// This struct contains various configuration parameters for the server,
/// such as the IP address, channel size, maximum connections, thread pool size, etc.
#[derive(Clone)]
pub(crate) struct LynnConfig {
//#[derive(Clone)]
pub struct LynnConfig<'a> {
// The IPv4 address of the server.
server_ipv4: String,
server_ipv4: &'a str,
// The size of a single channel.
server_single_channel_size: usize,
server_single_channel_size: &'a usize,
// The maximum number of connections for the server.
server_max_connections: Option<usize>,
server_max_connections: Option<&'a usize>,
// The maximum number of threads for the server.
server_max_threadpool_size: usize,
server_max_threadpool_size: &'a usize,
// The maximum number of bytes the server can receive.
server_max_receive_bytes_size: usize,
server_max_receive_bytes_size: &'a usize,
// The permit size for a single process.
server_single_processs_permit: usize,
server_single_processs_permit: &'a usize,
// The interval for checking heartbeats.
server_check_heart_interval: u64,
server_check_heart_interval: &'a u64,
// The timeout time for checking heartbeats.
server_check_heart_timeout_time: u64,
server_check_heart_timeout_time: &'a u64,
}

/// Implementation for LynnConfig, providing methods to create and get the configuration.
///
/// This implementation includes a constructor for the default configuration and methods to get the configuration parameters.
impl LynnConfig {
impl<'a> LynnConfig<'a> {
/// Creates a new LynnConfig instance with the given parameters.
///
/// # Parameters
Expand All @@ -49,14 +49,14 @@ impl LynnConfig {
///
/// A new LynnConfig instance.
fn new(
server_ipv4: String,
server_single_channel_size: usize,
server_max_connections: Option<usize>,
server_max_threadpool_size: usize,
server_max_receive_bytes_size: usize,
server_single_processs_permit: usize,
server_check_heart_interval: u64,
server_check_heart_timeout_time: u64,
server_ipv4: &'a str,
server_single_channel_size: &'a usize,
server_max_connections: Option<&'a usize>,
server_max_threadpool_size: &'a usize,
server_max_receive_bytes_size: &'a usize,
server_single_processs_permit: &'a usize,
server_check_heart_interval: &'a u64,
server_check_heart_timeout_time: &'a u64,
) -> Self {
Self {
server_ipv4,
Expand All @@ -77,14 +77,14 @@ impl LynnConfig {
/// A default LynnConfig instance.
pub(crate) fn default() -> Self {
Self {
server_ipv4: DEFAULT_IPV4.to_string(),
server_max_connections: Some(DEFAULT_MAX_CONNECTIONS),
server_max_threadpool_size: DEFAULT_MAX_THREADPOOL_SIZE,
server_max_receive_bytes_size: DEFAULT_MAX_RECEIVE_BYTES_SIZE,
server_single_channel_size: DEFAULT_CHANNEL_SIZE,
server_single_processs_permit: DEFAULT_PROCESS_PERMIT_SIZE,
server_check_heart_interval: DEFAULT_CHECK_HEART_INTERVAL,
server_check_heart_timeout_time: DEFAULT_CHECK_HEART_TIMEOUT_TIME,
server_ipv4: DEFAULT_IPV4,
server_max_connections: Some(&DEFAULT_MAX_CONNECTIONS),
server_max_threadpool_size: &DEFAULT_MAX_THREADPOOL_SIZE,
server_max_receive_bytes_size: &DEFAULT_MAX_RECEIVE_BYTES_SIZE,
server_single_channel_size: &DEFAULT_CHANNEL_SIZE,
server_single_processs_permit: &DEFAULT_PROCESS_PERMIT_SIZE,
server_check_heart_interval: &DEFAULT_CHECK_HEART_INTERVAL,
server_check_heart_timeout_time: &DEFAULT_CHECK_HEART_TIMEOUT_TIME,
}
}

Expand All @@ -93,86 +93,86 @@ impl LynnConfig {
/// # Returns
///
/// The IPv4 address of the server.
pub(crate) fn get_server_ipv4(&self) -> String {
self.server_ipv4.clone()
pub(crate) fn get_server_ipv4(&self) -> &str {
&self.server_ipv4
}

/// Gets the size of a single channel.
///
/// # Returns
///
/// The size of a single channel.
pub(crate) fn get_server_single_channel_size(&self) -> usize {
self.server_single_channel_size.clone()
pub(crate) fn get_server_single_channel_size(&self) -> &usize {
&self.server_single_channel_size
}

/// Gets the permit size for a single process.
///
/// # Returns
///
/// The permit size for a single process.
pub(crate) fn get_server_single_processs_permit(&self) -> usize {
self.server_single_processs_permit.clone()
pub(crate) fn get_server_single_processs_permit(&self) -> &usize {
self.server_single_processs_permit
}

/// Gets the interval for checking heartbeats.
///
/// # Returns
///
/// The interval for checking heartbeats.
pub(crate) fn get_server_check_heart_interval(&self) -> u64 {
self.server_check_heart_interval.clone()
pub(crate) fn get_server_check_heart_interval(&self) -> &u64 {
self.server_check_heart_interval
}

/// Gets the timeout time for checking heartbeats.
///
/// # Returns
///
/// The timeout time for checking heartbeats.
pub(crate) fn get_server_check_heart_timeout_time(&self) -> u64 {
self.server_check_heart_timeout_time.clone()
pub(crate) fn get_server_check_heart_timeout_time(&self) -> &u64 {
self.server_check_heart_timeout_time
}

/// Gets the maximum number of connections for the server.
///
/// # Returns
///
/// The maximum number of connections for the server.
pub(crate) fn get_server_max_connections(&self) -> Option<usize> {
self.server_max_connections.clone()
pub(crate) fn get_server_max_connections(&self) -> Option<&usize> {
self.server_max_connections
}

/// Gets the maximum number of threads for the server.
///
/// # Returns
///
/// The maximum number of threads for the server.
pub(crate) fn get_server_max_threadpool_size(&self) -> usize {
self.server_max_threadpool_size.clone()
pub(crate) fn get_server_max_threadpool_size(&self) -> &usize {
self.server_max_threadpool_size
}

/// Gets the maximum number of bytes the server can receive.
///
/// # Returns
///
/// The maximum number of bytes the server can receive.
pub(crate) fn get_server_max_receive_bytes_size(&self) -> usize {
self.server_max_receive_bytes_size.clone()
pub(crate) fn get_server_max_receive_bytes_size(&self) -> &usize {
self.server_max_receive_bytes_size
}
}

/// Builder for constructing a LynnConfig instance.
///
/// This builder provides a series of methods to set the various parameters of LynnConfig
/// and finally builds a LynnConfig instance.
pub struct LynnConfigBuilder {
lynn_config: LynnConfig,
pub struct LynnConfigBuilder<'a> {
pub lynn_config: LynnConfig<'a>,
}

/// Implementation for LynnConfigBuilder, providing methods to set the configuration parameters.
///
/// This implementation includes a constructor for the default configuration and methods to set the configuration parameters.
impl LynnConfigBuilder {
impl<'a> LynnConfigBuilder<'a> {
/// Creates a new LynnConfigBuilder instance.
///
/// # Returns
Expand All @@ -193,7 +193,7 @@ impl LynnConfigBuilder {
/// # Returns
///
/// The modified LynnConfigBuilder instance.
pub fn with_server_ipv4(mut self, server_ipv4: String) -> Self {
pub fn with_server_ipv4(mut self, server_ipv4: &'a str) -> Self {
self.lynn_config.server_ipv4 = server_ipv4;
self
}
Expand All @@ -207,7 +207,10 @@ impl LynnConfigBuilder {
/// # Returns
///
/// The modified LynnConfigBuilder instance.
pub fn with_server_single_channel_size(mut self, server_single_channel_size: usize) -> Self {
pub fn with_server_single_channel_size(
mut self,
server_single_channel_size: &'a usize,
) -> Self {
self.lynn_config.server_single_channel_size = server_single_channel_size;
self
}
Expand All @@ -223,7 +226,7 @@ impl LynnConfigBuilder {
/// The modified LynnConfigBuilder instance.
pub fn with_server_single_processs_permit(
mut self,
server_single_processs_permit: usize,
server_single_processs_permit: &'a usize,
) -> Self {
self.lynn_config.server_single_processs_permit = server_single_processs_permit;
self
Expand All @@ -237,7 +240,10 @@ impl LynnConfigBuilder {
/// # Returns
///
/// The modified LynnConfigBuilder instance.
pub fn with_server_check_heart_interval(mut self, server_check_heart_interval: u64) -> Self {
pub fn with_server_check_heart_interval(
mut self,
server_check_heart_interval: &'a u64,
) -> Self {
self.lynn_config.server_check_heart_interval = server_check_heart_interval;
self
}
Expand All @@ -253,7 +259,7 @@ impl LynnConfigBuilder {
/// The modified LynnConfigBuilder instance.
pub fn with_server_check_heart_timeout_time(
mut self,
server_check_heart_timeout_time: u64,
server_check_heart_timeout_time: &'a u64,
) -> Self {
self.lynn_config.server_check_heart_timeout_time = server_check_heart_timeout_time;
self
Expand All @@ -268,7 +274,10 @@ impl LynnConfigBuilder {
/// # Returns
///
/// The modified LynnConfigBuilder instance.
pub fn with_server_max_connections(mut self, server_max_connections: Option<usize>) -> Self {
pub fn with_server_max_connections(
mut self,
server_max_connections: Option<&'a usize>,
) -> Self {
self.lynn_config.server_max_connections = server_max_connections;
self
}
Expand All @@ -282,7 +291,10 @@ impl LynnConfigBuilder {
/// # Returns
///
/// The modified LynnConfigBuilder instance.
pub fn with_server_max_threadpool_size(mut self, server_max_threadpool_size: usize) -> Self {
pub fn with_server_max_threadpool_size(
mut self,
server_max_threadpool_size: &'a usize,
) -> Self {
self.lynn_config.server_max_threadpool_size = server_max_threadpool_size;
self
}
Expand All @@ -298,7 +310,7 @@ impl LynnConfigBuilder {
/// The modified LynnConfigBuilder instance.
pub fn with_server_max_receive_bytes_size(
mut self,
server_max_receive_bytes_size: usize,
server_max_receive_bytes_size: &'a usize,
) -> Self {
self.lynn_config.server_max_receive_bytes_size = server_max_receive_bytes_size;
self
Expand All @@ -309,7 +321,7 @@ impl LynnConfigBuilder {
/// # Returns
///
/// The final LynnConfig instance.
pub fn build(self) -> LynnConfig {
pub fn build(self) -> LynnConfig<'a> {
self.lynn_config
}
}
}
2 changes: 1 addition & 1 deletion src/app/lynn_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,4 @@ impl Drop for LynnUser {
}
self.thread = None;
}
}
}
Loading

0 comments on commit ba8a3d8

Please sign in to comment.