Remove ICallRequest

This commit is contained in:
2025-08-03 00:32:32 +03:00
parent 0818ed6a5a
commit 2cbcc6b201
11 changed files with 109 additions and 50 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ using mROA.Implementation;
Console.WriteLine("Hello, World!");
var test = new CborTest();
test.ArrayWrite();
BenchmarkRunner.Run<CborTest>();
BenchmarkRunner.Run<VirtualOverhead>();
public static class CborExtensions
{
+75
View File
@@ -0,0 +1,75 @@
using BenchmarkDotNet.Attributes;
using mROA.Implementation;
namespace mROA.Benchmark;
public class VirtualOverhead
{
private ICallRequest _request;
private CallRequest _directRequest;
private RawCallRequest _rawRequest;
public VirtualOverhead()
{
_request = new CallRequest
{
CommandId = 5, Id = new RequestId(), ObjectId = ComplexObjectIdentifier.Null, Parameters = null
};
_directRequest = (CallRequest)_request;
_rawRequest = new RawCallRequest
{
CommandId = _directRequest.CommandId, Id = _directRequest.Id, ObjectId = _directRequest.ObjectId,
Parameters = null
};
}
[Benchmark(Baseline = true)]
public long VirtualUsage()
{
var req = _request;
long acc = 0;
acc += req.CommandId;
acc += (long)(req.Id.P1 + req.Id.P0);
acc += req.ObjectId.ContextId + req.ObjectId.OwnerId;
acc += (req.Parameters ?? []).Length;
return acc;
}
[Benchmark]
public long DirectUsage()
{
var req = _directRequest;
long acc = 0;
acc += req.CommandId;
acc += (long)(req.Id.P1 + req.Id.P0);
acc += req.ObjectId.ContextId + req.ObjectId.OwnerId;
acc += (req.Parameters ?? []).Length;
return acc;
}
[Benchmark]
public long RawUsage()
{
var req = _rawRequest;
long acc = 0;
acc += req.CommandId;
acc += (long)(req.Id.P1 + req.Id.P0);
acc += req.ObjectId.ContextId + req.ObjectId.OwnerId;
acc += (req.Parameters ?? []).Length;
return acc;
}
}
public struct RawCallRequest
{
public RequestId Id;
public int CommandId;
public ComplexObjectIdentifier ObjectId;
public object?[]? Parameters;
public override string ToString()
{
return $"Call request {{ Id : {Id}, CommandId : {CommandId}, ObjectId : {ObjectId} }}";
}
}
+2 -2
View File
@@ -18,7 +18,7 @@ namespace mROA.Cbor
private readonly IOrdinaryStructureParser[] _parsers =
{
new DefaultCallRequestParser(), new FinalCommandExecutionParser(),
new CallRequestParser(), new FinalCommandExecutionParser(),
new FinalCommandExecutionResultlessParser()
};
@@ -27,7 +27,7 @@ namespace mROA.Cbor
private bool FindParser(Type t, out IOrdinaryStructureParser parser)
{
if (t == typeof(DefaultCallRequest))
if (t == typeof(CallRequest))
{
parser = _parsers[0];
return true;
+3 -3
View File
@@ -12,11 +12,11 @@ namespace mROA.Cbor
object Read(CborReader reader, IEndPointContext context, CborSerializationToolkit serialization);
}
public class DefaultCallRequestParser : IOrdinaryStructureParser
public class CallRequestParser : IOrdinaryStructureParser
{
public void Write(CborWriter writer, object value, IEndPointContext context, CborSerializationToolkit serialization)
{
var v = (DefaultCallRequest)value;
var v = (CallRequest)value;
writer.WriteStartArray(4);
v.Id.WriteToCborInline(writer);
// writer.WriteByteString(v.Id.ToByteArray());
@@ -31,7 +31,7 @@ namespace mROA.Cbor
public object Read(CborReader reader, IEndPointContext context, CborSerializationToolkit serialization)
{
reader.ReadStartArray();
var value = new DefaultCallRequest
var value = new CallRequest
{
Id = new RequestId(reader.ReadByteString()),
CommandId = reader.ReadInt32(),
+1 -1
View File
@@ -29,7 +29,7 @@ namespace mROA.Codegen
Console.WriteLine($"Try to send to {ownerId} with hash code {context.GetHashCode()}");
<!I callFilter>
Console.WriteLine("Sending event...");
var request = new DefaultCallRequest
var request = new CallRequest
{
Id = RequestId.Generate(),
CommandId = <!L commandId>,
+4 -1
View File
@@ -1,10 +1,13 @@
using mROA.Implementation;
using mROA.Implementation.CommandExecution;
namespace mROA.Abstract
{
public interface IExecuteModule
{
ICommandExecution? Execute(ICallRequest command, IInstanceRepository instanceRepository,
ICommandExecution? Execute(CallRequest command, IInstanceRepository instanceRepository,
IRepresentationModule representationModule, IEndPointContext context);
ICommandExecution Cancel(CancelRequest command);
}
}
@@ -19,17 +19,12 @@ namespace mROA.Implementation.Backend
_serialization = serialization;
}
public ICommandExecution? Execute(ICallRequest command, IInstanceRepository instanceRepository,
public ICommandExecution? Execute(CallRequest command, IInstanceRepository instanceRepository,
IRepresentationModule representationModule, IEndPointContext endPointContext)
{
// _logger.LogInformation("Executing {0}", command.Id);
try
{
if (command is CancelRequest)
{
return CancelExecution(command);
}
var invoker = _methodRepo.GetMethod(command.CommandId);
if (invoker == null)
throw new Exception($"Command {command.CommandId} not found");
@@ -63,7 +58,7 @@ namespace mROA.Implementation.Backend
}
}
private ICommandExecution? ExecuteRequest(ICallRequest command, IInstanceRepository instanceRepository,
private ICommandExecution? ExecuteRequest(CallRequest command, IInstanceRepository instanceRepository,
IRepresentationModule representationModule, IEndPointContext endPointContext, IMethodInvoker invoker,
object context, object?[]? castedParams, RequestContext execContext)
{
@@ -87,7 +82,7 @@ namespace mROA.Implementation.Backend
}
}
private static object GetInstance(ICallRequest command, IInstanceRepository instanceRepository,
private static object GetInstance(CallRequest command, IInstanceRepository instanceRepository,
IMethodInvoker invoker, IEndPointContext endPointContext)
{
var context = command.ObjectId.ContextId != -1
@@ -97,7 +92,7 @@ namespace mROA.Implementation.Backend
return context;
}
private object?[] CastedParams(ICallRequest command, IMethodInvoker invoker, IEndPointContext context)
private object?[] CastedParams(CallRequest command, IMethodInvoker invoker, IEndPointContext context)
{
object?[] castedParams = new object[invoker.ParameterTypes.Length];
for (var i = 0; i < castedParams.Length; i++)
@@ -108,7 +103,7 @@ namespace mROA.Implementation.Backend
return castedParams;
}
private FinalCommandExecution CancelExecution(ICallRequest command)
public ICommandExecution Cancel(CancelRequest command)
{
var cts = _cancellationRepo.GetCancellation(command.Id);
if (cts == null)
@@ -123,7 +118,7 @@ namespace mROA.Implementation.Backend
}
private static ICommandExecution? Execute(MethodInvoker invoker, object instance, object?[] parameter,
ICallRequest command, RequestContext executionContext)
CallRequest command, RequestContext executionContext)
{
var finalResult = invoker.Invoke(instance, parameter, new object[] { executionContext });
@@ -148,7 +143,7 @@ namespace mROA.Implementation.Backend
}
private ICommandExecution? ExecuteAsync(AsyncMethodInvoker invoker, object instance, object?[]? parameters,
ICallRequest command, ICancellationRepository cancellationRepository,
CallRequest command, ICancellationRepository cancellationRepository,
IRepresentationModule representationModule, RequestContext executionContext, IEndPointContext context)
{
var tokenSource = new CancellationTokenSource();
@@ -177,7 +172,7 @@ namespace mROA.Implementation.Backend
}
private ICommandExecution? TypedExecuteAsync(AsyncMethodInvoker invoker, object instance, object?[]? parameters,
ICallRequest command, ICancellationRepository cancellationRepository,
CallRequest command, ICancellationRepository cancellationRepository,
IRepresentationModule representationModule, RequestContext executionContext, IEndPointContext context)
{
var tokenSource = new CancellationTokenSource();
+1 -1
View File
@@ -73,7 +73,7 @@ namespace mROA.Implementation.Backend
{
await foreach (var post in interaction.UntrustedPostChanel.ReadAllAsync())
{
if (post.MessageType is not (CallRequest or EMessageType.CancelRequest
if (post.MessageType is not (EMessageType.CallRequest or EMessageType.CancelRequest
or EventRequest))
continue;
+3 -17
View File
@@ -2,15 +2,9 @@
namespace mROA.Implementation
{
public interface ICallRequest
{
RequestId Id { get; }
int CommandId { get; }
ComplexObjectIdentifier ObjectId { get; }
object?[]? Parameters { get; }
}
public struct DefaultCallRequest : ICallRequest
public struct CallRequest
{
public RequestId Id { get; set; }
public int CommandId { get; set; }
@@ -24,16 +18,8 @@ namespace mROA.Implementation
}
}
public class CancelRequest : ICallRequest
public struct CancelRequest
{
public RequestId Id { get; set; }
public int CommandId { get; set; } = -2;
public ComplexObjectIdentifier ObjectId { get; set; } = ComplexObjectIdentifier.Null;
public object?[]? Parameters { get; set; } = null;
public override string ToString()
{
return $"Cancel request {{ Id : {Id}, CommandId : {CommandId}, ObjectId : {ObjectId} }}";
}
}
}
@@ -41,15 +41,15 @@ namespace mROA.Implementation.Frontend
switch (originalType)
{
case EMessageType.CallRequest:
HandleCallRequest((DefaultCallRequest)parced);
HandleCallRequest((CallRequest)parced);
break;
case EMessageType.ClientDisconnect:
return;
case EMessageType.EventRequest:
HandleEventRequest((DefaultCallRequest)parced);
HandleEventRequest((CallRequest)parced);
break;
case EMessageType.CancelRequest:
HandleCancelRequest((parced as CancelRequest)!);
HandleCancelRequest((CancelRequest)parced);
break;
default:
throw new ArgumentOutOfRangeException();
@@ -62,18 +62,18 @@ namespace mROA.Implementation.Frontend
public Func<NetworkMessage, Type?>[] Converters { get; } =
{
m => m.MessageType == EMessageType.CallRequest ? typeof(DefaultCallRequest) : null,
m => m.MessageType == EMessageType.CallRequest ? typeof(CallRequest) : null,
m => m.MessageType == EMessageType.CancelRequest ? typeof(CancelRequest) : null,
m => m.MessageType == EMessageType.EventRequest ? typeof(DefaultCallRequest) : null,
m => m.MessageType == EMessageType.EventRequest ? typeof(CallRequest) : null,
m => m.MessageType == EMessageType.ClientDisconnect ? typeof(ClientDisconnect) : null
};
private void HandleCancelRequest(CancelRequest req)
{
_executeModule.Execute(req, _context.RealRepository, _representationModule, _context);
_executeModule.Cancel(req);
}
private void HandleCallRequest(DefaultCallRequest request)
private void HandleCallRequest(CallRequest request)
{
var result = _executeModule.Execute(request, _context.RealRepository, _representationModule, _context);
@@ -86,7 +86,7 @@ namespace mROA.Implementation.Frontend
_representationModule.PostCallMessageAsync(request.Id, resultType, result, _context).ConfigureAwait(false);
}
private void HandleEventRequest(DefaultCallRequest request)
private void HandleEventRequest(CallRequest request)
{
_executeModule.Execute(request, _context.RemoteRepository, _representationModule, _context);
}
+3 -3
View File
@@ -55,7 +55,7 @@ namespace mROA.Implementation
protected async Task<T> GetResultAsync<T>(int methodId, object?[]? parameters = null,
CancellationToken cancellationToken = default)
{
var request = new DefaultCallRequest
var request = new CallRequest
{
Id = RequestId.Generate(), CommandId = methodId, ObjectId = _identifier, Parameters = parameters
};
@@ -100,7 +100,7 @@ namespace mROA.Implementation
protected async Task CallAsync(int methodId, object?[]? parameters = null,
CancellationToken cancellationToken = default)
{
var request = new DefaultCallRequest
var request = new CallRequest
{
Id = RequestId.Generate(), CommandId = methodId, ObjectId = _identifier, Parameters = parameters
};
@@ -147,7 +147,7 @@ namespace mROA.Implementation
protected async Task CallUntrustedAsync(int methodId, object?[]? parameters = null)
{
var request = new DefaultCallRequest
var request = new CallRequest
{
Id = RequestId.Generate(), CommandId = methodId, ObjectId = _identifier, Parameters = parameters
};