2024-08-17 14:27:18 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Cysharp.Threading.Tasks;
|
2024-08-19 11:51:17 +08:00
|
|
|
|
using Plugins.JNGame.Network;
|
|
|
|
|
using Plugins.JNGame.Network.Action;
|
2024-08-17 14:27:18 +08:00
|
|
|
|
using Plugins.JNGame.Network.Entity;
|
2024-08-19 11:51:17 +08:00
|
|
|
|
using Plugins.JNGame.Network.Util;
|
|
|
|
|
using TouchSocket.Core;
|
|
|
|
|
using TouchSocket.Sockets;
|
2024-08-17 14:27:18 +08:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
using NotImplementedException = System.NotImplementedException;
|
|
|
|
|
|
2024-08-19 11:51:17 +08:00
|
|
|
|
namespace JNGame.Network
|
2024-08-17 14:27:18 +08:00
|
|
|
|
{
|
|
|
|
|
public class JNTCPClient : JNClientBase
|
|
|
|
|
{
|
|
|
|
|
|
2024-08-19 11:51:17 +08:00
|
|
|
|
private TcpClient tcpClient;
|
|
|
|
|
public bool IsOpen => tcpClient is not null && tcpClient.Online;
|
|
|
|
|
|
|
|
|
|
|
2024-08-17 14:27:18 +08:00
|
|
|
|
public override async Task OnInit()
|
|
|
|
|
{
|
2024-08-19 11:51:17 +08:00
|
|
|
|
tcpClient = new TcpClient();
|
|
|
|
|
await tcpClient.SetupAsync(
|
|
|
|
|
new TouchSocketConfig()
|
|
|
|
|
.ConfigurePlugins(a =>
|
|
|
|
|
{
|
|
|
|
|
a.UseReconnection(-1, true, 1000); //如需永远尝试连接,tryCount设置为-1即可。
|
|
|
|
|
})
|
|
|
|
|
.SetTcpDataHandlingAdapter(() => new FixedHeaderPackageAdapter())
|
|
|
|
|
);
|
|
|
|
|
tcpClient.Connecting = OnConnecting;
|
|
|
|
|
tcpClient.Connected = OnConnected;//成功连接到服务器
|
|
|
|
|
tcpClient.Disconnected = OnDisconnected;//从服务器断开连接,当连接不成功时不会触发。
|
|
|
|
|
tcpClient.Received = OnReceived;
|
2024-08-17 14:27:18 +08:00
|
|
|
|
|
2024-08-22 20:37:39 +08:00
|
|
|
|
await tcpClient.ConnectAsync(await GetEndPoint());
|
2024-08-17 14:27:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-19 11:51:17 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
|
|
|
private Task OnConnecting(ITcpClient client, ConnectingEventArgs e)
|
2024-08-17 14:27:18 +08:00
|
|
|
|
{
|
2024-08-19 11:51:17 +08:00
|
|
|
|
Debug.Log($"[JNTCPClient] 开始连接服务器");
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
2024-08-17 14:27:18 +08:00
|
|
|
|
|
2024-08-19 11:51:17 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 成功连接到服务器
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
|
|
|
private Task OnConnected(ITcpClient client, ConnectedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Debug.Log($"[JNTCPClient] 服务器连接成功");
|
2024-08-21 16:18:52 +08:00
|
|
|
|
Dispatch(new JNetParam(_id++,(int)NActionEnum.LocalClientConnect));
|
2024-08-19 11:51:17 +08:00
|
|
|
|
return Task.CompletedTask;
|
2024-08-17 14:27:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-19 11:51:17 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 从服务器断开连接,当连接不成功时不会触发。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
|
|
|
private Task OnDisconnected(ITcpClientBase client, DisconnectEventArgs e)
|
2024-08-17 14:27:18 +08:00
|
|
|
|
{
|
2024-08-19 11:51:17 +08:00
|
|
|
|
Debug.Log($"[JNTCPClient] 服务器断开");
|
2024-08-21 16:18:52 +08:00
|
|
|
|
Dispatch(new JNetParam(_id++,(int)NActionEnum.LocalClientDisconnect));
|
2024-08-19 11:51:17 +08:00
|
|
|
|
return Task.CompletedTask;
|
2024-08-17 14:27:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-19 11:51:17 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 接收到消息
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private Task OnReceived(TcpClient client, ReceivedDataEventArgs e)
|
2024-08-17 14:27:18 +08:00
|
|
|
|
{
|
2024-08-19 11:51:17 +08:00
|
|
|
|
byte[] data = new byte[e.ByteBlock.Len];
|
|
|
|
|
Array.Copy(e.ByteBlock.Buffer,data, data.Length);
|
|
|
|
|
var param = NDataUtil.Parse(data);
|
|
|
|
|
Dispatch(param);
|
|
|
|
|
return Task.CompletedTask;
|
2024-08-17 14:27:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-08-19 11:51:17 +08:00
|
|
|
|
public override void SendBytes(byte[] data)
|
2024-08-17 14:27:18 +08:00
|
|
|
|
{
|
|
|
|
|
if (IsOpen)
|
|
|
|
|
{
|
2024-08-19 11:51:17 +08:00
|
|
|
|
tcpClient.Send(data);
|
2024-08-17 14:27:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-19 11:51:17 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override void OnClose()
|
|
|
|
|
{
|
|
|
|
|
tcpClient.Close();
|
2024-08-22 20:37:39 +08:00
|
|
|
|
tcpClient.Dispose();
|
|
|
|
|
Debug.Log($"[JNTCPClient] 关闭对象");
|
|
|
|
|
base.OnClose();
|
2024-08-19 11:51:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual async UniTask<string> GetEndPoint()
|
|
|
|
|
{
|
|
|
|
|
await UniTask.NextFrame();
|
|
|
|
|
return "127.0.0.1:9001";
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-17 14:27:18 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|