11using System . Net ;
22using GameFrameX . NetWork . Abstractions ;
3+ using GameFrameX . NetWork . Message ;
34using GameFrameX . NetWork . Messages ;
45using GameFrameX . Proto . Proto ;
56using GameFrameX . ProtoBuf . Net ;
910using GameFrameX . Utility . Log ;
1011using ErrorEventArgs = GameFrameX . SuperSocket . ClientEngine . ErrorEventArgs ;
1112
12- namespace GameFrameX . Bot ;
13+ namespace GameFrameX . Client . Bot ;
1314
15+ /// <summary>
16+ /// 机器人TCP客户端事件结构体,包含各种回调事件
17+ /// </summary>
1418public 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}
0 commit comments