Local endpoint added successful and negative ownerId implemented

This commit is contained in:
2025-05-05 16:30:23 +03:00
parent cc5f82c7e7
commit ff4c3ecee3
27 changed files with 110 additions and 114 deletions
@@ -8,7 +8,7 @@ namespace mROA.Implementation.Backend
public int GetNextIdentity()
{
return ++_currentId;
return -++_currentId;
}
public void Inject<T>(T dependency)
@@ -23,7 +23,6 @@ namespace mROA.Implementation.Backend
{
var repo = new ContextRepository();
repo.FillSingletons(assemblies);
TransmissionConfig.RealContextRepository = repo;
builder.Modules.Add(repo);
}
@@ -79,7 +79,7 @@ namespace mROA.Implementation.Backend
#if TRACE
Console.WriteLine("Disposing object");
#endif
contextRepository.ClearObject(command.ObjectId);
contextRepository.ClearObject(command.ObjectId, endPointContext);
}
return result;
@@ -100,7 +100,7 @@ namespace mROA.Implementation.Backend
{
var context = command.ObjectId.ContextId != -1
? contextRepository.GetObject<object>(command.ObjectId, endPointContext)
: contextRepository.GetSingleObject(invoker.SuitableType, command.ObjectId.OwnerId);
: contextRepository.GetSingleObject(invoker.SuitableType, endPointContext);
return context;
}
@@ -205,14 +205,10 @@ namespace mROA.Implementation.Backend
};
_cancellationRepo?.FreeCancelation(command.Id);
var multiClientOwnershipRepository =
TransmissionConfig.OwnershipRepository as MultiClientOwnershipRepository;
multiClientOwnershipRepository?.RegisterOwnership(representationModule.Id);
if (invoker.IsTrusted)
representationModule.PostCallMessage(command.Id, EMessageType.FinishedCommandExecution,
payload, context);
multiClientOwnershipRepository?.FreeOwnership();
});
return new AsyncCommandExecution
@@ -254,13 +250,9 @@ namespace mROA.Implementation.Backend
Result = finalResult
};
_cancellationRepo!.FreeCancelation(command.Id);
var multiClientOwnershipRepository =
TransmissionConfig.OwnershipRepository as MultiClientOwnershipRepository;
multiClientOwnershipRepository?.RegisterOwnership(representationModule.Id);
representationModule.PostCallMessage(command.Id, EMessageType.FinishedCommandExecution,
payload, context);
multiClientOwnershipRepository?.FreeOwnership();
});
return new AsyncCommandExecution
+1 -1
View File
@@ -23,7 +23,7 @@ namespace mROA.Implementation.Backend
public IChannelInteractionModule GetInteraction(int id)
{
return _connections!.GetValueOrDefault(id, null) ?? throw new Exception("No connection found");
return _connections!.GetValueOrDefault(id, null) ?? _connections!.GetValueOrDefault(-id, null) ?? throw new Exception("No connection found");
}
public event ConnectionHandler? OnConnected;
@@ -39,7 +39,7 @@ namespace mROA.Implementation.Backend
return last;
}
public void ClearObject(ComplexObjectIdentifier id)
public void ClearObject(ComplexObjectIdentifier id, IEndPointContext context)
{
_storage.Free(id.ContextId);
}
@@ -56,7 +56,7 @@ namespace mROA.Implementation.Backend
return (T)value;
}
public object GetSingleObject(Type type, int ownerId)
public object GetSingleObject(Type type, IEndPointContext context)
{
return _singletons.GetValueOrDefault(type.GetHashCode()) ??
throw new ArgumentException("Unregistered singleton type");
@@ -57,16 +57,17 @@ namespace mROA.Implementation.Backend
var extractor = new RequestExtractor();
var context = new EndPointContext
{
HostId = 0, OwnerId = interaction.Id
HostId = 0, OwnerId = -interaction.Id
};
extractor.Inject(interaction);
if (_contextRepository is IContextRepositoryHub contextHub)
context.RealRepository = contextHub.GetRepository(interaction.Id);
else
context.RealRepository = _contextRepository!;
context.RemoteRepository = _remoteContextRepository!;
extractor.Inject(context);
extractor.Inject(_methodRepository);
extractor.Inject(_serializationToolkit);
extractor.Inject(_executeModule);
@@ -22,31 +22,31 @@ namespace mROA.Implementation.Backend
public int ResisterObject<T>(object o, IEndPointContext context)
{
var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId());
var repository = GetRepositoryByClientId(context.OwnerId);
return repository.ResisterObject<T>(o, context);
}
public void ClearObject(ComplexObjectIdentifier id)
public void ClearObject(ComplexObjectIdentifier id, IEndPointContext context)
{
var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId());
repository.ClearObject(id);
var repository = GetRepositoryByClientId(context.OwnerId);
repository.ClearObject(id, context);
}
public T GetObject<T>(ComplexObjectIdentifier id, IEndPointContext context)
{
var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId());
var repository = GetRepositoryByClientId(context.OwnerId);
return repository.GetObject<T>(id, context);
}
public object GetSingleObject(Type type, int ownerId)
public object GetSingleObject(Type type, IEndPointContext context)
{
var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId());
return repository.GetSingleObject(type, ownerId);
var repository = GetRepositoryByClientId(context.OwnerId);
return repository.GetSingleObject(type, context);
}
public int GetObjectIndex<T>(object o, IEndPointContext context)
{
var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId());
var repository = GetRepositoryByClientId(context.OwnerId);
return repository.GetObjectIndex<T>(o, context);
}
@@ -94,11 +94,11 @@ namespace mROA.Implementation.Backend
{
case EMessageType.ClientConnect:
context.HostId = 0;
context.OwnerId = interaction.ConnectionId;
context.OwnerId = -interaction.ConnectionId;
Task.Run(async () => await streamExtractor.LoopedReceive(cts.Token));
_ = streamExtractor.SendFromChannel(interaction.TrustedPostChanel, cts.Token);
interaction.PostMessageAsync(new NetworkMessageHeader(_serialization!,
new IdAssignment { Id = -interaction.ConnectionId }, null));
new IdAssignment { Id = interaction.ConnectionId }, null));
_extractorsCTS[interaction.ConnectionId] = cts;
_hub!.RegisterInteraction(interaction);
Console.WriteLine("Client registered");
@@ -108,7 +108,7 @@ namespace mROA.Implementation.Backend
var recoveryRequest = _serialization!.Deserialize<ClientRecovery>(connectionRequest.Data, null);
var recoveryInteraction = _hub.GetInteraction(recoveryRequest.Id);
_extractorsCTS[recoveryRequest.Id].Cancel();
_extractorsCTS[-recoveryRequest.Id].Cancel();
recoveryInteraction.IsConnected = () => streamExtractor.IsConnected;
streamExtractor.MessageReceived = message =>
@@ -47,7 +47,7 @@ namespace mROA.Implementation
return placedIndex;
}
public void ClearObject(ComplexObjectIdentifier id)
public void ClearObject(ComplexObjectIdentifier id, IEndPointContext context)
{
_storages.Find(i => i.Key == id.OwnerId).Value.Free(id.ContextId);
}
@@ -57,7 +57,7 @@ namespace mROA.Implementation
throw new NotImplementedException();
}
public object GetSingleObject(Type type, int ownerId)
public object GetSingleObject(Type type, IEndPointContext context)
{
throw new NotImplementedException();
}
@@ -32,7 +32,7 @@ namespace mROA.Implementation
public ulong Flat
{
get => (ulong)OwnerId << 32 | (uint)ContextId;
get => (ulong)((long)OwnerId << 32 | (uint)ContextId);
set
{
OwnerId = (int)(value >> 32);
@@ -73,7 +73,6 @@ namespace mROA.Implementation.Frontend
var assignment = _serialization.Deserialize<IdAssignment>(idMessage.Data, _context);
_interactionModule.ConnectionId = -assignment.Id;
TransmissionConfig.OwnershipRepository = new StaticOwnershipRepository(assignment.Id);
_context.HostId = assignment.Id;
_context.OwnerId = assignment.Id;
}
@@ -46,7 +46,7 @@ namespace mROA.Implementation.Frontend
var initMessage = new NetworkMessageHeader
{
MessageType = EMessageType.UntrustedConnect, Id = Guid.NewGuid(),
Data = BitConverter.GetBytes(Math.Abs(_channelInteractionModule.ConnectionId))
Data = BitConverter.GetBytes(_channelInteractionModule.ConnectionId)
};
var initParsed = _serializationToolkit.Serialize(initMessage, _context);
@@ -18,7 +18,7 @@ namespace mROA.Implementation
throw new NotSupportedException();
}
public void ClearObject(ComplexObjectIdentifier id)
public void ClearObject(ComplexObjectIdentifier id, IEndPointContext context)
{
throw new NotSupportedException();
}
@@ -33,7 +33,7 @@ namespace mROA.Implementation
if (!RemoteTypes.TryGetValue(typeof(T), out var remoteType)) throw new NotSupportedException();
var representationModule =
_representationProducer.Produce(TransmissionConfig.OwnershipRepository.GetOwnershipId());
_representationProducer.Produce(context.OwnerId);
var remote = (T)Activator.CreateInstance(remoteType, id.ContextId,
representationModule, context)!;
@@ -42,16 +42,16 @@ namespace mROA.Implementation
return remote;
}
public object GetSingleObject(Type type, int ownerId)
public object GetSingleObject(Type type, IEndPointContext context)
{
if (_representationProducer == null)
throw new NullReferenceException("representation producer is not initialized");
var representationModule =
_representationProducer.Produce(ownerId);
_representationProducer.Produce(context.OwnerId);
_producedRemoteEndpoints.Add((Activator.CreateInstance(RemoteTypes[type], -1,
representationModule) as RemoteObjectBase)!);
representationModule, context) as RemoteObjectBase)!);
return _producedRemoteEndpoints.Last();
}
+2 -2
View File
@@ -9,14 +9,14 @@ namespace mROA.Implementation
public static Dictionary<Type, Type> RemoteTypes = new();
private IRepresentationModuleProducer? _representationProducer;
public T Produce<T>(ComplexObjectIdentifier id)
public T Produce<T>(ComplexObjectIdentifier id, IEndPointContext context)
{
if (_representationProducer == null)
throw new NullReferenceException("representation producer is not initialized");
if (!RemoteTypes.TryGetValue(typeof(T), out var remoteType)) throw new NotSupportedException();
var representationModule =
_representationProducer.Produce(TransmissionConfig.OwnershipRepository.GetOwnershipId());
_representationProducer.Produce(context.OwnerId);
var remote = (T)Activator.CreateInstance(remoteType, id.ContextId,
representationModule)!;
return remote;
+11 -10
View File
@@ -55,7 +55,8 @@ namespace mROA.Implementation
}
public async IAsyncEnumerable<(object parced, EMessageType originalType)> GetStream(
Predicate<NetworkMessageHeader> rule, IEndPointContext? context, [EnumeratorCancellation] CancellationToken token = default,
Predicate<NetworkMessageHeader> rule, IEndPointContext? context,
[EnumeratorCancellation] CancellationToken token = default,
params Func<NetworkMessageHeader, Type?>[] converter)
{
var writer = _interaction?.ReceiveChanel.Writer;
@@ -72,15 +73,14 @@ namespace mROA.Implementation
yield return (deserialized, message.MessageType)!;
}
}
public async Task PostCallMessageAsync<T>(Guid id, EMessageType eMessageType, T payload,
IEndPointContext? context) where T : notnull
{
await PostCallMessageAsync(id, eMessageType, payload, context);
}
public async Task PostCallMessageAsync(Guid id, EMessageType eMessageType, object payload,
IEndPointContext? context)
// public async Task PostCallMessageAsync<T>(Guid id, EMessageType eMessageType, T payload,
// IEndPointContext? context) where T : notnull
// {
// await this.PostCallMessageAsync(id, eMessageType, payload, context);
// }
public async Task PostCallMessageAsync<T>(Guid id, EMessageType eMessageType, T payload, IEndPointContext? context) where T : notnull
{
if (_interaction == null)
throw new NullReferenceException("Interaction toolkit is not initialized");
@@ -92,7 +92,8 @@ namespace mROA.Implementation
{ Id = id, MessageType = eMessageType, Data = serialized });
}
public void PostCallMessage<T>(Guid id, EMessageType eMessageType, T payload, IEndPointContext? context) where T : notnull
public void PostCallMessage<T>(Guid id, EMessageType eMessageType, T payload, IEndPointContext? context)
where T : notnull
{
PostCallMessageAsync(id, eMessageType, payload, context).GetAwaiter().GetResult();
}
+6 -13
View File
@@ -4,7 +4,7 @@ using mROA.Abstract;
using mROA.Implementation.Attributes;
// ReSharper disable UnusedMember.Global
#pragma warning disable CS8618, CS9264
// #pragma warning disable CS8618, CS9264
namespace mROA.Implementation
{
@@ -30,8 +30,9 @@ namespace mROA.Implementation
// ReSharper disable once UnusedMember.Global
// ReSharper disable once MemberCanBePrivate.Global
public SharedObjectShellShell(T value)
public SharedObjectShellShell(T value, IEndPointContext endPointContext)
{
EndPointContext = endPointContext;
Value = value;
}
@@ -57,15 +58,7 @@ namespace mROA.Implementation
}
}
[SerializationIgnore]
[JsonIgnore]
public IEndPointContext EndPointContext { get; set; } = new EndPointContext
{
RealRepository = TransmissionConfig.RealContextRepository,
RemoteRepository = TransmissionConfig.RemoteEndpointContextRepository,
HostId = TransmissionConfig.OwnershipRepository.GetHostOwnershipId(),
OwnerId = 0
};
[SerializationIgnore] [JsonIgnore] public IEndPointContext EndPointContext { get; set; }
public ComplexObjectIdentifier Identifier
{
@@ -96,7 +89,7 @@ namespace mROA.Implementation
public static implicit operator T(SharedObjectShellShell<T> value) => value.Value;
public static implicit operator SharedObjectShellShell<T>(T value) =>
new(value);
// public static implicit operator SharedObjectShellShell<T>(T value) =>
// new(value);
}
}
+22 -22
View File
@@ -9,27 +9,27 @@ namespace mROA.Implementation
#if TRACE
public static int TotalTransmittedBytes { get; set; }
#endif
private static IContextRepository? _realContextRepository;
private static IContextRepository? _remoteEndpointContextRepository;
private static IOwnershipRepository? _ownershipRepository;
public static IContextRepository RealContextRepository
{
get => _realContextRepository ?? throw new NullReferenceException("RealContextRepository is null");
set => _realContextRepository = value;
}
public static IContextRepository RemoteEndpointContextRepository
{
get => _remoteEndpointContextRepository ??
throw new NullReferenceException("RemoteEndpointContextRepository is null");
set => _remoteEndpointContextRepository = value;
}
public static IOwnershipRepository OwnershipRepository
{
get => _ownershipRepository ?? throw new NullReferenceException("OwnershipRepository is null");
set => _ownershipRepository = value;
}
// private static IContextRepository? _realContextRepository;
// private static IContextRepository? _remoteEndpointContextRepository;
// private static IOwnershipRepository? _ownershipRepository;
//
// public static IContextRepository RealContextRepository
// {
// get => _realContextRepository ?? throw new NullReferenceException("RealContextRepository is null");
// set => _realContextRepository = value;
// }
//
// public static IContextRepository RemoteEndpointContextRepository
// {
// get => _remoteEndpointContextRepository ??
// throw new NullReferenceException("RemoteEndpointContextRepository is null");
// set => _remoteEndpointContextRepository = value;
// }
//
// public static IOwnershipRepository OwnershipRepository
// {
// get => _ownershipRepository ?? throw new NullReferenceException("OwnershipRepository is null");
// set => _ownershipRepository = value;
// }
}
}