From b3e0ef5104dc5b2d6d2e7232f4801e296144261a Mon Sep 17 00:00:00 2001 From: TheRedApricot Date: Mon, 14 Jul 2025 16:50:37 +0800 Subject: [PATCH] Remove redundant modules --- Client/Assets/Scripts/Network.meta | 9 +- .../Assets/Scripts/Network/UnityTcpClient.cs | 152 ------------------ .../Scripts/Network/UnityTcpClient.cs.meta | 2 - .../Scripts/Test/NetworkConnectionTest.cs | 26 --- .../Test/NetworkConnectionTest.cs.meta | 2 - 5 files changed, 2 insertions(+), 189 deletions(-) delete mode 100644 Client/Assets/Scripts/Network/UnityTcpClient.cs delete mode 100644 Client/Assets/Scripts/Network/UnityTcpClient.cs.meta delete mode 100644 Client/Assets/Scripts/Test/NetworkConnectionTest.cs delete mode 100644 Client/Assets/Scripts/Test/NetworkConnectionTest.cs.meta diff --git a/Client/Assets/Scripts/Network.meta b/Client/Assets/Scripts/Network.meta index ec7e1d2..386e0e1 100644 --- a/Client/Assets/Scripts/Network.meta +++ b/Client/Assets/Scripts/Network.meta @@ -1,8 +1,3 @@ fileFormatVersion: 2 -guid: 36bb272d9a70ff347965919811a887de -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: +guid: 178450a3675642d799764a426bcb236f +timeCreated: 1752480692 \ No newline at end of file diff --git a/Client/Assets/Scripts/Network/UnityTcpClient.cs b/Client/Assets/Scripts/Network/UnityTcpClient.cs deleted file mode 100644 index be2977b..0000000 --- a/Client/Assets/Scripts/Network/UnityTcpClient.cs +++ /dev/null @@ -1,152 +0,0 @@ -using System; -using System.Net.Sockets; -using System.Text; -using System.Threading.Tasks; -using UnityEngine; -using Utils; - -namespace Network -{ - public class UnityTcpClient : Singleton - { - private TcpClient _tcpClient; - public bool IsConnected => _tcpClient?.Connected ?? false; - - /// - /// 尝试连接到指定的地址和端口 - /// - /// 服务器地址 - /// 端口号 - /// 连接是否成功 - public async Task Connect(string address, int port) - { - if (IsConnected) - { - Debug.LogWarning("Already connected to a server."); - return true; - } - - try - { - // 创建一个新的 TcpClient 实例 - _tcpClient = new TcpClient(); - - // 尝试连接到指定的地址和端口 - await _tcpClient.ConnectAsync(address, port); - - // 如果连接成功 - if (IsConnected) - { - Debug.Log($"Successfully connected to server at {address}:{port}"); - return true; - } - } - catch (SocketException ex) - { - Debug.LogError($"Failed to connect to server at {address}:{port}. Error: {ex.Message}"); - } - catch (Exception ex) - { - Debug.LogError($"An unexpected error occurred: {ex.Message}"); - } - - // 如果发生任何错误或连接失败 - Disconnect(); - return false; - } - - /// - /// 断开与服务器的连接 - /// - public void Disconnect() - { - if (!IsConnected) - { - Debug.LogWarning("Not currently connected to any server."); - return; - } - - try - { - // 关闭 TcpClient 连接 - _tcpClient?.Close(); - - Debug.Log("Disconnected from the server."); - } - catch (Exception ex) - { - Debug.LogError($"Error while disconnecting: {ex.Message}"); - } - } - - /// - /// 发送数据到服务器 - /// - /// 要发送的消息 - public async Task Send(string message) - { - if (!IsConnected) - { - Debug.LogError("Cannot send data. Not connected to any server."); - return; - } - - try - { - // 获取网络流 - var stream = _tcpClient.GetStream(); - - // 将消息转换为字节数组 - var data = Encoding.UTF8.GetBytes(message); - - // 发送数据 - await stream.WriteAsync(data, 0, data.Length); - - Debug.Log($"Sent message to server: {message}"); - } - catch (Exception ex) - { - Debug.LogError($"Error while sending: {ex.Message}"); - } - } - - /// - /// 接收来自服务器的数据 - /// - /// 接收到的消息 - public async Task Receive() - { - if (!IsConnected) - { - Debug.LogError("Cannot receive data. Not connected to any server."); - return null; - } - - try - { - // 获取网络流 - var stream = _tcpClient.GetStream(); - - // 检查是否有可用数据 - if (stream.DataAvailable) - { - // 读取数据 - var buffer = new byte[1024]; - var bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length); - - // 将字节数组转换为字符串 - var message = Encoding.UTF8.GetString(buffer, 0, bytesRead); - - Debug.Log($"Received message from server: {message}"); - return message; - } - } - catch (Exception ex) - { - Debug.LogError($"Error while receiving: {ex.Message}"); - } - - return null; - } - } -} \ No newline at end of file diff --git a/Client/Assets/Scripts/Network/UnityTcpClient.cs.meta b/Client/Assets/Scripts/Network/UnityTcpClient.cs.meta deleted file mode 100644 index a153c7c..0000000 --- a/Client/Assets/Scripts/Network/UnityTcpClient.cs.meta +++ /dev/null @@ -1,2 +0,0 @@ -fileFormatVersion: 2 -guid: 3cfed6461b8d6714bbc44fdac509c880 \ No newline at end of file diff --git a/Client/Assets/Scripts/Test/NetworkConnectionTest.cs b/Client/Assets/Scripts/Test/NetworkConnectionTest.cs deleted file mode 100644 index 3ea60b8..0000000 --- a/Client/Assets/Scripts/Test/NetworkConnectionTest.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System.Threading.Tasks; -using Network; -using UnityEngine; - -namespace Test -{ - public class NetworkConnectionTest : MonoBehaviour - { - private async void Start() - { - await BasicTest(); - } - - private static async Task BasicTest() - { - var result = await UnityTcpClient.Instance.Connect("127.0.0.1", 12345); - if (result) Debug.Log("Connected to server!"); - else Debug.LogError("Failed to connect to server!"); - - string buffer = null; - while (buffer is null) buffer = await UnityTcpClient.Instance.Receive(); - - Debug.Log($"Received contents: {buffer}"); - } - } -} \ No newline at end of file diff --git a/Client/Assets/Scripts/Test/NetworkConnectionTest.cs.meta b/Client/Assets/Scripts/Test/NetworkConnectionTest.cs.meta deleted file mode 100644 index e6418ad..0000000 --- a/Client/Assets/Scripts/Test/NetworkConnectionTest.cs.meta +++ /dev/null @@ -1,2 +0,0 @@ -fileFormatVersion: 2 -guid: 289a7efc822fe7347adfbf218522e8a2 \ No newline at end of file