diff --git a/Example.Frontend/Program.cs b/Example.Frontend/Program.cs index 6ac3cb7..08ec27a 100644 --- a/Example.Frontend/Program.cs +++ b/Example.Frontend/Program.cs @@ -73,6 +73,7 @@ class Program var page = disposingPrinter.Print("Test Page", new CancellationToken()).GetAwaiter().GetResult(); Console.WriteLine("Page printed"); + Console.WriteLine(page.Value.ToString()); var data = page.Value.GetData(); Console.WriteLine("Data : {0}", Encoding.UTF8.GetString(data)); diff --git a/mROA/Abstract/IContextRepository.cs b/mROA/Abstract/IContextRepository.cs index a54d5d6..c298688 100644 --- a/mROA/Abstract/IContextRepository.cs +++ b/mROA/Abstract/IContextRepository.cs @@ -1,4 +1,5 @@ using System; +using mROA.Implementation; namespace mROA.Abstract { @@ -6,7 +7,7 @@ namespace mROA.Abstract { int ResisterObject(object o); void ClearObject(int id); - object GetObject(int id); + T GetObjectBySharedObject(SharedObject sharedObject); T? GetObject(int id); object GetSingleObject(Type type); int GetObjectIndex(object o); diff --git a/mROA/Implementation/Backend/BasicExecutionModule.cs b/mROA/Implementation/Backend/BasicExecutionModule.cs index 8e8114e..1ee01c8 100644 --- a/mROA/Implementation/Backend/BasicExecutionModule.cs +++ b/mROA/Implementation/Backend/BasicExecutionModule.cs @@ -50,7 +50,7 @@ namespace mROA.Implementation.Backend throw new Exception($"Command {command.CommandId} not found"); var context = command.ObjectId != -1 - ? contextRepository.GetObject(command.ObjectId) + ? contextRepository.GetObject(command.ObjectId) : contextRepository.GetSingleObject(currentCommand.DeclaringType!); var parameter = command.Parameter; diff --git a/mROA/Implementation/Backend/ContextRepository.cs b/mROA/Implementation/Backend/ContextRepository.cs index 2831a38..b771a3d 100644 --- a/mROA/Implementation/Backend/ContextRepository.cs +++ b/mROA/Implementation/Backend/ContextRepository.cs @@ -10,8 +10,11 @@ namespace mROA.Implementation.Backend { public class ContextRepository : IContextRepository { - private Dictionary? _singletons; - private object?[] _storage = new object[StartupSize]; + private int _debugId = -1; + private static int LastDebugId = -1; + // [CanBeNull] + private Dictionary _singletons; + private object?[] _storage; private Task _lastIndexFinder = Task.FromResult(0); @@ -19,6 +22,11 @@ namespace mROA.Implementation.Backend 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 => @@ -50,8 +58,14 @@ namespace mROA.Implementation.Backend _lastIndexFinder = Task.FromResult(id); } + public T GetObjectBySharedObject(SharedObject sharedObject) + { + return (T)GetObject(sharedObject.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(); } diff --git a/mROA/Implementation/Backend/MultiClientContextRepository.cs b/mROA/Implementation/Backend/MultiClientContextRepository.cs index 5aa906a..4d76987 100644 --- a/mROA/Implementation/Backend/MultiClientContextRepository.cs +++ b/mROA/Implementation/Backend/MultiClientContextRepository.cs @@ -37,9 +37,14 @@ namespace mROA.Implementation.Backend GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId()).ClearObject(id); } + public T GetObjectBySharedObject(SharedObject sharedObject) + { + return GetRepository(sharedObject.OwnerId).GetObject(sharedObject.ContextId); + } + public object GetObject(int id) { - return GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId()).GetObject(id); + return GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId()).GetObject(id); } public T? GetObject(int id) diff --git a/mROA/Implementation/CommandExecution/AsyncCommandExecution.cs b/mROA/Implementation/CommandExecution/AsyncCommandExecution.cs new file mode 100644 index 0000000..fcddf6e --- /dev/null +++ b/mROA/Implementation/CommandExecution/AsyncCommandExecution.cs @@ -0,0 +1,12 @@ +using System; +using mROA.Abstract; + +namespace mROA.Implementation.CommandExecution +{ + public class AsyncCommandExecution : ICommandExecution + { + public Guid Id { get; set; } + public int ClientId { get; set; } + public int CommandId { get; set; } + } +} \ No newline at end of file diff --git a/mROA/Implementation/CommandExecution/ExceptionCommandExecution.cs b/mROA/Implementation/CommandExecution/ExceptionCommandExecution.cs index 6d18129..c9d32ab 100644 --- a/mROA/Implementation/CommandExecution/ExceptionCommandExecution.cs +++ b/mROA/Implementation/CommandExecution/ExceptionCommandExecution.cs @@ -16,11 +16,4 @@ namespace mROA.Implementation.CommandExecution return new RemoteException(Exception) { CallRequestId = Id }; } } - - public class AsyncCommandExecution : ICommandExecution - { - public Guid Id { get; set; } - public int ClientId { get; set; } - public int CommandId { get; set; } - } } \ No newline at end of file diff --git a/mROA/Implementation/RemoteContextRepository.cs b/mROA/Implementation/RemoteContextRepository.cs index 0b7611d..70cd543 100644 --- a/mROA/Implementation/RemoteContextRepository.cs +++ b/mROA/Implementation/RemoteContextRepository.cs @@ -8,6 +8,7 @@ namespace mROA.Implementation { private IRepresentationModuleProducer? _representationProducer; public static Dictionary RemoteTypes = new(); + public int ResisterObject(object o) { throw new NotSupportedException(); @@ -18,6 +19,17 @@ namespace mROA.Implementation throw new NotSupportedException(); } + public T GetObjectBySharedObject(SharedObject sharedObject) + { + if (_representationProducer == null) + throw new NullReferenceException("representation producer is not initialized"); + + if (!RemoteTypes.TryGetValue(typeof(T), out var remoteType)) throw new NotSupportedException(); + var remote = (T)Activator.CreateInstance(remoteType, sharedObject.ContextId, + _representationProducer.Produce(sharedObject.OwnerId))!; + return remote; + } + public object GetObject(int id) { throw new NotSupportedException(); @@ -27,9 +39,10 @@ namespace mROA.Implementation { if (_representationProducer == null) throw new NullReferenceException("representation producer is not initialized"); - + if (!RemoteTypes.TryGetValue(typeof(T), out var remoteType)) throw new NotSupportedException(); - var remote = (T)Activator.CreateInstance(remoteType, id, _representationProducer.Produce(TransmissionConfig.OwnershipRepository.GetOwnershipId()))!; + var remote = (T)Activator.CreateInstance(remoteType, id, + _representationProducer.Produce(TransmissionConfig.OwnershipRepository.GetOwnershipId()))!; return remote; } @@ -37,8 +50,9 @@ namespace mROA.Implementation { if (_representationProducer == null) throw new NullReferenceException("representation producer is not initialized"); - - return Activator.CreateInstance(RemoteTypes[type], -1, _representationProducer.Produce(TransmissionConfig.OwnershipRepository.GetOwnershipId()))!; + + return Activator.CreateInstance(RemoteTypes[type], -1, + _representationProducer.Produce(TransmissionConfig.OwnershipRepository.GetOwnershipId()))!; } public int GetObjectIndex(object o) @@ -47,6 +61,7 @@ namespace mROA.Implementation { return remote.Id; } + throw new NotSupportedException(); } diff --git a/mROA/Implementation/RemoteObjectBase.cs b/mROA/Implementation/RemoteObjectBase.cs index 5afc4c9..00f89c4 100644 --- a/mROA/Implementation/RemoteObjectBase.cs +++ b/mROA/Implementation/RemoteObjectBase.cs @@ -39,17 +39,28 @@ namespace mROA.Implementation _representationModule.GetMessageAsync(requestId: request.Id, MessageType.ExceptionCommandExecution, localTokenSource.Token); + cancellationToken.Register(async () => + { + Console.WriteLine("Cancelling task"); + await _representationModule.PostCallMessageAsync(request.Id, MessageType.CancelRequest, + new CancelRequest + { + Id = request.Id + }); + localTokenSource.Cancel(); + }); + Task.WaitAny(new Task[] { successResponse, errorResponse }, cancellationToken); - if (cancellationToken.IsCancellationRequested) - { - await _representationModule.PostCallMessageAsync(request.Id, MessageType.CancelRequest, request.Id); - localTokenSource.Cancel(); - cancellationToken.ThrowIfCancellationRequested(); - } + // if (cancellationToken.IsCancellationRequested) + // { + // await _representationModule.PostCallMessageAsync(request.Id, MessageType.CancelRequest, request.Id); + // localTokenSource.Cancel(); + // cancellationToken.ThrowIfCancellationRequested(); + // } if (successResponse.IsCompletedSuccessfully) { @@ -96,12 +107,6 @@ namespace mROA.Implementation Console.WriteLine($"Handling message"); - // if (cancellationToken.IsCancellationRequested) - // { - // localTokenSource.Cancel(); - // return; - // } - if (successResponse.IsCompletedSuccessfully) return; @@ -115,5 +120,10 @@ namespace mROA.Implementation return; CallAsync(-1).Wait(); } + + public override string ToString() + { + return $"{{Id : {_id}, OwnerId : {OwnerId} }}"; + } } } \ No newline at end of file diff --git a/mROA/Implementation/SharedObject.cs b/mROA/Implementation/SharedObject.cs index cf44c46..8008b1f 100644 --- a/mROA/Implementation/SharedObject.cs +++ b/mROA/Implementation/SharedObject.cs @@ -69,7 +69,7 @@ namespace mROA.Implementation set { _contextId = value; - Value = GetDefaultContextRepository().GetObject(_contextId)!; + Value = GetDefaultContextRepository().GetObjectBySharedObject(this)!; } }