Skip to content

Commit 5c6323e

Browse files
committed
feat(社交服务): 添加社交服务启动类及发现中心注册功能
实现社交服务的基础启动类AppStartUpSocial,包含数据库初始化、消息处理等核心功能 新增AppStartUpSocialByDiscoveryCenter类用于处理服务注册到发现中心的逻辑 添加心跳检测和消息处理机制,支持内外网消息区分处理
1 parent 002d0dd commit 5c6323e

File tree

2 files changed

+175
-0
lines changed

2 files changed

+175
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using GameFrameX.Core.Components;
2+
using GameFrameX.DataBase.Abstractions;
3+
using GameFrameX.NetWork.Abstractions;
4+
using GameFrameX.NetWork.HTTP;
5+
using GameFrameX.NetWork.Message;
6+
7+
namespace GameFrameX.Launcher.StartUp.Social;
8+
9+
/// <summary>
10+
/// 游戏服务器
11+
/// </summary>
12+
[StartUpTag(GlobalConst.SocialServiceName)]
13+
internal sealed partial class AppStartUpSocial : AppStartUpBase
14+
{
15+
protected override bool IsRegisterToDiscoveryCenter { get; set; } = true;
16+
17+
public override async Task StartAsync()
18+
{
19+
try
20+
{
21+
var aopHandlerTypes = AssemblyHelper.GetRuntimeImplementTypeNamesInstance<IHttpAopHandler>();
22+
aopHandlerTypes.Sort((handlerX, handlerY) => handlerX.Priority.CompareTo(handlerY.Priority));
23+
LogHelper.Debug("开始启动数据库服务...");
24+
LogHelper.Debug("开始配置Actor限制逻辑...");
25+
ActorLimit.Init(ActorLimit.RuleType.None);
26+
LogHelper.Debug("配置Actor限制逻辑结束...");
27+
var initResult = await GameDb.Init<MongoDbService>(new DbOptions { ConnectionString = Setting.DataBaseUrl, Name = Setting.DataBaseName, });
28+
if (initResult == false)
29+
{
30+
throw new InvalidOperationException("数据库服务启动失败");
31+
}
32+
33+
await ComponentRegister.Init(typeof(AppsHandler).Assembly);
34+
HotfixManager.LoadHotfix(Setting);
35+
await StartServerAsync<DefaultMessageDecoderHandler, DefaultMessageEncoderHandler>(new DefaultMessageCompressHandler(), new DefaultMessageDecompressHandler(), HotfixManager.GetListHttpHandler(), HotfixManager.GetHttpHandler, aopHandlerTypes);
36+
37+
await AppExitToken;
38+
}
39+
catch (Exception e)
40+
{
41+
LogHelper.Info($"服务器{ServerType}执行异常,e:{e}");
42+
LogHelper.Fatal(e);
43+
}
44+
45+
await StopAsync();
46+
}
47+
48+
protected override async ValueTask PackageHandler(IAppSession session, IMessage message)
49+
{
50+
await base.PackageHandler(session, message);
51+
if (message is INetworkMessagePackage messageObject)
52+
{
53+
if (Setting.IsDebug && Setting.IsDebugReceive)
54+
{
55+
// 当需要打印心跳,或当前非心跳消息时才输出日志
56+
// if (Setting.IsDebugReceiveHeartBeat || messageObject.Header.OperationType != (byte)MessageOperationType.HeartBeat)
57+
// {
58+
//
59+
// LogHelper.Debug($"---收到[{from} To {ServerType}] {message.ToFormatMessageString()}");
60+
// }
61+
}
62+
63+
var handler = HotfixManager.GetTcpHandler(messageObject.Header.MessageId);
64+
await InvokeMessageHandler(handler, messageObject.DeserializeMessageObject(), new DefaultNetWorkChannel(session, Setting));
65+
}
66+
}
67+
68+
protected override void Init()
69+
{
70+
if (Setting == null)
71+
{
72+
Setting = new AppSetting
73+
{
74+
ServerType = GlobalConst.SocialServiceName,
75+
ServerId = GlobalConst.SocialServiceServerId,
76+
InnerPort = 29400,
77+
HttpIsDevelopment = true,
78+
IsDebug = true,
79+
IsDebugSend = true,
80+
IsDebugReceive = true,
81+
IsDebugReceiveHeartBeat = false,
82+
IsDebugSendHeartBeat = false,
83+
DataBaseUrl = "mongodb://gameframex:[email protected]:27017/?authSource=admin",
84+
DataBaseName = "gameframex"
85+
};
86+
}
87+
88+
base.Init();
89+
}
90+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// ==========================================================================================
2+
// GameFrameX 组织及其衍生项目的版权、商标、专利及其他相关权利
3+
// GameFrameX organization and its derivative projects' copyrights, trademarks, patents, and related rights
4+
// 均受中华人民共和国及相关国际法律法规保护。
5+
// are protected by the laws of the People's Republic of China and relevant international regulations.
6+
//
7+
// 使用本项目须严格遵守相应法律法规及开源许可证之规定。
8+
// Usage of this project must strictly comply with applicable laws, regulations, and open-source licenses.
9+
//
10+
// 本项目采用 MIT 许可证与 Apache License 2.0 双许可证分发,
11+
// This project is dual-licensed under the MIT License and Apache License 2.0,
12+
// 完整许可证文本请参见源代码根目录下的 LICENSE 文件。
13+
// please refer to the LICENSE file in the root directory of the source code for the full license text.
14+
//
15+
// 禁止利用本项目实施任何危害国家安全、破坏社会秩序、
16+
// It is prohibited to use this project to engage in any activities that endanger national security, disrupt social order,
17+
// 侵犯他人合法权益等法律法规所禁止的行为!
18+
// or infringe upon the legitimate rights and interests of others, as prohibited by laws and regulations!
19+
// 因基于本项目二次开发所产生的一切法律纠纷与责任,
20+
// Any legal disputes and liabilities arising from secondary development based on this project
21+
// 本项目组织与贡献者概不承担。
22+
// shall be borne solely by the developer; the project organization and contributors assume no responsibility.
23+
//
24+
// GitHub 仓库:https://github.com/GameFrameX
25+
// GitHub Repository: https://github.com/GameFrameX
26+
// Gitee 仓库:https://gitee.com/GameFrameX
27+
// Gitee Repository: https://gitee.com/GameFrameX
28+
// 官方文档:https://gameframex.doc.alianblank.com/
29+
// Official Documentation: https://gameframex.doc.alianblank.com/
30+
// ==========================================================================================
31+
32+
using GameFrameX.Foundation.Utility;
33+
using GameFrameX.NetWork.Abstractions;
34+
using GameFrameX.Proto.BuiltIn;
35+
36+
namespace GameFrameX.Launcher.StartUp.Social;
37+
38+
internal partial class AppStartUpSocial
39+
{
40+
protected override void GameAppClientOnConnected(string id)
41+
{
42+
base.GameAppClientOnConnected(id);
43+
// 注册服务器到发现中心
44+
var reqRegisterServer = new ReqServiceRegister
45+
{
46+
ServerId = Setting.ServerId,
47+
ServerName = Setting.ServerName,
48+
ServerType = Setting.ServerType,
49+
ServerInstanceId = Setting.ServerInstanceId,
50+
InnerHost = Setting.InnerHost,
51+
InnerPort = Setting.InnerPort,
52+
OuterHost = Setting.OuterHost,
53+
OuterPort = Setting.OuterPort,
54+
};
55+
Send(reqRegisterServer);
56+
}
57+
58+
readonly ReqActorHeartBeat _reqActorHeartBeat = new ReqActorHeartBeat();
59+
60+
protected override MessageObject GameAppClientOnHeartBeat(string id)
61+
{
62+
_reqActorHeartBeat.Timestamp = TimerHelper.UnixTimeMilliseconds();
63+
_reqActorHeartBeat.UpdateUniqueId();
64+
return _reqActorHeartBeat;
65+
}
66+
67+
protected override void GameAppClientOnMessage(string id, MessageObject message)
68+
{
69+
if (MessageProtoHelper.IsHeartbeat(message))
70+
{
71+
return;
72+
}
73+
74+
base.GameAppClientOnMessage(id, message);
75+
76+
if (message.MessageId >= 0)
77+
{
78+
// outer message
79+
}
80+
else
81+
{
82+
// inner message
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)