diff options
| author | 魏曹先生 <1992414357@qq.com> | 2025-09-14 22:42:20 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2025-09-14 22:42:20 +0800 |
| commit | d79f1ffeecc7539b9f13bf1edbc3f1a9e82448b2 (patch) | |
| tree | 786eb17d441f3ea4c84960e57c66457a4ac23694 /crates/utils/tcp_connection | |
| parent | bd923afe53de552c1f69e0db5a4490c73a294b91 (diff) | |
Fixed codes by Zed
Diffstat (limited to 'crates/utils/tcp_connection')
5 files changed, 111 insertions, 9 deletions
diff --git a/crates/utils/tcp_connection/src/handle.rs b/crates/utils/tcp_connection/src/handle.rs index ab3a5ff..706d386 100644 --- a/crates/utils/tcp_connection/src/handle.rs +++ b/crates/utils/tcp_connection/src/handle.rs @@ -1,9 +1,10 @@ -use tokio::net::TcpStream; +use crate::instance::ConnectionInstance; +use std::future::Future; pub trait ClientHandle<RequestServer> { - fn process(stream: TcpStream); + fn process(instance: ConnectionInstance) -> impl Future<Output = ()> + Send + Sync; } pub trait ServerHandle<RequestClient> { - fn process(stream: TcpStream); + fn process(instance: ConnectionInstance) -> impl Future<Output = ()> + Send + Sync; } diff --git a/crates/utils/tcp_connection/src/lib.rs b/crates/utils/tcp_connection/src/lib.rs index e38fea1..a3d5c0a 100644 --- a/crates/utils/tcp_connection/src/lib.rs +++ b/crates/utils/tcp_connection/src/lib.rs @@ -1,4 +1,15 @@ #[allow(dead_code)] pub mod target; +#[allow(dead_code)] +pub mod target_configure; +#[allow(dead_code)] +pub mod target_connection; + +#[allow(dead_code)] +pub mod instance; +#[allow(dead_code)] pub mod handle; + +#[allow(dead_code)] +pub mod error; diff --git a/crates/utils/tcp_connection/src/target.rs b/crates/utils/tcp_connection/src/target.rs index 84c9029..2b82e7e 100644 --- a/crates/utils/tcp_connection/src/target.rs +++ b/crates/utils/tcp_connection/src/target.rs @@ -1,4 +1,5 @@ use crate::handle::{ClientHandle, ServerHandle}; +use crate::target_configure::{ClientTargetConfig, ServerTargetConfig}; use std::fmt::{Display, Formatter}; use std::net::{AddrParseError, IpAddr, Ipv4Addr, SocketAddr}; use std::str::FromStr; @@ -6,7 +7,7 @@ use tokio::net::lookup_host; const DEFAULT_PORT: u16 = 8080; -#[derive(Debug, Eq, PartialEq)] +#[derive(Debug)] pub struct TcpServerTarget<Client, Server> where Client: ClientHandle<Server>, @@ -18,6 +19,12 @@ where /// Server Handle server_handle: Option<Server>, + /// Client Config + client_cfg: Option<ClientTargetConfig>, + + /// Server Config + server_cfg: Option<ServerTargetConfig>, + /// Server port port: u16, @@ -34,6 +41,8 @@ where Self { client_handle: None, server_handle: None, + client_cfg: None, + server_cfg: None, port: DEFAULT_PORT, bind_addr: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), } @@ -106,6 +115,75 @@ where Err(e) => Err(e), } } + + /// Set client handle + pub fn client(mut self, handle: Client) -> Self { + self.client_handle = Some(handle); + self + } + + /// Set server handle + pub fn server(mut self, handle: Server) -> Self { + self.server_handle = Some(handle); + self + } + + /// Set client config + pub fn client_cfg(mut self, config: ClientTargetConfig) -> Self { + self.client_cfg = Some(config); + self + } + + /// Set server config + pub fn server_cfg(mut self, config: ServerTargetConfig) -> Self { + self.server_cfg = Some(config); + self + } + + /// Add client handle + pub fn add_client_handle(&mut self, client: Client) { + self.client_handle = Some(client); + } + + /// Add server handle + pub fn add_server_handle(&mut self, server: Server) { + self.server_handle = Some(server); + } + + /// Add client config + pub fn add_client_cfg(&mut self, config: ClientTargetConfig) { + self.client_cfg = Some(config); + } + + /// Add server config + pub fn add_server_cfg(&mut self, config: ServerTargetConfig) { + self.server_cfg = Some(config); + } + + /// Get client handle ref + pub fn get_client(&self) -> Option<&Client> { + self.client_handle.as_ref() + } + + /// Get server handle ref + pub fn get_server(&self) -> Option<&Server> { + self.server_handle.as_ref() + } + + /// Get client config ref + pub fn get_client_cfg(&self) -> Option<&ClientTargetConfig> { + self.client_cfg.as_ref() + } + + /// Get server config ref + pub fn get_server_cfg(&self) -> Option<&ServerTargetConfig> { + self.server_cfg.as_ref() + } + + /// Get SocketAddr of TcpServerTarget + pub fn get_addr(&self) -> SocketAddr { + SocketAddr::new(self.bind_addr, self.port) + } } /// Parse Domain Name to IpAddr via DNS diff --git a/crates/utils/tcp_connection/tcp_connection_test/src/example_handle.rs b/crates/utils/tcp_connection/tcp_connection_test/src/example_handle.rs index cefeef0..2c84bd1 100644 --- a/crates/utils/tcp_connection/tcp_connection_test/src/example_handle.rs +++ b/crates/utils/tcp_connection/tcp_connection_test/src/example_handle.rs @@ -1,14 +1,26 @@ -use tcp_connection::handle::{ClientHandle, ServerHandle}; -use tokio::net::TcpStream; +use tcp_connection::{ + handle::{ClientHandle, ServerHandle}, + instance::ConnectionInstance, +}; pub(crate) struct ExampleClientHandle; impl ClientHandle<ExampleServerHandle> for ExampleClientHandle { - fn process(stream: TcpStream) {} + fn process( + instance: ConnectionInstance, + ) -> impl std::future::Future<Output = ()> + Send + Sync { + let _ = instance; + async {} + } } pub(crate) struct ExampleServerHandle; impl ServerHandle<ExampleClientHandle> for ExampleServerHandle { - fn process(stream: TcpStream) {} + fn process( + instance: ConnectionInstance, + ) -> impl std::future::Future<Output = ()> + Send + Sync { + let _ = instance; + async {} + } } diff --git a/crates/utils/tcp_connection/tcp_connection_test/src/test_tcp_target_build.rs b/crates/utils/tcp_connection/tcp_connection_test/src/test_tcp_target_build.rs index b375671..c108e42 100644 --- a/crates/utils/tcp_connection/tcp_connection_test/src/test_tcp_target_build.rs +++ b/crates/utils/tcp_connection/tcp_connection_test/src/test_tcp_target_build.rs @@ -26,4 +26,4 @@ async fn test_tcp_test_target_build_domain() { // Test into string assert_eq!(target.to_string(), "127.0.0.1:8080"); -} default_port, +} |
