259 lines
7.7 KiB
C#
259 lines
7.7 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Runtime.Remoting;
|
|
using Cysharp.Threading.Tasks;
|
|
using JetBrains.Annotations;
|
|
using UnityEngine;
|
|
using Net;
|
|
using Net.Client;
|
|
using Net.Component;
|
|
using Net.Share;
|
|
|
|
namespace BITKit
|
|
{
|
|
/// <summary>
|
|
/// 基于GDNet的网络服务
|
|
/// </summary>
|
|
[Serializable]
|
|
public class GDNetService:MonoBehaviour, INetProvider,INetClient
|
|
{
|
|
internal static GDNetService Singleton;
|
|
//服务器地址与端口号
|
|
[Header(Constant.Header.Settings)]
|
|
[SerializeField] private string address;
|
|
[SerializeField] private ushort port;
|
|
[Header(Constant.Header.Components)]
|
|
//绑定GDNet的ClientManager
|
|
[SerializeField,SerializeReference,SubclassSelector]
|
|
private IGDNetClientProvider clientProvider=null;
|
|
//内部事件系统
|
|
private readonly GenericEvent eventSystem = new();
|
|
//内部变量
|
|
[Header(Constant.Header.InternalVariables)]
|
|
//已保存的Rpc对象
|
|
private readonly List<object> rpcHandles = new();
|
|
//GDNet的网络客户端
|
|
private ClientBase client;
|
|
|
|
public bool IsConnected => client.Connected;
|
|
|
|
public int Ping { get; private set; }
|
|
|
|
public int Id => client.UID;
|
|
//客户端的基本回调
|
|
public event Action OnStartConnect;
|
|
public event Action OnConnected;
|
|
public event Action OnDisconnected;
|
|
public event Action OnConnectedFailed;
|
|
|
|
public void ServerCommand<T>(T command=default)
|
|
{
|
|
//SendRT("OnServerCommand",command);
|
|
var bytes = BITBinary.WriteAsBytes(command);
|
|
client.Send(bytes);
|
|
}
|
|
public void RpcClientCommand<T>(T command=default)
|
|
{
|
|
SendRT("CallRpcClientCommand",command);
|
|
}
|
|
|
|
public void ClientCommand<T>(int id,T command=default)
|
|
{
|
|
SendRT("CallClientCommand",id,command);
|
|
}
|
|
|
|
public UniTask<T> GetFromServer<T>(string addressablePath = "Internal")
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public UniTask<T> GetFromClient<T>(int id, string addressablePath = "Internal")
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public void AddRpcHandle(object rpcHandle)
|
|
{
|
|
rpcHandles.Add(rpcHandle);
|
|
}
|
|
|
|
public void AddCommandListener<T>(Action<T> handle)
|
|
{
|
|
eventSystem.AddListener(handle);
|
|
}
|
|
|
|
public void RemoveCommandListener<T>(Action<T> handle)
|
|
{
|
|
eventSystem.RemoveListener(handle);
|
|
}
|
|
|
|
public void SendRT(string rpcName, params object[] pars)
|
|
{
|
|
// client.SendRT("Test",rpcName);
|
|
// client.SendRT(rpcName,pars);
|
|
// client.SendRT("TestMessage",rpcName);
|
|
}
|
|
|
|
public void SendTargetRT(int id, string rpcName, params object[] pars)
|
|
{
|
|
SendRT("CallTargetRT",id,rpcName,pars);
|
|
}
|
|
|
|
public void SendAllRT(string rpcName, params object[] pars)
|
|
{
|
|
SendRT("CallSendAllRT",rpcName,pars);
|
|
}
|
|
|
|
public async UniTask<bool> Connect(string _address = "localhost", ushort _port = 27014)
|
|
{
|
|
if (client.Connected) return true;
|
|
client.host = _address;
|
|
client.port = _port;
|
|
foreach (var x in rpcHandles)
|
|
{
|
|
client.AddRpcHandle(x);
|
|
}
|
|
client.AddRpcHandle(this);
|
|
OnStartConnect?.Invoke();
|
|
if (await client.Connect())
|
|
{
|
|
OnConnected?.Invoke();
|
|
}
|
|
else
|
|
{
|
|
OnConnectedFailed?.Invoke();
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public void Disconnect()
|
|
{
|
|
if (!IsConnected) return;
|
|
client.Close();
|
|
OnDisconnected?.Invoke();
|
|
}
|
|
private void OnReceiveCommand(object command)
|
|
{
|
|
eventSystem.Invoke($"{command.GetType().FullName}.{Constant.System.Internal}",command);
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
Singleton = this;
|
|
}
|
|
private void Start()
|
|
{
|
|
client = clientProvider.GetClient();
|
|
client.AddRpcHandle(this);
|
|
}
|
|
|
|
[Rpc]
|
|
private void OnClientCommand(object command)
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public class GDNetServiceSingleton : INetClient,INetProvider
|
|
{
|
|
private INetProvider _netProviderImplementation => GDNetService.Singleton;
|
|
private INetClient _netClientImplementation => GDNetService.Singleton;
|
|
|
|
public event Action OnStartConnect
|
|
{
|
|
add => _netClientImplementation.OnStartConnect += value;
|
|
remove => _netClientImplementation.OnStartConnect -= value;
|
|
}
|
|
|
|
public event Action OnConnected
|
|
{
|
|
add => _netClientImplementation.OnConnected += value;
|
|
remove => _netClientImplementation.OnConnected -= value;
|
|
}
|
|
|
|
public event Action OnDisconnected
|
|
{
|
|
add => _netClientImplementation.OnDisconnected += value;
|
|
remove => _netClientImplementation.OnDisconnected -= value;
|
|
}
|
|
|
|
public event Action OnConnectedFailed
|
|
{
|
|
add => _netClientImplementation.OnConnectedFailed += value;
|
|
remove => _netClientImplementation.OnConnectedFailed -= value;
|
|
}
|
|
|
|
public UniTask<bool> Connect(string address = "localhost", ushort port = 27014)
|
|
{
|
|
return _netClientImplementation.Connect(address, port);
|
|
}
|
|
|
|
public bool IsConnected => _netClientImplementation.IsConnected;
|
|
|
|
public int Ping => _netClientImplementation.Ping;
|
|
public int Id => _netClientImplementation.Id;
|
|
|
|
public void Disconnect()
|
|
{
|
|
_netClientImplementation.Disconnect();
|
|
}
|
|
|
|
public void ServerCommand<T>(T command = default)
|
|
{
|
|
_netProviderImplementation.ServerCommand(command);
|
|
}
|
|
|
|
public void RpcClientCommand<T>(T command = default)
|
|
{
|
|
_netProviderImplementation.RpcClientCommand(command);
|
|
}
|
|
|
|
public void ClientCommand<T>(int id, T command)
|
|
{
|
|
_netProviderImplementation.ClientCommand(id, command);
|
|
}
|
|
|
|
public UniTask<T> GetFromServer<T>(string addressablePath = Constant.System.Internal)
|
|
{
|
|
return _netProviderImplementation.GetFromServer<T>(addressablePath);
|
|
}
|
|
|
|
public UniTask<T> GetFromClient<T>(int id, string addressablePath = Constant.System.Internal)
|
|
{
|
|
return _netProviderImplementation.GetFromClient<T>(id, addressablePath);
|
|
}
|
|
|
|
public void AddRpcHandle(object rpcHandle)
|
|
{
|
|
_netProviderImplementation.AddRpcHandle(rpcHandle);
|
|
}
|
|
|
|
public void AddCommandListener<T>(Action<T> handle)
|
|
{
|
|
_netProviderImplementation.AddCommandListener(handle);
|
|
}
|
|
|
|
public void RemoveCommandListener<T>(Action<T> handle)
|
|
{
|
|
_netProviderImplementation.RemoveCommandListener(handle);
|
|
}
|
|
|
|
public void SendRT(string rpcName, params object[] pars)
|
|
{
|
|
_netProviderImplementation.SendRT(rpcName, pars);
|
|
}
|
|
|
|
public void SendTargetRT(int id, string rpcName, params object[] pars)
|
|
{
|
|
_netProviderImplementation.SendTargetRT(id, rpcName, pars);
|
|
}
|
|
|
|
public void SendAllRT(string rpcName, params object[] pars)
|
|
{
|
|
_netProviderImplementation.SendAllRT(rpcName, pars);
|
|
}
|
|
}
|
|
}
|