feat(network)!: add v2 framing, pooled buffers and bounded dispatch
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user