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

Cleanup MagicOnion.Shared project #699

Merged
merged 10 commits into from
Oct 26, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static (Compilation Compilation, SemanticModel SemanticModel) Create(stri
MetadataReference.CreateFromFile(typeof(MessagePack.MessagePackObjectAttribute).Assembly.Location), // MessagePack.Annotations
MetadataReference.CreateFromFile(typeof(MagicOnion.IService<>).Assembly.Location), // MagicOnion.Abstractions
MetadataReference.CreateFromFile(typeof(MagicOnion.Client.MagicOnionClient).Assembly.Location), // MagicOnion.Client
MetadataReference.CreateFromFile(typeof(MagicOnion.GrpcMethodHelper).Assembly.Location), // MagicOnion.Shared
MetadataReference.CreateFromFile(typeof(MagicOnion.Internal.GrpcMethodHelper).Assembly.Location), // MagicOnion.Shared
};
var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ static void EmitConstructor(StreamingHubClientBuildContext ctx)
public {{ctx.Hub.GetClientFullName()}}(global::Grpc.Core.CallInvoker callInvoker, global::System.String host, global::Grpc.Core.CallOptions options, global::MagicOnion.Serialization.IMagicOnionSerializerProvider serializerProvider, global::MagicOnion.Client.IMagicOnionClientLogger logger)
: base(callInvoker, host, options, serializerProvider, logger)
{
var marshaller = global::MagicOnion.MagicOnionMarshallers.ThroughMarshaller;
var marshaller = global::MagicOnion.Internal.MagicOnionMarshallers.ThroughMarshaller;
DuplexStreamingAsyncMethod = new global::Grpc.Core.Method<global::System.Byte[], global::System.Byte[]>(global::Grpc.Core.MethodType.DuplexStreaming, "{{ctx.Hub.ServiceType.Name}}", "Connect", marshaller, marshaller);
}
""");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#if NON_UNITY || ((!ENABLE_IL2CPP || UNITY_EDITOR) && !NET_STANDARD_2_0)
using MagicOnion.Utils;
using MagicOnion.Internal.Reflection;

namespace MagicOnion.Client.DynamicClient
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#if NON_UNITY || ((!ENABLE_IL2CPP || UNITY_EDITOR) && !NET_STANDARD_2_0)

using Grpc.Core;
using MagicOnion.Internal;
using MagicOnion.Internal.Reflection;
using MagicOnion.Serialization;
using MagicOnion.Server.Hubs;
using MagicOnion.Utils;
using MessagePack;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -372,7 +373,7 @@ static void DefineMethods(TypeBuilder typeBuilder, Type interfaceType, Type rece
}
else
{
var deserializeType = BroadcasterHelper.dynamicArgumentTupleTypes[parameters.Length - 2]
var deserializeType = BroadcasterHelper.DynamicArgumentTupleTypes[parameters.Length - 2]
.MakeGenericType(parameters.Select(x => x.ParameterType).ToArray());
var lc = il.DeclareLocal(deserializeType);

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,20 @@
using System.Threading;
using System.Threading.Tasks;

namespace MagicOnion
namespace MagicOnion.Client.Internal.Threading
{
public class AsyncLock
internal class AsyncLock
{
readonly SemaphoreSlim semaphore;

#if NON_UNITY
public static readonly ValueTask<LockReleaser> EmptyLock = new ValueTask<LockReleaser>(new LockReleaser(null));
#endif

public AsyncLock()
{
this.semaphore = new SemaphoreSlim(1, 1);
}

#if NON_UNITY
public async ValueTask<LockReleaser> LockAsync()
#else
public async Task<LockReleaser> LockAsync()
#endif
{
await semaphore.WaitAsync().ConfigureAwait(false);
return new LockReleaser(semaphore);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Threading.Tasks;

namespace MagicOnion.Utils
namespace MagicOnion.Client.Internal.Threading.Tasks
{
internal interface ITaskCompletion
{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
using MagicOnion.Utils;
using MagicOnion.Server.Hubs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace MagicOnion.Server.Hubs
namespace MagicOnion.Internal
{
public static class BroadcasterHelper
{
internal static readonly Type[] dynamicArgumentTupleTypes = typeof(DynamicArgumentTuple<,>).GetTypeInfo().Assembly
internal static Type[] DynamicArgumentTupleTypes { get; } = typeof(DynamicArgumentTuple<,>)
.GetTypeInfo()
.Assembly
.GetTypes()
.Where(x => x.Name.StartsWith("DynamicArgumentTuple") && !x.Name.Contains("Formatter"))
.OrderBy(x => x.GetGenericArguments().Length)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System;
using System.Buffers;
using System.Diagnostics.CodeAnalysis;

namespace MagicOnion.Utils
namespace MagicOnion.Internal.Buffers
{
internal sealed class ArrayPoolBufferWriter : IBufferWriter<byte>, IDisposable
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text;
using System.Text;

namespace MagicOnion.Utils
namespace MagicOnion.Internal
{
public static class FNV1A32
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using Grpc.Core;
using MagicOnion.Internal;
using MagicOnion.Serialization;
using MessagePack;

namespace MagicOnion
namespace MagicOnion.Internal
{
public static class GrpcMethodHelper
{
Expand Down Expand Up @@ -60,7 +59,7 @@ public static MagicOnionMethod<Nil, TResponse, Box<Nil>, TRawResponse> CreateMet
// so as a special case we do not serialize/deserialize and always convert to a fixed values.
var isMethodResponseTypeBoxed = typeof(TResponse).IsValueType;
var responseMarshaller = isMethodResponseTypeBoxed
? (object)CreateBoxedMarshaller<TResponse>(messageSerializer)
? CreateBoxedMarshaller<TResponse>(messageSerializer)
: (object)CreateMarshaller<TResponse>(messageSerializer);

return new MagicOnionMethod<Nil, TResponse, Box<Nil>, TRawResponse>(new Method<Box<Nil>, TRawResponse>(
Expand All @@ -82,10 +81,10 @@ public static MagicOnionMethod<TRequest, TResponse, TRawRequest, TRawResponse> C
var isMethodResponseTypeBoxed = typeof(TResponse).IsValueType;

var requestMarshaller = isMethodRequestTypeBoxed
? (object)CreateBoxedMarshaller<TRequest>(messageSerializer)
? CreateBoxedMarshaller<TRequest>(messageSerializer)
: (object)CreateMarshaller<TRequest>(messageSerializer);
var responseMarshaller = isMethodResponseTypeBoxed
? (object)CreateBoxedMarshaller<TResponse>(messageSerializer)
? CreateBoxedMarshaller<TResponse>(messageSerializer)
: (object)CreateMarshaller<TResponse>(messageSerializer);

return new MagicOnionMethod<TRequest, TResponse, TRawRequest, TRawResponse>(new Method<TRawRequest, TRawResponse>(
Expand All @@ -103,7 +102,7 @@ public static MagicOnionMethod<TRequest, TResponse, TRawRequest, TRawResponse> C
public static Marshaller<Box<Nil>> IgnoreNilMarshaller { get; } = new Marshaller<Box<Nil>>(
serializer: (obj, ctx) =>
{
ReadOnlySpan<byte> unsafeNilBytes = new [] { MessagePackCode.Nil };
ReadOnlySpan<byte> unsafeNilBytes = new[] { MessagePackCode.Nil };

var writer = ctx.GetBufferWriter();
var buffer = writer.GetSpan(unsafeNilBytes.Length); // Write `Nil` as `byte[]` to the buffer.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using Grpc.Core;
using MessagePack;
using MessagePack.Formatters;
using System;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;

namespace MagicOnion
namespace MagicOnion.Internal
{
// invoke from dynamic methods so must be public
public static class MagicOnionMarshallers
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Reflection.Emit;
using System.Runtime.CompilerServices;

namespace MagicOnion.Utils
namespace MagicOnion.Internal.Reflection
{
#if ENABLE_SAVE_ASSEMBLY
public
Expand All @@ -32,14 +32,14 @@ public DynamicAssembly(string moduleName)
this.assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName(moduleName), AssemblyBuilderAccess.RunAndSave);
this.moduleBuilder = assemblyBuilder.DefineDynamicModule(moduleName, moduleName + ".dll");
#else
this.assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName(moduleName), AssemblyBuilderAccess.Run);
this.moduleBuilder = assemblyBuilder.DefineDynamicModule(moduleName);
assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName(moduleName), AssemblyBuilderAccess.Run);
moduleBuilder = assemblyBuilder.DefineDynamicModule(moduleName);
#endif

// HACK: Allow access to `internal` classes from dynamically generated assembly.
// https://www.strathweb.com/2018/10/no-internalvisibleto-no-problem-bypassing-c-visibility-rules-with-roslyn/
this.assemblyBuilder.SetCustomAttribute(new CustomAttributeBuilder(typeof(IgnoresAccessChecksToAttribute).GetConstructor(new[] { typeof(string) })!, new[] { "MagicOnion.Client" }));
this.assemblyBuilder.SetCustomAttribute(new CustomAttributeBuilder(typeof(IgnoresAccessChecksToAttribute).GetConstructor(new[] { typeof(string) })!, new[] { "MagicOnion.Server" }));
assemblyBuilder.SetCustomAttribute(new CustomAttributeBuilder(typeof(IgnoresAccessChecksToAttribute).GetConstructor(new[] { typeof(string) })!, new[] { "MagicOnion.Client" }));
assemblyBuilder.SetCustomAttribute(new CustomAttributeBuilder(typeof(IgnoresAccessChecksToAttribute).GetConstructor(new[] { typeof(string) })!, new[] { "MagicOnion.Server" }));
}

// requires lock on mono environment(for example, UnityEditor). see: https://github.com/neuecc/MessagePack-CSharp/issues/161
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Reflection;
using System.Reflection.Emit;

namespace MagicOnion.Utils
namespace MagicOnion.Internal.Reflection
{
// full list of can create optimize helper -> https://github.com/kevin-montrose/Sigil#automated-opcode-choice

Expand Down Expand Up @@ -161,7 +161,7 @@ public static void EmitNullReturn(this ILGenerator il)

public static void EmitThrowNotimplemented(this ILGenerator il)
{
il.Emit(OpCodes.Newobj, typeof(System.NotImplementedException).GetConstructor(Type.EmptyTypes)!);
il.Emit(OpCodes.Newobj, typeof(NotImplementedException).GetConstructor(Type.EmptyTypes)!);
il.Emit(OpCodes.Throw);
}
}
Expand Down

This file was deleted.

This file was deleted.

Loading
Loading