Skip to content

Commit a0413b1

Browse files
committed
[修改]1. 修改和补充客户端的代码注释和压缩解压缩
1 parent 0e263c5 commit a0413b1

File tree

4 files changed

+132
-38
lines changed

4 files changed

+132
-38
lines changed

GameFrameX.Client/Bot/BotClient.cs

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,23 @@
33
using GameFrameX.Utility.Log;
44
using ErrorEventArgs = GameFrameX.SuperSocket.ClientEngine.ErrorEventArgs;
55

6-
namespace GameFrameX.Bot;
6+
namespace GameFrameX.Client.Bot;
77

8-
public class BotClient
8+
/// <summary>
9+
/// 机器人客户端类,用于模拟玩家行为进行测试
10+
/// </summary>
11+
public sealed class BotClient
912
{
1013
private readonly BotTcpClient m_TcpClient;
1114
private readonly BotHttpClient m_HttpClient;
1215
private readonly string m_BotName;
1316
private readonly BotTcpClientEvent m_BotTcpClientEvent;
1417
private const string m_LoginUrl = "http://127.0.0.1:29200/game/api/";
1518

19+
/// <summary>
20+
/// 初始化机器人客户端
21+
/// </summary>
22+
/// <param name="botName">机器人名称</param>
1623
public BotClient(string botName)
1724
{
1825
m_BotName = botName;
@@ -24,6 +31,10 @@ public BotClient(string botName)
2431
m_HttpClient = new BotHttpClient();
2532
}
2633

34+
/// <summary>
35+
/// 启动机器人客户端
36+
/// </summary>
37+
/// <returns>异步任务</returns>
2738
public async Task EntryAsync()
2839
{
2940
try
@@ -36,6 +47,10 @@ public async Task EntryAsync()
3647
}
3748
}
3849

