1
This commit is contained in:
212
Src/Unity/Scripts/NetProvider/GameNetProvider.cs
Normal file
212
Src/Unity/Scripts/NetProvider/GameNetProvider.cs
Normal file
@@ -0,0 +1,212 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using BITKit;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BITKit.Net
|
||||
{
|
||||
[Serializable]
|
||||
public sealed class GameNet : INetClient,INetServer,INetProvider
|
||||
{
|
||||
private static INetClient _netClientImplementation=>GameNetProvider.NetClient;
|
||||
private static INetProvider _netProviderImplementation=>GameNetProvider.NetProvider;
|
||||
private static INetServer _netServerImplementation1=>GameNetProvider.NetServer;
|
||||
|
||||
object INetClient.Source => _netClientImplementation.Source;
|
||||
object INetServer.Source => _netServerImplementation1.Source;
|
||||
|
||||
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 bool IsConnected => _netClientImplementation.IsConnected;
|
||||
|
||||
bool INetServer.ManualTick
|
||||
{
|
||||
get => _netServerImplementation1.ManualTick;
|
||||
set => _netServerImplementation1.ManualTick = value;
|
||||
}
|
||||
bool INetClient.ManualTick
|
||||
{
|
||||
get => _netClientImplementation.ManualTick;
|
||||
set => _netClientImplementation.ManualTick = value;
|
||||
}
|
||||
|
||||
public int Ping => _netClientImplementation.Ping;
|
||||
|
||||
public int Id => _netClientImplementation.Id;
|
||||
|
||||
public void Disconnect()
|
||||
{
|
||||
_netClientImplementation.Disconnect();
|
||||
}
|
||||
|
||||
public UniTask<bool> Connect(string address = "localhost", ushort port = 27014)
|
||||
{
|
||||
return _netClientImplementation.Connect(address, port);
|
||||
}
|
||||
|
||||
public void SendServerMessage(string message)
|
||||
{
|
||||
_netClientImplementation.SendServerMessage(message);
|
||||
}
|
||||
|
||||
public void ServerCommand<T>(T command = default)
|
||||
{
|
||||
_netProviderImplementation.ServerCommand(command);
|
||||
}
|
||||
|
||||
public void AllClientCommand<T>(T command = default)
|
||||
{
|
||||
_netProviderImplementation.AllClientCommand(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);
|
||||
}
|
||||
|
||||
public void Tick()
|
||||
{
|
||||
_netProviderImplementation.Tick();
|
||||
}
|
||||
|
||||
public void HandShake()
|
||||
{
|
||||
_netProviderImplementation.HandShake();
|
||||
}
|
||||
|
||||
public event Action<int> OnClientConnected
|
||||
{
|
||||
add => _netServerImplementation1.OnClientConnected += value;
|
||||
remove => _netServerImplementation1.OnClientConnected -= value;
|
||||
}
|
||||
|
||||
public event Action<int> OnClientDisconnected
|
||||
{
|
||||
add => _netServerImplementation1.OnClientDisconnected += value;
|
||||
remove => _netServerImplementation1.OnClientDisconnected -= value;
|
||||
}
|
||||
|
||||
public event Action OnStartServer
|
||||
{
|
||||
add => _netServerImplementation1.OnStartServer += value;
|
||||
remove => _netServerImplementation1.OnStartServer -= value;
|
||||
}
|
||||
|
||||
public event Action OnStopServer
|
||||
{
|
||||
add => _netServerImplementation1.OnStopServer += value;
|
||||
remove => _netServerImplementation1.OnStopServer -= value;
|
||||
}
|
||||
|
||||
public void StartServer(ushort port = 27014)
|
||||
{
|
||||
_netServerImplementation1.StartServer(port);
|
||||
}
|
||||
|
||||
public void StopServer(bool dispose = false)
|
||||
{
|
||||
_netServerImplementation1.StopServer(dispose);
|
||||
}
|
||||
|
||||
public bool IsRunningServer => _netServerImplementation1.IsRunningServer;
|
||||
|
||||
public void SendMessageToClient(int id, string message)
|
||||
{
|
||||
_netServerImplementation1.SendMessageToClient(id, message);
|
||||
}
|
||||
|
||||
public void SendMessageToAll(string message)
|
||||
{
|
||||
_netServerImplementation1.SendMessageToAll(message);
|
||||
}
|
||||
|
||||
public IDictionary<int, EndPoint> Connections => _netServerImplementation1.Connections;
|
||||
|
||||
public void AddCommandListenerWithId<T>(Action<int, T> handle)
|
||||
{
|
||||
_netServerImplementation1.AddCommandListenerWithId(handle);
|
||||
}
|
||||
}
|
||||
|
||||
public class GameNetProvider : MonoBehaviour
|
||||
{
|
||||
public static INetClient NetClient { get; private set; }
|
||||
public static INetServer NetServer { get; private set; }
|
||||
public static INetProvider NetProvider=>NetClient.IsConnected?NetClient as INetProvider:NetServer as INetProvider;
|
||||
|
||||
[SerializeField] private MonoBehaviour netClient;
|
||||
[SerializeField] private MonoBehaviour netServer;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
NetClient = netClient as INetClient;
|
||||
NetServer = netServer as INetServer;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user