1- using System . Text . Json . Serialization ;
2- using GameFrameX . Foundation . Json ;
3- using GameFrameX . NetWork . Abstractions ;
4- using GameFrameX . Utility ;
5- using GameFrameX . Utility . Extensions ;
6- using ProtoBuf ;
7-
8- namespace GameFrameX . NetWork . Messages ;
9-
10- /// <summary>
11- /// 消息对象
12- /// </summary>
13- [ ProtoContract ]
14- public abstract class MessageObject : INetworkMessage
15- {
16- /// <summary>
17- /// </summary>
18- protected MessageObject ( )
19- {
20- UpdateUniqueId ( ) ;
21- }
22- /*/// <summary>
23- /// 单位id
24- /// </summary>
25- [JsonIgnore]
26- public int UniId { get; set; }*/
27-
28- /// <summary>
29- /// 消息ID
30- /// </summary>
31- [ JsonIgnore ]
32- public int MessageId { get ; private set ; }
33-
34- /// <summary>
35- /// 消息业务类型
36- /// </summary>
37- [ JsonIgnore ]
38- public MessageOperationType OperationType { get ; private set ; }
39-
40- /// <summary>
41- /// 设置消息ID
42- /// </summary>
43- /// <param name="messageId"></param>
44- public void SetMessageId ( int messageId )
45- {
46- MessageId = messageId ;
47- }
48-
49- /// <summary>
50- /// 消息的唯一ID
51- /// </summary>
52- [ JsonIgnore ]
53- public int UniqueId { get ; set ; }
54-
55-
56- /// <summary>
57- /// 更新唯一消息ID
58- /// </summary>
59- public void UpdateUniqueId ( )
60- {
61- UniqueId = IdGenerator . GetNextUniqueIntId ( ) ;
62- }
63-
64- /// <summary>
65- /// 设置唯一消息ID
66- /// </summary>
67- /// <param name="uniqueId"></param>
68- public void SetUniqueId ( int uniqueId )
69- {
70- UniqueId = uniqueId ;
71- }
72-
73- /// <summary>
74- /// 获取格式化后的消息字符串
75- /// </summary>
76- /// <returns></returns>
77- public string ToFormatMessageString ( )
78- {
79- var stringBuilder = StringBuilderCache . Acquire ( ) ;
80- stringBuilder . Clear ( ) ;
81- stringBuilder . AppendLine ( ) ;
82- // 向下的箭头
83- stringBuilder . AppendLine ( $ "{ '\u2193 ' . RepeatChar ( 140 ) } ") ;
84- // 消息的头部信息
85- // 消息类型
86- stringBuilder . Append ( $ "---MessageType:[{ GetType ( ) . Name . CenterAlignedText ( 30 ) } ]") ;
87- // 消息ID
88- stringBuilder . Append ( $ "--MsgId:[{ MessageId . ToString ( ) . CenterAlignedText ( 11 ) } ]({ MessageIdUtility . GetMainId ( MessageId ) . ToString ( ) . CenterAlignedText ( 6 ) } ,{ MessageIdUtility . GetSubId ( MessageId ) . ToString ( ) . CenterAlignedText ( 6 ) } )") ;
89- // 操作类型
90- stringBuilder . Append ( $ "--OpType:[{ OperationType . ToString ( ) . CenterAlignedText ( 20 ) } ]") ;
91- // 唯一ID
92- stringBuilder . Append ( $ "--UniqueId:[{ UniqueId . ToString ( ) . CenterAlignedText ( 13 ) } ]---") ;
93- // 消息的内容 分割
94- stringBuilder . AppendLine ( ) ;
95- // 消息内容
96- stringBuilder . AppendLine ( $ "{ ToJsonString ( ) } ") ;
97- // 向上的箭头
98- stringBuilder . AppendLine ( $ "{ '\u2191 ' . RepeatChar ( 140 ) } ") ;
99- stringBuilder . AppendLine ( ) ;
100- return StringBuilderCache . GetStringAndRelease ( stringBuilder ) ;
101- }
102-
103- /// <summary>
104- /// 获取JSON格式化后的消息字符串
105- /// </summary>
106- /// <returns></returns>
107- public string ToJsonString ( )
108- {
109- return JsonHelper . SerializeFormat ( this ) ;
110- }
111-
112- /// <summary>
113- /// 设置消息业务类型
114- /// </summary>
115- /// <param name="messageOperationType">消息业务类型 </param>
116- public void SetOperationType ( MessageOperationType messageOperationType )
117- {
118- OperationType = messageOperationType ;
119- }
120-
121- /// <summary>
122- /// 转换为字符串
123- /// </summary>
124- /// <returns></returns>
125- public override string ToString ( )
126- {
127- return JsonHelper . Serialize ( this ) ;
128- }
1+ using System . Text . Json . Serialization ;
2+ using GameFrameX . Foundation . Json ;
3+ using GameFrameX . Foundation . Logger ;
4+ using GameFrameX . NetWork . Abstractions ;
5+ using GameFrameX . Utility ;
6+ using GameFrameX . Utility . Extensions ;
7+ using ProtoBuf ;
8+
9+ namespace GameFrameX . NetWork . Messages ;
10+
11+ /// <summary>
12+ /// 消息对象
13+ /// </summary>
14+ [ ProtoContract ]
15+ public abstract class MessageObject : INetworkMessage
16+ {
17+ /// <summary>
18+ /// </summary>
19+ protected MessageObject ( )
20+ {
21+ UpdateUniqueId ( ) ;
22+ }
23+ /*/// <summary>
24+ /// 单位id
25+ /// </summary>
26+ [JsonIgnore]
27+ public int UniId { get; set; }*/
28+
29+ /// <summary>
30+ /// 消息ID
31+ /// </summary>
32+ [ JsonIgnore ]
33+ public int MessageId { get ; private set ; }
34+
35+ /// <summary>
36+ /// 消息业务类型
37+ /// </summary>
38+ [ JsonIgnore ]
39+ public MessageOperationType OperationType { get ; private set ; }
40+
41+ /// <summary>
42+ /// 设置消息ID
43+ /// </summary>
44+ /// <param name="messageId"></param>
45+ public void SetMessageId ( int messageId )
46+ {
47+ MessageId = messageId ;
48+ }
49+
50+ /// <summary>
51+ /// 消息的唯一ID
52+ /// </summary>
53+ [ JsonIgnore ]
54+ public int UniqueId { get ; set ; }
55+
56+
57+ /// <summary>
58+ /// 更新唯一消息ID
59+ /// </summary>
60+ public void UpdateUniqueId ( )
61+ {
62+ UniqueId = IdGenerator . GetNextUniqueIntId ( ) ;
63+ }
64+
65+ /// <summary>
66+ /// 设置唯一消息ID
67+ /// </summary>
68+ /// <param name="uniqueId"></param>
69+ public void SetUniqueId ( int uniqueId )
70+ {
71+ UniqueId = uniqueId ;
72+ }
73+
74+ /// <summary>
75+ /// 获取格式化后的消息字符串
76+ /// </summary>
77+ /// <returns></returns>
78+ public string ToFormatMessageString ( )
79+ {
80+ try
81+ {
82+ var stringBuilder = StringBuilderCache . Acquire ( ) ;
83+ stringBuilder . Clear ( ) ;
84+ stringBuilder . AppendLine ( ) ;
85+ // 向下的箭头
86+ stringBuilder . AppendLine ( $ "{ '\u2193 ' . RepeatChar ( 140 ) } ") ;
87+ // 消息的头部信息
88+ // 消息类型
89+ stringBuilder . Append ( $ "---MessageType:[{ GetType ( ) . Name . CenterAlignedText ( 30 ) } ]") ;
90+ // 消息ID
91+ stringBuilder . Append ( $ "--MsgId:[{ MessageId . ToString ( ) . CenterAlignedText ( 11 ) } ]({ MessageIdUtility . GetMainId ( MessageId ) . ToString ( ) . CenterAlignedText ( 6 ) } ,{ MessageIdUtility . GetSubId ( MessageId ) . ToString ( ) . CenterAlignedText ( 6 ) } )") ;
92+ // 操作类型
93+ stringBuilder . Append ( $ "--OpType:[{ OperationType . ToString ( ) . CenterAlignedText ( 20 ) } ]") ;
94+ // 唯一ID
95+ stringBuilder . Append ( $ "--UniqueId:[{ UniqueId . ToString ( ) . CenterAlignedText ( 13 ) } ]---") ;
96+ // 消息的内容 分割
97+ stringBuilder . AppendLine ( ) ;
98+ // 消息内容
99+ stringBuilder . AppendLine ( $ "{ ToJsonString ( ) } ") ;
100+ // 向上的箭头
101+ stringBuilder . AppendLine ( $ "{ '\u2191 ' . RepeatChar ( 140 ) } ") ;
102+ stringBuilder . AppendLine ( ) ;
103+ return StringBuilderCache . GetStringAndRelease ( stringBuilder ) ;
104+ }
105+ catch ( Exception e )
106+ {
107+ LogHelper . Error ( e ) ;
108+ }
109+
110+ return string . Empty ;
111+ }
112+
113+ /// <summary>
114+ /// 获取JSON格式化后的消息字符串
115+ /// </summary>
116+ /// <returns></returns>
117+ public string ToJsonString ( )
118+ {
119+ return JsonHelper . SerializeFormat ( this ) ;
120+ }
121+
122+ /// <summary>
123+ /// 设置消息业务类型
124+ /// </summary>
125+ /// <param name="messageOperationType">消息业务类型 </param>
126+ public void SetOperationType ( MessageOperationType messageOperationType )
127+ {
128+ OperationType = messageOperationType ;
129+ }
130+
131+ /// <summary>
132+ /// 转换为字符串
133+ /// </summary>
134+ /// <returns></returns>
135+ public override string ToString ( )
136+ {
137+ return JsonHelper . Serialize ( this ) ;
138+ }
129139}
0 commit comments