@@ -72,25 +72,34 @@ public static int CalcMessageHash(ulong msgId, uint seq)
7272 return ( ( ushort ) seq << 16 ) | ( ushort ) msgId ;
7373 }
7474
75- public static implicit operator MessageRecord ( MessageChain chain ) => new ( )
75+ public static implicit operator MessageRecord ( MessageChain chain )
7676 {
77- Id = CalcMessageHash ( chain . MessageId , chain . Sequence ) ,
78- Type = chain . Type ,
79- Sequence = chain . Sequence ,
80- ClientSequence = chain . ClientSequence ,
81- MessageId = chain . MessageId ,
82- Time = chain . Time ,
83- FromUin = chain . FriendUin ,
84- ToUin = chain . Type switch
77+ // The `static byte[] Serialize<T>(T, MessagePackSerializerOptions?, CancellationToken)` method
78+ // may have resource reuse issues that could lead to incorrect serialization results.
79+ // Use a separate stream to resolve this problem.
80+ using MemoryStream stream = new MemoryStream ( ) ;
81+ MessagePackSerializer . Serialize < List < IMessageEntity > > ( stream , chain , OPTIONS ) ;
82+
83+ return new ( )
8584 {
86- MessageType . Group => ( ulong ) chain . GroupUin ! ,
87- MessageType . Temp or
88- MessageType . Friend => chain . TargetUin ,
89- _ => throw new NotImplementedException ( ) ,
90- } ,
91- Style = chain . Style != null ? ( MessageStyleRecord ) chain . Style : null ,
92- Entities = MessagePackSerializer . Serialize < List < IMessageEntity > > ( chain , OPTIONS )
93- } ;
85+ Id = CalcMessageHash ( chain . MessageId , chain . Sequence ) ,
86+ Type = chain . Type ,
87+ Sequence = chain . Sequence ,
88+ ClientSequence = chain . ClientSequence ,
89+ MessageId = chain . MessageId ,
90+ Time = chain . Time ,
91+ FromUin = chain . FriendUin ,
92+ ToUin = chain . Type switch
93+ {
94+ MessageType . Group => ( ulong ) chain . GroupUin ! ,
95+ MessageType . Temp or
96+ MessageType . Friend => chain . TargetUin ,
97+ _ => throw new NotSupportedException ( ) ,
98+ } ,
99+ Style = chain . Style != null ? ( MessageStyleRecord ) chain . Style : null ,
100+ Entities = stream . ToArray ( ) ,
101+ } ;
102+ }
94103
95104 public static implicit operator MessageChain ( MessageRecord record )
96105 {
@@ -112,7 +121,7 @@ MessageType.Temp or
112121 ( uint ) record . ClientSequence ,
113122 record . MessageId
114123 ) ,
115- _ => throw new NotImplementedException ( ) ,
124+ _ => throw new NotSupportedException ( ) ,
116125 } ;
117126
118127 var entities = MessagePackSerializer . Deserialize < List < IMessageEntity > > ( record . Entities , OPTIONS ) ;
0 commit comments