chore(m2): 保存功能验收及后续清理前检查点

This commit is contained in:
Liubing\LB
2026-10-10 20:40:27 +08:00
parent d3c5394a55
commit 144e7f739f
110 changed files with 12672 additions and 7514 deletions

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Net;
@@ -76,7 +77,7 @@ namespace game.PlayerDataStorage
internal static async Task<PlayerKVSessionOpenResult> CreateAsync(S101AccountSession session, string cacheRoot,
string expectedPlayerId, Uri expectedApiBaseUri, long epoch, Func<long, bool> isCurrent,
CancellationToken lifetimeToken, Func<CookieContainer, HttpMessageHandler> handlerFactory,
TimeSpan? transportTimeout = null)
TimeSpan? transportTimeout = null, bool prepareOriginalComparison = false)
{
if (session == null || expectedApiBaseUri == null || isCurrent == null || handlerFactory == null || epoch < 0 ||
!long.TryParse(expectedPlayerId, NumberStyles.None, CultureInfo.InvariantCulture, out var numericId) || numericId <= 0)
@@ -112,7 +113,7 @@ namespace game.PlayerDataStorage
using (var lease = owner.TryEnter())
{
if (lease == null) return await owner.FailCreate(PlayerKVSessionOpenError.Cancelled);
read = await owner.coordinator.InitializeAsync();
read = await owner.coordinator.InitializeAsync(prepareOriginalComparison);
}
if (!read.Success)
return await owner.FailCreate(read.Error == PlayerKVError.Unauthorized
@@ -166,9 +167,49 @@ namespace game.PlayerDataStorage
PlayerKVLocalReadResult ReadFailure(PlayerKVLocalReadError error) =>
new PlayerKVLocalReadResult(error, ownerId, PlayerId, Epoch);
public PlayerKVEditResult Update(string key, string value, bool remoteStore = true)
{
return Update(key, value, remoteStore, null);
}
internal PlayerKVStateResult CompareDatas(long nowTicks)
{
using (var lease = TryEnter())
return lease == null ? ClosedEdit() : coordinator.Update(key, value, remoteStore);
{
return lease == null ? new PlayerKVStateResult(PlayerKVChangeError.Blocked) : coordinator.CompareDatas(nowTicks);
}
}
internal PlayerKVEditResult Update(string key, string value, bool remoteStore, long? updateTicks)
{
using (var lease = TryEnter())
{
return lease == null ? ClosedEdit() : coordinator.Update(key, value, remoteStore, updateTicks);
}
}
public Task<PlayerKVFlushResult> SyncDataAsync(CancellationToken waitToken = default)
{
var lease = TryEnter();
if (lease == null)
{
return Task.FromResult(new PlayerKVFlushResult(0, 0, 1, 0, PlayerKVError.Cancelled, state.Status));
}
return WaitForFlushAsync(RunOrdinarySyncAsync(lease), waitToken);
}
private async Task<PlayerKVFlushResult> RunOrdinarySyncAsync(OperationLease lease)
{
using (lease)
{
try
{
return await coordinator.SyncDataAsync();
}
catch (Exception)
{
return new PlayerKVFlushResult(0, 0, 1, 0, PlayerKVError.Protocol, state.Status);
}
}
}
public PlayerKVEditResult Delete(string key)
@@ -177,6 +218,30 @@ namespace game.PlayerDataStorage
return lease == null ? ClosedEdit() : coordinator.Delete(key);
}
public async Task<bool> SyncDataImmidiate(string key, string value)
{
using (var lease = TryEnter())
{
if (lease == null)
{
return false;
}
return await coordinator.SyncDataImmidiate(key, value);
}
}
internal async Task<bool> SyncDataImmidiate(IReadOnlyDictionary<string, string> values)
{
using (var lease = TryEnter())
{
if (lease == null)
{
return false;
}
return await coordinator.SyncDataImmidiate(values);
}
}
public Task<PlayerKVFlushResult> FlushAsync(CancellationToken waitToken = default)
{
var lease = TryEnter();
@@ -308,7 +373,10 @@ namespace game.PlayerDataStorage
bool IsCapturedOwnerCurrent()
{
if (lifetime.IsCancellationRequested || !isCurrent(Epoch)) return false;
if (lifetime.IsCancellationRequested || !isCurrent(Epoch) || state.AuthenticationInvalid)
{
return false;
}
return session.IsAuthenticated && ReferenceEquals(session.Cookies, cookies) &&
session.PlayerId == PlayerId && NormalizeApi(session.ApiBaseUri) == ApiBaseUri;
}