Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unix sockets support #499

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions rd-net/RdFramework.Reflection/BindableChildrenUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal static class BindableChildrenUtil

internal static void PrettyPrint(PrettyPrinter p, IReflectionBindable instance)
{
Action<IReflectionBindable, PrettyPrinter> prettyPrinter;
Action<IReflectionBindable, PrettyPrinter>? prettyPrinter;
lock (ourPrettyPrintersLock)
{
ourPrettyPrinters.TryGetValue(instance.GetType(), out prettyPrinter);
Expand Down Expand Up @@ -65,7 +65,7 @@ internal static void PrettyPrint(PrettyPrinter p, IReflectionBindable instance)
internal static void FillBindableFields(IReflectionBindable instance)
{
var type = instance.GetType();
Action<IReflectionBindable> fillBindableFields;
Action<IReflectionBindable>? fillBindableFields;
lock (ourFillBindableChildren)
{
ourFillBindableChildren.TryGetValue(type, out fillBindableFields);
Expand Down
4 changes: 2 additions & 2 deletions rd-net/RdFramework.Reflection/BuiltInSerializers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ public static bool HasBuiltInFields(TypeInfo t)
Assertion.Fail($"Invalid BuiltIn serializer for type {typeInfo}. Static field 'Read' with type {typeof(CtxReadDelegate<>).ToString(true)} not found");
if (writeField == null)
Assertion.Fail($"Invalid BuiltIn serializer for type {typeInfo}. Static field 'Write' with type {typeof(CtxWriteDelegate<>).ToString(true)} not found");
var reader = readField.GetValue(null);
var writer = writeField.GetValue(null);
var reader = readField.GetValue(null)!;
var writer = writeField.GetValue(null)!;
return new SerializerPair(reader, writer);
}

Expand Down
6 changes: 3 additions & 3 deletions rd-net/RdFramework.Reflection/CollectionSerializers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static SerializerPair CreateListSerializerPair<T>(SerializerPair itemSeri
}

public static SerializerPair CreateDictionarySerializerPair<TKey, TValue>(
SerializerPair keySerializer, SerializerPair valueSerializer)
SerializerPair keySerializer, SerializerPair valueSerializer) where TKey : notnull
{
var read = CreateReadDictionary<TKey, TValue>(keySerializer, valueSerializer);

Expand Down Expand Up @@ -53,7 +53,7 @@ public static SerializerPair CreateDictionarySerializerPair<TKey, TValue>(
}

public static SerializerPair CreateReadOnlyDictionarySerializerPair<TKey, TValue>(
SerializerPair keySerializer, SerializerPair valueSerializer)
SerializerPair keySerializer, SerializerPair valueSerializer) where TKey : notnull
{
#if NET35
throw new NotSupportedException();
Expand Down Expand Up @@ -88,7 +88,7 @@ public static SerializerPair CreateReadOnlyDictionarySerializerPair<TKey, TValue
}

private static CtxReadDelegate<Dictionary<TKey, TValue>?> CreateReadDictionary<TKey, TValue>(
SerializerPair keySerializer, SerializerPair valueSerializer)
SerializerPair keySerializer, SerializerPair valueSerializer) where TKey : notnull
{
CtxReadDelegate<Dictionary<TKey, TValue>?> read = (context, reader) =>
{
Expand Down
11 changes: 6 additions & 5 deletions rd-net/RdFramework.Reflection/ProxyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public struct FakeTuple<T1, T2, T3, T4, T5, T6, T7, TRest> {
public ProxyGenerator(bool allowSave = false)
{
myAllowSave = allowSave;
#if NETSTANDARD
#if NETSTANDARD || NETCOREAPP
myAssemblyBuilder = new Lazy<AssemblyBuilder>(() => AssemblyBuilder.DefineDynamicAssembly(new AssemblyName(DynamicAssemblyName), AssemblyBuilderAccess.Run));
myModuleBuilder = new Lazy<ModuleBuilder>(() => myAssemblyBuilder.Value.DefineDynamicModule(DynamicAssemblyName));
#else
Expand Down Expand Up @@ -325,14 +325,15 @@ private void ImplementProperty(TypeBuilderContext ctx, PropertyInfo propertyInfo
throw new Exception("Setter for properties in proxy interface is prohibited due to unclear semantic");
}

if (propertyInfo.GetGetMethod() != null)
var methodInfo = propertyInfo.GetGetMethod();
if (methodInfo != null)
{
var getMethod = typebuilder.DefineMethod(propertyInfo.GetGetMethod().Name, MethodAttributes.Final | MethodAttributes.Virtual | MethodAttributes.Private, type, EmptyArray<Type>.Instance);
var getMethod = typebuilder.DefineMethod(methodInfo.Name, MethodAttributes.Final | MethodAttributes.Virtual | MethodAttributes.Private, type, EmptyArray<Type>.Instance);
var il = getMethod.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldfld, field);
il.Emit(OpCodes.Ret);
typebuilder.DefineMethodOverride(getMethod, propertyInfo.GetGetMethod());
typebuilder.DefineMethodOverride(getMethod, methodInfo);
}
}

Expand Down Expand Up @@ -636,7 +637,7 @@ internal class ProxyGeneratorMembers

// ReSharper disable once PossibleNullReferenceException
public readonly MethodInfo EternalLifetimeGet = typeof(Lifetime)
.GetProperty(nameof(Lifetime.Eternal), BindingFlags.Static | BindingFlags.Public)
.GetProperty(nameof(Lifetime.Eternal), BindingFlags.Static | BindingFlags.Public)!
.GetGetMethod()
.NotNull(nameof(EternalLifetimeGet));

Expand Down
2 changes: 1 addition & 1 deletion rd-net/RdFramework.Reflection/ProxyGeneratorCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ProxyGeneratorCache : IProxyGenerator
private sealed class TokenComparer : IComparer<MethodInfo>
{
public static IComparer<MethodInfo> Instance { get; } = new TokenComparer();
public int Compare(MethodInfo x, MethodInfo y) => (x?.MetadataToken ?? -1).CompareTo(y?.MetadataToken ?? -1);
public int Compare(MethodInfo? x, MethodInfo? y) => (x?.MetadataToken ?? -1).CompareTo(y?.MetadataToken ?? -1);
}

public ProxyGeneratorCache(ProxyGenerator generator)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net35;net472</TargetFrameworks>
<TargetFramework>net8.0</TargetFramework>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before merging, we'll have to restore the compatibility with the older frameworks.

<AssemblyName>JetBrains.RdFramework.Reflection</AssemblyName>
<RootNamespace>JetBrains.Rd.Reflection</RootNamespace>

Expand Down
7 changes: 4 additions & 3 deletions rd-net/RdFramework.Reflection/ReflectionRdActivator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ private object ActivateRd(Type type)
object instance;
try
{
instance = Activator.CreateInstance(implementingType);
instance = Activator.CreateInstance(implementingType)
?? throw new InvalidOperationException($"Unable to create instance of: {implementingType.ToString(true)}");
Comment on lines +151 to +152
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(MInor) Sorry, why this change? It is not practical to expect null from Activator.CreateInstance: it will only return null if you pass a Nullable as the implementingType. It will throw an exception by itself otherwise; it doesn't need our additional assertion here.

}
catch (MissingMethodException e)
{
Expand Down Expand Up @@ -451,10 +452,10 @@ public static string GetTypeName(Type type)
{
var rpcInterface = ReflectionSerializerVerifier.GetRpcInterface(type.GetTypeInfo());
if (rpcInterface != null)
return rpcInterface.FullName;
return rpcInterface.FullName!;
}

return typename;
return typename!;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ bool IsValidArray()
if (!typeInfo.IsArray) return false;
if (typeInfo.GetArrayRank() != 1) return false;

var arrayType = typeInfo.GetElementType().GetTypeInfo();
var elementType = typeInfo.GetElementType();
if (elementType == null) return false;
Comment on lines +151 to +152
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should never be null here, I'd prefer this code to fail in such a case (as it was before) instead of trying to handle the logic error.


var arrayType = elementType.GetTypeInfo();
return IsFieldType(arrayType, false);
}

Expand Down
4 changes: 3 additions & 1 deletion rd-net/RdFramework.Reflection/ReflectionSerializers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,13 @@ private void RegisterModelSerializer<T>()
object instance;
if (isScalar)
{
#pragma warning disable SYSLIB0050
instance = FormatterServices.GetUninitializedObject(type);
#pragma warning restore SYSLIB0050
}
else
{
instance = Activator.CreateInstance(type);
instance = Activator.CreateInstance(type)!;
}

var bindableInstance = instance as IRdBindable;
Expand Down
2 changes: 1 addition & 1 deletion rd-net/RdFramework.Reflection/ScalarCollectionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static void AttachCollectionSerializers(ReflectionSerializers self)
}
else if (type.IsArray)
{
var result = (SerializerPair)ReflectionUtil.InvokeStaticGeneric(typeof(ScalarCollectionExtension), nameof(CreateArraySerializer), type.GetElementType(), new object[] { self })!;
var result = (SerializerPair)ReflectionUtil.InvokeStaticGeneric(typeof(ScalarCollectionExtension), nameof(CreateArraySerializer), type.GetElementType()!, self)!;
self.Register(type, result);
}
});
Expand Down
6 changes: 4 additions & 2 deletions rd-net/RdFramework.Reflection/ScalarSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ private SerializerPair CreateCustomScalar<T>(ISerializersSource serializers)
if (allowNullable && !unsafeReader.ReadNullness())
return default;

