2024-08-19 11:51:17 +08:00
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Sockets;
|
2024-08-17 14:27:18 +08:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Cysharp.Threading.Tasks;
|
|
|
|
|
using DotRecast.Core.Collections;
|
|
|
|
|
using Google.Protobuf;
|
|
|
|
|
using Plugins.JNGame.Network;
|
2024-08-19 11:51:17 +08:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
namespace JNGame.Network
|
|
|
|
|
{
|
|
|
|
|
public class JNTCPServer : JNServerBase
|
|
|
|
|
{
|
2024-08-19 11:51:17 +08:00
|
|
|
|
|
|
|
|
|
private TcpService service;
|
2024-08-17 14:27:18 +08:00
|
|
|
|
|
2024-08-19 11:51:17 +08:00
|
|
|
|
private int _port;
|
2024-08-17 14:27:18 +08:00
|
|
|
|
public int Port => _port;
|
|
|
|
|
|
|
|
|
|
public override async Task OnInit()
|
|
|
|
|
{
|
2024-08-19 11:51:17 +08:00
|
|
|
|
|
|
|
|
|
service = new TcpService();
|
|
|
|
|
await service.SetupAsync(
|
|
|
|
|
new TouchSocketConfig()
|
|
|
|
|
.SetTcpDataHandlingAdapter(() => new FixedHeaderPackageAdapter())
|
|
|
|
|
);
|
|
|
|
|
service.Connecting = OnConnecting;//有客户端正在连接
|
|
|
|
|
service.Connected = OnConnected;//有客户端连接
|
|
|
|
|
service.Disconnected = OnDisconnected;//有客户端断开连接
|
|
|
|
|
service.Received = OnReceived;//客户端接收到消息
|
|
|
|
|
await service.StartAsync(_port = await GetPort());//启动
|
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>
|
|
|
|
|
private Task OnConnecting(SocketClient client, ConnectingEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Debug.Log($"[JNTCPServer] 有客户端正在连接");
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 有客户端连接
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
|
|
|
private Task OnConnected(SocketClient client, ConnectedEventArgs e)
|
2024-08-17 14:27:18 +08:00
|
|
|
|
{
|
2024-08-19 11:51:17 +08:00
|
|
|
|
Debug.Log($"[JNTCPServer] 有客户端连接成功");
|
2024-08-21 16:18:52 +08:00
|
|
|
|
Dispatch((int)NActionEnum.LocalClientConnect,new JNServerParam()
|
2024-08-17 14:27:18 +08:00
|
|
|
|
{
|
2024-08-19 11:51:17 +08:00
|
|
|
|
Client = client.Id
|
|
|
|
|
});
|
|
|
|
|
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(SocketClient client, DisconnectEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Debug.Log($"[JNTCPServer] 有客户端断开连接");
|
2024-08-21 16:18:52 +08:00
|
|
|
|
Dispatch((int)NActionEnum.LocalClientDisconnect,new JNServerParam()
|
2024-08-19 11:51:17 +08:00
|
|
|
|
{
|
|
|
|
|
Client = client.Id
|
|
|
|
|
});
|
|
|
|
|
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 async Task OnReceived(SocketClient 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.HId,new JNServerParam()
|
|
|
|
|
{
|
|
|
|
|
Client = client.Id,
|
|
|
|
|
Message = param.Bytes,
|
|
|
|
|
});
|
2024-08-17 14:27:18 +08:00
|
|
|
|
await UniTask.NextFrame();
|
2024-08-19 11:51:17 +08:00
|
|
|
|
|
2024-08-17 14:27:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnClose()
|
|
|
|
|
{
|
|
|
|
|
base.OnClose();
|
2024-08-19 11:51:17 +08:00
|
|
|
|
service.Stop();
|
2024-08-17 14:27:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-19 11:51:17 +08:00
|
|
|
|
protected virtual async UniTask<int> GetPort()
|
2024-08-17 14:27:18 +08:00
|
|
|
|
{
|
2024-08-19 11:51:17 +08:00
|
|
|
|
await UniTask.NextFrame();
|
|
|
|
|
return 9001;
|
2024-08-17 14:27:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-19 11:51:17 +08:00
|
|
|
|
|
|
|
|
|
public void Send(SocketClient client,int hId,IMessage data = null)
|
2024-08-17 14:27:18 +08:00
|
|
|
|
{
|
2024-08-19 11:51:17 +08:00
|
|
|
|
var bytes = NDataUtil.Encrypt(JNetParam.Build(this._id++, hId).SetData(data));
|
|
|
|
|
_byteSize[hId] = bytes.Length;
|
|
|
|
|
service.SendAsync(client.Id, bytes);
|
2024-08-17 14:27:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-19 11:51:17 +08:00
|
|
|
|
public void AllSend(int hId,IMessage data = null)
|
2024-08-17 14:27:18 +08:00
|
|
|
|
{
|
2024-08-19 11:51:17 +08:00
|
|
|
|
service.GetClients().ForEach(client =>
|
|
|
|
|
{
|
|
|
|
|
Send(client,hId,data);
|
|
|
|
|
});
|
2024-08-17 14:27:18 +08:00
|
|
|
|
}
|
2024-08-19 11:51:17 +08:00
|
|
|
|
|
2024-08-17 14:27:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|