Skip to content

Commit 04bbe3f

Browse files
committed
refactor(DiscoverCenter): 重构 GameAppClient 配置参数为选项类
将分散的配置参数封装为 GameAppClientOption 类,提高代码可维护性 添加 ConfigureGameAppClient 方法允许子类自定义配置
1 parent ea58264 commit 04bbe3f

3 files changed

Lines changed: 78 additions & 11 deletions

File tree

GameFrameX.StartUp/AppStartUpByGameAppClient.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,25 @@ private async void StartGameAppClient()
5858
OnError = GameAppClientOnError,
5959
OnHeartBeat = GameAppClientOnHeartBeat,
6060
};
61-
var endPoint = new DnsEndPoint(Setting.DiscoveryCenterIp, Setting.DiscoveryCenterPort);
61+
var endPoint = new DnsEndPoint(Setting.DiscoveryCenterHost, Setting.DiscoveryCenterPort);
62+
GameAppClientOption option = new();
63+
ConfigureGameAppClient(option);
6264
// 根据配置创建发现中心终结点并初始化客户端
63-
_gameAppClient = new GameAppClient(gameAppClientEvent, endPoint);
65+
_gameAppClient = new GameAppClient(gameAppClientEvent, endPoint, option);
6466

6567
// 异步启动客户端,开始与发现中心建立连接
6668
await _gameAppClient.EntryAsync();
6769
}
6870

71+
/// <summary>
72+
/// 配置 GameAppClient 的可选参数,子类可重写此方法以自定义连接行为
73+
/// </summary>
74+
/// <param name="option">GameAppClient 的配置选项实例</param>
75+
protected virtual void ConfigureGameAppClient(GameAppClientOption option)
76+
{
77+
// 默认不做任何配置,子类可根据需要重写此方法
78+
}
79+
6980
/// <summary>
7081
/// 心跳回调:当需要向发现中心发送心跳消息时触发,可在此构造并返回心跳数据
7182
/// </summary>

GameFrameX.StartUp/DiscoverCenter/GameAppClient.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,16 @@ internal sealed class GameAppClient
9595
/// </summary>
9696
/// <param name="clientEvent">客户端事件回调结构体,包含连接、断开、消息等事件的处理委托</param>
9797
/// <param name="endPoint">服务器端点信息(IP和端口)</param>
98-
/// <param name="heartBeatInterval">心跳包发送间隔(毫秒),默认5000</param>
99-
/// <param name="connectDelay">连接尝试间隔(毫秒),默认5000</param>
100-
/// <param name="retryDelay">重连延迟时间(毫秒),默认5000</param>
101-
/// <param name="maxRetryCount">最大重连次数,-1表示无限重试</param>
102-
public GameAppClient(GameAppClientEvent clientEvent, EndPoint endPoint, int heartBeatInterval = 5000, int connectDelay = 5000, int retryDelay = 5000, int maxRetryCount = -1)
98+
/// <param name="option">配置信息</param>
99+
public GameAppClient(GameAppClientEvent clientEvent, EndPoint endPoint, GameAppClientOption option)
103100
{
104-
ConnectDelay = connectDelay;
101+
ArgumentNullException.ThrowIfNull(option, nameof(option));
102+
ConnectDelay = option.ConnectDelay;
105103
_mGameAppClientEvent = clientEvent;
106104
_serverHost = endPoint;
107-
_heartBeatInterval = heartBeatInterval;
108-
_retryDelay = retryDelay;
109-
MaxRetryCount = maxRetryCount;
105+
_heartBeatInterval = option.HeartBeatInterval;
106+
_retryDelay = option.RetryDelay;
107+
MaxRetryCount = option.MaxRetryCount;
110108
_mTcpClient = new AsyncTcpSession();
111109
_mTcpClient.Connected += OnClientOnConnected;
112110
_mTcpClient.Closed += OnClientOnClosed;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
namespace GameFrameX.StartUp.DiscoverCenter;
33+
34+
/// <summary>
35+
/// 游戏应用客户端配置选项
36+
/// </summary>
37+
public sealed class GameAppClientOption
38+
{
39+
/// <summary>
40+
/// 心跳间隔(毫秒),默认 5000 毫秒
41+
/// </summary>
42+
public int HeartBeatInterval { get; set; } = 5000;
43+
44+
/// <summary>
45+
/// 连接延迟(毫秒),默认 5000 毫秒
46+
/// </summary>
47+
public int ConnectDelay { get; set; } = 5000;
48+
49+
/// <summary>
50+
/// 重试延迟(毫秒),默认 5000 毫秒
51+
/// </summary>
52+
public int RetryDelay { get; set; } = 5000;
53+
54+
/// <summary>
55+
/// 最大重试次数,默认 -1 表示无限重试
56+
/// </summary>
57+
public int MaxRetryCount { get; set; } = -1;
58+
}

0 commit comments

Comments
 (0)