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

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
@@ -117,3 +117,48 @@ public class TestParameter
public int A { get; set; } public int A { get; set; }
public TransmittedSharedObject<ITestParameter> LinkedObject { 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;
}
+16 -6
View File
@@ -1,5 +1,6 @@
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Text.Json;
using mROA.Implementation; using mROA.Implementation;
using mROA.Implementation.Test; using mROA.Implementation.Test;
@@ -12,6 +13,7 @@ public class StreamTest
private JsonFrontendSerialisationModule _frontendSerialisationModule; private JsonFrontendSerialisationModule _frontendSerialisationModule;
private ISerialisationModule _serialisationModule; private ISerialisationModule _serialisationModule;
private IExecuteModule _executeModule; private IExecuteModule _executeModule;
bool isTestNotFinished = true;
[SetUp] [SetUp]
public void Setup() public void Setup()
@@ -25,12 +27,7 @@ public class StreamTest
_frontendInteractionModule = new StreamBasedFrontendInteractionModule(); _frontendInteractionModule = new StreamBasedFrontendInteractionModule();
_frontendSerialisationModule = new JsonFrontendSerialisationModule(_frontendInteractionModule); _frontendSerialisationModule = new JsonFrontendSerialisationModule(_frontendInteractionModule);
}
[Test]
public void StreamingTest()
{
bool isTestNotFinished = true;
Task.Run(() => Task.Run(() =>
{ {
TcpListener listener = new TcpListener(IPAddress.Loopback, 4567); TcpListener listener = new TcpListener(IPAddress.Loopback, 4567);
@@ -44,13 +41,26 @@ public class StreamTest
while (isTestNotFinished) ; while (isTestNotFinished) ;
}); });
}
[Test]
public void StreamingTest()
{
var tcpClient = new TcpClient(); var tcpClient = new TcpClient();
tcpClient.Connect(IPAddress.Loopback, 4567); tcpClient.Connect(IPAddress.Loopback, 4567);
_frontendInteractionModule.ServerStream = tcpClient.GetStream(); _frontendInteractionModule.ServerStream = tcpClient.GetStream();
var req = new JsonCallRequest { CommandId = 1, ObjectId = -1 }; var req = new JsonCallRequest { CommandId = 1, ObjectId = -1 };
_frontendSerialisationModule.PostCallRequest(req); _frontendSerialisationModule.PostCallRequest(req);
var res =_frontendSerialisationModule.GetNextCommandExecution<FinalCommandExecution>(req.CallRequestId).Result; var res = ((JsonElement)_frontendSerialisationModule.GetNextCommandExecution<FinalCommandExecution>(req.CallRequestId).Result).Deserialize<MockResult>();
isTestNotFinished = false; 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> <RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile> <ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile>
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot> <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> <NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.12.0</NuGetToolVersion> <NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.12.0</NuGetToolVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' "> <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\" /> <SourceRoot Include="C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages\" />
</ItemGroup> </ItemGroup>
<ImportGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' "> <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')" /> <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> </ImportGroup>
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' "> <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> </PropertyGroup>
</Project> </Project>
+5 -2
View File
@@ -1,10 +1,13 @@
namespace mROA; using mROA.Implementation;
namespace mROA;
public interface IContextRepository public interface IContextRepository
{ {
int ResisterObject(object o); int ResisterObject(object o);
void ClearObject(int id); void ClearObject(int id);
object GetObject(int id); object? GetObject(int id);
T? GetObject<T>(int id);
object GetSingleObject(Type type); object GetSingleObject(Type type);
int GetObjectIndex(object o); int GetObjectIndex(object o);
} }
+12 -3
View File
@@ -24,7 +24,11 @@ public class ContextRepository : IContextRepository
var types = assembly.GetTypes().Where(type => var types = assembly.GetTypes().Where(type =>
type is { IsClass: true, IsAbstract: false, IsGenericType: false } && type is { IsClass: true, IsAbstract: false, IsGenericType: false } &&
type.GetCustomAttributes(typeof(SharedObjectSingletonAttribute), true).Length > 0); 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) public int ResisterObject(object o)
@@ -46,14 +50,19 @@ public class ContextRepository : IContextRepository
_lastIndexFinder = Task.FromResult(id); _lastIndexFinder = Task.FromResult(id);
} }
public object GetObject(int id) public object? GetObject(int id)
{ {
return id == -1 || _storage.Length <= id ? null : _storage[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) 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) 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; }
}
@@ -26,7 +26,6 @@ public class StreamBasedInteractionModule : IInteractionModule
stream.Write(BitConverter.GetBytes((ushort)message.Length), 0, sizeof(ushort)); stream.Write(BitConverter.GetBytes((ushort)message.Length), 0, sizeof(ushort));
stream.Write(message, 0, message.Length); stream.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)
@@ -40,15 +39,13 @@ public class StreamBasedInteractionModule : IInteractionModule
await client.stream.ReadExactlyAsync(buffer, 0, 2); await client.stream.ReadExactlyAsync(buffer, 0, 2);
var len = BitConverter.ToUInt16(buffer, 0); var len = BitConverter.ToUInt16(buffer, 0);
await client.stream.ReadExactlyAsync(buffer, 0, len); await client.stream.ReadExactlyAsync(buffer, 0, len);
Task.Run(() => action(client.id, buffer[..len]));
action(client.id, buffer[..len]);
} }
} }
catch (Exception e) catch (Exception)
{ {
Console.WriteLine($"Client handling finished:{client.id}"); Console.WriteLine($"Client handling finished:{client.id}");
_streams.Remove(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 class MockExecModule : IExecuteModule
{ {
public ICommandExecution Execute(ICallRequest command) public ICommandExecution Execute(ICallRequest command)
{ {
return new FinalCommandExecution 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; public int ContextId { get; set; } = -1;
[JsonIgnore] [JsonIgnore]
public T Value => (T)TransmissionConfig.DefaultContextRepository.GetObject(ContextId); public T? Value => TransmissionConfig.DefaultContextRepository.GetObject<T>(ContextId);
[JsonIgnore] [JsonIgnore]
public IContextRepository Context { get; set; } 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) => public static implicit operator TransmittedSharedObject<T>(T value) =>
new() { ContextId = TransmissionConfig.DefaultContextRepository.GetObjectIndex(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> <RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile> <ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile>
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot> <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> <NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.12.0</NuGetToolVersion> <NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.12.0</NuGetToolVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' "> <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\" /> <SourceRoot Include="C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages\" />
</ItemGroup> </ItemGroup>
</Project> </Project>