Skip to content

Commit

Permalink
Use MagicOnion DynamicClient, except for Unity.
Browse files Browse the repository at this point in the history
  • Loading branch information
mayuki committed Aug 5, 2024
1 parent 81b053e commit 6f5993b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
15 changes: 10 additions & 5 deletions src/DFrame.Unity/Assets/Plugins/DFrame/Runtime/DFrameWorkerApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using MagicOnion.Client.DynamicClient;
using MagicOnion.Serialization.MessagePack;

namespace DFrame
Expand Down Expand Up @@ -225,7 +226,6 @@ async Task ConnectAsync(CancellationToken applicationLifeTime)
#if UNITY_2020_1_OR_NEWER
HttpHandler = new Cysharp.Net.Http.YetAnotherHttpHandler()
{
Http2Only = true,
Http2KeepAliveInterval = options.HttpHandlerOptions.KeepAlivePingDelay,
Http2KeepAliveTimeout = options.HttpHandlerOptions.KeepAlivePingTimeout,
},
Expand All @@ -243,13 +243,18 @@ async Task ConnectAsync(CancellationToken applicationLifeTime)

var callInvoker = channel.CreateCallInvoker();
var callOption = new CallOptions(new Metadata { { "worker-id", workerId.ToString() } });
var connectTask = StreamingHubClient.ConnectAsync<IControllerHub, IWorkerReceiver>(callInvoker, this, option: callOption, serializerProvider: MessagePackMagicOnionSerializerProvider.Default.WithOptions(
#if UNITY_2020_1_OR_NEWER
DFrameResolver.Options
var factoryProvider = MagicOnionDFrameGeneratedClientInitializer.StreamingHubClientFactoryProvider;
var serializerOptions = DFrameResolver.Options;
#else
MessagePackSerializerOptions.Standard
var factoryProvider = DynamicStreamingHubClientFactoryProvider.Instance;
var serializerOptions = MessagePackSerializerOptions.Standard;
#endif
), factoryProvider: MagicOnionDFrameGeneratedClientInitializer.StreamingHubClientFactoryProvider);
var connectTask = StreamingHubClient.ConnectAsync<IControllerHub, IWorkerReceiver>(callInvoker, this,
option: callOption,
serializerProvider: MessagePackMagicOnionSerializerProvider.Default.WithOptions(serializerOptions),
factoryProvider: factoryProvider
);
client = await connectTask.WaitAsync(connectTimeout);

this.connectionLifeTime = CancellationTokenSource.CreateLinkedTokenSource(client!.WaitForDisconnect().ToCancellationToken(), applicationLifeTime);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using MagicOnion.Client;
#if UNITY_2020_1_OR_NEWER
using MagicOnion.Client;

namespace DFrame.Internal
{
[MagicOnionClientGeneration(typeof(DFrame.IControllerHub), DisableAutoRegistration = true)]
internal partial class MagicOnionDFrameGeneratedClientInitializer { }
}
}
#endif
15 changes: 11 additions & 4 deletions src/DFrame.Worker/DFrameWorkerApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using MagicOnion.Client.DynamicClient;
using MagicOnion.Serialization.MessagePack;

namespace DFrame
Expand Down Expand Up @@ -225,6 +226,7 @@ async Task ConnectAsync(CancellationToken applicationLifeTime)
#if UNITY_2020_1_OR_NEWER
HttpHandler = new Cysharp.Net.Http.YetAnotherHttpHandler()
{
Http2Only = true,
Http2KeepAliveInterval = options.HttpHandlerOptions.KeepAlivePingDelay,
Http2KeepAliveTimeout = options.HttpHandlerOptions.KeepAlivePingTimeout,
},
Expand All @@ -242,13 +244,18 @@ async Task ConnectAsync(CancellationToken applicationLifeTime)

var callInvoker = channel.CreateCallInvoker();
var callOption = new CallOptions(new Metadata { { "worker-id", workerId.ToString() } });
var connectTask = StreamingHubClient.ConnectAsync<IControllerHub, IWorkerReceiver>(callInvoker, this, option: callOption, serializerProvider: MessagePackMagicOnionSerializerProvider.Default.WithOptions(
#if UNITY_2020_1_OR_NEWER
DFrameResolver.Options
var factoryProvider = MagicOnionDFrameGeneratedClientInitializer.StreamingHubClientFactoryProvider;
var serializerOptions = DFrameResolver.Options;
#else
MessagePackSerializerOptions.Standard
var factoryProvider = DynamicStreamingHubClientFactoryProvider.Instance;
var serializerOptions = MessagePackSerializerOptions.Standard;
#endif
), factoryProvider: MagicOnionDFrameGeneratedClientInitializer.StreamingHubClientFactoryProvider);
var connectTask = StreamingHubClient.ConnectAsync<IControllerHub, IWorkerReceiver>(callInvoker, this,
option: callOption,
serializerProvider: MessagePackMagicOnionSerializerProvider.Default.WithOptions(serializerOptions),
factoryProvider: factoryProvider
);
client = await connectTask.WaitAsync(connectTimeout);

this.connectionLifeTime = CancellationTokenSource.CreateLinkedTokenSource(client!.WaitForDisconnect().ToCancellationToken(), applicationLifeTime);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using MagicOnion.Client;
#if UNITY_2020_1_OR_NEWER
using MagicOnion.Client;

namespace DFrame.Internal
{
[MagicOnionClientGeneration(typeof(DFrame.IControllerHub), DisableAutoRegistration = true)]
internal partial class MagicOnionDFrameGeneratedClientInitializer { }
}
}
#endif

0 comments on commit 6f5993b

Please sign in to comment.