Basic setup of endpoint context
This commit is contained in:
@@ -22,7 +22,7 @@ class Program
|
||||
new RemoteTypeBinder();
|
||||
// builder.Modules.Add(new JsonSerializationToolkit());
|
||||
builder.Modules.Add(new CborSerializationToolkit());
|
||||
|
||||
builder.Modules.Add(new EndPointContext());
|
||||
builder.Modules.Add(new RemoteContextRepository());
|
||||
builder.Modules.Add(new ChannelInteractionModule());
|
||||
builder.Modules.Add(new UdpUntrustedInteraction());
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace mROA.Abstract
|
||||
{
|
||||
public interface IContextualSerializationToolKit
|
||||
public interface IContextualSerializationToolKit : IInjectableModule
|
||||
{
|
||||
byte[] Serialize(object objectToSerialize, IEndPointContext? context);
|
||||
void Serialize(object objectToSerialize, Span<byte> destination, IEndPointContext? context);
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
{
|
||||
IContextRepository RealRepository { get; }
|
||||
IContextRepository RemoteRepository { get; }
|
||||
int HostId { get; }
|
||||
int OwnerId { get; }
|
||||
int HostId { get; set; }
|
||||
int OwnerId { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -18,8 +18,6 @@ namespace mROA.Implementation.Backend
|
||||
private IContextualSerializationToolKit? _serialization;
|
||||
private Dictionary<int, CancellationTokenSource> _extractorsCTS = new();
|
||||
|
||||
|
||||
|
||||
public NetworkGatewayModule(IPEndPoint endpoint, Type interactionModuleType,
|
||||
IInjectableModule[] injectableModules)
|
||||
{
|
||||
@@ -82,8 +80,9 @@ namespace mROA.Implementation.Backend
|
||||
|
||||
//TODO сделать контекст
|
||||
var context = new EndPointContext();
|
||||
|
||||
var streamExtractor = new ChannelInteractionModule.StreamExtractor(client.GetStream(), _serialization, context);
|
||||
|
||||
var streamExtractor =
|
||||
new ChannelInteractionModule.StreamExtractor(client.GetStream(), _serialization, context);
|
||||
interaction.IsConnected = () => streamExtractor.IsConnected;
|
||||
streamExtractor.MessageReceived = message => { interaction.ReceiveChanel.Writer.WriteAsync(message); };
|
||||
streamExtractor.SingleReceive();
|
||||
@@ -94,6 +93,8 @@ namespace mROA.Implementation.Backend
|
||||
switch (connectionRequest.MessageType)
|
||||
{
|
||||
case EMessageType.ClientConnect:
|
||||
context.HostId = 0;
|
||||
context.OwnerId = interaction.ConnectionId;
|
||||
Task.Run(async () => await streamExtractor.LoopedReceive(cts.Token));
|
||||
_ = streamExtractor.SendFromChannel(interaction.TrustedPostChanel, cts.Token);
|
||||
interaction.PostMessageAsync(new NetworkMessageHeader(_serialization!,
|
||||
@@ -129,7 +130,7 @@ namespace mROA.Implementation.Backend
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void ThrowIfNotInjected()
|
||||
{
|
||||
if (_hub is null)
|
||||
|
||||
@@ -1,24 +1,28 @@
|
||||
using System;
|
||||
using mROA.Abstract;
|
||||
using mROA.Implementation.Backend;
|
||||
|
||||
namespace mROA.Implementation
|
||||
{
|
||||
public class EndPointContext : IEndPointContext
|
||||
{
|
||||
public Func<int> OwnerFunc;
|
||||
public IContextRepository RealRepository { get; set; }
|
||||
public IContextRepository RemoteRepository { get; set; }
|
||||
public int HostId { get; set; }
|
||||
|
||||
public int OwnerId
|
||||
{
|
||||
get => OwnerFunc();
|
||||
// ReSharper disable once UnusedMember.Global
|
||||
set { OwnerFunc = () => value; }
|
||||
}
|
||||
public int OwnerId { get; set; }
|
||||
|
||||
public void Inject<T>(T dependency)
|
||||
{
|
||||
switch (dependency)
|
||||
{
|
||||
case RemoteContextRepository remoteRepository:
|
||||
RemoteRepository = remoteRepository;
|
||||
break;
|
||||
case ContextRepository realRepository:
|
||||
RealRepository = realRepository;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,7 @@ namespace mROA.Implementation.Frontend
|
||||
private ChannelInteractionModule.StreamExtractor _currentExtractor;
|
||||
private CancellationTokenSource _rawExtractorCancellation;
|
||||
private IEndPointContext _context;
|
||||
|
||||
public NetworkFrontendBridge(IPEndPoint serverEndPoint)
|
||||
{
|
||||
_serverEndPoint = serverEndPoint;
|
||||
@@ -55,7 +56,8 @@ namespace mROA.Implementation.Frontend
|
||||
_interactionModule.IsConnected = () => _currentExtractor.IsConnected;
|
||||
_interactionModule.OnDisconnected += _ => { Reconnect(); };
|
||||
|
||||
_interactionModule.PostMessageAsync(new NetworkMessageHeader(_serialization, new ClientConnect(), _context)).Wait();
|
||||
_interactionModule.PostMessageAsync(new NetworkMessageHeader(_serialization, new ClientConnect(), _context))
|
||||
.Wait();
|
||||
|
||||
_currentExtractor.SingleReceive();
|
||||
var idMessage = _interactionModule.GetNextMessageReceiving(false).GetAwaiter().GetResult();
|
||||
@@ -69,14 +71,17 @@ namespace mROA.Implementation.Frontend
|
||||
|
||||
Task.Run(async () => await _currentExtractor.LoopedReceive(_rawExtractorCancellation.Token));
|
||||
|
||||
var assignment = _serialization.Deserialize<IdAssignment>(idMessage.Data, _context)!;
|
||||
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;
|
||||
}
|
||||
|
||||
private void PrepareExtractor()
|
||||
{
|
||||
_currentExtractor = new ChannelInteractionModule.StreamExtractor(_tcpClient.GetStream(), _serialization!, _context);
|
||||
_currentExtractor =
|
||||
new ChannelInteractionModule.StreamExtractor(_tcpClient.GetStream(), _serialization!, _context);
|
||||
|
||||
_ = _currentExtractor.SendFromChannel(_interactionModule!.TrustedPostChanel,
|
||||
_rawExtractorCancellation.Token);
|
||||
@@ -108,7 +113,8 @@ namespace mROA.Implementation.Frontend
|
||||
|
||||
public void Disconnect()
|
||||
{
|
||||
_ = _interactionModule!.PostMessageAsync(new NetworkMessageHeader(_serialization!, new ClientDisconnect(), _context));
|
||||
_ = _interactionModule!.PostMessageAsync(new NetworkMessageHeader(_serialization!, new ClientDisconnect(),
|
||||
_context));
|
||||
_interactionModule.Dispose();
|
||||
_tcpClient.Dispose();
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ using System.Diagnostics;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using mROA.Abstract;
|
||||
using mROA.Implementation.Backend;
|
||||
|
||||
// ReSharper disable MethodHasAsyncOverload
|
||||
|
||||
@@ -14,9 +13,11 @@ namespace mROA.Implementation.Frontend
|
||||
public class RequestExtractor : IRequestExtractor
|
||||
{
|
||||
private IExecuteModule? _executeModule;
|
||||
|
||||
private IMethodRepository? _methodRepository;
|
||||
private IContextRepository? _realContextRepository;
|
||||
private IContextRepository? _remoteContextRepository;
|
||||
|
||||
// private IContextRepository? _realContextRepository;
|
||||
// private IContextRepository? _remoteContextRepository;
|
||||
private IRepresentationModule? _representationModule;
|
||||
private IContextualSerializationToolKit? _serializationToolkit;
|
||||
private IEndPointContext _context;
|
||||
@@ -28,13 +29,6 @@ namespace mROA.Implementation.Frontend
|
||||
case IExecuteModule executeModule:
|
||||
_executeModule = executeModule;
|
||||
break;
|
||||
case MultiClientContextRepository:
|
||||
case ContextRepository:
|
||||
_realContextRepository = dependency as IContextRepository;
|
||||
break;
|
||||
case RemoteContextRepository remoteContextRepository:
|
||||
_remoteContextRepository = remoteContextRepository;
|
||||
break;
|
||||
case IMethodRepository methodRepository:
|
||||
_methodRepository = methodRepository;
|
||||
break;
|
||||
@@ -55,14 +49,6 @@ namespace mROA.Implementation.Frontend
|
||||
ThrowIfNotInjected();
|
||||
|
||||
|
||||
var multiClientOwnershipRepository =
|
||||
TransmissionConfig.OwnershipRepository as MultiClientOwnershipRepository;
|
||||
multiClientOwnershipRepository?.RegisterOwnership(_representationModule!.Id);
|
||||
if (multiClientOwnershipRepository is not null)
|
||||
{
|
||||
TransmissionConfig.OwnershipRepository = new StaticOwnershipRepository(_representationModule.Id);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
#if TRACE
|
||||
@@ -117,7 +103,6 @@ namespace mROA.Implementation.Frontend
|
||||
}
|
||||
catch
|
||||
{
|
||||
multiClientOwnershipRepository?.FreeOwnership();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,8 +112,6 @@ namespace mROA.Implementation.Frontend
|
||||
throw new NullReferenceException("Serializing toolkit is null.");
|
||||
if (_executeModule == null)
|
||||
throw new NullReferenceException("Execute module is null.");
|
||||
if (_realContextRepository == null)
|
||||
throw new NullReferenceException("Context repository is null.");
|
||||
if (_representationModule == null)
|
||||
throw new NullReferenceException("Representation module is null.");
|
||||
if (_methodRepository == null)
|
||||
@@ -137,12 +120,12 @@ namespace mROA.Implementation.Frontend
|
||||
|
||||
private void HandleCancelRequest(CancelRequest req)
|
||||
{
|
||||
_executeModule!.Execute(req, _realContextRepository!, _representationModule!, _context);
|
||||
_executeModule!.Execute(req, _context.RealRepository, _representationModule!, _context);
|
||||
}
|
||||
|
||||
private void HandleCallRequest(DefaultCallRequest request)
|
||||
{
|
||||
var result = _executeModule!.Execute(request, _realContextRepository!, _representationModule!, _context);
|
||||
var result = _executeModule!.Execute(request, _context.RealRepository, _representationModule!, _context);
|
||||
|
||||
var resultType = result.MessageType;
|
||||
|
||||
@@ -156,7 +139,7 @@ namespace mROA.Implementation.Frontend
|
||||
|
||||
private void HandleEventRequest(DefaultCallRequest request)
|
||||
{
|
||||
_executeModule!.Execute(request, _remoteContextRepository!, _representationModule!, _context);
|
||||
_executeModule!.Execute(request, _context.RemoteRepository, _representationModule!, _context);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -63,8 +63,8 @@ namespace mROA.Implementation
|
||||
{
|
||||
RealRepository = TransmissionConfig.RealContextRepository,
|
||||
RemoteRepository = TransmissionConfig.RemoteEndpointContextRepository,
|
||||
HostId = TransmissionConfig.OwnershipRepository.GetHostOwnershipId(),
|
||||
OwnerFunc = TransmissionConfig.OwnershipRepository.GetOwnershipId
|
||||
HostId = TransmissionConfig.OwnershipRepository.GetHostOwnershipId(),
|
||||
OwnerId = 0
|
||||
};
|
||||
|
||||
public ComplexObjectIdentifier Identifier
|
||||
|
||||
Reference in New Issue
Block a user