breakpoint
This commit is contained in:
@@ -1,253 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using BITKit;
|
||||
using BITKit.UX;
|
||||
using BITKit.HttpNet;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Net.Client;
|
||||
using UnityEngine.UIElements;
|
||||
using System.Text;
|
||||
using Net.Share;
|
||||
using Object = System.Object;
|
||||
|
||||
namespace BITKit
|
||||
{
|
||||
public interface INetClientProvider
|
||||
{
|
||||
ClientBase GetClient();
|
||||
}
|
||||
//[CreateOnStart(BITAppForUnity.Path.Services,nameof(BITNet))]
|
||||
public class BITNet : MonoBehaviour
|
||||
{
|
||||
[UnityEngine.RuntimeInitializeOnLoadMethod]
|
||||
static void Reload()
|
||||
{
|
||||
OnStartConnect = null;
|
||||
OnConnected = null;
|
||||
OnConnectFailed = null;
|
||||
OnTryToConnect = null;
|
||||
OnTryToConnectFailed = null;
|
||||
OnDisconnect = null;
|
||||
OnConnectLost = null;
|
||||
OnConnection = null;
|
||||
OnReconnect = null;
|
||||
OnOnWhenQueuing = null;
|
||||
OnOnQueueCancellation = null;
|
||||
RpcHandles.Clear();
|
||||
}
|
||||
public static event Action OnStartConnect;
|
||||
public static event Action OnConnected;
|
||||
public static event Action OnConnectFailed;
|
||||
public static event Action OnTryToConnect;
|
||||
public static event Action OnTryToConnectFailed;
|
||||
public static event Action OnDisconnect;
|
||||
public static event Action OnConnectLost;
|
||||
public static event Action OnConnection;
|
||||
public static event Action OnReconnect;
|
||||
public static event Action OnOnWhenQueuing;
|
||||
public static event Action OnOnQueueCancellation;
|
||||
|
||||
private static BITNet Singleton;
|
||||
private static readonly List<object> RpcHandles = new();
|
||||
private static readonly Queue<(string name,Object[] pars)> Rpcs = new();
|
||||
[BITCommand]
|
||||
public static void Connect(string address, int port)
|
||||
{
|
||||
Singleton.ConnectInternal(address, port);
|
||||
}
|
||||
public static Action<object> AddRpcHandle => RpcHandles.Add;
|
||||
public static void SendRT(string rpcName, params object[] pars)
|
||||
{
|
||||
Rpcs.Enqueue(new()
|
||||
{
|
||||
name = rpcName,
|
||||
pars = pars
|
||||
});
|
||||
}
|
||||
[Header(Constant.Header.Settings)]
|
||||
public string address = "localhost";
|
||||
public int port = 666;
|
||||
public int apiPort = 27014;
|
||||
[Header(Constant.Header.Settings)]
|
||||
[SerializeReference, SubclassSelector] public IWebProvider webProvider;
|
||||
[SerializeReference, SubclassSelector] public INetClientProvider clientProvider;
|
||||
[Header(Constant.Header.InternalVariables)]
|
||||
ClientBase client;
|
||||
private string GetApiUrl(string address) => $"http://{address}:{apiPort}/api";
|
||||
public string GetAPI(params string[] path)
|
||||
{
|
||||
return GetApiUrl(address) + "/" + String.Join("/", path);
|
||||
}
|
||||
[BIT]
|
||||
public void ConnectToServer()
|
||||
{
|
||||
ConnectInternal(address, port);
|
||||
}
|
||||
[BIT]
|
||||
public void ConnectToLocal()
|
||||
{
|
||||
ConnectInternal("localhost", port);
|
||||
}
|
||||
[BIT]
|
||||
public void Disconnect()
|
||||
{
|
||||
client.Close();
|
||||
//clientManager.client.Close();
|
||||
}
|
||||
async void ConnectInternal(string address, int port)
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (var x in RpcHandles)
|
||||
{
|
||||
client.AddRpcHandle(x);
|
||||
}
|
||||
|
||||
OnStartConnect?.Invoke();
|
||||
var message = $"正在获取api:{GetApiUrl(address)}";
|
||||
|
||||
Data.Set<IProgress>(new ProgressInfo()
|
||||
{
|
||||
Progress = 0.1f,
|
||||
Message = message,
|
||||
});
|
||||
|
||||
Debug.Log(message);
|
||||
await webProvider
|
||||
.GetAsync<Package>(GetApiUrl(address),this.GetCancellationTokenOnDestroy());
|
||||
|
||||
message = $"已获取到服务器信息,正在连接到服务器";
|
||||
|
||||
Data.Set<IProgress>(new ProgressInfo()
|
||||
{
|
||||
Progress = 0.2f,
|
||||
Message = message,
|
||||
});
|
||||
client.host = address;
|
||||
client.port = port;
|
||||
//client.ip=address;
|
||||
//clientManager.port=port;
|
||||
|
||||
if (await client.Connect())
|
||||
{
|
||||
Data.Set<IProgress>(new ProgressInfo()
|
||||
{
|
||||
Progress = 0.1f,
|
||||
Message = "已获取到在线服务器",
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
bool isConnected = false;
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
if(await client.Connect())
|
||||
{
|
||||
isConnected=true;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
Data.Set<IProgress>(new ProgressInfo()
|
||||
{
|
||||
Progress = 0.1f + i*0.1f,
|
||||
Message = "正在尝试重新连接到服务器",
|
||||
});
|
||||
}
|
||||
}
|
||||
if(isConnected)
|
||||
{
|
||||
Data.Set<IProgress>(new ProgressInfo()
|
||||
{
|
||||
Progress = 0.5f,
|
||||
Message = "已连接,正在加载中...",
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
Alert.Print(new()
|
||||
{
|
||||
title = "连接失败",
|
||||
message = "已找到服务端但未找到服务器",
|
||||
});
|
||||
OnConnectFailed?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (System.Net.Http.HttpRequestException e)
|
||||
{
|
||||
Alert.Print(new AlertMessage()
|
||||
{
|
||||
title = "未找到服务端:",
|
||||
message = e.Message,
|
||||
});
|
||||
OnConnectFailed?.Invoke();
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Alert.Print(new AlertMessage()
|
||||
{
|
||||
title = "无法连接到服务端",
|
||||
message = e.Message,
|
||||
});
|
||||
Debug.Log(e.GetType().FullName);
|
||||
BIT4Log.LogException(e);
|
||||
OnConnectFailed?.Invoke();
|
||||
}
|
||||
}
|
||||
void Awake()
|
||||
{
|
||||
Singleton = this;
|
||||
DI.Register(this);
|
||||
client = clientProvider.GetClient();
|
||||
DI.Register(client);
|
||||
|
||||
}
|
||||
void Start()
|
||||
{
|
||||
/*
|
||||
* None,
|
||||
Connected,
|
||||
ConnectFailed,
|
||||
TryToConnect,
|
||||
TryToConnectFailed,
|
||||
Disconnect,
|
||||
ConnectLost,
|
||||
ConnectClosed,
|
||||
Connection,
|
||||
Reconnect,
|
||||
OnWhenQueuing,
|
||||
OnQueueCancellation,
|
||||
*/
|
||||
client.AddStateHandler(NetworkState.Connected,OnConnected);
|
||||
client.AddStateHandler(NetworkState.ConnectFailed,OnConnectFailed);
|
||||
client.AddStateHandler(NetworkState.TryToConnect,OnTryToConnect);
|
||||
client.AddStateHandler(NetworkState.TryToConnectFailed,OnTryToConnectFailed);
|
||||
client.AddStateHandler(NetworkState.Disconnect,OnDisconnect);
|
||||
client.AddStateHandler(NetworkState.ConnectLost,OnConnectLost);
|
||||
client.AddStateHandler(NetworkState.ConnectClosed,OnConnectLost);
|
||||
client.AddStateHandler(NetworkState.Connection,OnConnection);
|
||||
client.AddStateHandler(NetworkState.Reconnect,OnReconnect);
|
||||
client.AddStateHandler(NetworkState.OnWhenQueuing,OnOnWhenQueuing);
|
||||
client.AddStateHandler(NetworkState.OnQueueCancellation,OnOnQueueCancellation);
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (client is null || client.Client is null || client.Client.Connected is false) return;
|
||||
while (Rpcs.TryDequeue(out var rpc))
|
||||
{
|
||||
client.SendRT(rpc.name,rpc.pars);
|
||||
}
|
||||
}
|
||||
}
|
||||
#if UNITY_EDITOR
|
||||
[UnityEditor.CustomEditor(typeof(BITNet))]
|
||||
public class BITNetInspector : BITInspector<BITNet>
|
||||
{
|
||||
|
||||
}
|
||||
#endif
|
||||
}
|
13
Assets/BITKit/Extensions/GameDesigner/GDNetClientProvider.cs
Normal file
13
Assets/BITKit/Extensions/GameDesigner/GDNetClientProvider.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Net.Client;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BITKit
|
||||
{
|
||||
public interface IGDNetClientProvider
|
||||
{
|
||||
ClientBase GetClient();
|
||||
}
|
||||
}
|
||||
|
@@ -1,7 +1,9 @@
|
||||
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;
|
||||
@@ -16,6 +18,7 @@ namespace BITKit
|
||||
[Serializable]
|
||||
public class GDNetService:MonoBehaviour, INetProvider,INetClient
|
||||
{
|
||||
internal static GDNetService Singleton;
|
||||
//服务器地址与端口号
|
||||
[Header(Constant.Header.Settings)]
|
||||
[SerializeField] private string address;
|
||||
@@ -23,7 +26,7 @@ namespace BITKit
|
||||
[Header(Constant.Header.Components)]
|
||||
//绑定GDNet的ClientManager
|
||||
[SerializeField,SerializeReference,SubclassSelector]
|
||||
private INetClientProvider clientProvider;
|
||||
private IGDNetClientProvider clientProvider=null;
|
||||
//内部事件系统
|
||||
private readonly GenericEvent eventSystem = new();
|
||||
//内部变量
|
||||
@@ -32,14 +35,12 @@ namespace BITKit
|
||||
private readonly List<object> rpcHandles = new();
|
||||
//GDNet的网络客户端
|
||||
private ClientBase client;
|
||||
/// <summary>
|
||||
/// 是否已连接到服务端
|
||||
/// </summary>
|
||||
|
||||
public bool IsConnected => client.Connected;
|
||||
/// <summary>
|
||||
/// 连接服务端的延迟
|
||||
/// </summary>
|
||||
|
||||
public int Ping { get; private set; }
|
||||
|
||||
public int Id => client.UID;
|
||||
//客户端的基本回调
|
||||
public event Action OnStartConnect;
|
||||
public event Action OnConnected;
|
||||
@@ -48,9 +49,10 @@ namespace BITKit
|
||||
|
||||
public void ServerCommand<T>(T command=default)
|
||||
{
|
||||
SendRT("CallServerCommand",command);
|
||||
//SendRT("OnServerCommand",command);
|
||||
var bytes = BITBinary.WriteAsBytes(command);
|
||||
client.Send(bytes);
|
||||
}
|
||||
|
||||
public void RpcClientCommand<T>(T command=default)
|
||||
{
|
||||
SendRT("CallRpcClientCommand",command);
|
||||
@@ -88,7 +90,9 @@ namespace BITKit
|
||||
|
||||
public void SendRT(string rpcName, params object[] pars)
|
||||
{
|
||||
client.SendRT(rpcName,pars);
|
||||
// client.SendRT("Test",rpcName);
|
||||
// client.SendRT(rpcName,pars);
|
||||
// client.SendRT("TestMessage",rpcName);
|
||||
}
|
||||
|
||||
public void SendTargetRT(int id, string rpcName, params object[] pars)
|
||||
@@ -110,12 +114,16 @@ namespace BITKit
|
||||
{
|
||||
client.AddRpcHandle(x);
|
||||
}
|
||||
client.AddRpcHandle(this);
|
||||
OnStartConnect?.Invoke();
|
||||
if (await client.Connect())
|
||||
{
|
||||
OnConnected?.Invoke();
|
||||
}
|
||||
OnConnectedFailed?.Invoke();
|
||||
else
|
||||
{
|
||||
OnConnectedFailed?.Invoke();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -130,10 +138,121 @@ namespace BITKit
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -6,8 +6,9 @@ namespace BITKit.Steamwork
|
||||
{
|
||||
public interface ISteamService
|
||||
{
|
||||
ulong GetSteamId { get; }
|
||||
string GetName { get; }
|
||||
int Id { get; }
|
||||
ulong SteamId { get; }
|
||||
string Name { get; }
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -7,11 +7,18 @@ namespace BITKit.Steamwork
|
||||
{
|
||||
public class SteamService : MonoBehaviour,ISteamService
|
||||
{
|
||||
internal static SteamService Singleton;
|
||||
[Header(Constant.Header.Settings)]
|
||||
[SerializeField]private uint appId=480;
|
||||
//接口实现
|
||||
public ulong GetSteamId => SteamClient.SteamId;
|
||||
public string GetName => SteamClient.Name;
|
||||
public ulong SteamId => SteamClient.SteamId;
|
||||
public string Name => SteamClient.Name;
|
||||
public int Id => (int)SteamClient.SteamId.AccountId;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
Singleton = this;
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
@@ -33,5 +40,12 @@ namespace BITKit.Steamwork
|
||||
SteamClient.Shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class SteamServiceSingleton:ISteamService
|
||||
{
|
||||
private ISteamService _steamServiceImplementation=>SteamService.Singleton;
|
||||
public int Id => _steamServiceImplementation.Id;
|
||||
public ulong SteamId => _steamServiceImplementation.SteamId;
|
||||
public string Name => _steamServiceImplementation.Name;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user