新增相关协议

This commit is contained in:
2025-08-26 18:03:52 +08:00
parent e0665aae01
commit 7325e268ce
36 changed files with 1402 additions and 1085 deletions

View File

@@ -42,32 +42,5 @@ public sealed class C2G_LoginRequestHandler : MessageRPC<C2G_LoginRequest, G2C_L
response.ErrorCode = await GateLoginHelper.Online(gateUnit);
Log.Debug($"当前的Gate服务器:{session.Scene.SceneConfigId} accountId:{accountId}");
// var gameSceneConfig = GameSceneHelper.GetSceneConfig(session);
//
// // 通过chatSceneConfig拿到这个Scene的RouteId
// var gameRouteId = gameSceneConfig.RouteId;
// //连接到游戏中心服
// var gameResponse = (Game2G_EnterResponse)await session.Scene.NetworkMessagingComponent.CallInnerRoute(
// gameRouteId, new G2Game_EnterRequest()
// {
// AccountId = accountId,
// GateRouteId = session.RuntimeId
// });
//
// if (gameResponse.ErrorCode != 0)
// {
// // 如果ErrorCode不是0表示请求的协议发生错误应该提示给客户端。
// response.ErrorCode = gameResponse.ErrorCode;
// return;
// }
//
// // 要实现Route协议的转发需要给Session添加一个RouteComponent这个非常重要。
// var routeComponent = session.AddComponent<RouteComponent>();
// routeComponent.AddAddress(RouteType.GameRoute, gameResponse.RoleRouteId);
//
// // 给当前Session添加一个组件当Session销毁的时候会销毁这个组件。
// var accountFlagComponent = session.AddComponent<SessionPlayerComponent>();
// accountFlagComponent.AccountID = accountId;
}
}

View File

@@ -5,6 +5,8 @@ using Fantasy.Platform.Net;
using NB.Chat;
using NB.Game;
// ReSharper disable ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
namespace NB.Gate;
public static class GateLoginHelper
@@ -37,43 +39,13 @@ public static class GateLoginHelper
var routeComponent = session.GetComponent<RouteComponent>();
if (routeComponent == null)
{
routeComponent = session.AddComponent<RouteComponent>();
session.AddComponent<RouteComponent>();
}
gateUnitSessionComponent.AccountID = gateUnit.AccountID;
gateUnitSessionComponent.SessionId = session.RuntimeId;
//安排游戏服务器,并通知进入
var (gameRouteId, roleSimpleInfo) =
await GameSceneHelper.Online(session.Scene, gateUnit.AccountID, session.RuntimeId);
if (gameRouteId <= 0 || roleSimpleInfo == null)
{
return ErrorCode.OnlineSceneFailed;
}
Log.Info($"连接游戏服成功gameRouteId:{gameRouteId}");
routeComponent.AddAddress(RouteType.GameRoute, gameRouteId);
gateUnit.GameSceneRouteId = gameRouteId;
//安排进入的聊天服
var (chatRouteId, sceneRouteId) =
await SocialSceneHelper.Online(session.Scene, roleSimpleInfo, session.RuntimeId);
if (chatRouteId <= 0)
{
return ErrorCode.OnlineSceneFailed;
}
routeComponent.AddAddress(RouteType.SocialRoute, chatRouteId);
gateUnit.ChatSceneRouteId = sceneRouteId;
Log.Info($"连接聊天服成功gameRouteId:{gameRouteId}");
var gameSceneConfigs = SceneConfigData.Instance.GetSceneBySceneType(SceneType.Map);
var mapSceneConfig = gameSceneConfigs.First();
routeComponent.AddAddress(RouteType.MapRoute, mapSceneConfig.RouteId);
return ErrorCode.Successful;
return await gateUnit.Online(RouteType.GameRoute, RouteType.SocialRoute, RouteType.MapRoute);
}
#endregion
@@ -88,11 +60,13 @@ public static class GateLoginHelper
public static async FTask<uint> Offline(GateUnit gateUnit, long sessionId)
{
//通知服务器下线
Log.Info($"断线的session id={sessionId} ChatSceneRouteId={gateUnit.ChatSceneRouteId}");
await GameSceneHelper.Offline(gateUnit.Scene, gateUnit.AccountID, sessionId);
await SocialSceneHelper.Offline(gateUnit.Scene, gateUnit.AccountID, sessionId,
gateUnit.ChatSceneRouteId);
return ErrorCode.Successful;
Log.Info($"断线的session id={sessionId}");
var ret = await gateUnit.Offline(sessionId, RouteType.GameRoute, RouteType.SocialRoute, RouteType.MapRoute);
// await GameSceneHelper.Offline(gateUnit.Scene, gateUnit.AccountID, sessionId);
// await SocialSceneHelper.Offline(gateUnit.Scene, gateUnit.AccountID, sessionId,
// gateUnit.ChatSceneRouteId);
return ret;
}
#endregion

