1 #region Copyright notice and license 2 3 // Copyright 2018 The gRPC Authors 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 #endregion 18 19 using System; 20 using System.Buffers; 21 22 namespace Grpc.Core 23 { 24 /// <summary> 25 /// Provides storage for payload when serializing a message. 26 /// </summary> 27 public abstract class SerializationContext 28 { 29 /// <summary> 30 /// Use the byte array as serialized form of current message and mark serialization process as complete. 31 /// <c>Complete(byte[])</c> can only be called once. By calling this method the caller gives up the ownership of the 32 /// payload which must not be accessed afterwards. 33 /// </summary> 34 /// <param name="payload">the serialized form of current message</param> Complete(byte[] payload)35 public virtual void Complete(byte[] payload) 36 { 37 throw new NotImplementedException(); 38 } 39 40 /// <summary> 41 /// Gets buffer writer that can be used to write the serialized data. Once serialization is finished, 42 /// <c>Complete()</c> needs to be called. 43 /// </summary> GetBufferWriter()44 public virtual IBufferWriter<byte> GetBufferWriter() 45 { 46 throw new NotImplementedException(); 47 } 48 49 /// <summary> 50 /// Sets the payload length when writing serialized data into a buffer writer. If the serializer knows the full payload 51 /// length in advance, providing that information before obtaining the buffer writer using <c>GetBufferWriter()</c> can improve 52 /// serialization efficiency by avoiding copies. The provided payload length must be the same as the data written to the writer. 53 /// Calling this method is optional. If the payload length is not set then the length is calculated using the data written to 54 /// the buffer writer when <c>Complete()</c> is called. 55 /// </summary> 56 /// <param name="payloadLength">The total length of the payload in bytes.</param> SetPayloadLength(int payloadLength)57 public virtual void SetPayloadLength(int payloadLength) 58 { 59 } 60 61 /// <summary> 62 /// Complete the payload written to the buffer writer. <c>Complete()</c> can only be called once. 63 /// </summary> Complete()64 public virtual void Complete() 65 { 66 throw new NotImplementedException(); 67 } 68 } 69 } 70