Before Rebuild NetProvider

This commit is contained in:
CortexCore
2025-01-24 10:36:58 +08:00
parent 0823a00396
commit 4b72602bfa
13 changed files with 402 additions and 387 deletions

View File

@@ -1,6 +1,9 @@
using MemoryPack;
namespace BITKit.Net.Examples
{
public struct NetClientPingCommand
[MemoryPackable]
public partial struct NetClientPingCommand
{
public int Ping;
}

View File

@@ -1,9 +1,13 @@
using System;
using System.Data;
using System.Runtime.InteropServices;
using MemoryPack;
namespace BITKit.Net.Examples
{
public struct LogTime
[MemoryPackable]
[StructLayout(LayoutKind.Auto)]
public partial struct LogTime
{
public Guid Id;
public DateTime CreateTime;
@@ -15,7 +19,9 @@ namespace BITKit.Net.Examples
}
#endif
}
public struct SimplePing
[MemoryPackable]
[StructLayout(LayoutKind.Auto)]
public partial struct SimplePing
{
public DateTime StartTime;
public DateTime EndTime;

View File

@@ -6,8 +6,11 @@
*/
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Net;
using System.Reflection;
using System.Text;
using Cysharp.Threading.Tasks;
#if UNITY
using UnityEngine;
@@ -91,6 +94,7 @@ namespace BITKit
/// </summary>
public interface INetProvider
{
public uint TickRate { get; set; }
/// <summary>
/// 向服务端发送指令
/// </summary>
@@ -324,7 +328,7 @@ namespace BITKit
/// <param name="address">服务端地址</param>
/// <param name="port">端口</param>
/// <returns></returns>
UniTask<bool> Connect(string address = "localhost", ushort port = 27014);
UniTask<bool> Connect(string address = "127.0.0.1", ushort port = 27014);
/// <summary>
/// 向服务端发送消息
@@ -332,4 +336,48 @@ namespace BITKit
/// <param name="message">消息</param>
void SendServerMessage(string message);
}
public class NetProviderCommon
{
public readonly GenericEvent Events = new();
public readonly ConcurrentDictionary<int, UniTaskCompletionSource<object>> P2P = new();
public readonly ConcurrentDictionary<string,Func<object,UniTask<object>>> Rpc = new();
public readonly ConcurrentDictionary<string,MethodInfo> RpcMethods = new();
public readonly ConcurrentDictionary<string,EventInfo> RpcEvents = new();
public readonly ConcurrentDictionary<string,object> RpcHandles = new();
public readonly ConcurrentDictionary<int,DateTime> LastHeartbeat = new();
public readonly ConcurrentQueue<(int id,byte[] bytes)> SendQueue = new();
public readonly ConcurrentDictionary<int,int> DropCount = new();
public readonly byte[] HeartBeat = new byte[] { (byte)NetCommandType.Heartbeat };
public void AddRpcHandle(object rpcHandle)
{
var reportBuilder = new StringBuilder();
reportBuilder.AppendLine($"正在通过反射注册{rpcHandle.GetType().Name}");
foreach (var methodInfo in rpcHandle.GetType().GetMethods())
{
var att = methodInfo.GetCustomAttribute<NetRpcAttribute>();
if (att is null) continue;
RpcMethods.AddOrUpdate(methodInfo.Name, methodInfo, (s, info) => methodInfo);
RpcHandles.AddOrUpdate(methodInfo.Name, rpcHandle, (s, o) => rpcHandle);
reportBuilder.AppendLine($"Add [{methodInfo.Name}] as MethodInfo");
}
foreach (var eventInfo in rpcHandle.GetType().GetEvents())
{
var att = eventInfo.GetCustomAttribute<NetRpcAttribute>();
if (att is null) continue;
RpcEvents.TryAdd(eventInfo.Name, eventInfo);
RpcHandles.TryAdd(eventInfo.Name, rpcHandle);
reportBuilder.AppendLine($"Add [{eventInfo.Name}] as EventInfo");
}
}
}
}