tanghai 6 лет назад
Родитель
Сommit
3af4226b02

+ 13 - 4
Unity/Assets/ThirdParty/Google.Protobuf/CodedOutputStream.cs

@@ -65,11 +65,11 @@ namespace Google.Protobuf
         /// </summary>
         public static readonly int DefaultBufferSize = 4096;
 
-        private readonly bool leaveOpen;
-        private readonly byte[] buffer;
-        private readonly int limit;
+        private bool leaveOpen;
+        private byte[] buffer;
+        private int limit;
         private int position;
-        private readonly Stream output;
+        private Stream output;
 
         #region Construction
         /// <summary>
@@ -94,6 +94,15 @@ namespace Google.Protobuf
             this.limit = offset + length;
             leaveOpen = true; // Simple way of avoiding trying to dispose of a null reference
         }
+        
+        public void Reset(byte[] buf, int offset, int length)
+        {
+            this.output = null;
+            this.buffer = buf;
+            this.position = offset;
+            this.limit = offset + length;
+            leaveOpen = true; // Simple way of avoiding trying to dispose of a null reference
+        }
 
         private CodedOutputStream(Stream output, byte[] buffer, bool leaveOpen)
         {

+ 6 - 4
Unity/Assets/ThirdParty/Google.Protobuf/MessageExtensions.cs

@@ -40,6 +40,7 @@ namespace Google.Protobuf
     public static class MessageExtensions
     {
         public static CodedInputStream inputStream = new CodedInputStream(new byte[0]);
+        public static CodedOutputStream outputStream = new CodedOutputStream(new byte[0]);
         
         /// <summary>
         /// Merges data from the given byte array into an existing message.
@@ -138,13 +139,14 @@ namespace Google.Protobuf
         /// </summary>
         /// <param name="message">The message to write to the stream.</param>
         /// <param name="output">The stream to write to.</param>
-        public static void WriteTo(this IMessage message, Stream output)
+        public static void WriteTo(this IMessage message, MemoryStream output)
         {
+            // 这里做了修改,去掉CodedOutputStream的gc
             ProtoPreconditions.CheckNotNull(message, "message");
             ProtoPreconditions.CheckNotNull(output, "output");
-            CodedOutputStream codedOutput = new CodedOutputStream(output);
-            message.WriteTo(codedOutput);
-            codedOutput.Flush();
+            outputStream.Reset(output.GetBuffer(), (int)output.Length, (int)(output.Capacity - output.Length));
+            message.WriteTo(outputStream);
+            output.SetLength(outputStream.Position);
         }
 
         /// <summary>