summaryrefslogtreecommitdiff
path: root/crates/utils/tcp_connection/src/target_connection.rs
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2025-09-26 17:38:54 +0800
committer魏曹先生 <1992414357@qq.com>2025-09-26 17:39:36 +0800
commit4951e2e98bab7a2996893939ee77f0279145b556 (patch)
tree78138b8564d132edba20226a7522532746bfb79e /crates/utils/tcp_connection/src/target_connection.rs
parente8160eda1b68a42b8d861bbec5e9c1dc555ea783 (diff)
refactor: downgrade tcp_connection functionality to test utilities
- Remove handle, target, target_configure, target_connection modules from main library - Create test_utils module in test project to contain temporary connection functionality - Update import paths in test files - Keep instance and error modules as core functionality - Adjust vcs_test configurations to adapt to new test structure
Diffstat (limited to 'crates/utils/tcp_connection/src/target_connection.rs')
-rw-r--r--crates/utils/tcp_connection/src/target_connection.rs90
1 files changed, 0 insertions, 90 deletions
diff --git a/crates/utils/tcp_connection/src/target_connection.rs b/crates/utils/tcp_connection/src/target_connection.rs
deleted file mode 100644
index 0462f7b..0000000
--- a/crates/utils/tcp_connection/src/target_connection.rs
+++ /dev/null
@@ -1,90 +0,0 @@
-use tokio::{
- net::{TcpListener, TcpSocket},
- spawn,
-};
-
-use crate::{
- error::TcpTargetError,
- handle::{ClientHandle, ServerHandle},
- instance::ConnectionInstance,
- target::TcpServerTarget,
- target_configure::ServerTargetConfig,
-};
-
-impl<Client, Server> TcpServerTarget<Client, Server>
-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).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).await {
- Ok(listener) => listener,
- Err(_) => {
- let err = format!("Bind to `{}` failed", addr);
- return Err(TcpTargetError::from(err));
- }
- };
-
- let cfg: ServerTargetConfig = match self.get_server_cfg() {
- Some(cfg) => *cfg,
- None => ServerTargetConfig::default(),
- };
-
- if cfg.is_once() {
- // Process once (Blocked)
- let (stream, _) = match listener.accept().await {
- Ok(result) => result,
- Err(e) => {
- let err = format!("Accept connection failed: {}", e);
- return Err(TcpTargetError::from(err));
- }
- };
- let instance = ConnectionInstance::from(stream);
- Server::process(instance).await;
- } else {
- loop {
- // Process multiple times (Concurrent)
- let (stream, _) = match listener.accept().await {
- Ok(result) => result,
- Err(e) => {
- let err = format!("Accept connection failed: {}", e);
- return Err(TcpTargetError::from(err));
- }
- };
- let instance = ConnectionInstance::from(stream);
- spawn(async move {
- Server::process(instance).await;
- });
- }
- }
- Ok(())
- }
-}