summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2025-09-17 12:44:16 +0800
committer魏曹先生 <1992414357@qq.com>2025-09-17 12:44:16 +0800
commitbdaab12e6ece9e1c1a0f444ccd890ba47b49789f (patch)
tree45cea7cd4a090944884191f2669d66beda23de89
parent145eec4d85c634ed582e1d082b846bc70c6dea33 (diff)
Update TcpServerTarget
-rw-r--r--crates/utils/tcp_connection/src/target.rs57
-rw-r--r--crates/utils/tcp_connection/src/target_connection.rs26
2 files changed, 38 insertions, 45 deletions
diff --git a/crates/utils/tcp_connection/src/target.rs b/crates/utils/tcp_connection/src/target.rs
index 2b82e7e..0172d62 100644
--- a/crates/utils/tcp_connection/src/target.rs
+++ b/crates/utils/tcp_connection/src/target.rs
@@ -1,8 +1,11 @@
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;
+use std::{
+ fmt::{Display, Formatter},
+ marker::PhantomData,
+ net::{AddrParseError, IpAddr, Ipv4Addr, SocketAddr},
+ str::FromStr,
+};
use tokio::net::lookup_host;
const DEFAULT_PORT: u16 = 8080;
@@ -13,12 +16,6 @@ where
Client: ClientHandle<Server>,
Server: ServerHandle<Client>,
{
- /// Client Handle
- client_handle: Option<Client>,
-
- /// Server Handle
- server_handle: Option<Server>,
-
/// Client Config
client_cfg: Option<ClientTargetConfig>,
@@ -30,6 +27,12 @@ where
/// Bind addr
bind_addr: IpAddr,
+
+ /// Client Phantom Data
+ _client: PhantomData<Client>,
+
+ /// Server Phantom Data
+ _server: PhantomData<Server>,
}
impl<Client, Server> Default for TcpServerTarget<Client, Server>
@@ -39,12 +42,12 @@ where
{
fn default() -> Self {
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)),
+ _client: PhantomData,
+ _server: PhantomData,
}
}
}
@@ -116,18 +119,6 @@ where
}
}
- /// 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);
@@ -140,16 +131,6 @@ where
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);
@@ -160,16 +141,6 @@ where
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()
diff --git a/crates/utils/tcp_connection/src/target_connection.rs b/crates/utils/tcp_connection/src/target_connection.rs
index 1e25edd..b03093c 100644
--- a/crates/utils/tcp_connection/src/target_connection.rs
+++ b/crates/utils/tcp_connection/src/target_connection.rs
@@ -1,4 +1,7 @@
-use tokio::{net::TcpListener, spawn};
+use tokio::{
+ net::{TcpListener, TcpSocket},
+ spawn,
+};
use crate::{
error::TcpTargetError,
@@ -13,10 +16,28 @@ where
Client: ClientHandle<Server>,
Server: ServerHandle<Client>,
{
+ /// Attempts to establish a connection to the TCP server.
+ /// This function initiates a connection to the server address specified in the target configuration.
+ /// This is a Block operation.
pub async fn connect(&self) -> Result<(), TcpTargetError> {
+ let addr = self.get_addr();
+ let Ok(socket) = TcpSocket::new_v4() else {
+ return Err(TcpTargetError::from("Create tcp socket failed!"));
+ };
+ let stream = match socket.connect(addr.clone()).await {
+ Ok(stream) => stream,
+ Err(e) => {
+ let err = format!("Connect to `{}` failed: {}", addr, e);
+ return Err(TcpTargetError::from(err));
+ }
+ };
+ let instance = ConnectionInstance::from(stream);
+ Client::process(instance).await;
Ok(())
}
+ /// Attempts to establish a connection to the TCP server.
+ /// This function initiates a connection to the server address specified in the target configuration.
pub async fn listen(&self) -> Result<(), TcpTargetError> {
let addr = self.get_addr();
let listener = match TcpListener::bind(addr.clone()).await {
@@ -33,6 +54,7 @@ where
};
if cfg.is_once() {
+ // Process once (Blocked)
let (stream, _) = match listener.accept().await {
Ok(result) => result,
Err(e) => {
@@ -44,6 +66,7 @@ where
Server::process(instance).await;
} else {
loop {
+ // Process multiple times (Concurrent)
let (stream, _) = match listener.accept().await {
Ok(result) => result,
Err(e) => {
@@ -57,7 +80,6 @@ where
});
}
}
-
Ok(())
}
}