From 2155c8f0d8c25d4851503f7e7683bb158103ee09 Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Tue, 6 May 2025 23:25:52 +0300 Subject: [PATCH] Second Unity code equating session --- .../Backend/BasicExecutionModule.cs | 13 +--------- .../Backend/NetworkGatewayModule.cs | 24 +++++++------------ .../Frontend/NetworkFrontendBridge.cs | 3 --- .../Frontend/RequestExtractor.cs | 22 ----------------- .../Frontend/UdpUntrustedInteraction.cs | 8 ++----- 5 files changed, 11 insertions(+), 59 deletions(-) diff --git a/mROA/Implementation/Backend/BasicExecutionModule.cs b/mROA/Implementation/Backend/BasicExecutionModule.cs index d5f3f09..5848de9 100644 --- a/mROA/Implementation/Backend/BasicExecutionModule.cs +++ b/mROA/Implementation/Backend/BasicExecutionModule.cs @@ -30,18 +30,12 @@ namespace mROA.Implementation.Backend public ICommandExecution Execute(ICallRequest command, IContextRepository contextRepository, IRepresentationModule representationModule, IEndPointContext endPointContext) { -#if TRACE - Console.WriteLine(command.GetType().Name); -#endif try { ThrowIfNotInjected(contextRepository); if (command is CancelRequest) { -#if TRACE - Console.WriteLine("Final cancelling request"); -#endif return CancelExecution(command); } @@ -76,9 +70,6 @@ namespace mROA.Implementation.Backend var result = Execute((invoker as MethodInvoker)!, context, castedParams!, command, execContext); if (command.CommandId == -1) { -#if TRACE - Console.WriteLine("Disposing object"); -#endif contextRepository.ClearObject(command.ObjectId, endPointContext); } @@ -189,9 +180,7 @@ namespace mROA.Implementation.Backend var tokenSource = new CancellationTokenSource(); cancellationRepository.RegisterCancellation(command.Id, tokenSource); var token = tokenSource.Token; -#if TRACE - token.Register(() => Console.WriteLine($"Cancellation requested check {command.Id}")); -#endif + try { invoker.Invoke(instance, parameters, new object[] { executionContext, token }, _ => diff --git a/mROA/Implementation/Backend/NetworkGatewayModule.cs b/mROA/Implementation/Backend/NetworkGatewayModule.cs index 8fe538c..5fc2548 100644 --- a/mROA/Implementation/Backend/NetworkGatewayModule.cs +++ b/mROA/Implementation/Backend/NetworkGatewayModule.cs @@ -33,15 +33,6 @@ namespace mROA.Implementation.Backend Console.WriteLine("Enter Backspace to stop"); Task.Run(HandleIncomingConnections); - - while (true) - { - var key = Console.ReadKey(); - if (key.Key == ConsoleKey.Backspace) - break; - } - - Console.WriteLine("Stopping"); } public void Dispose() @@ -62,13 +53,13 @@ namespace mROA.Implementation.Backend } } - private void HandleIncomingConnections() + private async Task HandleIncomingConnections() { ThrowIfNotInjected(); while (true) { - var client = _tcpListener.AcceptTcpClient(); + var client = await _tcpListener.AcceptTcpClientAsync(); Console.WriteLine($"Client connected from {client.Client.RemoteEndPoint}"); var interaction = Activator.CreateInstance(_interactionModuleType!) as IChannelInteractionModule; @@ -84,10 +75,12 @@ namespace mROA.Implementation.Backend var streamExtractor = new ChannelInteractionModule.StreamExtractor(client.GetStream(), _serialization, context); interaction.IsConnected = () => streamExtractor.IsConnected; - streamExtractor.MessageReceived = message => { interaction.ReceiveChanel.Writer.WriteAsync(message); }; - streamExtractor.SingleReceive(); - var connectionRequest = interaction.GetNextMessageReceiving(false) - .GetAwaiter().GetResult()!; + streamExtractor.MessageReceived = async message => + { + await interaction.ReceiveChanel.Writer.WriteAsync(message); + }; + Task.Run(() => streamExtractor.SingleReceive()); + var connectionRequest = await interaction.ReceiveChanel.Reader.ReadAsync(); var cts = new CancellationTokenSource(); switch (connectionRequest.MessageType) @@ -121,7 +114,6 @@ namespace mROA.Implementation.Backend recoveryInteraction.Restart(false); - Console.WriteLine("Connection recovery for client {0} finished", recoveryRequest.Id); break; } default: diff --git a/mROA/Implementation/Frontend/NetworkFrontendBridge.cs b/mROA/Implementation/Frontend/NetworkFrontendBridge.cs index 2b287fa..04d6248 100644 --- a/mROA/Implementation/Frontend/NetworkFrontendBridge.cs +++ b/mROA/Implementation/Frontend/NetworkFrontendBridge.cs @@ -1,10 +1,7 @@ using System; -using System.Collections.Generic; -using System.Linq; using System.Net; using System.Net.Sockets; using System.Threading; -using System.Threading.Channels; using System.Threading.Tasks; using mROA.Abstract; using Exception = System.Exception; diff --git a/mROA/Implementation/Frontend/RequestExtractor.cs b/mROA/Implementation/Frontend/RequestExtractor.cs index 3311ca6..3edccaf 100644 --- a/mROA/Implementation/Frontend/RequestExtractor.cs +++ b/mROA/Implementation/Frontend/RequestExtractor.cs @@ -1,7 +1,4 @@ using System; -#if TRACE -using System.Diagnostics; -#endif using System.Threading; using System.Threading.Tasks; using mROA.Abstract; @@ -16,8 +13,6 @@ namespace mROA.Implementation.Frontend private IMethodRepository? _methodRepository; - // private IContextRepository? _realContextRepository; - // private IContextRepository? _remoteContextRepository; private IRepresentationModule? _representationModule; private IContextualSerializationToolKit? _serializationToolkit; private IEndPointContext _context; @@ -51,9 +46,6 @@ namespace mROA.Implementation.Frontend try { -#if TRACE - var sw = new Stopwatch(); -#endif var streamTokenSource = new CancellationTokenSource(); @@ -69,20 +61,6 @@ namespace mROA.Implementation.Frontend await foreach (var command in query) { -#if TRACE - Console.WriteLine("Waiting for request..."); - if (sw.IsRunning) - { - sw.Stop(); - Console.WriteLine( - $"Request handling took {Math.Round(sw.Elapsed.TotalMilliseconds * 1000.0)} microseconds."); - } -#endif - -#if TRACE - Console.WriteLine("Request received"); - sw.Restart(); -#endif switch (command.originalType) { case EMessageType.CallRequest: diff --git a/mROA/Implementation/Frontend/UdpUntrustedInteraction.cs b/mROA/Implementation/Frontend/UdpUntrustedInteraction.cs index 941ac08..5d4d4cf 100644 --- a/mROA/Implementation/Frontend/UdpUntrustedInteraction.cs +++ b/mROA/Implementation/Frontend/UdpUntrustedInteraction.cs @@ -60,13 +60,9 @@ namespace mROA.Implementation.Frontend continue; var serialized = _serializationToolkit.Serialize(post, _context); -#if TRACE - Console.WriteLine("Untrusted write start"); -#endif + await udpClient.SendAsync(serialized, serialized.Length); -#if TRACE - Console.WriteLine("Untrusted write finished"); -#endif + } }