using Cysharp.Threading.Tasks;
|
using System;
|
using UnityEngine;
|
|
/// <summary>
|
/// ClientSocket 适配器 - 将 ClientSocket 适配为 INetworkService 接口
|
/// (不修改已验证的 ClientSocket 代码)
|
/// </summary>
|
public class ClientSocketAdapter : INetworkService
|
{
|
private ClientSocket clientSocket;
|
private NetworkState _state = NetworkState.Disconnected;
|
|
public NetworkState State
|
{
|
get => _state;
|
private set
|
{
|
if (_state != value)
|
{
|
_state = value;
|
OnStateChanged?.Invoke(value);
|
}
|
}
|
}
|
|
/// <summary>
|
/// 是否已连接(适配 ClientSocket.connected)
|
/// </summary>
|
public bool IsConnected => clientSocket != null && clientSocket.connected;
|
|
/// <summary>
|
/// 最后收包时间(适配 ClientSocket.lastPackageTime)
|
/// </summary>
|
public DateTime LastPackageTime => clientSocket?.lastPackageTime ?? DateTime.MinValue;
|
|
public event Action<NetworkState> OnStateChanged;
|
|
/// <summary>
|
/// 构造函数
|
/// </summary>
|
public ClientSocketAdapter(ServerType type)
|
{
|
clientSocket = new ClientSocket(type);
|
Debug.Log($"[ClientSocketAdapter] 初始化 TCP Socket({type})");
|
}
|
|
/// <summary>
|
/// 连接到 TCP 服务器(适配 ClientSocket.Connect)
|
/// </summary>
|
public async UniTask<bool> ConnectAsync(string url)
|
{
|
try
|
{
|
State = NetworkState.Connecting;
|
|
// 解析 URL(支持 "ip:port" 或 "tcp://ip:port")
|
string ip;
|
int port;
|
ParseUrl(url, out ip, out port);
|
|
Debug.Log($"[ClientSocketAdapter] 连接 {ip}:{port}");
|
|
// 使用 UniTask 包装回调
|
var tcs = new UniTaskCompletionSource<bool>();
|
|
clientSocket.Connect(ip, port, (success) =>
|
{
|
if (success)
|
{
|
State = NetworkState.Connected;
|
Debug.Log("[ClientSocketAdapter] 连接成功");
|
}
|
else
|
{
|
State = NetworkState.Disconnected;
|
Debug.LogError("[ClientSocketAdapter] 连接失败");
|
}
|
|
tcs.TrySetResult(success);
|
});
|
|
return await tcs.Task;
|
}
|
catch (Exception ex)
|
{
|
Debug.LogError($"[ClientSocketAdapter] 连接异常: {ex.Message}");
|
State = NetworkState.Disconnected;
|
return false;
|
}
|
}
|
|
/// <summary>
|
/// 断开连接(异步)
|
/// </summary>
|
public async UniTask DisconnectAsync()
|
{
|
if (clientSocket != null)
|
{
|
clientSocket.CloseConnect();
|
State = NetworkState.Disconnected;
|
}
|
await UniTask.CompletedTask;
|
}
|
|
/// <summary>
|
/// 断开连接(同步,适配 ClientSocket.CloseConnect)
|
/// </summary>
|
public void Disconnect()
|
{
|
if (clientSocket != null)
|
{
|
clientSocket.CloseConnect();
|
State = NetworkState.Disconnected;
|
}
|
}
|
|
/// <summary>
|
/// 发送协议包(适配 ClientSocket.SendInfo)
|
/// </summary>
|
public void SendInfo(GameNetPackBasic protocol)
|
{
|
if (!IsConnected)
|
{
|
Debug.LogError("[ClientSocketAdapter] 未连接,无法发送协议包");
|
return;
|
}
|
|
clientSocket.SendInfo(protocol);
|
}
|
|
/// <summary>
|
/// 发送二进制数据(适配 ClientSocket.SendInfo)
|
/// </summary>
|
public void SendInfo(byte[] data)
|
{
|
if (!IsConnected)
|
{
|
Debug.LogError("[ClientSocketAdapter] 未连接,无法发送数据");
|
return;
|
}
|
|
clientSocket.SendInfo(data);
|
}
|
|
/// <summary>
|
/// 发送二进制数据(异步)
|
/// </summary>
|
public async UniTask SendAsync(byte[] data)
|
{
|
SendInfo(data);
|
await UniTask.CompletedTask;
|
}
|
|
/// <summary>
|
/// 获取内部的 ClientSocket(兼容性访问)
|
/// </summary>
|
public ClientSocket GetClientSocket()
|
{
|
return clientSocket;
|
}
|
|
/// <summary>
|
/// 解析 URL
|
/// </summary>
|
private void ParseUrl(string url, out string ip, out int port)
|
{
|
// 默认端口
|
port = 8080;
|
|
// 移除协议前缀
|
if (url.StartsWith("tcp://"))
|
{
|
url = url.Substring(6);
|
}
|
|
// 分割 IP 和端口
|
var parts = url.Split(':');
|
ip = parts[0];
|
|
if (parts.Length > 1 && int.TryParse(parts[1], out int parsedPort))
|
{
|
port = parsedPort;
|
}
|
}
|
}
|