This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "ShrinkNetwork.Integration.App",
|
||||
"rootNamespace": "ShrinkNetwork.Integration.App",
|
||||
"references": [
|
||||
"ShrinkNetwork.Runtime",
|
||||
"ShrinkApp.Core.Runtime",
|
||||
"ShrinkContext.Core.Runtime",
|
||||
"UniTask"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6396a2709306e6245892177071b1c3f2
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,89 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using ShrinkContext;
|
||||
using ShrinkNetwork;
|
||||
using UnityEngine;
|
||||
|
||||
namespace ShrinkNetwork.Integration.App
|
||||
{
|
||||
/// <summary>ShrinkNetwork 组件的配置。</summary>
|
||||
public sealed class ShrinkNetworkAppModuleConfig
|
||||
{
|
||||
/// <summary>是否在激活时自动绑定 loopback 传输并打开默认会话(演示/调试台用)。</summary>
|
||||
public bool BindLoopbackTransport;
|
||||
|
||||
public int LoopbackSessionId = 1;
|
||||
|
||||
/// <summary>可选:把网络服务同时注册进 ShrinkApp 服务容器(兼容旧调试台读取路径)。</summary>
|
||||
public ShrinkApp.ShrinkAppServices? Services;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ShrinkNetwork 的原生 Cordis 组件(阶段 3):
|
||||
/// - 提供 <c>shrink.service.network</c> 余效应键(服务实例),依赖者按键注入;
|
||||
/// - 可选的 loopback 传输绑定是**可逆效应**:停用时解绑传输、清理会话并使挂起 RPC 失败——
|
||||
/// 全部由效应回滚完成,而非手写卸载路径;
|
||||
/// - 与经典 ShrinkNetworkAppInstaller(ModuleId "shrink.network")二选一使用:
|
||||
/// 两者都发布 "app.module.shrink.network",同时激活会触发供给冲突。
|
||||
/// </summary>
|
||||
public sealed class ShrinkNetworkAppComponent : IShrinkComponent
|
||||
{
|
||||
public const string ServiceKey = "shrink.service.network";
|
||||
public const string ModuleKey = "app.module.shrink.network";
|
||||
|
||||
private static readonly string[] ProvideKeys = { ModuleKey, ServiceKey };
|
||||
|
||||
public string Name => "shrink.network";
|
||||
|
||||
public IReadOnlyList<string> Inject => Array.Empty<string>();
|
||||
|
||||
public IReadOnlyList<string> Provide => ProvideKeys;
|
||||
|
||||
public async UniTask ApplyAsync(ShrinkCtx ctx, object? config)
|
||||
{
|
||||
var moduleConfig = config as ShrinkNetworkAppModuleConfig ?? new ShrinkNetworkAppModuleConfig();
|
||||
var service = ShrinkNetworkRuntime.Default;
|
||||
|
||||
// set 是可逆效应:停用自动撤回服务键,依赖者随之停用
|
||||
ctx.Set(ServiceKey, service);
|
||||
var facade = new ShrinkNetworkAppService(service);
|
||||
moduleConfig.Services?.Register(facade);
|
||||
if (config is ShrinkApp.ShrinkAppServices appServices)
|
||||
{
|
||||
appServices.Register(facade);
|
||||
ctx.EffectInverse(() =>
|
||||
{
|
||||
appServices.TryUnregister(facade);
|
||||
return UniTask.CompletedTask;
|
||||
});
|
||||
}
|
||||
ctx.Set(ModuleKey, Name);
|
||||
|
||||
if (moduleConfig.BindLoopbackTransport)
|
||||
{
|
||||
await ctx.EffectAsync(() => BindLoopbackAsync(service, moduleConfig.LoopbackSessionId));
|
||||
}
|
||||
}
|
||||
|
||||
private static async UniTask<Func<UniTask>> BindLoopbackAsync(ShrinkNetworkService service, int sessionId)
|
||||
{
|
||||
var client = new ShrinkLoopbackTransport();
|
||||
var server = new ShrinkLoopbackTransport();
|
||||
client.LinkPeer(server);
|
||||
|
||||
service.BindTransport(client);
|
||||
client.OpenSession(sessionId);
|
||||
await UniTask.CompletedTask;
|
||||
|
||||
return () =>
|
||||
{
|
||||
// 逆操作:关闭会话并解绑传输;BindTransport(null) 触发会话清理与挂起 RPC 失败
|
||||
client.CloseSession(sessionId);
|
||||
service.BindTransport(null);
|
||||
return UniTask.CompletedTask;
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f956f1f1465b7c749844f59dfd12412a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,49 @@
|
||||
#nullable enable
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using ShrinkApp;
|
||||
|
||||
namespace ShrinkNetwork.Integration.App
|
||||
{
|
||||
[ShrinkAppModuleInstaller]
|
||||
[Obsolete("ClassicHost compatibility only. ContextLoader projects should use ShrinkNetworkAppComponent from a composition root.")]
|
||||
public sealed class ShrinkNetworkAppInstaller : IShrinkAppModuleInstaller
|
||||
{
|
||||
public string ModuleId => "shrink.network";
|
||||
public int Order => -1400;
|
||||
public System.Collections.Generic.IReadOnlyList<string> DependsOn => Array.Empty<string>();
|
||||
|
||||
public void RegisterServices(ShrinkAppContext context)
|
||||
{
|
||||
context.Services.Register(new ShrinkNetworkAppService(ShrinkNetworkRuntime.Default));
|
||||
}
|
||||
|
||||
public UniTask InitializeAsync(ShrinkAppContext context)
|
||||
{
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ShrinkNetworkAppService
|
||||
{
|
||||
public ShrinkNetworkAppService(ShrinkNetworkService service)
|
||||
{
|
||||
Service = service ?? throw new ArgumentNullException(nameof(service));
|
||||
}
|
||||
|
||||
public ShrinkNetworkService Service { get; }
|
||||
|
||||
public ShrinkNetworkServiceDiagnosticsSnapshot GetDiagnosticsSnapshot()
|
||||
=> Service.GetDiagnosticsSnapshot();
|
||||
|
||||
public System.Collections.Generic.IReadOnlyList<string> GetSessionSummaries()
|
||||
{
|
||||
return Service.Sessions.Values
|
||||
.OrderBy(session => session.SessionId)
|
||||
.Select(session => $"session={session.SessionId}, remote={session.RemoteAddress}, peer={session.PeerKind}")
|
||||
.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ff40e733ef9026441b4698e55e421922
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user