начало кодгена

This commit is contained in:
2025-02-03 10:06:05 +03:00
parent 3c73701197
commit 1f2df53c12
11 changed files with 152 additions and 32 deletions
+45
View File
@@ -116,4 +116,49 @@ public class TestParameter
{
public int A { get; set; }
public TransmittedSharedObject<ITestParameter> LinkedObject { get; set; }
}
public class TestControllerRemoteEndoint : ITestController, IRemoteObject
{
public TestControllerRemoteEndoint(int id)
{
_id = id;
}
public void A()
{
}
public Task AAsync(CancellationToken cancellationToken)
{
}
public int B()
{
}
public Task<int> BAsync(CancellationToken cancellationToken)
{
}
public TransmittedSharedObject<ITestController> SharedObjectTransmitionTest()
{
}
public int Parametrized(TestParameter parameter)
{
}
public TransmittedSharedObject<ITestParameter> GetTestParameter()
{
}
private int _id;
public int Id => _id;
}
+17 -7
View File
@@ -1,5 +1,6 @@
using System.Net;
using System.Net.Sockets;
using System.Text.Json;
using mROA.Implementation;
using mROA.Implementation.Test;
@@ -12,6 +13,7 @@ public class StreamTest
private JsonFrontendSerialisationModule _frontendSerialisationModule;
private ISerialisationModule _serialisationModule;
private IExecuteModule _executeModule;
bool isTestNotFinished = true;
[SetUp]
public void Setup()
@@ -25,12 +27,7 @@ public class StreamTest
_frontendInteractionModule = new StreamBasedFrontendInteractionModule();
_frontendSerialisationModule = new JsonFrontendSerialisationModule(_frontendInteractionModule);
}
[Test]
public void StreamingTest()
{
bool isTestNotFinished = true;
Task.Run(() =>
{
TcpListener listener = new TcpListener(IPAddress.Loopback, 4567);
@@ -44,13 +41,26 @@ public class StreamTest
while (isTestNotFinished) ;
});
}
[Test]
public void StreamingTest()
{
var tcpClient = new TcpClient();
tcpClient.Connect(IPAddress.Loopback, 4567);
_frontendInteractionModule.ServerStream = tcpClient.GetStream();
var req = new JsonCallRequest { CommandId = 1, ObjectId = -1 };
_frontendSerialisationModule.PostCallRequest(req);
var res =_frontendSerialisationModule.GetNextCommandExecution<FinalCommandExecution>(req.CallRequestId).Result;
var res = ((JsonElement)_frontendSerialisationModule.GetNextCommandExecution<FinalCommandExecution>(req.CallRequestId).Result).Deserialize<MockResult>();
isTestNotFinished = false;
Assert.That(res.A == "wqer" && res.B == 5);
}
[Test]
public void RemoteObjectTest()
{
}
}
+3 -3
View File
@@ -5,12 +5,12 @@
<RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile>
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot>
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\Mikhail\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages</NuGetPackageFolders>
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\Mimm\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages</NuGetPackageFolders>
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.12.0</NuGetToolVersion>
</PropertyGroup>
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<SourceRoot Include="C:\Users\Mikhail\.nuget\packages\" />
<SourceRoot Include="C:\Users\Mimm\.nuget\packages\" />
<SourceRoot Include="C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages\" />
</ItemGroup>
<ImportGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
@@ -21,6 +21,6 @@
<Import Project="$(NuGetPackageRoot)microsoft.net.test.sdk\17.12.0\build\netcoreapp3.1\Microsoft.NET.Test.Sdk.props" Condition="Exists('$(NuGetPackageRoot)microsoft.net.test.sdk\17.12.0\build\netcoreapp3.1\Microsoft.NET.Test.Sdk.props')" />
</ImportGroup>
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<PkgNUnit_Analyzers Condition=" '$(PkgNUnit_Analyzers)' == '' ">C:\Users\Mikhail\.nuget\packages\nunit.analyzers\4.6.0</PkgNUnit_Analyzers>
<PkgNUnit_Analyzers Condition=" '$(PkgNUnit_Analyzers)' == '' ">C:\Users\Mimm\.nuget\packages\nunit.analyzers\4.6.0</PkgNUnit_Analyzers>
</PropertyGroup>
</Project>
+5 -2
View File
@@ -1,10 +1,13 @@
namespace mROA;
using mROA.Implementation;
namespace mROA;
public interface IContextRepository
{
int ResisterObject(object o);
void ClearObject(int id);
object GetObject(int id);
object? GetObject(int id);
T? GetObject<T>(int id);
object GetSingleObject(Type type);
int GetObjectIndex(object o);
}
+13 -4
View File
@@ -24,14 +24,18 @@ public class ContextRepository : IContextRepository
var types = assembly.GetTypes().Where(type =>
type is { IsClass: true, IsAbstract: false, IsGenericType: false } &&
type.GetCustomAttributes(typeof(SharedObjectSingletonAttribute), true).Length > 0);
_singletons = types.ToFrozenDictionary(t => t.GetInterfaces().FirstOrDefault(i => i.GetCustomAttributes(typeof(SharedObjectInterafceAttribute), true).Length > 0)!.GetHashCode(), Activator.CreateInstance);
_singletons =
types.ToFrozenDictionary(
t => t.GetInterfaces().FirstOrDefault(i =>
i.GetCustomAttributes(typeof(SharedObjectInterafceAttribute), true).Length > 0)!.GetHashCode(),
Activator.CreateInstance);
}
public int ResisterObject(object o)
{
if (!_lastIndexFinder.IsCompleted)
_lastIndexFinder.Wait();
_storage[_lastIndexFinder.Result] = o;
var last = _lastIndexFinder.Result;
@@ -46,14 +50,19 @@ public class ContextRepository : IContextRepository
_lastIndexFinder = Task.FromResult(id);
}
public object GetObject(int id)
public object? GetObject(int id)
{
return id == -1 || _storage.Length <= id ? null : _storage[id];
}
public T GetObject<T>(int id)
{
return id == -1 || _storage.Length <= id ? throw new NullReferenceException("Cannot find that object. It is null"): (T)_storage[id];
}
public object GetSingleObject(Type type)
{
return _singletons.GetValueOrDefault(type.GetHashCode());
return _singletons.GetValueOrDefault(type.GetHashCode()) ?? throw new ArgumentException("Unregistered singleton type");
}
public int GetObjectIndex(object o)
@@ -0,0 +1,47 @@
using System.Collections.Frozen;
namespace mROA.Implementation;
public class FrontendContextRepository(Dictionary<Type, Type> remoteTypes) : IContextRepository
{
private FrozenDictionary<Type, Type> _remoteTypes = remoteTypes.ToFrozenDictionary();
public int ResisterObject(object o)
{
throw new NotSupportedException();
}
public void ClearObject(int id)
{
throw new NotSupportedException();
}
public object GetObject(int id)
{
throw new NotSupportedException();
}
public T GetObject<T>(int id)
{
if (_remoteTypes.TryGetValue(typeof(T), out var remoteType))
{
var remote = (T)Activator.CreateInstance(remoteType, id)!;
return remote;
}
throw new NotSupportedException();
}
public object GetSingleObject(Type type)
{
throw new NotSupportedException();
}
public int GetObjectIndex(object o)
{
if (o is IRemoteObject remote)
{
return remote.Id;
}
throw new NotSupportedException();
}
}
+6
View File
@@ -0,0 +1,6 @@
namespace mROA.Implementation;
public interface IRemoteObject
{
internal int Id { get; }
}
@@ -4,14 +4,14 @@ public class StreamBasedInteractionModule : IInteractionModule
{
private Dictionary<int, Stream> _streams = new();
private Action<int, byte[]> _handler;
public void RegisterClient(Stream stream)
{
var id = Random.Shared.Next();
_streams.Add(id, stream);
ListenTo((id, stream), _handler);
}
public void SetMessageHandler(Action<int, byte[]> handler)
{
_handler = handler;
@@ -26,7 +26,6 @@ public class StreamBasedInteractionModule : IInteractionModule
stream.Write(BitConverter.GetBytes((ushort)message.Length), 0, sizeof(ushort));
stream.Write(message, 0, message.Length);
}
private async Task ListenTo((int id, Stream stream) client, Action<int, byte[]> action)
@@ -40,15 +39,13 @@ public class StreamBasedInteractionModule : IInteractionModule
await client.stream.ReadExactlyAsync(buffer, 0, 2);
var len = BitConverter.ToUInt16(buffer, 0);
await client.stream.ReadExactlyAsync(buffer, 0, len);
action(client.id, buffer[..len]);
Task.Run(() => action(client.id, buffer[..len]));
}
}
catch (Exception e)
catch (Exception)
{
Console.WriteLine($"Client handling finished:{client.id}");
_streams.Remove(client.id);
}
}
}
+8 -2
View File
@@ -1,10 +1,16 @@
namespace mROA.Implementation;
namespace mROA.Implementation.Test;
public class MockExecModule : IExecuteModule
{
public ICommandExecution Execute(ICallRequest command)
{
return new FinalCommandExecution
{ CommandId = command.CommandId, Result = new { A = "wqer", B = 5 }, CallRequestId = command.CallRequestId };
{ CommandId = command.CommandId, Result = new MockResult { A = "wqer", B = 5 }, CallRequestId = command.CallRequestId };
}
}
public class MockResult
{
public string A { get; set; }
public int B { get; set; }
}
@@ -12,16 +12,13 @@ public class TransmittedSharedObject<T>
public int ContextId { get; set; } = -1;
[JsonIgnore]
public T Value => (T)TransmissionConfig.DefaultContextRepository.GetObject(ContextId);
public T? Value => TransmissionConfig.DefaultContextRepository.GetObject<T>(ContextId);
[JsonIgnore]
public IContextRepository Context { get; set; }
public static implicit operator T(TransmittedSharedObject<T> value) => value.Value;
public static implicit operator T(TransmittedSharedObject<T> value) => value.Value!;
public static implicit operator TransmittedSharedObject<T>(T value) =>
new() { ContextId = TransmissionConfig.DefaultContextRepository.GetObjectIndex(value) };
// public void Fill() => Value = (T)TransmissionConfig.DefaultContextRepository.GetObject(ContextId);
// public void Collect() => ContextId = TransmissionConfig.DefaultContextRepository.GetObjectIndex(ContextId);
}
+2 -2
View File
@@ -5,12 +5,12 @@
<RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile>
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot>
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\Mikhail\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages</NuGetPackageFolders>
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\Mimm\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages</NuGetPackageFolders>
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.12.0</NuGetToolVersion>
</PropertyGroup>
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<SourceRoot Include="C:\Users\Mikhail\.nuget\packages\" />
<SourceRoot Include="C:\Users\Mimm\.nuget\packages\" />
<SourceRoot Include="C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages\" />
</ItemGroup>
</Project>