Теоретически должно работать!!!

This commit is contained in:
2025-03-10 16:35:56 +03:00
parent e3710ffe20
commit f4a92e4aac
6 changed files with 128 additions and 47 deletions
@@ -10,38 +10,27 @@ namespace mROA.Implementation.Backend
{
public class ContextRepository : IContextRepository
{
public static object[] EventBinders = new object[]{};
private int _debugId = -1;
private const int StartupSize = 1024;
private const int GrowSize = 128;
public static object[] EventBinders = new object[] { };
private static int LastDebugId = -1;
private int _debugId = -1;
private Task<int> _lastIndexFinder = Task.FromResult(0);
private IRepresentationModuleProducer? _representationModuleProducer;
// [CanBeNull]
private Dictionary<int, object?> _singletons;
private object?[] _storage;
private Task<int> _lastIndexFinder = Task.FromResult(0);
private const int StartupSize = 1024;
private const int GrowSize = 128;
public ContextRepository()
{
_storage = new object[StartupSize];
}
public void FillSingletons(params Assembly[] assembly)
{
var types = assembly.SelectMany(x => x.GetTypes()).Where(type =>
type is { IsClass: true, IsAbstract: false, IsGenericType: false } &&
type.GetCustomAttributes(typeof(SharedObjectSingletonAttribute), true).Length > 0);
_singletons =
types.ToDictionary(
t => t.GetInterfaces().FirstOrDefault(i =>
i.GetCustomAttributes(typeof(SharedObjectInterfaceAttribute), true).Length > 0)!.GetHashCode(),
Activator.CreateInstance);
}
public int ResisterObject<T>(object o, IEndPointContext context)
{
if (!_lastIndexFinder.IsCompleted)
@@ -49,10 +38,11 @@ namespace mROA.Implementation.Backend
_storage[_lastIndexFinder.Result] = o;
EventBinders.OfType<IEventBinder<T>>().FirstOrDefault()?.BindEvents((T)o, context);
var last = _lastIndexFinder.Result;
_lastIndexFinder = Task.Run(FindLastIndex);
EventBinders.OfType<IEventBinder<T>>().FirstOrDefault()
?.BindEvents((T)o, context, _representationModuleProducer!, last);
return last;
}
@@ -68,12 +58,6 @@ namespace mROA.Implementation.Backend
return (T)GetObject(sharedObjectShellShell.Identifier.ContextId);
}
public object GetObject(int id)
{
// Debug.Log($"Reading object {id} from repository with debug ID {_debugId}");
return (id == -1 || _storage.Length <= id ? null : _storage[id]) ?? throw new NullReferenceException();
}
public T GetObject<T>(int id)
{
return id == -1 || _storage.Length <= id
@@ -93,6 +77,32 @@ namespace mROA.Implementation.Backend
return index == -1 ? ResisterObject<T>(o, context) : index;
}
public void Inject<T>(T dependency)
{
if (dependency is IRepresentationModuleProducer moduleProducer)
{
_representationModuleProducer = moduleProducer;
}
}
public void FillSingletons(params Assembly[] assembly)
{
var types = assembly.SelectMany(x => x.GetTypes()).Where(type =>
type is { IsClass: true, IsAbstract: false, IsGenericType: false } &&
type.GetCustomAttributes(typeof(SharedObjectSingletonAttribute), true).Length > 0);
_singletons =
types.ToDictionary(
t => t.GetInterfaces().FirstOrDefault(i =>
i.GetCustomAttributes(typeof(SharedObjectInterfaceAttribute), true).Length > 0)!.GetHashCode(),
Activator.CreateInstance);
}
public object GetObject(int id)
{
// Debug.Log($"Reading object {id} from repository with debug ID {_debugId}");
return (id == -1 || _storage.Length <= id ? null : _storage[id]) ?? throw new NullReferenceException();
}
private int FindLastIndex()
{
for (var i = 0; i < _storage.Length; i++)
@@ -106,9 +116,5 @@ namespace mROA.Implementation.Backend
_storage = nextStorage;
return _storage.Length;
}
public void Inject<T>(T dependency)
{
}
}
}
+15
View File
@@ -0,0 +1,15 @@
using System;
namespace mROA.Abstract
{
public class EventBinder<T> : IEventBinder<T>
{
public Action<T, IEndPointContext, IRepresentationModuleProducer, int> BindAction { get; set; }
public void BindEvents(T source, IEndPointContext context,
IRepresentationModuleProducer representationModuleProducer, int index)
{
BindAction(source, context, representationModuleProducer, index);
}
}
}
@@ -75,8 +75,10 @@ namespace mROA.Implementation.Frontend
var cancelRequest =
_representationModule!.GetMessageAsync<CancelRequest>(
messageType: MessageType.CancelRequest, token: token);
Task.WaitAny(defaultRequest, cancelRequest);
var eventRequest =
_representationModule!.GetMessageAsync<DefaultCallRequest>(
messageType: MessageType.EventRequest, token: token);
Task.WaitAny(defaultRequest, cancelRequest, eventRequest);
#if TRACE
Console.WriteLine("Request received");
#endif
@@ -89,7 +91,7 @@ namespace mROA.Implementation.Frontend
tokenSource.Cancel();
_executeModule.Execute(req, _contextRepository, _representationModule);
}
else
else if (defaultRequest.IsCompleted)
{
tokenSource.Cancel();
var request = defaultRequest.Result;
@@ -97,7 +99,7 @@ namespace mROA.Implementation.Frontend
var result = _executeModule.Execute(request, _contextRepository, _representationModule);
var resultType = MessageType.Unknown;
switch (result)
{
case FinalCommandExecution:
@@ -113,6 +115,12 @@ namespace mROA.Implementation.Frontend
_representationModule.PostCallMessage(request.Id, resultType, result, result.GetType());
}
else
{
tokenSource.Cancel();
var request = defaultRequest.Result;
_executeModule.Execute(request, _contextRepository, _representationModule);
}
}
}
catch