1
This commit is contained in:
24
Src/Core/Net/Exception.cs
Normal file
24
Src/Core/Net/Exception.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
|
||||
namespace BITKit
|
||||
{
|
||||
public class NetOfflineException : System.Exception
|
||||
{
|
||||
public NetOfflineException() : base("Client is not connected") { }
|
||||
}
|
||||
public class NetAuthorizeException : System.Exception
|
||||
{
|
||||
public NetAuthorizeException() : base("Client is not authorized") { }
|
||||
}
|
||||
public abstract class NetAuthorityException : System.Exception
|
||||
{
|
||||
protected NetAuthorityException(string message =null) : base(string.IsNullOrEmpty(message)?"Authority is not valid":message) { }
|
||||
}
|
||||
public class NetClientOnlyException : NetAuthorityException
|
||||
{
|
||||
public NetClientOnlyException() : base("This method is only available on client") { }
|
||||
}
|
||||
public class NetServerOnlyException : NetAuthorityException
|
||||
{
|
||||
public NetServerOnlyException() : base("This method is only available on server") { }
|
||||
}
|
||||
}
|
11
Src/Core/Net/Exception.cs.meta
Normal file
11
Src/Core/Net/Exception.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d38fad9bff7582b4b8287df7c47a47b7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -18,7 +18,14 @@ namespace BITKit
|
||||
[AttributeUsage(AttributeTargets.Method|AttributeTargets.Event)]
|
||||
public sealed class NetRpcAttribute : Attribute
|
||||
{
|
||||
|
||||
public readonly bool AddTypeNamePrefix;
|
||||
public NetRpcAttribute()
|
||||
{
|
||||
}
|
||||
public NetRpcAttribute(bool addTypeNamePrefix)
|
||||
{
|
||||
AddTypeNamePrefix = addTypeNamePrefix;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 帮助类
|
||||
@@ -84,7 +91,6 @@ namespace BITKit
|
||||
/// </summary>
|
||||
public interface INetProvider
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 向服务端发送指令
|
||||
/// </summary>
|
||||
@@ -192,6 +198,10 @@ namespace BITKit
|
||||
/// </summary>
|
||||
public interface INetServer
|
||||
{
|
||||
/// <summary>
|
||||
/// 通信接口
|
||||
/// </summary>
|
||||
public INetProvider NetProvider=>this as INetProvider;
|
||||
/// <summary>
|
||||
/// 源物体,用于通过代理直接访问
|
||||
/// </summary>
|
||||
@@ -274,6 +284,10 @@ namespace BITKit
|
||||
/// </summary>
|
||||
public interface INetClient
|
||||
{
|
||||
/// <summary>
|
||||
/// 通讯接口
|
||||
/// </summary>
|
||||
public INetProvider NetProvider=>this as INetProvider;
|
||||
/// <summary>
|
||||
/// 源物体,用于通过代理直接访问
|
||||
/// </summary>
|
||||
@@ -318,85 +332,4 @@ namespace BITKit
|
||||
/// <param name="message">消息</param>
|
||||
void SendServerMessage(string message);
|
||||
}
|
||||
|
||||
#if UNITY
|
||||
/// <summary>
|
||||
/// 有关NetProvider的Unity代理
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public class NetProviderProxy : INetProvider
|
||||
{
|
||||
[SerializeField] private MonoBehaviour monoBehaviour;
|
||||
INetProvider netProvider => monoBehaviour as INetProvider;
|
||||
void INetProvider.ServerCommand<T>(T command) => netProvider.ServerCommand(command);
|
||||
void INetProvider.RpcClientCommand<T>(T command) => netProvider.RpcClientCommand(command);
|
||||
void INetProvider.ClientCommand<T>(int id, T command) => netProvider.ClientCommand(id, command);
|
||||
|
||||
async UniTask<T> INetProvider.GetFromServer<T>(string addressablePath) =>
|
||||
await netProvider.GetFromServer<T>(addressablePath);
|
||||
|
||||
UniTask<T> INetProvider.GetFromClient<T>(int id, string addressablePath) =>
|
||||
netProvider.GetFromClient<T>(id, addressablePath);
|
||||
|
||||
void INetProvider.AddRpcHandle(object rpcHandle) => netProvider.AddRpcHandle(rpcHandle);
|
||||
|
||||
void INetProvider.AddCommandListener<T>(Action<T> handle) => netProvider.AddCommandListener<T>(handle);
|
||||
|
||||
void INetProvider.RemoveCommandListener<T>(Action<T> handle) => netProvider.RemoveCommandListener<T>(handle);
|
||||
|
||||
void INetProvider.SendRT(string rpcName, params object[] pars) => netProvider.SendRT(rpcName, pars);
|
||||
|
||||
void INetProvider.SendTargetRT(int id, string rpcName, params object[] pars) =>
|
||||
netProvider.SendTargetRT(id, rpcName, pars);
|
||||
|
||||
void INetProvider.SendAllRT(string rpcName, params object[] pars) => netProvider.SendAllRT(rpcName, pars);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 有关Unity的NetClient的代理
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public class GDNetClientProxy : INetClient
|
||||
{
|
||||
[SerializeField] private MonoBehaviour monoBehaviour;
|
||||
private INetClient _netClientImplementation=>monoBehaviour as INetClient;
|
||||
event Action INetClient.OnStartConnect
|
||||
{
|
||||
add => _netClientImplementation.OnStartConnect += value;
|
||||
remove => _netClientImplementation.OnStartConnect -= value;
|
||||
}
|
||||
|
||||
event Action INetClient.OnConnected
|
||||
{
|
||||
add => _netClientImplementation.OnConnected += value;
|
||||
remove => _netClientImplementation.OnConnected -= value;
|
||||
}
|
||||
|
||||
event Action INetClient.OnDisconnected
|
||||
{
|
||||
add => _netClientImplementation.OnDisconnected += value;
|
||||
remove => _netClientImplementation.OnDisconnected -= value;
|
||||
}
|
||||
|
||||
event Action INetClient.OnConnectedFailed
|
||||
{
|
||||
add => _netClientImplementation.OnConnectedFailed += value;
|
||||
remove => _netClientImplementation.OnConnectedFailed -= value;
|
||||
}
|
||||
|
||||
UniTask<bool> INetClient.Connect(string address, ushort port)
|
||||
{
|
||||
return _netClientImplementation.Connect(address, port);
|
||||
}
|
||||
|
||||
bool INetClient.IsConnected => _netClientImplementation.IsConnected;
|
||||
|
||||
int INetClient.Ping => _netClientImplementation.Ping;
|
||||
public int Id => _netClientImplementation.Id;
|
||||
void INetClient.Disconnect()
|
||||
{
|
||||
_netClientImplementation.Disconnect();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
Reference in New Issue
Block a user