1
This commit is contained in:
@@ -10,6 +10,7 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using BITKit.Net.Examples;
|
||||
|
||||
@@ -32,7 +33,8 @@ namespace BITKit.Net
|
||||
private int _index = 1001;
|
||||
private readonly ConcurrentDictionary<int, object> _p2p = new();
|
||||
private readonly ConcurrentDictionary<string,Func<object,UniTask<object>>> _rpc = new();
|
||||
|
||||
private readonly ConcurrentDictionary<string,MethodInfo> _rpcMethods = new();
|
||||
private readonly ConcurrentDictionary<string,object> _rpcHandles = new();
|
||||
|
||||
public KCPNetServer()
|
||||
{
|
||||
@@ -148,7 +150,19 @@ namespace BITKit.Net
|
||||
OnClientDisconnected?.Invoke(Id);
|
||||
BIT4Log.Log<KCPNetServer>($"{Id}已断开");
|
||||
}
|
||||
private async void OnData(int Id, ArraySegment<byte> bytes, KcpChannel channel)
|
||||
|
||||
private void OnData(int Id, ArraySegment<byte> bytes, KcpChannel channel)
|
||||
{
|
||||
try
|
||||
{
|
||||
OnDataInternel(Id, bytes, channel);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
BIT4Log.LogException(e);
|
||||
}
|
||||
}
|
||||
private async void OnDataInternel(int Id, ArraySegment<byte> bytes, KcpChannel channel)
|
||||
{
|
||||
using var ms = new MemoryStream(bytes.ToArray());
|
||||
using var reader = new BinaryReader(ms);
|
||||
@@ -191,37 +205,60 @@ namespace BITKit.Net
|
||||
case NetCommandType.GetFromServer:
|
||||
{
|
||||
var requestId = reader.ReadInt32();
|
||||
var commandObj = BITBinary.Read(reader);
|
||||
|
||||
|
||||
using var _ms = new MemoryStream();
|
||||
using var _writer = new BinaryWriter(_ms);
|
||||
_writer.Write((byte)NetCommandType.ReturnToClient);
|
||||
_writer.Write(requestId);
|
||||
using var returnMS = new MemoryStream();
|
||||
using var returnWriter = new BinaryWriter(returnMS);
|
||||
returnWriter.Write((byte)NetCommandType.ReturnToClient);
|
||||
returnWriter.Write(requestId);
|
||||
|
||||
try
|
||||
{
|
||||
if (_rpc.TryGetValue(commandObj.GetType()!.FullName!, out var func) is false)
|
||||
if (reader.ReadBoolean())
|
||||
{
|
||||
_writer.Write(false);
|
||||
_writer.Write("未找到对应的Rpc方法");
|
||||
var path = reader.ReadString();
|
||||
var pars = BITBinary.Read(reader).As<object[]>();
|
||||
if (_rpcMethods.TryGetValue(path, out var methodInfo))
|
||||
{
|
||||
var value = methodInfo.Invoke(_rpcHandles[path], pars);
|
||||
returnWriter.Write(true);
|
||||
BITBinary.Write(returnWriter, value);
|
||||
}
|
||||
else
|
||||
{
|
||||
returnWriter.Write(false);
|
||||
returnWriter.Write("未找到对应的Rpc方法");
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
var commandObj = BITBinary.Read(reader);
|
||||
if (_rpc.TryGetValue(commandObj.GetType()!.FullName!, out var func))
|
||||
{
|
||||
var value = await func.As<Func<object, UniTask<object>>>().Invoke(commandObj);
|
||||
_writer.Write(true);
|
||||
BITBinary.Write(_writer, value);
|
||||
try
|
||||
{
|
||||
var value = await func.As<Func<object, UniTask<object>>>().Invoke(commandObj);
|
||||
returnWriter.Write(true);
|
||||
BITBinary.Write(returnWriter, value);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
BIT4Log.LogException(e);
|
||||
returnWriter.Write(false);
|
||||
returnWriter.Write(e.Message);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
else
|
||||
{
|
||||
BIT4Log.LogException(e);
|
||||
_writer.Write(false);
|
||||
_writer.Write(e.Message);
|
||||
returnWriter.Write(false);
|
||||
returnWriter.Write("未找到对应的Rpc方法");
|
||||
}
|
||||
}
|
||||
var _bytes = _ms.ToArray();
|
||||
server.Send(Id, _bytes, KcpChannel.Reliable);
|
||||
{
|
||||
var _bytes = returnMS.ToArray();
|
||||
server.Send(Id, _bytes, KcpChannel.Reliable);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -273,18 +310,27 @@ namespace BITKit.Net
|
||||
Send(id,NetCommandType.Command,command);
|
||||
}
|
||||
|
||||
public UniTask<T> GetFromServer<T>(T command = default)
|
||||
public UniTask<T> GetFromServer<T>(string path=default,T command = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public async UniTask<T> GetFromClient<T>(int id, T command = default)
|
||||
public async UniTask<T> GetFromClient<T>(int id,string path=default, T command = default)
|
||||
{
|
||||
var index = _index++;
|
||||
using var ms = new MemoryStream();
|
||||
await using var writer = new BinaryWriter(ms);
|
||||
writer.Write((byte)NetCommandType.GetFromClient);
|
||||
writer.Write(index);
|
||||
if (string.IsNullOrEmpty(path))
|
||||
{
|
||||
writer.Write(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.Write(true);
|
||||
writer.Write(path);
|
||||
}
|
||||
BITBinary.Write(writer,command);
|
||||
|
||||
var bytes = ms.ToArray();
|
||||
|
Reference in New Issue
Block a user