не работает на создании синглтона

This commit is contained in:
2025-02-15 16:09:32 +03:00
parent 602d45c894
commit d1f42e4c33
4 changed files with 45 additions and 16 deletions
+9 -2
View File
@@ -14,14 +14,21 @@ builder.Modules.Add(new JsonFrontendSerialisationModule());
builder.Modules.Add(new StreamBasedFrontendInteractionModule()); builder.Modules.Add(new StreamBasedFrontendInteractionModule());
builder.Modules.Add(new NetworkFrontendBridge(new IPEndPoint(IPAddress.Loopback, 4567))); builder.Modules.Add(new NetworkFrontendBridge(new IPEndPoint(IPAddress.Loopback, 4567)));
builder.Modules.Add(new StaticSerialisationModuleProducer()); builder.Modules.Add(new StaticSerialisationModuleProducer());
builder.Modules.Add(new BasicExecutionModule());
builder.Modules.Add(new CoCodegenMethodRepository());
builder.Modules.Add(new StreamBasedVirtualBackendInteractionModule());
builder.UseCollectableContextRepository(); builder.UseCollectableContextRepository();
builder.Build(); builder.Build();
TransmissionConfig.RealContextRepository = builder.GetModule<ContextRepository>(); TransmissionConfig.RealContextRepository = builder.GetModule<ContextRepository>();
TransmissionConfig.RemoteEndpointContextRepository = builder.GetModule<RemoteContextRepository>(); TransmissionConfig.RemoteEndpointContextRepository = builder.GetModule<RemoteContextRepository>();
builder.GetModule<NetworkFrontendBridge>().Connect();
Console.WriteLine(TransmissionConfig.OwnershipRepository.GetOwnershipId()); builder.GetModule<NetworkFrontendBridge>()!.Connect();
builder.GetModule<StreamBasedVirtualBackendInteractionModule>()!.StartVirtualInteraction();
Console.WriteLine(TransmissionConfig.OwnershipRepository!.GetOwnershipId());
var context = builder.GetModule<RemoteContextRepository>(); var context = builder.GetModule<RemoteContextRepository>();
var factory = context.GetSingleObject(typeof(IPrinterFactory)) as IPrinterFactory; var factory = context.GetSingleObject(typeof(IPrinterFactory)) as IPrinterFactory;
@@ -13,8 +13,13 @@ public class JsonSerialisationModule : ISerialisationModule
public void HandleIncomingRequest(int clientId, byte[] message) public void HandleIncomingRequest(int clientId, byte[] message)
{ {
var ownership = TransmissionConfig.OwnershipRepository as MultiClientOwnershipRepository ?? throw new Exception("Set ownership repository type is incorrect"); MultiClientOwnershipRepository? ownership = null;
ownership.RegisterOwnership(clientId); if (TransmissionConfig.OwnershipRepository is MultiClientOwnershipRepository)
{
ownership = TransmissionConfig.OwnershipRepository as MultiClientOwnershipRepository;
ownership.RegisterOwnership(clientId);
}
NetworkMessage input = JsonSerializer.Deserialize<NetworkMessage>(message)!; NetworkMessage input = JsonSerializer.Deserialize<NetworkMessage>(message)!;
Console.WriteLine(Encoding.Default.GetString(input.Data)); Console.WriteLine(Encoding.Default.GetString(input.Data));
if (input.SchemaId == MessageType.CallRequest) if (input.SchemaId == MessageType.CallRequest)
@@ -40,7 +45,8 @@ public class JsonSerialisationModule : ISerialisationModule
Data = JsonSerializer.SerializeToUtf8Bytes(response, response.GetType()) Data = JsonSerializer.SerializeToUtf8Bytes(response, response.GetType())
}, clientId); }, clientId);
} }
ownership.FreeOwnership();
ownership?.FreeOwnership();
} }
public void PostResponse(NetworkMessage message, int clientId) public void PostResponse(NetworkMessage message, int clientId)
@@ -4,9 +4,9 @@ namespace mROA.Implementation.Backend;
public class StreamBasedInteractionModule : IInteractionModule public class StreamBasedInteractionModule : IInteractionModule
{ {
private ISerialisationModule _serialisationModule; internal ISerialisationModule _serialisationModule;
private readonly Dictionary<int, Stream> _streams = new(); private readonly Dictionary<int, Stream> _streams = new();
private Action<int, byte[]>? _handler; internal Action<int, byte[]>? _handler;
public void RegisterSource(Stream stream) public void RegisterSource(Stream stream)
{ {
@@ -1,25 +1,26 @@
using mROA.Implementation.Backend; using mROA.Abstract;
using mROA.Implementation.Backend;
namespace mROA.Implementation.Frontend; namespace mROA.Implementation.Frontend;
public class StreamBasedVirtualBackendInteractionModule : StreamBasedInteractionModule public class StreamBasedVirtualBackendInteractionModule : StreamBasedInteractionModule, IInteractionModule
{ {
public Stream ServerStream { get; set; } private Stream? _serverStream { get; set; }
private StreamBasedFrontendInteractionModule? _interactionModule;
public void RegisterSource(Stream stream) public void RegisterSource(Stream stream)
{ {
ServerStream = stream;
} }
public Stream GetSource(int clientId) public Stream GetSource(int clientId)
{ {
return ServerStream; return _serverStream;
} }
public void SendTo(int clientId, byte[] message) public void SendTo(int clientId, byte[] message)
{ {
ServerStream.Write(BitConverter.GetBytes((ushort)message.Length), 0, sizeof(ushort)); _serverStream.Write(BitConverter.GetBytes((ushort)message.Length), 0, sizeof(ushort));
ServerStream.Write(message, 0, message.Length); _serverStream.Write(message, 0, message.Length);
} }
private async Task ListenTo((int id, Stream stream) client, Action<int, byte[]> action) private async Task ListenTo((int id, Stream stream) client, Action<int, byte[]> action)
@@ -41,4 +42,19 @@ public class StreamBasedVirtualBackendInteractionModule : StreamBasedInteraction
} }
} }
public void Inject<T>(T dependency)
{
base.Inject(dependency);
if (dependency is StreamBasedFrontendInteractionModule interactionModule)
{
_interactionModule = interactionModule;
}
}
public void StartVirtualInteraction()
{
_serverStream = _interactionModule.ServerStream;
_ = ListenTo((0, _serverStream), _handler!);
}
} }