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

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
{
// [SharedObjectInterface]
// public interface IDataList<T>
// public interface IDataList<T> : IShared
// {
// T Get(int index);
// void Add(T item);
+2 -1
View File
@@ -1,11 +1,12 @@
using System.Threading;
using System.Threading.Tasks;
using mROA.Implementation;
using mROA.Implementation.Attributes;
namespace Example.Shared
{
[SharedObjectInterface]
public interface ILoadTest
public interface ILoadTest : IShared
{
int Next(int last);
int Last(int next);
+2 -1
View File
@@ -1,9 +1,10 @@
using mROA.Implementation;
using mROA.Implementation.Attributes;
namespace Example.Shared
{
[SharedObjectInterface]
public interface IPage
public interface IPage : IShared
{
byte[] GetData();
}
+1 -1
View File
@@ -7,7 +7,7 @@ using mROA.Implementation.Attributes;
namespace Example.Shared
{
[SharedObjectInterface]
public interface IPrinter : IDisposable
public interface IPrinter : IDisposable, IShared
{
string GetName();
Task<IPage> Print(string text, CancellationToken cancellationToken);
+1 -2
View File
@@ -4,13 +4,12 @@ using mROA.Implementation.Attributes;
namespace Example.Shared
{
[SharedObjectInterface]
public interface IPrinterFactory
public interface IPrinterFactory : IShared
{
IPrinter Create(string printerName);
void Register(IPrinter printer);
IPrinter GetPrinterByName(string printerName);
IPrinter GetFirstPrinter();
string[] CollectAllNames();
}
}
+42 -15
View File
@@ -12,7 +12,6 @@ namespace mROA.Cbor
{
public class CborSerializationToolkit : IContextualSerializationToolKit
{
private List<Type> _remoteTypes;
public byte[] Serialize(object objectToSerialize, IEndPointContext context)
{
var writer = new CborWriter();
@@ -79,9 +78,6 @@ namespace mROA.Cbor
case double d:
writer.WriteDouble(d);
break;
// case decimal dec:
// writer.WriteDecimal(dec);
// break;
case bool b:
writer.WriteBoolean(b);
break;
@@ -112,7 +108,7 @@ namespace mROA.Cbor
case IList enumerable:
WriteList(enumerable, writer, context);
break;
case ISharedObject sharedObject:
case ISharedObjectShell sharedObject:
if (context != null)
sharedObject.EndPointContext = context;
WriteObject(sharedObject, writer, context);
@@ -160,9 +156,24 @@ namespace mROA.Cbor
private void WriteObject(object obj, CborWriter writer, IEndPointContext? context)
{
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 values = properties.Select(property => property.GetValue(obj)).ToList();
WriteList(values, writer, context);
@@ -204,7 +215,7 @@ namespace mROA.Cbor
if (type == null)
return ReadList(reader, null, context);
if (type.IsSubclassOf(typeof(ISharedObject)))
if (type.IsSubclassOf(typeof(ISharedObjectShell)))
return ReadSharedObject(reader, type, context);
if (typeof(IList).IsAssignableFrom(type) || type.IsArray)
@@ -287,12 +298,30 @@ namespace mROA.Cbor
private object ReadObject(CborReader reader, Type type, IEndPointContext? context)
{
if (type == typeof(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)!;
FillObject(instance, type, reader, context);
@@ -300,9 +329,9 @@ namespace mROA.Cbor
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)
{
sharedObject.EndPointContext = context;
@@ -321,9 +350,8 @@ namespace mROA.Cbor
var length = reader.ReadStartArray();
try
{
#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
for (var index = 0; index < length; index++)
@@ -358,7 +386,6 @@ namespace mROA.Cbor
public void Inject<T>(T dependency)
{
_remoteTypes = RemoteContextRepository.RemoteTypes.Keys.ToList();
}
public byte[] Serialize<T>(T objectToSerialize)
+15 -1
View File
@@ -21,11 +21,25 @@ namespace mROA.Cbor
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);
if (instance == null)
return null;
if (instance is ISharedObject sharedObject && context != null)
if (instance is ISharedObjectShell sharedObject && context != null)
{
sharedObject.EndPointContext = context;
}
+1 -1
View File
@@ -7,7 +7,7 @@ namespace mROA.Abstract
{
int ResisterObject(object o);
void ClearObject(int id);
T GetObjectBySharedObject<T>(SharedObject<T> sharedObject);
T GetObjectBySharedObject<T>(SharedObjectShellShell<T> sharedObjectShellShell);
T? GetObject<T>(int id);
object GetSingleObject(Type type);
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);
}
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)
@@ -39,10 +39,10 @@ namespace mROA.Implementation.Backend
repository.ClearObject(id);
}
public T GetObjectBySharedObject<T>(SharedObject<T> sharedObject)
public T GetObjectBySharedObject<T>(SharedObjectShellShell<T> sharedObjectShellShell)
{
var repository = GetRepository(sharedObject.Identifier.OwnerId);
return repository.GetObject<T>(sharedObject.Identifier.ContextId);
var repository = GetRepository(sharedObjectShellShell.Identifier.OwnerId);
return repository.GetObject<T>(sharedObjectShellShell.Identifier.ContextId);
}
public object GetObject(int id)
@@ -19,14 +19,14 @@ namespace mROA.Implementation
throw new NotSupportedException();
}
public T GetObjectBySharedObject<T>(SharedObject<T> sharedObject)
public T GetObjectBySharedObject<T>(SharedObjectShellShell<T> sharedObjectShellShell)
{
if (_representationProducer == null)
throw new NullReferenceException("representation producer is not initialized");
if (!RemoteTypes.TryGetValue(typeof(T), out var remoteType)) throw new NotSupportedException();
var representationModule = _representationProducer.Produce(sharedObject.Identifier.OwnerId);
var remote = (T)Activator.CreateInstance(remoteType, sharedObject.Identifier.ContextId,
var representationModule = _representationProducer.Produce(sharedObjectShellShell.Identifier.OwnerId);
var remote = (T)Activator.CreateInstance(remoteType, sharedObjectShellShell.Identifier.ContextId,
representationModule)!;
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,19 +9,18 @@ namespace mROA.Implementation
public int OwnerId;
public static UniversalObjectIdentifier Null = new UniversalObjectIdentifier { ContextId = -2, OwnerId = -1 };
public static UniversalObjectIdentifier FromFlat(ulong flat) => new() { Flat = flat };
public override string ToString()
{
return $"{{ {nameof(ContextId)}: {ContextId}, {nameof(OwnerId)}: {OwnerId} }}";
}
public bool IsStatic => ContextId == -1;
public ulong Flat
{
get
{
return (ulong)OwnerId << 32 | (uint)ContextId;
}
get { return (ulong)OwnerId << 32 | (uint)ContextId; }
set
{
OwnerId = (int)(value >> 32);