public void WriteTo(ref WriteContext ctx)
 {
     foreach (KeyValuePair <int, UnknownField> entry in fields)
     {
         entry.Value.WriteTo(entry.Key, ref ctx);
     }
 }
Exemple #2
0
 internal static void Initialize(ref Span <byte> buffer, out WriteContext ctx)
 {
     ctx.buffer         = buffer;
     ctx.state          = default;
     ctx.state.limit    = ctx.buffer.Length;
     ctx.state.position = 0;
     WriteBufferHelper.InitializeNonRefreshable(out ctx.state.writeBufferHelper);
 }
Exemple #3
0
 internal static void Initialize(IBufferWriter <byte> output, out WriteContext ctx)
 {
     ctx.buffer = default;
     ctx.state  = default;
     WriteBufferHelper.Initialize(output, out ctx.state.writeBufferHelper, out ctx.buffer);
     ctx.state.limit    = ctx.buffer.Length;
     ctx.state.position = 0;
 }
Exemple #4
0
 internal static void Initialize(CodedOutputStream output, out WriteContext ctx)
 {
     ctx.buffer = new Span <byte>(output.InternalBuffer);
     // ideally we would use a reference to the original state, but that doesn't seem possible
     // so we just copy the struct that holds the state. We will need to later store the state back
     // into CodedOutputStream if we want to keep it usable.
     ctx.state = output.InternalState;
 }
        public static void WriteTo(this IMessage message, IBufferWriter <byte> output)
        {
            ProtoPreconditions.CheckNotNull(message, nameof(message));
            ProtoPreconditions.CheckNotNull(output, nameof(output));

            WriteContext.Initialize(output, out WriteContext ctx);
            WritingPrimitivesMessages.WriteRawMessage(ref ctx, message);
            ctx.Flush();
        }
Exemple #6
0
 public void WriteTo(ref WriteContext ctx)
 {
     ctx.WriteTag(codec.Tag);
     codec.ValueWriter(ref ctx, field);
     if (codec.EndTag != 0)
     {
         ctx.WriteTag(codec.EndTag);
     }
 }
 /// <summary>
 /// Serializes the set and writes it to <paramref name="output"/>.
 /// </summary>
 public void WriteTo(CodedOutputStream output)
 {
     WriteContext.Initialize(output, out WriteContext ctx);
     try
     {
         WriteTo(ref ctx);
     }
     finally
     {
         ctx.CopyStateTo(output);
     }
 }
Exemple #8
0
 /// <summary>
 /// Serializes the field, including the field number, and writes it to
 /// <paramref name="output"/>
 /// </summary>
 /// <param name="fieldNumber">The unknown field number.</param>
 /// <param name="output">The write context to write to.</param>
 internal void WriteTo(int fieldNumber, ref WriteContext output)
 {
     if (varintList != null)
     {
         foreach (ulong value in varintList)
         {
             output.WriteTag(fieldNumber, WireFormat.WireType.Varint);
             output.WriteUInt64(value);
         }
     }
     if (fixed32List != null)
     {
         foreach (uint value in fixed32List)
         {
             output.WriteTag(fieldNumber, WireFormat.WireType.Fixed32);
             output.WriteFixed32(value);
         }
     }
     if (fixed64List != null)
     {
         foreach (ulong value in fixed64List)
         {
             output.WriteTag(fieldNumber, WireFormat.WireType.Fixed64);
             output.WriteFixed64(value);
         }
     }
     if (lengthDelimitedList != null)
     {
         foreach (ByteString value in lengthDelimitedList)
         {
             output.WriteTag(fieldNumber, WireFormat.WireType.LengthDelimited);
             output.WriteBytes(value);
         }
     }
     if (groupList != null)
     {
         foreach (UnknownFieldSet value in groupList)
         {
             output.WriteTag(fieldNumber, WireFormat.WireType.StartGroup);
             value.WriteTo(ref output);
             output.WriteTag(fieldNumber, WireFormat.WireType.EndGroup);
         }
     }
 }
Exemple #9
0
        public static void WriteRawMessage(ref WriteContext ctx, IMessage message)
        {
            if (message is IBufferMessage bufferMessage)
            {
                bufferMessage.InternalWriteTo(ref ctx);
            }
            else
            {
                // If we reached here, it means we've ran into a nested message with older generated code
                // which doesn't provide the InternalWriteTo method that takes a WriteContext.
                // With a slight performance overhead, we can still serialize this message just fine,
                // but we need to find the original CodedOutputStream instance that initiated this
                // serialization process and make sure its internal state is up to date.
                // Note that this performance overhead is not very high (basically copying contents of a struct)
                // and it will only be incurred in case the application mixes older and newer generated code.
                // Regenerating the code from .proto files will remove this overhead because it will
                // generate the InternalWriteTo method we need.

                if (ctx.state.CodedOutputStream == null)
                {
                    // This can only happen when the serialization started without providing a CodedOutputStream instance
                    // (e.g. WriteContext was created directly from a IBufferWriter).
                    // That also means that one of the new parsing APIs was used at the top level
                    // and in such case it is reasonable to require that all the nested message provide
                    // up-to-date generated code with WriteContext support (and fail otherwise).
                    throw new InvalidProtocolBufferException($"Message {message.GetType().Name} doesn't provide the generated method that enables WriteContext-based serialization. You might need to regenerate the generated protobuf code.");
                }

                ctx.CopyStateTo(ctx.state.CodedOutputStream);
                try
                {
                    // fallback parse using the CodedOutputStream that started current serialization tree
                    message.WriteTo(ctx.state.CodedOutputStream);
                }
                finally
                {
                    ctx.LoadStateFrom(ctx.state.CodedOutputStream);
                }
            }
        }
Exemple #10
0
 public static void WriteGroup(ref WriteContext ctx, IMessage value)
 {
     WriteRawMessage(ref ctx, value);
 }
Exemple #11
0
 public static void WriteMessage(ref WriteContext ctx, IMessage value)
 {
     WritingPrimitives.WriteLength(ref ctx.buffer, ref ctx.state, value.CalculateSize());
     WriteRawMessage(ref ctx, value);
 }
Exemple #12
0
 public void WriteTo(ref WriteContext ctx)
 {
     field.WriteTo(ref ctx, codec);
 }
Exemple #13
0
 internal static void Initialize(ref Span <byte> buffer, ref WriterInternalState state, out WriteContext ctx)
 {
     ctx.buffer = buffer;
     ctx.state  = state;
 }
Exemple #14
0
 internal static void Write <T>(ref WriteContext ctx, T value, FieldCodec <T> codec)
 {
     ctx.WriteLength(codec.CalculateSizeWithTag(value));
     codec.WriteTagAndValue(ref ctx, value);
 }