#pragma warning disable SYSLIB0050
object instance = FormatterServices.GetUninitializedObject(typeof(T));
#pragma warning restore SYSLIB0050

try
{
Expand Down Expand Up @@ -247,7 +249,7 @@ private SerializerPair CreateValueTupleSerializer<T>(ISerializersSource serializ
}

var type = typeInfo.AsType();
CtxReadDelegate<T> readerDelegate = (ctx, unsafeReader) =>
CtxReadDelegate<T?> readerDelegate = (ctx, unsafeReader) =>
{
// todo: consider using IL emit
var activatorArgs = new object[argumentTypes.Length];
Expand All @@ -258,7 +260,7 @@ private SerializerPair CreateValueTupleSerializer<T>(ISerializersSource serializ
}

var instance = Activator.CreateInstance(type, activatorArgs);
return (T) instance;
return (T?) instance;
};

CtxWriteDelegate<T> writerDelegate = (ctx, unsafeWriter, value) =>
Expand Down
22 changes: 11 additions & 11 deletions rd-net/RdFramework.Reflection/SerializerPair.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ void WriterDelegate(SerializationCtx ctx, UnsafeWriter writer, T value) =>
void WriterDelegateStatic(SerializationCtx ctx, UnsafeWriter writer, T value) =>
writeMethod.Invoke(null, new object?[] { ctx, writer, value, });

T ReaderDelegate(SerializationCtx ctx, UnsafeReader reader) =>
(T) readMethod.Invoke(null, new object?[] { ctx, reader });
T? ReaderDelegate(SerializationCtx ctx, UnsafeReader reader) =>
(T?) readMethod.Invoke(null, new object?[] { ctx, reader });

CtxReadDelegate<T> ctxReadDelegate = ReaderDelegate;
CtxReadDelegate<T?> ctxReadDelegate = ReaderDelegate;
CtxWriteDelegate<T> ctxWriteDelegate = writeMethod.IsStatic ? WriterDelegateStatic : WriterDelegate;
return new SerializerPair(ctxReadDelegate, ctxWriteDelegate);
}
Expand All @@ -111,13 +111,13 @@ private static SerializerPair CreateFromMethodsImpl1<T>(MethodInfo readMethod, M
void WriterDelegate(SerializationCtx ctx, UnsafeWriter writer, T value) =>
writeMethod.Invoke(null, new object?[] {ctx, writer, value});

T ReaderDelegate(SerializationCtx ctx, UnsafeReader reader)
T? ReaderDelegate(SerializationCtx ctx, UnsafeReader reader)
{
return (T)readMethod.Invoke(null,
return (T?)readMethod.Invoke(null,
new[] {ctx, reader, ctxKeyReadDelegate, ctxKeyWriteDelegate});
}

CtxReadDelegate<T> ctxReadDelegate = ReaderDelegate;
CtxReadDelegate<T?> ctxReadDelegate = ReaderDelegate;
CtxWriteDelegate<T> ctxWriteDelegate = WriterDelegate;
return new SerializerPair(ctxReadDelegate, ctxWriteDelegate);
}
Expand All @@ -133,13 +133,13 @@ private static SerializerPair CreateFromMethodsImpl2<T>(MethodInfo readMethod, M
void WriterDelegate(SerializationCtx ctx, UnsafeWriter writer, T value) =>
writeMethod.Invoke(null, new object?[] {ctx, writer, value});

T ReaderDelegate(SerializationCtx ctx, UnsafeReader reader)
T? ReaderDelegate(SerializationCtx ctx, UnsafeReader reader)
{
return (T)readMethod.Invoke(null,
return (T?)readMethod.Invoke(null,
new[] {ctx, reader, ctxKeyReadDelegate, ctxKeyWriteDelegate, ctxValueReadDelegate, ctxValueWriteDelegate});
}

CtxReadDelegate<T> ctxReadDelegate = ReaderDelegate;
CtxReadDelegate<T?> ctxReadDelegate = ReaderDelegate;
CtxWriteDelegate<T> ctxWriteDelegate = WriterDelegate;
return new SerializerPair(ctxReadDelegate, ctxWriteDelegate);
}
Expand All @@ -153,7 +153,7 @@ public static SerializerPair FromMarshaller<T>(IBuiltInMarshaller<T> marshaller)

private static SerializerPair CreateFromNonProtocolMethodsT<T>(MethodInfo readMethod, MethodInfo writeMethod)
{
Assertion.Require(readMethod.IsStatic, $"Read method should be static ({readMethod.DeclaringType.ToString(true)})");
Assertion.Require(readMethod.IsStatic, $"Read method should be static ({readMethod.DeclaringType?.ToString(true)})");

void WriterDelegate(SerializationCtx ctx, UnsafeWriter writer, T value)
{
Expand All @@ -174,7 +174,7 @@ void WriterDelegateStatic(SerializationCtx ctx, UnsafeWriter writer, T value)
if (!typeof(T).IsValueType && !reader.ReadNullness())
return default;

return (T) readMethod.Invoke(null, new object[] {reader});
return (T?) readMethod.Invoke(null, new object[] {reader});
}

CtxReadDelegate<T?> ctxReadDelegate = ReaderDelegate;
Expand Down
12 changes: 6 additions & 6 deletions rd-net/RdFramework.Reflection/SerializerReflectionUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ internal static FieldInfo[] GetBindableFields(TypeInfo typeInfo)
return list.ToArray();
}

private static IEnumerable<FieldInfo> GetFields(Type type, Type baseType)
private static IEnumerable<FieldInfo> GetFields(Type? type, Type baseType)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, should type really be nullable here? Who calls this on a null type?

{
foreach (var field in type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
foreach (var field in type?.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public) ?? Array.Empty<FieldInfo>())
yield return field;

// private fields only being returned for the current type
while ((type = type.BaseType) != baseType && type != null)
while ((type = type?.BaseType) != baseType && type != null)
{
// but protected fields are returned in first step
foreach (var baseField in type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic))
Expand All @@ -85,7 +85,7 @@ private static IEnumerable<FieldInfo> GetFields(Type type, Type baseType)

internal static SerializerPair ConvertPair(SerializerPair serializers, Type desiredType)
{
return (SerializerPair)ourConvertSerializerPair.MakeGenericMethod(serializers.Writer.GetType().GetGenericArguments()[0], desiredType).Invoke(null, new object[] { serializers });
return (SerializerPair)ourConvertSerializerPair.MakeGenericMethod(serializers.Writer.GetType().GetGenericArguments()[0], desiredType).Invoke(null, new object[] { serializers })!;
}

private static readonly MethodInfo ourConvertSerializerPair = typeof(SerializerReflectionUtil).GetTypeInfo().GetMethod(nameof(ConvertPairGeneric), BindingFlags.Static | BindingFlags.NonPublic)!;
Expand Down Expand Up @@ -113,15 +113,15 @@ internal static CtxReadDelegate<TOut> ConvertReader<TOut>(object reader)

var genericTypedRead = ourConvertTypedCtxRead.MakeGenericMethod(reader.GetType().GetGenericArguments()[0], typeof(object));
var result = genericTypedRead.Invoke(null, new[] { reader });
return (CtxReadDelegate<TOut>)result;
return (CtxReadDelegate<TOut>)result!;
}

internal static CtxWriteDelegate<TOut> ConvertWriter<TOut>(object writer)
{
if (writer is CtxWriteDelegate<TOut> objWriter)
return objWriter;

return (CtxWriteDelegate<TOut>)ourConvertTypedCtxWrite.MakeGenericMethod(writer.GetType().GetGenericArguments()[0], typeof(TOut)).Invoke(null, new[] { writer });
return (CtxWriteDelegate<TOut>)ourConvertTypedCtxWrite.MakeGenericMethod(writer.GetType().GetGenericArguments()[0], typeof(TOut)).Invoke(null, new[] { writer })!;
}

private static readonly MethodInfo ourConvertTypedCtxRead = typeof(SerializerReflectionUtil).GetTypeInfo().GetMethod(nameof(CtxReadTypedToObject), BindingFlags.Static | BindingFlags.NonPublic)!;
Expand Down
4 changes: 2 additions & 2 deletions rd-net/RdFramework/Base/IRdBindable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static class RdDynamicEx
{
public static IProtocol GetProtoOrThrow(this IRdDynamic dynamic)
{
return dynamic.TryGetProto() ?? throw new ProtocolNotBoundException(dynamic.ToString());
return dynamic.TryGetProto() ?? throw new ProtocolNotBoundException(dynamic.ToString() ?? "'dynamic.ToString() was null'");
}
}

Expand Down Expand Up @@ -322,7 +322,7 @@ public static void PrintEx(this object? me, PrettyPrinter printer)
break;
}
default:
printer.Print(me.ToString());
printer.Print(me.ToString() ?? "");
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion rd-net/RdFramework/Base/RdBindableBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ private T GetOrCreateExtension<T>(string name, bool highPriorityExtension, Func<

// NOTE: dummy implementation which prevents WPF from hanging the viewmodel forever on reflection property descriptor fabricated change events:
// when it sees PropertyChanged, it does not look for property descriptor events
public virtual event PropertyChangedEventHandler PropertyChanged { add { } remove { } }
public virtual event PropertyChangedEventHandler? PropertyChanged { add { } remove { } }
}

public enum BindState
Expand Down
49 changes: 49 additions & 0 deletions rd-net/RdFramework/Impl/EndPointWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace JetBrains.Rd.Impl;

public class EndPointWrapper
{
public EndPoint EndPointImpl { get; }
public IPAddress? LocalAddress { get; private set; }
public int? LocalPort { get; private set; }
public string? LocalPath { get; private set; }
public AddressFamily AddressFamily { get; private set; }
public SocketType SocketType { get; private set; }
public ProtocolType ProtocolType { get; private set; }

private EndPointWrapper(EndPoint endPoint)
{
EndPointImpl = endPoint;
}

public static EndPointWrapper CreateIpEndPoint(IPAddress? address = null, int? port = null)
{
var address1 = address ?? IPAddress.Loopback;
var port1 = port ?? 0;
return new EndPointWrapper(new IPEndPoint(address1, port1))
{
AddressFamily = AddressFamily.InterNetwork,
SocketType = SocketType.Stream,
ProtocolType = ProtocolType.Tcp,
LocalAddress = address1,
LocalPort = port1,
LocalPath = null,
};
}

public static EndPointWrapper CreateUnixEndPoint(string? path = null)
{
var path1 = path ?? Path.GetTempFileName();
return new EndPointWrapper(new UnixDomainSocketEndPoint(path1)) {
AddressFamily = AddressFamily.Unix,
SocketType = SocketType.Stream,
ProtocolType = ProtocolType.Unspecified,
LocalAddress = null,
LocalPort = null,
LocalPath = path1,
};
}
}
2 changes: 1 addition & 1 deletion rd-net/RdFramework/Impl/RdEntitiesRegistrar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal void Register(Lifetime lifetime, RdId rdId, IRdDynamic dynamic)
myMap.BlockingAddUnique(lifetime, myMap, rdId, dynamic);
}

public bool TryGetEntity(RdId rdId, out IRdDynamic entity)
public bool TryGetEntity(RdId rdId, out IRdDynamic? entity)
{
lock (myMap)
{
Expand Down
Loading
Loading