Теперь без шела работает

This commit is contained in:
2025-03-01 19:06:18 +03:00
parent b5b766342e
commit bd3c623626
16 changed files with 219 additions and 176 deletions
+1 -1
View File
@@ -3,7 +3,7 @@ using mROA.Implementation.Attributes;
namespace Example.Shared namespace Example.Shared
{ {
// [SharedObjectInterface] // [SharedObjectInterface]
// public interface IDataList<T> // public interface IDataList<T> : IShared
// { // {
// T Get(int index); // T Get(int index);
// void Add(T item); // void Add(T item);
+2 -1
View File
@@ -1,11 +1,12 @@
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using mROA.Implementation;
using mROA.Implementation.Attributes; using mROA.Implementation.Attributes;
namespace Example.Shared namespace Example.Shared
{ {
[SharedObjectInterface] [SharedObjectInterface]
public interface ILoadTest public interface ILoadTest : IShared
{ {
int Next(int last); int Next(int last);
int Last(int next); int Last(int next);
+2 -1
View File
@@ -1,9 +1,10 @@
using mROA.Implementation;
using mROA.Implementation.Attributes; using mROA.Implementation.Attributes;
namespace Example.Shared namespace Example.Shared
{ {
[SharedObjectInterface] [SharedObjectInterface]
public interface IPage public interface IPage : IShared
{ {
byte[] GetData(); byte[] GetData();
} }
+1 -1
View File
@@ -7,7 +7,7 @@ using mROA.Implementation.Attributes;
namespace Example.Shared namespace Example.Shared
{ {
[SharedObjectInterface] [SharedObjectInterface]
public interface IPrinter : IDisposable public interface IPrinter : IDisposable, IShared
{ {
string GetName(); string GetName();
Task<IPage> Print(string text, CancellationToken cancellationToken); Task<IPage> Print(string text, CancellationToken cancellationToken);
+1 -2
View File
@@ -4,13 +4,12 @@ using mROA.Implementation.Attributes;
namespace Example.Shared namespace Example.Shared
{ {
[SharedObjectInterface] [SharedObjectInterface]
public interface IPrinterFactory public interface IPrinterFactory : IShared
{ {
IPrinter Create(string printerName); IPrinter Create(string printerName);
void Register(IPrinter printer); void Register(IPrinter printer);
IPrinter GetPrinterByName(string printerName); IPrinter GetPrinterByName(string printerName);
IPrinter GetFirstPrinter(); IPrinter GetFirstPrinter();
string[] CollectAllNames(); string[] CollectAllNames();
} }
} }
+38 -11
View File
@@ -12,7 +12,6 @@ namespace mROA.Cbor
{ {
public class CborSerializationToolkit : IContextualSerializationToolKit public class CborSerializationToolkit : IContextualSerializationToolKit
{ {
private List<Type> _remoteTypes;
public byte[] Serialize(object objectToSerialize, IEndPointContext context) public byte[] Serialize(object objectToSerialize, IEndPointContext context)
{ {
var writer = new CborWriter(); var writer = new CborWriter();
@@ -79,9 +78,6 @@ namespace mROA.Cbor
case double d: case double d:
writer.WriteDouble(d); writer.WriteDouble(d);
break; break;
// case decimal dec:
// writer.WriteDecimal(dec);
// break;
case bool b: case bool b:
writer.WriteBoolean(b); writer.WriteBoolean(b);
break; break;
@@ -112,7 +108,7 @@ namespace mROA.Cbor
case IList enumerable: case IList enumerable:
WriteList(enumerable, writer, context); WriteList(enumerable, writer, context);
break; break;
case ISharedObject sharedObject: case ISharedObjectShell sharedObject:
if (context != null) if (context != null)
sharedObject.EndPointContext = context; sharedObject.EndPointContext = context;
WriteObject(sharedObject, writer, context); WriteObject(sharedObject, writer, context);
@@ -161,7 +157,22 @@ namespace mROA.Cbor
{ {
var type = obj.GetType(); var type = obj.GetType();
if (obj is IShared)
{
var generic = obj.GetType().GetInterfaces().FirstOrDefault(i => typeof(IShared).IsAssignableFrom(i));
var sharedShell = typeof(SharedObjectShellShell<>).MakeGenericType(generic);
var so =
Activator.CreateInstance(sharedShell, obj) as
ISharedObjectShell;
if (context != null)
so.EndPointContext = context;
writer.WriteStartArray(1);
writer.WriteUInt64(so.Identifier.Flat);
writer.WriteEndArray();
return;
}
var properties = FilterProperties(type.GetProperties()); var properties = FilterProperties(type.GetProperties());
var values = properties.Select(property => property.GetValue(obj)).ToList(); var values = properties.Select(property => property.GetValue(obj)).ToList();
@@ -204,7 +215,7 @@ namespace mROA.Cbor
if (type == null) if (type == null)
return ReadList(reader, null, context); return ReadList(reader, null, context);
if (type.IsSubclassOf(typeof(ISharedObject))) if (type.IsSubclassOf(typeof(ISharedObjectShell)))
return ReadSharedObject(reader, type, context); return ReadSharedObject(reader, type, context);
if (typeof(IList).IsAssignableFrom(type) || type.IsArray) if (typeof(IList).IsAssignableFrom(type) || type.IsArray)
@@ -287,12 +298,30 @@ namespace mROA.Cbor
private object ReadObject(CborReader reader, Type type, IEndPointContext? context) private object ReadObject(CborReader reader, Type type, IEndPointContext? context)
{ {
if (type == typeof(object)) if (type == typeof(object))
{ {
return new PreParsedValue(ReadList(reader, null, context) as List<object>); return new PreParsedValue(ReadList(reader, null, context) as List<object>);
} }
if (type.IsInterface)
{
Console.WriteLine("Interface");
var sharedShell = typeof(SharedObjectShellShell<>).MakeGenericType(type);
var so =
Activator.CreateInstance(sharedShell) as
ISharedObjectShell;
if (context != null)
so.EndPointContext = context;
reader.ReadStartArray();
var identifier = reader.ReadUInt64();
reader.ReadEndArray();
so.Identifier = UniversalObjectIdentifier.FromFlat(identifier);
return so.UniversalValue;
}
var instance = Activator.CreateInstance(type)!; var instance = Activator.CreateInstance(type)!;
FillObject(instance, type, reader, context); FillObject(instance, type, reader, context);
@@ -300,9 +329,9 @@ namespace mROA.Cbor
return instance; return instance;
} }
private ISharedObject ReadSharedObject(CborReader reader, Type type, IEndPointContext? context) private ISharedObjectShell ReadSharedObject(CborReader reader, Type type, IEndPointContext? context)
{ {
var sharedObject = (Activator.CreateInstance(type) as ISharedObject)!; var sharedObject = (Activator.CreateInstance(type) as ISharedObjectShell)!;
if (context != null) if (context != null)
{ {
sharedObject.EndPointContext = context; sharedObject.EndPointContext = context;
@@ -321,7 +350,6 @@ namespace mROA.Cbor
var length = reader.ReadStartArray(); var length = reader.ReadStartArray();
try try
{ {
#if TRACE #if TRACE
Console.WriteLine($"Reading list of {length} objects, {properties.Count} properties found"); Console.WriteLine($"Reading list of {length} objects, {properties.Count} properties found");
#endif #endif
@@ -358,7 +386,6 @@ namespace mROA.Cbor
public void Inject<T>(T dependency) public void Inject<T>(T dependency)
{ {
_remoteTypes = RemoteContextRepository.RemoteTypes.Keys.ToList();
} }
public byte[] Serialize<T>(T objectToSerialize) public byte[] Serialize<T>(T objectToSerialize)
+15 -1
View File
@@ -21,11 +21,25 @@ namespace mROA.Cbor
public object? ToObject(Type type, IEndPointContext? context) public object? ToObject(Type type, IEndPointContext? context)
{ {
if (type.IsInterface)
{
var sharedShell = typeof(SharedObjectShellShell<>).MakeGenericType(type);
var so =
Activator.CreateInstance(sharedShell) as
ISharedObjectShell;
if (context != null)
so.EndPointContext = context;
so.Identifier = UniversalObjectIdentifier.FromFlat((ulong)_properties[0]);
return so.UniversalValue;
}
var instance = Activator.CreateInstance(type); var instance = Activator.CreateInstance(type);
if (instance == null) if (instance == null)
return null; return null;
if (instance is ISharedObject sharedObject && context != null) if (instance is ISharedObjectShell sharedObject && context != null)
{ {
sharedObject.EndPointContext = context; sharedObject.EndPointContext = context;
} }
+1 -1
View File
@@ -7,7 +7,7 @@ namespace mROA.Abstract
{ {
int ResisterObject(object o); int ResisterObject(object o);
void ClearObject(int id); void ClearObject(int id);
T GetObjectBySharedObject<T>(SharedObject<T> sharedObject); T GetObjectBySharedObject<T>(SharedObjectShellShell<T> sharedObjectShellShell);
T? GetObject<T>(int id); T? GetObject<T>(int id);
object GetSingleObject(Type type); object GetSingleObject(Type type);
int GetObjectIndex(object o); int GetObjectIndex(object o);
+7
View File
@@ -0,0 +1,7 @@
namespace mROA.Implementation
{
#pragma warning disable CS8618, CS9264
public interface IShared
{
}
}
@@ -58,9 +58,9 @@ namespace mROA.Implementation.Backend
_lastIndexFinder = Task.FromResult(id); _lastIndexFinder = Task.FromResult(id);
} }
public T GetObjectBySharedObject<T>(SharedObject<T> sharedObject) public T GetObjectBySharedObject<T>(SharedObjectShellShell<T> sharedObjectShellShell)
{ {
return (T)GetObject(sharedObject.Identifier.ContextId); return (T)GetObject(sharedObjectShellShell.Identifier.ContextId);
} }
public object GetObject(int id) public object GetObject(int id)
@@ -39,10 +39,10 @@ namespace mROA.Implementation.Backend
repository.ClearObject(id); repository.ClearObject(id);
} }
public T GetObjectBySharedObject<T>(SharedObject<T> sharedObject) public T GetObjectBySharedObject<T>(SharedObjectShellShell<T> sharedObjectShellShell)
{ {
var repository = GetRepository(sharedObject.Identifier.OwnerId); var repository = GetRepository(sharedObjectShellShell.Identifier.OwnerId);
return repository.GetObject<T>(sharedObject.Identifier.ContextId); return repository.GetObject<T>(sharedObjectShellShell.Identifier.ContextId);
} }
public object GetObject(int id) public object GetObject(int id)
@@ -19,14 +19,14 @@ namespace mROA.Implementation
throw new NotSupportedException(); throw new NotSupportedException();
} }
public T GetObjectBySharedObject<T>(SharedObject<T> sharedObject) public T GetObjectBySharedObject<T>(SharedObjectShellShell<T> sharedObjectShellShell)
{ {
if (_representationProducer == null) if (_representationProducer == null)
throw new NullReferenceException("representation producer is not initialized"); throw new NullReferenceException("representation producer is not initialized");
if (!RemoteTypes.TryGetValue(typeof(T), out var remoteType)) throw new NotSupportedException(); if (!RemoteTypes.TryGetValue(typeof(T), out var remoteType)) throw new NotSupportedException();
var representationModule = _representationProducer.Produce(sharedObject.Identifier.OwnerId); var representationModule = _representationProducer.Produce(sharedObjectShellShell.Identifier.OwnerId);
var remote = (T)Activator.CreateInstance(remoteType, sharedObject.Identifier.ContextId, var remote = (T)Activator.CreateInstance(remoteType, sharedObjectShellShell.Identifier.ContextId,
representationModule)!; representationModule)!;
return remote; return remote;
} }
-140
View File
@@ -1,140 +0,0 @@
using System;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using mROA.Abstract;
using mROA.Implementation.Attributes;
// ReSharper disable UnusedMember.Global
#pragma warning disable CS8618, CS9264
namespace mROA.Implementation
{
public static class TransmissionConfig
{
#if TRACE
public static int TotalTransmittedBytes { get; set; }
#endif
private static IContextRepository? _realContextRepository;
private static IContextRepository? _remoteEndpointContextRepository;
private static IOwnershipRepository? _ownershipRepository;
public static IContextRepository RealContextRepository
{
get => _realContextRepository ?? throw new NullReferenceException("RealContextRepository is null");
set => _realContextRepository = value;
}
public static IContextRepository RemoteEndpointContextRepository
{
get => _remoteEndpointContextRepository ??
throw new NullReferenceException("RemoteEndpointContextRepository is null");
set => _remoteEndpointContextRepository = value;
}
public static IOwnershipRepository OwnershipRepository
{
get => _ownershipRepository ?? throw new NullReferenceException("OwnershipRepository is null");
set => _ownershipRepository = value;
}
}
public interface ISharedObject
{
IEndPointContext EndPointContext { get; set; }
}
public class SharedObject<T> : ISharedObject where T : notnull
{
[SerializationIgnore]
[JsonIgnore]
public IEndPointContext EndPointContext { get; set; } = new EndPointContext
{
RealRepository = TransmissionConfig.RealContextRepository,
RemoteRepository = TransmissionConfig.RemoteEndpointContextRepository,
HostId = TransmissionConfig.OwnershipRepository.GetHostOwnershipId(),
OwnerFunc = TransmissionConfig.OwnershipRepository.GetOwnershipId
};
private IContextRepository GetDefaultContextRepository() =>
(_identifier.OwnerId == EndPointContext.HostId
? EndPointContext.RealRepository
: EndPointContext.RemoteRepository) ??
throw new NullReferenceException(
"DefaultContextRepository was not defined");
private UniversalObjectIdentifier _identifier = UniversalObjectIdentifier.Null;
public UniversalObjectIdentifier Identifier
{
get
{
_identifier.OwnerId = _identifier.OwnerId == -1 ? EndPointContext.OwnerId : _identifier.OwnerId;
return _identifier;
}
set
{
_identifier = value;
Value = GetDefaultContextRepository().GetObjectBySharedObject(this);
}
}
// public int OwnerId
// {
// get
// {
// _ownerId = _ownerId == -1 ? EndPointContext.OwnerId : _ownerId;
// return _ownerId;
// }
// set => _ownerId = value;
// }
//
// // ReSharper disable once MemberCanBePrivate.Global
// public int ContextId
// {
// // ReSharper disable once UnusedMember.Global
// get
// {
// if (_contextId != -2)
// return _contextId;
//
// _contextId = EndPointContext.RealRepository.GetObjectIndex(Value);
// return _contextId;
// }
// set
// {
// _contextId = value;
// Value = GetDefaultContextRepository().GetObjectBySharedObject(this);
// }
// }
[JsonIgnore] [SerializationIgnore] public T Value { get; private set; }
// ReSharper disable once MemberCanBePrivate.Global
// ReSharper disable once UnusedMember.Global
public SharedObject()
{
}
// ReSharper disable once UnusedMember.Global
public SharedObject(T value)
{
Value = value;
if (value is RemoteObjectBase ro)
{
_identifier = ro.Identifier;
}
else
{
_identifier.OwnerId = EndPointContext.HostId;
_identifier.ContextId = EndPointContext.RealRepository.GetObjectIndex(Value);
}
}
public static implicit operator T(SharedObject<T> value) => value.Value;
public static implicit operator SharedObject<T>(T value) =>
new(value);
}
}
+100
View File
@@ -0,0 +1,100 @@
using System;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using mROA.Abstract;
using mROA.Implementation.Attributes;
// ReSharper disable UnusedMember.Global
#pragma warning disable CS8618, CS9264
namespace mROA.Implementation
{
public interface ISharedObjectShell
{
IEndPointContext EndPointContext { get; set; }
UniversalObjectIdentifier Identifier { get; set; }
object UniversalValue { get; set; }
}
public class SharedObjectShellShell<T> : ISharedObjectShell where T : notnull
{
[SerializationIgnore]
[JsonIgnore]
public IEndPointContext EndPointContext { get; set; } = new EndPointContext
{
RealRepository = TransmissionConfig.RealContextRepository,
RemoteRepository = TransmissionConfig.RemoteEndpointContextRepository,
HostId = TransmissionConfig.OwnershipRepository.GetHostOwnershipId(),
OwnerFunc = TransmissionConfig.OwnershipRepository.GetOwnershipId
};
private IContextRepository GetDefaultContextRepository() =>
(_identifier.OwnerId == EndPointContext.HostId
? EndPointContext.RealRepository
: EndPointContext.RemoteRepository) ??
throw new NullReferenceException(
"DefaultContextRepository was not defined");
private UniversalObjectIdentifier _identifier = UniversalObjectIdentifier.Null;
public UniversalObjectIdentifier Identifier
{
get
{
_identifier.OwnerId = _identifier.OwnerId == -1 ? EndPointContext.OwnerId : _identifier.OwnerId;
return _identifier;
}
set
{
_identifier = value;
Value = GetDefaultContextRepository().GetObjectBySharedObject(this);
}
}
public object UniversalValue
{
get => _value;
set => _value = (T)value;
}
private T _value;
[JsonIgnore]
[SerializationIgnore]
public T Value
{
get => _value;
set
{
_value = value;
if (value is RemoteObjectBase ro)
{
_identifier = ro.Identifier;
}
else
{
_identifier.OwnerId = EndPointContext.HostId;
_identifier.ContextId = EndPointContext.RealRepository.GetObjectIndex(Value);
}
}
}
// ReSharper disable once MemberCanBePrivate.Global
// ReSharper disable once UnusedMember.Global
public SharedObjectShellShell()
{
}
// ReSharper disable once UnusedMember.Global
public SharedObjectShellShell(T value)
{
Value = value;
}
public static implicit operator T(SharedObjectShellShell<T> value) => value.Value;
public static implicit operator SharedObjectShellShell<T>(T value) =>
new(value);
}
}
+35
View File
@@ -0,0 +1,35 @@
using System;
using mROA.Abstract;
namespace mROA.Implementation
{
#pragma warning disable CS8618, CS9264
public static class TransmissionConfig
{
#if TRACE
public static int TotalTransmittedBytes { get; set; }
#endif
private static IContextRepository? _realContextRepository;
private static IContextRepository? _remoteEndpointContextRepository;
private static IOwnershipRepository? _ownershipRepository;
public static IContextRepository RealContextRepository
{
get => _realContextRepository ?? throw new NullReferenceException("RealContextRepository is null");
set => _realContextRepository = value;
}
public static IContextRepository RemoteEndpointContextRepository
{
get => _remoteEndpointContextRepository ??
throw new NullReferenceException("RemoteEndpointContextRepository is null");
set => _remoteEndpointContextRepository = value;
}
public static IOwnershipRepository OwnershipRepository
{
get => _ownershipRepository ?? throw new NullReferenceException("OwnershipRepository is null");
set => _ownershipRepository = value;
}
}
}
@@ -9,6 +9,8 @@ namespace mROA.Implementation
public int OwnerId; public int OwnerId;
public static UniversalObjectIdentifier Null = new UniversalObjectIdentifier { ContextId = -2, OwnerId = -1 }; public static UniversalObjectIdentifier Null = new UniversalObjectIdentifier { ContextId = -2, OwnerId = -1 };
public static UniversalObjectIdentifier FromFlat(ulong flat) => new() { Flat = flat };
public override string ToString() public override string ToString()
{ {
return $"{{ {nameof(ContextId)}: {ContextId}, {nameof(OwnerId)}: {OwnerId} }}"; return $"{{ {nameof(ContextId)}: {ContextId}, {nameof(OwnerId)}: {OwnerId} }}";
@@ -18,10 +20,7 @@ namespace mROA.Implementation
public ulong Flat public ulong Flat
{ {
get get { return (ulong)OwnerId << 32 | (uint)ContextId; }
{
return (ulong)OwnerId << 32 | (uint)ContextId;
}
set set
{ {
OwnerId = (int)(value >> 32); OwnerId = (int)(value >> 32);