View File

@@ -1,4 +1,10 @@
using Fantasy.Entitas.Interface;
using Fantasy;
using Fantasy.Async;
using Fantasy.Entitas.Interface;
using Fantasy.Network;
using NB.Common;
// ReSharper disable ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
namespace NB.Gate;
@@ -11,6 +17,120 @@ public class GateUnitDestroySystem : DestroySystem<GateUnit>
}
}
public class GateUnitSystem
public static class GateUnitSystem
{
#region Address
private static void RemoveAddress(this GateUnit self, int routType, long routeId)
{
if (self.SceneRoutes.TryGetValue(routType, out var route))
{
if (route == routeId)
{
self.SceneRoutes.Remove(routType);
}
}
}
private static void AddAddress(this GateUnit self, int routType, long routeId)
{
self.SceneRoutes[routType] = routeId;
}
private static long GetAddress(this GateUnit self, int routType)
{
return self.SceneRoutes.GetValueOrDefault(routType, 0);
}
#endregion
#region 线
public static async FTask<uint> Online(this GateUnit self, params int[] routeType)
{
if (routeType != null && routeType.Length > 0)
{
foreach (var route in routeType)
{
var ret = await self.Online(route);
if (ret != ErrorCode.Successful) return ret;
}
}
return ErrorCode.Successful;
}
public static async FTask<uint> Online(this GateUnit self, int routeType)
{
Session session = self.Session;
var routeComponent = session.GetComponent<RouteComponent>();
if (routeComponent == null)
{
routeComponent = session.AddComponent<RouteComponent>();
}
var gameSceneConfig = SceneConfigHelper.GetConfig(routeType);
var gameRouteId = gameSceneConfig.RouteId;
//连接到游戏中心服
var gameResponse = (G2Common_EnterResponse)await self.Scene.NetworkMessagingComponent.CallInnerRoute(
gameRouteId, new G2Common_EnterRequest()
{
AccountId = self.AccountID,
GateRouteId = session.RuntimeId
});
if (gameResponse.ErrorCode != 0)
{
return gameResponse.ErrorCode;
}
routeComponent.AddAddress(routeType, gameRouteId);
self.AddAddress(routeType, gameRouteId);
return ErrorCode.Successful;
}
#endregion
#region 线
public static async FTask<uint> Offline(this GateUnit self, long sessionId, params int[] routeType)
{
if (routeType != null && routeType.Length > 0)
{
foreach (var route in routeType)
{
var ret = await self.Offline(sessionId, route);
if (ret != ErrorCode.Successful) return ret;
}
}
return ErrorCode.Successful;
}
public static async FTask<uint> Offline(this GateUnit self, long sessionId, int routeType)
{
var sceneRouteId = self.GetAddress(routeType);
if (sceneRouteId < 1) return ErrorCode.Successful;
for (int i = 0; i < 10; i++)
{
var gameResponse = (Common2G_ExitResponse)await self.Scene.NetworkMessagingComponent.CallInnerRoute(
sceneRouteId, new G2Common_ExitRequest()
{
AccountId = self.AccountID,
GateRouteId = sessionId
});
if (gameResponse.ErrorCode == 0)
{
self.RemoveAddress(routeType, sceneRouteId);
return ErrorCode.Successful;
}
}
Log.Error($"重试多次还是退出失败,需检查,sessionId={sessionId} route={routeType}");
return ErrorCode.ErrServer;
}
#endregion
}