EndPointManager.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //
  2. // EndPointManager.cs
  3. // Copied from System.Net.EndPointManager.cs
  4. //
  5. // Author:
  6. // Gonzalo Paniagua Javier (gonzalo@ximian.com)
  7. //
  8. // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
  9. // Copyright (c) 2012 sta.blockhead (sta.blockhead@gmail.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Collections;
  32. using System.Collections.Generic;
  33. using System.Net;
  34. namespace WebSocketSharp.Net {
  35. sealed class EndPointManager {
  36. #region Fields
  37. static Dictionary<IPAddress, Dictionary<int, EndPointListener>> ip_to_endpoints = new Dictionary<IPAddress, Dictionary<int, EndPointListener>> ();
  38. #endregion
  39. #region Constructor
  40. private EndPointManager ()
  41. {
  42. }
  43. #endregion
  44. #region Private Methods
  45. static void AddPrefixInternal (string p, HttpListener listener)
  46. {
  47. ListenerPrefix lp = new ListenerPrefix (p);
  48. if (lp.Path.IndexOf ('%') != -1)
  49. throw new HttpListenerException (400, "Invalid path.");
  50. if (lp.Path.IndexOf ("//", StringComparison.Ordinal) != -1) // TODO: Code?
  51. throw new HttpListenerException (400, "Invalid path.");
  52. // Always listens on all the interfaces, no matter the host name/ip used.
  53. EndPointListener epl = GetEPListener (IPAddress.Any, lp.Port, listener, lp.Secure);
  54. epl.AddPrefix (lp, listener);
  55. }
  56. static EndPointListener GetEPListener (IPAddress addr, int port, HttpListener listener, bool secure)
  57. {
  58. Dictionary<int, EndPointListener> p = null;
  59. if (ip_to_endpoints.ContainsKey (addr)) {
  60. p = ip_to_endpoints [addr];
  61. } else {
  62. p = new Dictionary<int, EndPointListener> ();
  63. ip_to_endpoints [addr] = p;
  64. }
  65. EndPointListener epl = null;
  66. if (p.ContainsKey (port)) {
  67. epl = p [port];
  68. } else {
  69. epl = new EndPointListener (addr, port, secure);
  70. p [port] = epl;
  71. }
  72. return epl;
  73. }
  74. static void RemovePrefixInternal (string prefix, HttpListener listener)
  75. {
  76. ListenerPrefix lp = new ListenerPrefix (prefix);
  77. if (lp.Path.IndexOf ('%') != -1)
  78. return;
  79. if (lp.Path.IndexOf ("//", StringComparison.Ordinal) != -1)
  80. return;
  81. EndPointListener epl = GetEPListener (IPAddress.Any, lp.Port, listener, lp.Secure);
  82. epl.RemovePrefix (lp, listener);
  83. }
  84. #endregion
  85. #region Public Methods
  86. public static void AddListener (HttpListener listener)
  87. {
  88. List<string> added = new List<string> ();
  89. try {
  90. lock (((ICollection)ip_to_endpoints).SyncRoot) {
  91. foreach (string prefix in listener.Prefixes) {
  92. AddPrefixInternal (prefix, listener);
  93. added.Add (prefix);
  94. }
  95. }
  96. } catch {
  97. foreach (string prefix in added) {
  98. RemovePrefix (prefix, listener);
  99. }
  100. throw;
  101. }
  102. }
  103. public static void AddPrefix (string prefix, HttpListener listener)
  104. {
  105. lock (((ICollection)ip_to_endpoints).SyncRoot) {
  106. AddPrefixInternal (prefix, listener);
  107. }
  108. }
  109. public static void RemoveEndPoint (EndPointListener epl, IPEndPoint ep)
  110. {
  111. lock (((ICollection)ip_to_endpoints).SyncRoot) {
  112. Dictionary<int, EndPointListener> p = null;
  113. p = ip_to_endpoints [ep.Address];
  114. p.Remove (ep.Port);
  115. if (p.Count == 0) {
  116. ip_to_endpoints.Remove (ep.Address);
  117. }
  118. epl.Close ();
  119. }
  120. }
  121. public static void RemoveListener (HttpListener listener)
  122. {
  123. lock (((ICollection)ip_to_endpoints).SyncRoot) {
  124. foreach (string prefix in listener.Prefixes) {
  125. RemovePrefixInternal (prefix, listener);
  126. }
  127. }
  128. }
  129. public static void RemovePrefix (string prefix, HttpListener listener)
  130. {
  131. lock (((ICollection)ip_to_endpoints).SyncRoot) {
  132. RemovePrefixInternal (prefix, listener);
  133. }
  134. }
  135. #endregion
  136. }
  137. }