WebSocketServiceHost.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #region License
  2. /*
  3. * WebSocketServiceHost.cs
  4. *
  5. * The MIT License
  6. *
  7. * Copyright (c) 2012-2023 sta.blockhead
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #endregion
  28. #region Contributors
  29. /*
  30. * Contributors:
  31. * - Juan Manuel Lallana <juan.manuel.lallana@gmail.com>
  32. */
  33. #endregion
  34. using System;
  35. using WebSocketSharp.Net.WebSockets;
  36. namespace WebSocketSharp.Server
  37. {
  38. /// <summary>
  39. /// Exposes the methods and properties used to access the information in
  40. /// a WebSocket service provided by the <see cref="WebSocketServer"/> or
  41. /// <see cref="HttpServer"/> class.
  42. /// </summary>
  43. /// <remarks>
  44. /// This class is an abstract class.
  45. /// </remarks>
  46. public abstract class WebSocketServiceHost
  47. {
  48. #region Private Fields
  49. private Logger _log;
  50. private string _path;
  51. private WebSocketSessionManager _sessions;
  52. #endregion
  53. #region Protected Constructors
  54. /// <summary>
  55. /// Initializes a new instance of the <see cref="WebSocketServiceHost"/>
  56. /// class with the specified path and logging function.
  57. /// </summary>
  58. /// <param name="path">
  59. /// A <see cref="string"/> that specifies the absolute path to
  60. /// the service.
  61. /// </param>
  62. /// <param name="log">
  63. /// A <see cref="Logger"/> that specifies the logging function for
  64. /// the service.
  65. /// </param>
  66. protected WebSocketServiceHost (string path, Logger log)
  67. {
  68. _path = path;
  69. _log = log;
  70. _sessions = new WebSocketSessionManager (log);
  71. }
  72. #endregion
  73. #region Internal Properties
  74. internal ServerState State {
  75. get {
  76. return _sessions.State;
  77. }
  78. }
  79. #endregion
  80. #region Protected Properties
  81. /// <summary>
  82. /// Gets the logging function for the service.
  83. /// </summary>
  84. /// <value>
  85. /// A <see cref="Logger"/> that provides the logging function.
  86. /// </value>
  87. protected Logger Log {
  88. get {
  89. return _log;
  90. }
  91. }
  92. #endregion
  93. #region Public Properties
  94. /// <summary>
  95. /// Gets or sets a value indicating whether the service cleans up
  96. /// the inactive sessions periodically.
  97. /// </summary>
  98. /// <remarks>
  99. /// The set operation works if the current state of the service is
  100. /// Ready or Stop.
  101. /// </remarks>
  102. /// <value>
  103. /// <c>true</c> if the service cleans up the inactive sessions every
  104. /// 60 seconds; otherwise, <c>false</c>.
  105. /// </value>
  106. public bool KeepClean {
  107. get {
  108. return _sessions.KeepClean;
  109. }
  110. set {
  111. _sessions.KeepClean = value;
  112. }
  113. }
  114. /// <summary>
  115. /// Gets the path to the service.
  116. /// </summary>
  117. /// <value>
  118. /// A <see cref="string"/> that represents the absolute path to
  119. /// the service.
  120. /// </value>
  121. public string Path {
  122. get {
  123. return _path;
  124. }
  125. }
  126. /// <summary>
  127. /// Gets the management function for the sessions in the service.
  128. /// </summary>
  129. /// <value>
  130. /// A <see cref="WebSocketSessionManager"/> that manages the sessions in
  131. /// the service.
  132. /// </value>
  133. public WebSocketSessionManager Sessions {
  134. get {
  135. return _sessions;
  136. }
  137. }
  138. /// <summary>
  139. /// Gets the type of the behavior of the service.
  140. /// </summary>
  141. /// <value>
  142. /// A <see cref="Type"/> that represents the type of the behavior of
  143. /// the service.
  144. /// </value>
  145. public abstract Type BehaviorType { get; }
  146. /// <summary>
  147. /// Gets or sets the time to wait for the response to the WebSocket
  148. /// Ping or Close.
  149. /// </summary>
  150. /// <remarks>
  151. /// The set operation works if the current state of the service is
  152. /// Ready or Stop.
  153. /// </remarks>
  154. /// <value>
  155. /// A <see cref="TimeSpan"/> that represents the time to wait for
  156. /// the response.
  157. /// </value>
  158. /// <exception cref="ArgumentOutOfRangeException">
  159. /// The value specified for a set operation is zero or less.
  160. /// </exception>
  161. public TimeSpan WaitTime {
  162. get {
  163. return _sessions.WaitTime;
  164. }
  165. set {
  166. _sessions.WaitTime = value;
  167. }
  168. }
  169. #endregion
  170. #region Internal Methods
  171. internal void Start ()
  172. {
  173. _sessions.Start ();
  174. }
  175. internal void StartSession (WebSocketContext context)
  176. {
  177. CreateSession ().Start (context, _sessions);
  178. }
  179. internal void Stop (ushort code, string reason)
  180. {
  181. _sessions.Stop (code, reason);
  182. }
  183. #endregion
  184. #region Protected Methods
  185. /// <summary>
  186. /// Creates a new session for the service.
  187. /// </summary>
  188. /// <returns>
  189. /// A <see cref="WebSocketBehavior"/> instance that represents
  190. /// the new session.
  191. /// </returns>
  192. protected abstract WebSocketBehavior CreateSession ();
  193. #endregion
  194. }
  195. }