MapFieldCodec.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2015 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. namespace XGame.Framework.Network.Protobuf
  33. {
  34. public sealed class MapFieldCodec<TKey, TValue>
  35. {
  36. private readonly FieldCodec<TKey> keyCodec;
  37. private readonly FieldCodec<TValue> valueCodec;
  38. private readonly uint mapTag;
  39. /// <summary>
  40. /// Creates a new entry codec based on a separate key codec and value codec,
  41. /// and the tag to use for each map entry.
  42. /// </summary>
  43. /// <param name="keyCodec">The key codec.</param>
  44. /// <param name="valueCodec">The value codec.</param>
  45. /// <param name="mapTag">The map tag to use to introduce each map entry.</param>
  46. public MapFieldCodec(FieldCodec<TKey> keyCodec, FieldCodec<TValue> valueCodec, uint mapTag)
  47. {
  48. this.keyCodec = keyCodec;
  49. this.valueCodec = valueCodec;
  50. this.mapTag = mapTag;
  51. }
  52. /// <summary>
  53. /// The tag used in the enclosing message to indicate map entries.
  54. /// </summary>
  55. public uint MapTag { get { return mapTag; } }
  56. /// <summary>
  57. /// A mutable message class, used for parsing and serializing. This
  58. /// delegates the work to a codec, but implements the <see cref="IMessage"/> interface
  59. /// for interop with <see cref="CodedInputStream"/> and <see cref="CodedOutputStream"/>.
  60. /// This is nested inside Codec as it's tightly coupled to the associated codec,
  61. /// and it's simpler if it has direct access to all its fields.
  62. /// </summary>
  63. public class MessageAdapter : IMsgParser
  64. {
  65. private static readonly byte[] ZeroLengthMessageStreamData = new byte[] { 0 };
  66. private readonly MapFieldCodec<TKey, TValue> codec;
  67. public TKey Key { get; set; }
  68. public TValue Value { get; set; }
  69. public MessageAdapter(MapFieldCodec<TKey, TValue> codec)
  70. {
  71. this.codec = codec;
  72. }
  73. public void Reset()
  74. {
  75. Key = codec.keyCodec.DefaultValue;
  76. Value = codec.valueCodec.DefaultValue;
  77. }
  78. public void MergeFrom(CodedInputStream input)
  79. {
  80. uint tag;
  81. while ((tag = input.ReadTag()) != 0)
  82. {
  83. if (tag == codec.keyCodec.Tag)
  84. {
  85. Key = codec.keyCodec.Read(input);
  86. }
  87. else if (tag == codec.valueCodec.Tag)
  88. {
  89. Value = codec.valueCodec.Read(input);
  90. }
  91. else
  92. {
  93. input.SkipLastField();
  94. }
  95. }
  96. // Corner case: a map entry with a key but no value, where the value type is a message.
  97. // Read it as if we'd seen an input stream with no data (i.e. create a "default" message).
  98. if (Value == null)
  99. {
  100. Value = codec.valueCodec.Read(new CodedInputStream(ZeroLengthMessageStreamData));
  101. }
  102. }
  103. public void WriteTo(CodedOutputStream output)
  104. {
  105. codec.keyCodec.WriteTagAndValue(output, Key);
  106. codec.valueCodec.WriteTagAndValue(output, Value);
  107. }
  108. public int CalculateSize()
  109. {
  110. return codec.keyCodec.CalculateSizeWithTag(Key) + codec.valueCodec.CalculateSizeWithTag(Value);
  111. }
  112. public void Clear() { }
  113. //MessageDescriptor IMessage.Descriptor { get { return null; } }
  114. }
  115. }
  116. }