summaryrefslogtreecommitdiff
path: root/crates/utils/tcp_connection/src
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2025-09-14 13:12:27 +0800
committer魏曹先生 <1992414357@qq.com>2025-09-14 13:12:27 +0800
commit0ad594277e61e9fb41b2e470c34cff7534d6c780 (patch)
tree3d14c3ad518c273105c89e5c3f39e38f4cfa45c2 /crates/utils/tcp_connection/src
parentb8844ab55a6d622370be8ce41387ff9d9897302e (diff)
Fixed codes by Zed
Diffstat (limited to 'crates/utils/tcp_connection/src')
-rw-r--r--crates/utils/tcp_connection/src/handle.rs3
-rw-r--r--crates/utils/tcp_connection/src/lib.rs2
-rw-r--r--crates/utils/tcp_connection/src/target.rs61
3 files changed, 35 insertions, 31 deletions
diff --git a/crates/utils/tcp_connection/src/handle.rs b/crates/utils/tcp_connection/src/handle.rs
index de7815d..ab3a5ff 100644
--- a/crates/utils/tcp_connection/src/handle.rs
+++ b/crates/utils/tcp_connection/src/handle.rs
@@ -1,12 +1,9 @@
use tokio::net::TcpStream;
pub trait ClientHandle<RequestServer> {
-
fn process(stream: TcpStream);
}
pub trait ServerHandle<RequestClient> {
-
fn process(stream: TcpStream);
}
-
diff --git a/crates/utils/tcp_connection/src/lib.rs b/crates/utils/tcp_connection/src/lib.rs
index 4c72735..e38fea1 100644
--- a/crates/utils/tcp_connection/src/lib.rs
+++ b/crates/utils/tcp_connection/src/lib.rs
@@ -1,4 +1,4 @@
#[allow(dead_code)]
pub mod target;
-pub mod handle; \ No newline at end of file
+pub mod handle;
diff --git a/crates/utils/tcp_connection/src/target.rs b/crates/utils/tcp_connection/src/target.rs
index 8fc14ef..84c9029 100644
--- a/crates/utils/tcp_connection/src/target.rs
+++ b/crates/utils/tcp_connection/src/target.rs
@@ -1,16 +1,17 @@
+use crate::handle::{ClientHandle, ServerHandle};
use std::fmt::{Display, Formatter};
use std::net::{AddrParseError, IpAddr, Ipv4Addr, SocketAddr};
use std::str::FromStr;
use tokio::net::lookup_host;
-use crate::handle::{ClientHandle, ServerHandle};
const DEFAULT_PORT: u16 = 8080;
#[derive(Debug, Eq, PartialEq)]
pub struct TcpServerTarget<Client, Server>
-where Client: ClientHandle<Server>,
- Server: ServerHandle<Client> {
-
+where
+ Client: ClientHandle<Server>,
+ Server: ServerHandle<Client>,
+{
/// Client Handle
client_handle: Option<Client>,
@@ -25,8 +26,10 @@ where Client: ClientHandle<Server>,
}
impl<Client, Server> Default for TcpServerTarget<Client, Server>
-where Client: ClientHandle<Server>,
- Server: ServerHandle<Client> {
+where
+ Client: ClientHandle<Server>,
+ Server: ServerHandle<Client>,
+{
fn default() -> Self {
Self {
client_handle: None,
@@ -38,23 +41,25 @@ where Client: ClientHandle<Server>,
}
impl<Client, Server> From<SocketAddr> for TcpServerTarget<Client, Server>
-where Client: ClientHandle<Server>,
- Server: ServerHandle<Client> {
-
+where
+ Client: ClientHandle<Server>,
+ Server: ServerHandle<Client>,
+{
/// Convert SocketAddr to TcpServerTarget
fn from(value: SocketAddr) -> Self {
Self {
port: value.port(),
bind_addr: value.ip(),
- .. Self::default()
+ ..Self::default()
}
}
}
impl<Client, Server> From<TcpServerTarget<Client, Server>> for SocketAddr
-where Client: ClientHandle<Server>,
- Server: ServerHandle<Client> {
-
+where
+ Client: ClientHandle<Server>,
+ Server: ServerHandle<Client>,
+{
/// Convert TcpServerTarget to SocketAddr
fn from(val: TcpServerTarget<Client, Server>) -> Self {
SocketAddr::new(val.bind_addr, val.port)
@@ -62,23 +67,26 @@ where Client: ClientHandle<Server>,
}
impl<Client, Server> Display for TcpServerTarget<Client, Server>
-where Client: ClientHandle<Server>,
- Server: ServerHandle<Client> {
+where
+ Client: ClientHandle<Server>,
+ Server: ServerHandle<Client>,
+{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{}", self.bind_addr, self.port)
}
}
impl<Client, Server> TcpServerTarget<Client, Server>
-where Client: ClientHandle<Server>,
- Server: ServerHandle<Client> {
-
+where
+ Client: ClientHandle<Server>,
+ Server: ServerHandle<Client>,
+{
/// Create target by address
pub fn from_addr(addr: impl Into<IpAddr>, port: impl Into<u16>) -> Self {
Self {
port: port.into(),
bind_addr: addr.into(),
- .. Self::default()
+ ..Self::default()
}
}
@@ -86,12 +94,8 @@ where Client: ClientHandle<Server>,
pub fn from_str<'a>(addr_str: impl Into<&'a str>) -> Result<Self, AddrParseError> {
let socket_addr = SocketAddr::from_str(addr_str.into());
match socket_addr {
- Ok(socket_addr) => {
- Ok(Self::from_addr(socket_addr.ip(), socket_addr.port()))
- }
- Err(err) => {
- Err(err)
- }
+ Ok(socket_addr) => Ok(Self::from_addr(socket_addr.ip(), socket_addr.port())),
+ Err(err) => Err(err),
}
}
@@ -117,7 +121,10 @@ async fn domain_to_addr<'a>(domain: impl Into<&'a str>) -> Result<SocketAddr, st
}
if let Ok(_v6_addr) = domain.parse::<std::net::Ipv6Addr>() {
- return Ok(SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), default_port));
+ return Ok(SocketAddr::new(
+ IpAddr::V4(Ipv4Addr::LOCALHOST),
+ default_port,
+ ));
}
let (host, port_str) = if let Some((host, port)) = domain.rsplit_once(':') {
@@ -138,4 +145,4 @@ async fn domain_to_addr<'a>(domain: impl Into<&'a str>) -> Result<SocketAddr, st
}
Ok(SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), port))
-} \ No newline at end of file
+}