refactor(kv): 整理存档接线并清理迁移冗余实现
This commit is contained in:
@@ -8,7 +8,7 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using game.Account;
|
||||
using game.PlayerDataStorage;
|
||||
using game.KV;
|
||||
|
||||
public interface ICustomServerMgr
|
||||
{
|
||||
@@ -27,12 +27,14 @@ public interface ICustomServerMgr
|
||||
Uri ApiBaseUri { get; }
|
||||
int SessionVersion { get; }
|
||||
event Action SessionChanged;
|
||||
void UseAccountSession(S101AccountSession session);
|
||||
void UseAccountSession(GameServerAccountSession session);
|
||||
void ClearAccountSession();
|
||||
}
|
||||
|
||||
public class CustomServerMgr : ICustomServerMgr
|
||||
{
|
||||
public const string DefaultApiUrl = "http://s101.local:18080/";
|
||||
|
||||
IConfig config { get; set; }
|
||||
|
||||
//网络客户端
|
||||
@@ -43,34 +45,31 @@ public class CustomServerMgr : ICustomServerMgr
|
||||
private readonly SynchronizationContext ownerContext;
|
||||
public int SessionVersion { get; private set; }
|
||||
public event Action SessionChanged;
|
||||
PlayerKVAccountLease kvLease;
|
||||
bool kvOnly;
|
||||
bool kvBusinessReady;
|
||||
bool kvBusinessPublished;
|
||||
int pfRequests;
|
||||
|
||||
private KvAccountLease kvLease;
|
||||
private bool kvOnly;
|
||||
private bool kvBusinessReady;
|
||||
private bool kvBusinessPublished;
|
||||
private int pfRequests;
|
||||
internal event Action<int> KVUnauthorized;
|
||||
internal bool IsFreshForKVLoad => pfRequests==0 && kvLease==null;
|
||||
internal void BindKVForValidation(PlayerKVAccountLease lease)
|
||||
internal void BindKVForGame(KvAccountLease lease)
|
||||
{
|
||||
if(lease==null || lease.IsFormal || !lease.IsCurrent || lease.ApiBaseUri!=ApiBaseUri || !IsFreshForKVLoad)
|
||||
throw new InvalidOperationException("Fresh matching KV lease required.");
|
||||
kvOnly=true; kvBusinessReady=false; kvBusinessPublished=false; SessionVersion++; kvLease=lease; _cookie=null;
|
||||
var previous=client; client=null; previous?.Dispose(); SessionChanged?.Invoke();
|
||||
}
|
||||
internal void BindKVForGame(PlayerKVAccountLease lease)
|
||||
{
|
||||
if(lease==null || !lease.IsFormal || !lease.IsCurrent || lease.ApiBaseUri.AbsoluteUri!=PlayerKVRuntimeSelection.FormalApiUrl ||
|
||||
ApiBaseUri.AbsoluteUri!=PlayerKVRuntimeSelection.FormalApiUrl || !IsFreshForKVLoad)
|
||||
if (lease == null || !lease.IsCurrent ||
|
||||
lease.ApiBaseUri.AbsoluteUri != ApiBaseUri.AbsoluteUri || !IsFreshForKVLoad)
|
||||
{
|
||||
throw new InvalidOperationException("Fresh formal KV lease required.");
|
||||
}
|
||||
kvOnly=true; kvBusinessReady=false; kvBusinessPublished=false; SessionVersion++; kvLease=lease; _cookie=null;
|
||||
var previous=client; client=null; previous?.Dispose(); SessionChanged?.Invoke();
|
||||
}
|
||||
internal void EnableKVBusiness(long epoch)
|
||||
{
|
||||
if(!kvOnly || kvBusinessReady || kvBusinessPublished || kvLease==null || !kvLease.IsFormal || !kvLease.IsCurrent ||
|
||||
kvLease.Epoch!=epoch || kvLease.ApiBaseUri.AbsoluteUri!=PlayerKVRuntimeSelection.FormalApiUrl ||
|
||||
ApiBaseUri.AbsoluteUri!=PlayerKVRuntimeSelection.FormalApiUrl)
|
||||
if (!kvOnly || kvBusinessReady || kvBusinessPublished || kvLease == null || !kvLease.IsCurrent ||
|
||||
kvLease.Epoch != epoch || kvLease.ApiBaseUri.AbsoluteUri != ApiBaseUri.AbsoluteUri)
|
||||
{
|
||||
throw new InvalidOperationException("Current formal KV lease required for business transport.");
|
||||
}
|
||||
kvBusinessPublished=true; kvBusinessReady=true;
|
||||
}
|
||||
internal void DisableKVBusiness()
|
||||
@@ -80,30 +79,21 @@ public class CustomServerMgr : ICustomServerMgr
|
||||
try { SessionChanged?.Invoke(); }
|
||||
catch(Exception ex) { Debug.LogError($"KV session change failed: {ex.GetType().Name}"); }
|
||||
}
|
||||
internal Task ClearKVForGame() => ClearKVForValidation();
|
||||
internal Task ClearKVForValidation()
|
||||
internal Task ClearKVForGame()
|
||||
{
|
||||
kvBusinessReady=false; SessionVersion++; var previous=kvLease; kvLease=null; _cookie=null;
|
||||
var closing=previous?.CloseAsync() ?? Task.CompletedTask;
|
||||
SessionChanged?.Invoke(); return closing;
|
||||
}
|
||||
internal async Task<PlayerKVAccountResponse> SendKVForValidationAsync(string route)
|
||||
{
|
||||
var lease=kvLease; int version=SessionVersion;
|
||||
if(!kvOnly || lease==null) return new PlayerKVAccountResponse(false);
|
||||
var response=await lease.SendAsync(HttpMethod.Get,route);
|
||||
if(version!=SessionVersion || !ReferenceEquals(lease,kvLease) || !response.IsCurrent) return new PlayerKVAccountResponse(false);
|
||||
if(response.StatusCode==401 || response.StatusCode==403)
|
||||
{
|
||||
KVUnauthorized?.Invoke(version);
|
||||
return new PlayerKVAccountResponse(false,response.StatusCode);
|
||||
}
|
||||
return response;
|
||||
kvBusinessReady = false;
|
||||
SessionVersion++;
|
||||
var previous = kvLease;
|
||||
kvLease = null;
|
||||
_cookie = null;
|
||||
var closing = previous?.CloseAsync() ?? Task.CompletedTask;
|
||||
SessionChanged?.Invoke();
|
||||
return closing;
|
||||
}
|
||||
|
||||
|
||||
public CookieContainer Cookie => kvOnly
|
||||
? (kvBusinessReady && kvLease?.IsFormal==true ? kvLease.FormalCookies : null) ?? throw new NullReferenceException()
|
||||
? (kvBusinessReady && kvLease != null ? kvLease.FormalCookies : null) ?? throw new NullReferenceException()
|
||||
: _cookie ?? throw new NullReferenceException();
|
||||
public Uri ApiBaseUri { get; }
|
||||
|
||||
@@ -118,13 +108,16 @@ public class CustomServerMgr : ICustomServerMgr
|
||||
});
|
||||
this.config = config;
|
||||
|
||||
var url = config.Get<string>(GConstant.K_Event_API_URL, S101AccountSession.DefaultApiUrl);
|
||||
if (string.IsNullOrWhiteSpace(url)) url = S101AccountSession.DefaultApiUrl;
|
||||
var url = config.Get<string>(GConstant.K_Event_API_URL, DefaultApiUrl);
|
||||
if (string.IsNullOrWhiteSpace(url))
|
||||
{
|
||||
url = DefaultApiUrl;
|
||||
}
|
||||
ApiBaseUri = new Uri(url.TrimEnd('/') + "/");
|
||||
ClearAccountSession();
|
||||
}
|
||||
|
||||
public void UseAccountSession(S101AccountSession session)
|
||||
public void UseAccountSession(GameServerAccountSession session)
|
||||
{
|
||||
if(kvOnly) throw new InvalidOperationException("Controlled container cannot switch to PlayFab.");
|
||||
if (session == null || !session.IsAuthenticated || session.ApiBaseUri != ApiBaseUri)
|
||||
@@ -134,7 +127,11 @@ public class CustomServerMgr : ICustomServerMgr
|
||||
|
||||
public void ClearAccountSession()
|
||||
{
|
||||
if(kvOnly) { PlayerKVShutdownRegistry.Process.Register(ClearKVForValidation()); return; }
|
||||
if (kvOnly)
|
||||
{
|
||||
KvShutdownRegistry.Process.Register(ClearKVForGame());
|
||||
return;
|
||||
}
|
||||
ReplaceTransport(new CookieContainer());
|
||||
}
|
||||
|
||||
@@ -170,7 +167,10 @@ public class CustomServerMgr : ICustomServerMgr
|
||||
}
|
||||
if(!kvBusinessReady) throw new InvalidOperationException("Formal KV business transport is not ready.");
|
||||
var lease=kvLease; var kvVersion=SessionVersion;
|
||||
if(lease==null || !lease.IsFormal || !lease.IsCurrent) return null;
|
||||
if (lease == null || !lease.IsCurrent)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
try
|
||||
{
|
||||
var response=await lease.SendAsync(method,url,body,contentType);
|
||||
@@ -286,3 +286,4 @@ public class CustomServerMgr : ICustomServerMgr
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user