50+
/// <summary>
51+
/// 处理接收到的消息
52+
/// </summary>
53+
/// <param name="messageObject">接收到的消息对象</param>
3954
private void OnReceiveMsg(MessageObject messageObject)
4055
{
4156
switch (messageObject)
@@ -48,19 +63,33 @@ private void OnReceiveMsg(MessageObject messageObject)
4863

4964
#region 连接状态回调
5065

66+
/// <summary>
67+
/// 客户端连接成功的回调
68+
/// </summary>
5169
private void ClientConnectedCallback()
5270
{
5371
SendLoginMessage();
5472
}
5573

74+
/// <summary>
75+
/// 客户端连接关闭的回调
76+
/// </summary>
5677
private void ClientClosedCallback()
5778
{
5879
}
5980

81+
/// <summary>
82+
/// 客户端发生错误的回调
83+
/// </summary>
84+
/// <param name="error">错误信息</param>
6085
private void ClientErrorCallback(ErrorEventArgs error)
6186
{
6287
}
6388

89+
/// <summary>
90+
/// 客户端接收消息的回调
91+
/// </summary>
92+
/// <param name="outerMsg">接收到的消息</param>
6493
private void ClientReceiveCallback(MessageObject outerMsg)
6594
{
6695
OnReceiveMsg(outerMsg);
@@ -70,6 +99,9 @@ private void ClientReceiveCallback(MessageObject outerMsg)
7099

71100
#region 消息发送
72101

102+
/// <summary>
103+
/// 发送登录消息并处理登录流程
104+
/// </summary>
73105
private async void SendLoginMessage()
74106
{
75107
try
@@ -79,7 +111,7 @@ private async void SendLoginMessage()
79111
{
80112
UserName = m_BotName,
81113
Password = "12312",
82-
Platform ="LoginPlatform.Custom",
114+
Platform = "LoginPlatform.Custom",
83115
};
84116

85117
string respLoginUrl = $"{m_LoginUrl}{nameof(ReqLogin)}";
@@ -147,6 +179,10 @@ private async void SendLoginMessage()
147179

148180
#region 消息接收
149181

182+
/// <summary>
183+
/// 处理玩家登录成功的响应
184+
/// </summary>
185+
/// <param name="msg">登录成功的响应消息</param>
150186
private void OnPlayerLoginSuccess(RespPlayerLogin msg)
151187
{
152188
LogHelper.Info($"机器人-{m_BotName}登录成功,id:{msg.PlayerInfo.Id}");

GameFrameX.Client/Bot/BotHttpClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
using GameFrameX.Utility.Extensions;
88
using GameFrameX.Utility.Log;
99

10-
namespace GameFrameX.Bot;
10+
namespace GameFrameX.Client.Bot;
1111

12-
public class BotHttpClient
12+
public sealed class BotHttpClient
1313
{
1414
private readonly HttpClient m_HttpClient = new();
1515

GameFrameX.Client/Bot/BotTcpClient.cs

Lines changed: 88 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Net;
22
using GameFrameX.NetWork.Abstractions;
3+
using GameFrameX.NetWork.Message;
34
using GameFrameX.NetWork.Messages;
45
using GameFrameX.Proto.Proto;
56
using GameFrameX.ProtoBuf.Net;
@@ -9,17 +10,38 @@
910
using GameFrameX.Utility.Log;
1011
using ErrorEventArgs = GameFrameX.SuperSocket.ClientEngine.ErrorEventArgs;
1112

12-
namespace GameFrameX.Bot;
13+
namespace GameFrameX.Client.Bot;
1314

15+
/// <summary>
16+
/// 机器人TCP客户端事件结构体,包含各种回调事件
17+
/// </summary>
1418
public struct BotTcpClientEvent
1519
{
20+
/// <summary>
21+
/// 连接成功时的回调
22+
/// </summary>
1623
public Action OnConnectedCallback;
24+
25+
/// <summary>
26+
/// 连接关闭时的回调
27+
/// </summary>
1728
public Action OnClosedCallback;
29+
30+
/// <summary>
31+
/// 发生错误时的回调
32+
/// </summary>
1833
public Action<ErrorEventArgs> OnErrorCallback;
34+
35+
/// <summary>
36+
/// 接收到消息时的回调
37+
/// </summary>
1938
public Action<MessageObject> OnReceiveMsgCallback;
2039
}
2140

22-
public class BotTcpClient
41+
/// <summary>
42+
/// 机器人TCP客户端类,用于处理与服务器的TCP连接和消息收发
43+
/// </summary>
44+
public sealed class BotTcpClient
2345
{
2446
private const string ServerHost = "127.0.0.1";
2547
private const int ServerPort = 29100;
@@ -29,9 +51,15 @@ public class BotTcpClient
2951
private int m_RetryCount;
3052
private int m_RetryDelay = 5000;
3153
private readonly BotTcpClientEvent m_BotTcpClientEvent;
54+
private readonly IMessageDecompressHandler messageDecompressHandler;
55+
private readonly IMessageCompressHandler messageCompressHandler;
3256

3357
private const ushort InnerPackageHeaderLength = 14;
3458

59+
/// <summary>
60+
/// 初始化机器人TCP客户端
61+
/// </summary>
62+
/// <param name="clientEvent">客户端事件回调结构体</param>
3563
public BotTcpClient(BotTcpClientEvent clientEvent)
3664
{
3765
m_BotTcpClientEvent = clientEvent;
@@ -40,8 +68,14 @@ public BotTcpClient(BotTcpClientEvent clientEvent)
4068
m_TcpClient.Closed += OnMTcpClientOnClosed;
4169
m_TcpClient.DataReceived += OnMTcpClientOnDataReceived;
4270
m_TcpClient.Error += OnMTcpClientOnError;
71+
messageDecompressHandler = new DefaultMessageDecompressHandler();
72+
messageCompressHandler = new DefaultMessageCompressHandler();
4373
}
4474

75+
/// <summary>
76+
/// 启动客户端并尝试连接服务器
77+
/// </summary>
78+
/// <returns>异步任务</returns>
4579
public async Task EntryAsync()
4680
{
4781
while (true)
@@ -79,6 +113,9 @@ public async Task EntryAsync()
79113
}
80114
}
81115

116+
/// <summary>
117+
/// 发送心跳包到服务器
118+
/// </summary>
82119
private void SendHeartBeat()
83120
{
84121
ReqHeartBeat req = new ReqHeartBeat
@@ -88,6 +125,10 @@ private void SendHeartBeat()
88125
SendToServer(req);
89126
}
90127

128+
/// <summary>
129+
/// 发送消息到服务器
130+
/// </summary>
131+
/// <param name="messageObject">要发送的消息对象</param>
91132
public void SendToServer(MessageObject messageObject)
92133
{
93134
var buffer = Handler(messageObject);
@@ -97,29 +138,45 @@ public void SendToServer(MessageObject messageObject)
97138
}
98139
}
99140

141+
/// <summary>
142+
/// 处理客户端错误事件
143+
/// </summary>
100144
private void OnMTcpClientOnError(object? client, ErrorEventArgs e)
101145
{
102146
LogHelper.Info("客户端发生错误:" + e.Exception.Message);
103147
m_BotTcpClientEvent.OnErrorCallback(e);
104148
}
105149

150+
/// <summary>
151+
/// 处理客户端连接关闭事件
152+
/// </summary>
106153
private void OnMTcpClientOnClosed(object? client, EventArgs e)
107154
{
108155
LogHelper.Info("客户端断开连接");
109156
m_BotTcpClientEvent.OnClosedCallback();
110157
}
111158

159+
/// <summary>
160+
/// 处理客户端连接成功事件
161+
/// </summary>
112162
private void OnMTcpClientOnConnected(object? client, EventArgs e)
113163
{
114164
LogHelper.Info("客户端成功连接到服务器");
115165
m_BotTcpClientEvent.OnConnectedCallback();
116166
}
117167

168+
/// <summary>
169+
/// 处理接收到数据事件
170+
/// </summary>
118171
private void OnMTcpClientOnDataReceived(object? client, DataEventArgs e)
119172
{
120173
DecodeMessage(e.Data.ReadBytes(e.Offset, e.Length));
121174
}
122175

176+
/// <summary>
177+
/// 解码接收到的消息数据
178+
/// </summary>
179+
/// <param name="data">接收到的字节数据</param>
123180
private void DecodeMessage(byte[] data)
124181
{
125182
var offset = 0;
@@ -129,50 +186,53 @@ private void DecodeMessage(byte[] data)
129186
// 消息头长度
130187
var operationType = data.ReadByte(ref offset);
131188
var zipFlag = data.ReadByte(ref offset);
132-
var UniqueId = data.ReadInt(ref offset);
133-
var MessageId = data.ReadInt(ref offset);
134-
// ushort headerLength = data.ReadByte(ref offset);
135-
// 消息头字节数组
136-
// var messageHeaderData = data.ReadBytes(ref offset, headerLength);
137-
// 消息对象头
138-
// var messageObjectHeader = DecodeHeaderNetworkMessage(messageHeaderData);
139-
// 消息内容
140-
var messageData = data.ReadBytes(ref offset, totalLength - 14);
141-
var messageType = MessageProtoHelper.GetMessageTypeById(MessageId);
189+
var uniqueId = data.ReadInt(ref offset);
190+
var messageId = data.ReadInt(ref offset);
191+
var messageData = data.ReadBytes(ref offset, totalLength - InnerPackageHeaderLength);
192+
var messageType = MessageProtoHelper.GetMessageTypeById(messageId);
142193
if (messageType != null)
143194
{
195+
if (zipFlag > 0)
196+
{
197+
// 消息解压缩
198+
messageData = messageDecompressHandler.Handler(messageData);
199+
}
200+
144201
var messageObject = (MessageObject)ProtoBufSerializerHelper.Deserialize(messageData, messageType);
145-
messageObject.SetMessageId(MessageId);
202+
messageObject.SetMessageId(messageId);
146203
messageObject.SetOperationType((MessageOperationType)operationType);
147-
messageObject.SetUniqueId(UniqueId);
204+
messageObject.SetUniqueId(uniqueId);
148205
m_BotTcpClientEvent.OnReceiveMsgCallback(messageObject);
149206
}
150207
}
151208

152-
private static byte[] Handler(MessageObject message)
209+
/// <summary>
210+
/// 处理要发送的消息,将消息对象转换为字节数组
211+
/// </summary>
212+
/// <param name="message">要处理的消息对象</param>
213+
/// <returns>处理后的字节数组</returns>
214+
private byte[] Handler(MessageObject message)
153215
{
154216
MessageProtoHelper.SetMessageId(message);
155217
message.SetOperationType(MessageProtoHelper.GetMessageOperationType(message));
156218

157-
var messageObjectHeader = new MessageObjectHeader
158-
{
159-
OperationType = message.OperationType,
160-
UniqueId = message.UniqueId,
161-
MessageId = message.MessageId,
162-
};
163-
var header = ProtoBufSerializerHelper.Serialize(messageObjectHeader);
164219
var messageData = ProtoBufSerializerHelper.Serialize(message);
220+
byte zipFlag = 0;
221+
if (messageData.Length > 512)
222+
{
223+
messageData = messageCompressHandler.Handler(messageData);
224+
zipFlag = 1;
225+
}
226+
165227
var totalLength = messageData.Length + InnerPackageHeaderLength;
166228
var buffer = new byte[totalLength];
167229
var offset = 0;
168230
buffer.WriteInt(totalLength, ref offset);
169-
buffer.WriteByte((byte)messageObjectHeader.OperationType, ref offset);
170-
buffer.WriteByte(messageObjectHeader.ZipFlag, ref offset);
171-
buffer.WriteInt(messageObjectHeader.UniqueId, ref offset);
172-
buffer.WriteInt(messageObjectHeader.MessageId, ref offset);
173-
// buffer.WriteBytesWithoutLength(header, ref offset);
231+
buffer.WriteByte((byte)message.OperationType, ref offset);
232+
buffer.WriteByte(zipFlag, ref offset);
233+
buffer.WriteInt(message.UniqueId, ref offset);
234+
buffer.WriteInt(message.MessageId, ref offset);
174235
buffer.WriteBytesWithoutLength(messageData, ref offset);
175-
// Console.WriteLine($"客户端接发送信息:{message.ToFormatMessageString()}");
176236
return buffer;
177237
}
178238
}

GameFrameX.Client/Program.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1-
// See https://aka.ms/new-console-template for more information
2-
3-
using GameFrameX.Bot;
1+
using GameFrameX.Client.Bot;
42
using GameFrameX.NetWork.Abstractions;
53
using GameFrameX.Proto.Proto;
64
using GameFrameX.Utility.Log;
75

86
internal static class Program
97
{
10-
private const int m_BotCount = 1;
8+
private const int m_BotCount = 100;
119

1210
static async Task Main(string[] args)
1311
{
1412
var logOption = LogOptions.Default;
1513
logOption.IsConsole = true;
1614
logOption.LogEventLevel = Serilog.Events.LogEventLevel.Information;
1715
LoggerHandler.Start(logOption);
18-
16+
1917
MessageProtoHelper.Init(typeof(ReqLogin).Assembly);
2018

2119
for (int k = 0; k < m_BotCount; k++)

0 commit comments

Comments
 (0)