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+
33+ using System . Text . Json . Serialization ;
34+ using GameFrameX . Foundation . Logger ;
35+ using GameFrameX . NetWork . Abstractions ;
36+ using GameFrameX . NetWork . Messages ;
37+ using GameFrameX . ProtoBuf . Net ;
38+
39+ namespace GameFrameX . NetWork ;
40+
41+ /// <summary>
42+ /// Defines the interface for a network message package, including message data, header, and related operations.
43+ /// </summary>
44+ /// <remarks>
45+ /// 表示网络消息包的接口,包含消息数据、消息头及相关操作方法。
46+ /// </remarks>
47+ public sealed class NetworkMessagePackage : INetworkMessagePackage
48+ {
49+ /// <summary>
50+ /// The type of the message contained in the package.
51+ /// </summary>
52+ /// <remarks>
53+ /// 包含在消息包中的消息类型。
54+ /// </remarks>
55+ [ JsonIgnore ]
56+ public Type MessageType { get ; private set ; }
57+
58+ /// <summary>
59+ /// The message data, usually serialized as a byte array.
60+ /// </summary>
61+ /// <remarks>
62+ /// 消息数据,通常为序列化后的二进制内容。
63+ /// </remarks>
64+ public byte [ ] MessageData { get ; private set ; }
65+
66+ /// <summary>
67+ /// The message header object, containing metadata such as message ID, length, and type.
68+ /// </summary>
69+ /// <remarks>
70+ /// 消息头对象,包含消息标识、长度、类型等元数据。
71+ /// </remarks>
72+ public INetworkMessageHeader Header { get ; private set ; }
73+
74+ /// <summary>
75+ /// Deserializes the message data into a message object.
76+ /// </summary>
77+ /// <returns>The deserialized message object. / 反序列化后的消息对象。</returns>
78+ /// <remarks>
79+ /// 将消息数据反序列化为消息对象。
80+ /// </remarks>
81+ public INetworkMessage DeserializeMessageObject ( )
82+ {
83+ var message = ( INetworkMessage ) ProtoBufSerializerHelper . Deserialize ( MessageData , MessageType ) ;
84+ message . SetUniqueId ( Header . UniqueId ) ;
85+ MessageProtoHelper . SetMessageId ( message ) ;
86+ if ( message is MessageObject messageObject )
87+ {
88+ messageObject . SetOperationType ( Header . OperationType ) ;
89+ }
90+
91+ return message ;
92+ }
93+
94+ /// <summary>
95+ /// Sets the message data, typically used when receiving or constructing a message package.
96+ /// </summary>
97+ /// <remarks>
98+ /// 设置消息数据,通常用于接收或构建消息包时赋值。
99+ /// </remarks>
100+ /// <param name="messageData">The byte array of message data to set./ 要设置的消息数据字节数组。</param>
101+ public void SetMessageData ( byte [ ] messageData )
102+ {
103+ MessageData = messageData ;
104+ }
105+
106+ /// <summary>
107+ /// 获取格式化后的消息字符串
108+ /// </summary>
109+ /// <param name="actorId">ActorId,用于标识消息所属的角色或实体,默认值为0</param>
110+ /// <returns>格式化后的消息字符串,包含消息ID、操作类型、唯一ID、消息对象及ActorId</returns>
111+ public string ToFormatMessageString ( long actorId = default )
112+ {
113+ return MessageObjectLoggerHelper . FormatMessage ( Header . MessageId , Header . OperationType , Header . UniqueId , DeserializeMessageObject ( ) , actorId ) ;
114+ }
115+
116+ /// <summary>
117+ /// 设置消息头
118+ /// </summary>
119+ /// <param name="header">要设置的网络消息头对象,不能为空</param>
120+ private void SetMessageHeader ( INetworkMessageHeader header )
121+ {
122+ Header = header ;
123+ }
124+
125+ /// <summary>
126+ /// 设置消息类型
127+ /// </summary>
128+ /// <param name="messageType">要设置的消息类型,不能为空</param>
129+ private void SetMessageType ( Type messageType )
130+ {
131+ MessageType = messageType ;
132+ }
133+
134+ /// <summary>
135+ /// 根据消息对象和消息头创建网络消息包
136+ /// </summary>
137+ /// <param name="message">要封装的网络消息对象,不能为空</param>
138+ /// <param name="messageObjectHeader">消息头对象,不能为空</param>
139+ /// <returns>新创建并初始化完成的 NetworkMessagePackage 实例</returns>
140+ public static NetworkMessagePackage Create ( INetworkMessage message , INetworkMessageHeader messageObjectHeader )
141+ {
142+ ArgumentNullException . ThrowIfNull ( messageObjectHeader , nameof ( messageObjectHeader ) ) ;
143+ ArgumentNullException . ThrowIfNull ( message , nameof ( message ) ) ;
144+ var networkMessage = new NetworkMessagePackage ( ) ;
145+ messageObjectHeader . OperationType = MessageProtoHelper . GetMessageOperationType ( message ) ;
146+ messageObjectHeader . MessageId = MessageProtoHelper . GetMessageIdByType ( message ) ;
147+ messageObjectHeader . UniqueId = message . UniqueId ;
148+ networkMessage . SetMessageType ( message . GetType ( ) ) ;
149+ try
150+ {
151+ var buffer = ProtoBufSerializerHelper . Serialize ( message ) ;
152+ networkMessage . SetMessageData ( buffer ) ;
153+ networkMessage . SetMessageHeader ( messageObjectHeader ) ;
154+ return networkMessage ;
155+ }
156+ catch ( Exception e )
157+ {
158+ LogHelper . Error ( "If the message object is encoded abnormally, check the error log, exception:" + e ) ;
159+ throw ;
160+ }
161+ }
162+
163+ /// <summary>
164+ /// 根据消息头、消息数据及消息类型创建网络消息包
165+ /// </summary>
166+ /// <param name="messageObjectHeader">消息头对象,不能为空</param>
167+ /// <param name="messageData">已序列化的消息数据字节数组,不能为空</param>
168+ /// <param name="messageType">消息数据对应的类型,不能为空</param>
169+ /// <returns>新创建并初始化完成的 NetworkMessagePackage 实例</returns>
170+ public static NetworkMessagePackage Create ( INetworkMessageHeader messageObjectHeader , byte [ ] messageData , Type messageType )
171+ {
172+ ArgumentNullException . ThrowIfNull ( messageObjectHeader , nameof ( messageObjectHeader ) ) ;
173+ ArgumentNullException . ThrowIfNull ( messageData , nameof ( messageData ) ) ;
174+ ArgumentNullException . ThrowIfNull ( messageType , nameof ( messageType ) ) ;
175+ var networkMessagePackage = new NetworkMessagePackage ( ) ;
176+ networkMessagePackage . SetMessageHeader ( messageObjectHeader ) ;
177+ networkMessagePackage . SetMessageData ( messageData ) ;
178+ networkMessagePackage . SetMessageType ( messageType ) ;
179+ return networkMessagePackage ;
180+ }
181+ }
0 commit comments