PageRenderTime 55ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/src/MongoDB.Bson/Serialization/Serializers/IPEndPointSerializer.cs

https://github.com/mongodb/mongo-csharp-driver
C# | 89 lines | 48 code | 7 blank | 34 comment | 5 complexity | 3515de47d93edb2fb7fedb1e34ef8c68 MD5 | raw file
  1. /* Copyright 2010-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Net;
  17. using System.Net.Sockets;
  18. using System.Text.RegularExpressions;
  19. namespace MongoDB.Bson.Serialization.Serializers
  20. {
  21. /// <summary>
  22. /// Represents a serializer for IPEndPoints.
  23. /// </summary>
  24. public class IPEndPointSerializer : ClassSerializerBase<IPEndPoint>
  25. {
  26. // constructors
  27. /// <summary>
  28. /// Initializes a new instance of the IPEndPointSerializer class.
  29. /// </summary>
  30. public IPEndPointSerializer()
  31. {
  32. }
  33. // public methods
  34. /// <summary>
  35. /// Deserializes a value.
  36. /// </summary>
  37. /// <param name="context">The deserialization context.</param>
  38. /// <param name="args">The deserialization args.</param>
  39. /// <returns>A deserialized value.</returns>
  40. protected override IPEndPoint DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args)
  41. {
  42. var bsonReader = context.Reader;
  43. EnsureBsonTypeEquals(bsonReader, BsonType.String);
  44. var stringValue = bsonReader.ReadString();
  45. var match = Regex.Match(stringValue, @"^(?<address>(.+|\[.*\]))\:(?<port>\d+)$");
  46. if (match.Success)
  47. {
  48. IPAddress address;
  49. if (IPAddress.TryParse(match.Groups["address"].Value, out address))
  50. {
  51. int port;
  52. if (int.TryParse(match.Groups["port"].Value, out port))
  53. {
  54. return new IPEndPoint(address, port);
  55. }
  56. }
  57. }
  58. var message = string.Format("Invalid IPEndPoint value '{0}'.", stringValue);
  59. throw new FormatException(message);
  60. }
  61. /// <summary>
  62. /// Serializes a value.
  63. /// </summary>
  64. /// <param name="context">The serialization context.</param>
  65. /// <param name="args">The serialization args.</param>
  66. /// <param name="value">The object.</param>
  67. protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, IPEndPoint value)
  68. {
  69. var bsonWriter = context.Writer;
  70. string stringValue;
  71. if (value.AddressFamily == AddressFamily.InterNetwork)
  72. {
  73. stringValue = string.Format("{0}:{1}", value.Address, value.Port); // IPv4
  74. }
  75. else
  76. {
  77. stringValue = string.Format("[{0}]:{1}", value.Address, value.Port); // IPv6
  78. }
  79. bsonWriter.WriteString(stringValue);
  80. }
  81. }
  82. }