feat(network)!: add v2 framing, pooled buffers and bounded dispatch
Publish UPM package / publish (push) Successful in 2s
Publish NuGet packages / publish (push) Successful in 3m5s

This commit is contained in:
2026-09-29 10:14:47 +08:00
parent c7c45b26f8
commit 93c2b8520b
36 changed files with 752 additions and 398 deletions
@@ -9,6 +9,8 @@ namespace ShrinkNetwork
{
private readonly Dictionary<int, ShrinkNetworkMessageMeta> _opcodeToMeta = new();
private readonly Dictionary<Type, ShrinkNetworkMessageMeta> _typeToMeta = new();
private readonly HashSet<string> _routes = new(StringComparer.Ordinal);
public IReadOnlyCollection<ShrinkNetworkMessageMeta> Registrations => _typeToMeta.Values;
public void Register<TMessage>(int opcode, string? route = null) where TMessage : IShrinkNetworkMessage
=> Register(typeof(TMessage), opcode, route);
@@ -24,6 +26,8 @@ namespace ShrinkNetwork
if (_typeToMeta.ContainsKey(messageType))
throw new InvalidOperationException($"Message type {messageType.FullName} is already registered.");
route = string.IsNullOrWhiteSpace(route) ? null : route.Trim();
if (route != null && !_routes.Add(route)) throw new InvalidOperationException($"SHRINK002 Route '{route}' is already registered.");
var meta = new ShrinkNetworkMessageMeta(opcode, messageType, route);
_opcodeToMeta.Add(opcode, meta);
_typeToMeta.Add(messageType, meta);
+9
View File
@@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using Cysharp.Threading.Tasks;
namespace ShrinkNetwork
@@ -21,6 +22,8 @@ namespace ShrinkNetwork
public Func<ShrinkNetworkContext, object, UniTask<object?>> Handler = null!;
}
private readonly Dictionary<Type, MethodInfo> _bindingMethods = new();
public IReadOnlyDictionary<Type, MethodInfo> CaptureBindings() => new Dictionary<Type, MethodInfo>(_bindingMethods);
private readonly Dictionary<Type, MessageHandlerRegistration> _messageHandlers = new();
private readonly Dictionary<Type, RequestHandlerRegistration> _requestHandlers = new();
@@ -28,7 +31,9 @@ namespace ShrinkNetwork
ShrinkNetworkPermissionRequirement requirement = default)
where TMessage : IShrinkNetworkMessage
{
if (handler == null) throw new ArgumentNullException(nameof(handler));
RegisterHandler(typeof(TMessage), (context, message) => handler(context, (TMessage)message), requirement);
_bindingMethods[typeof(TMessage)] = handler.Method;
}
public void RegisterHandler(Type messageType, Func<ShrinkNetworkContext, object, UniTask> handler,
@@ -41,6 +46,7 @@ namespace ShrinkNetwork
if (_messageHandlers.ContainsKey(messageType) || _requestHandlers.ContainsKey(messageType))
throw new InvalidOperationException($"Handler already exists for {messageType.FullName}.");
_bindingMethods[messageType] = handler.Method;
_messageHandlers.Add(messageType, new MessageHandlerRegistration
{
Requirement = requirement,
@@ -53,8 +59,10 @@ namespace ShrinkNetwork
where TRequest : IShrinkNetworkRequest
where TResponse : class, IShrinkNetworkResponse
{
if (handler == null) throw new ArgumentNullException(nameof(handler));
RegisterRequestHandler(typeof(TRequest), typeof(TResponse),
async (context, message) => await handler(context, (TRequest)message), requirement);
_bindingMethods[typeof(TRequest)] = handler.Method;
}
public void RegisterRequestHandler(Type requestType, Type responseType,
@@ -70,6 +78,7 @@ namespace ShrinkNetwork
if (_messageHandlers.ContainsKey(requestType) || _requestHandlers.ContainsKey(requestType))
throw new InvalidOperationException($"Handler already exists for {requestType.FullName}.");
_bindingMethods[requestType] = handler.Method;
_requestHandlers.Add(requestType, new RequestHandlerRegistration
{
ResponseType = responseType,