WritingPrimitivesMessages.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // https://developers.google.com/protocol-buffers/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. #endregion
  32. using System;
  33. using System.Runtime.CompilerServices;
  34. using System.Runtime.InteropServices.ComTypes;
  35. using System.Security;
  36. namespace Google.Protobuf
  37. {
  38. /// <summary>
  39. /// Writing messages / groups.
  40. /// </summary>
  41. [SecuritySafeCritical]
  42. internal static class WritingPrimitivesMessages
  43. {
  44. /// <summary>
  45. /// Writes a message, without a tag.
  46. /// The data is length-prefixed.
  47. /// </summary>
  48. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  49. public static void WriteMessage(ref WriteContext ctx, IMessage value)
  50. {
  51. WritingPrimitives.WriteLength(ref ctx.buffer, ref ctx.state, value.CalculateSize());
  52. WriteRawMessage(ref ctx, value);
  53. }
  54. /// <summary>
  55. /// Writes a group, without a tag.
  56. /// </summary>
  57. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  58. public static void WriteGroup(ref WriteContext ctx, IMessage value)
  59. {
  60. WriteRawMessage(ref ctx, value);
  61. }
  62. /// <summary>
  63. /// Writes a message, without a tag.
  64. /// Message will be written without a length prefix.
  65. /// </summary>
  66. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  67. public static void WriteRawMessage(ref WriteContext ctx, IMessage message)
  68. {
  69. if (message is IBufferMessage bufferMessage)
  70. {
  71. bufferMessage.InternalWriteTo(ref ctx);
  72. }
  73. else
  74. {
  75. // If we reached here, it means we've ran into a nested message with older generated code
  76. // which doesn't provide the InternalWriteTo method that takes a WriteContext.
  77. // With a slight performance overhead, we can still serialize this message just fine,
  78. // but we need to find the original CodedOutputStream instance that initiated this
  79. // serialization process and make sure its internal state is up to date.
  80. // Note that this performance overhead is not very high (basically copying contents of a struct)
  81. // and it will only be incurred in case the application mixes older and newer generated code.
  82. // Regenerating the code from .proto files will remove this overhead because it will
  83. // generate the InternalWriteTo method we need.
  84. if (ctx.state.CodedOutputStream == null)
  85. {
  86. // This can only happen when the serialization started without providing a CodedOutputStream instance
  87. // (e.g. WriteContext was created directly from a IBufferWriter).
  88. // That also means that one of the new parsing APIs was used at the top level
  89. // and in such case it is reasonable to require that all the nested message provide
  90. // up-to-date generated code with WriteContext support (and fail otherwise).
  91. 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.");
  92. }
  93. ctx.CopyStateTo(ctx.state.CodedOutputStream);
  94. try
  95. {
  96. // fallback parse using the CodedOutputStream that started current serialization tree
  97. message.WriteTo(ctx.state.CodedOutputStream);
  98. }
  99. finally
  100. {
  101. ctx.LoadStateFrom(ctx.state.CodedOutputStream);
  102. }
  103. }
  104. }
  105. }
  106. }