Files
ShrinkNetwork.Integration.App/Runtime/ShrinkNetworkAppComponent.cs
T
cneicy b9011b9e65
Publish UPM package / publish (push) Failing after 1s
chore: initialize standalone UPM package
2026-08-26 02:50:36 +08:00

90 lines
3.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#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 失败——
/// 全部由效应回滚完成,而非手写卸载路径;
/// - 与经典 ShrinkNetworkAppInstallerModuleId "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;
};
}
}
}