Refactored IWebSocketSession.cs, WebSocketService.cs

This commit is contained in:
sta 2014-02-19 16:21:38 +09:00
parent b0bcdf33c4
commit 348aff642d
2 changed files with 75 additions and 90 deletions

View File

@ -32,18 +32,19 @@ using WebSocketSharp.Net.WebSockets;
namespace WebSocketSharp.Server namespace WebSocketSharp.Server
{ {
/// <summary> /// <summary>
/// Exposes the access to the session information in the WebSocket service. /// Exposes the properties used for accessing the information in a session in a WebSocket
/// service.
/// </summary> /// </summary>
public interface IWebSocketSession public interface IWebSocketSession
{ {
#region Properties #region Properties
/// <summary> /// <summary>
/// Gets the WebSocket connection request information. /// Gets the information in the WebSocket connection request.
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="WebSocketContext"/> that represents the WebSocket connection /// A <see cref="WebSocketContext"/> that provides the access to the WebSocket connection
/// request information. /// request.
/// </value> /// </value>
WebSocketContext Context { get; } WebSocketContext Context { get; }
@ -56,10 +57,11 @@ namespace WebSocketSharp.Server
string ID { get; } string ID { get; }
/// <summary> /// <summary>
/// Gets the subprotocol used on the WebSocket connection. /// Gets the subprotocol of the <see cref="WebSocket"/> used in the session.
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="string"/> that represents the subprotocol if any. /// A <see cref="string"/> that represents the subprotocol of the <see cref="WebSocket"/> used
/// in the session if any.
/// </value> /// </value>
string Protocol { get; } string Protocol { get; }
@ -67,17 +69,16 @@ namespace WebSocketSharp.Server
/// Gets the time that the session has started. /// Gets the time that the session has started.
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="DateTime"/> that represents the time that the session has /// A <see cref="DateTime"/> that represents the time that the session has started.
/// started.
/// </value> /// </value>
DateTime StartTime { get; } DateTime StartTime { get; }
/// <summary> /// <summary>
/// Gets the state of the WebSocket connection. /// Gets the state of the <see cref="WebSocket"/> used in the session.
/// </summary> /// </summary>
/// <value> /// <value>
/// One of the <see cref="WebSocketState"/> enum values, indicates the state of /// One of the <see cref="WebSocketState"/> enum values, indicates the state of the
/// the WebSocket connection. /// <see cref="WebSocket"/> used in the session.
/// </value> /// </value>
WebSocketState State { get; } WebSocketState State { get; }

View File

@ -34,7 +34,7 @@ using WebSocketSharp.Net.WebSockets;
namespace WebSocketSharp.Server namespace WebSocketSharp.Server
{ {
/// <summary> /// <summary>
/// Provides the basic functions of the WebSocket service provided by the /// Exposes the methods and properties for a WebSocket service provided by the
/// <see cref="HttpServer"/> or <see cref="WebSocketServer"/>. /// <see cref="HttpServer"/> or <see cref="WebSocketServer"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
@ -67,11 +67,10 @@ namespace WebSocketSharp.Server
#region Protected Properties #region Protected Properties
/// <summary> /// <summary>
/// Gets or sets the logging functions. /// Gets the logging functions.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// If you want to change the current logger to the service own logger, you /// This property is available after the WebSocket connection has been established.
/// set this property to a new <see cref="Logger"/> instance that you created.
/// </remarks> /// </remarks>
/// <value> /// <value>
/// A <see cref="Logger"/> that provides the logging functions. /// A <see cref="Logger"/> that provides the logging functions.
@ -82,19 +81,16 @@ namespace WebSocketSharp.Server
? _websocket.Log ? _websocket.Log
: null; : null;
} }
set {
if (_websocket != null)
_websocket.Log = value;
}
} }
/// <summary> /// <summary>
/// Gets the manager of the sessions to the WebSocket service. /// Gets the access to the sessions in the WebSocket service.
/// </summary> /// </summary>
/// <remarks>
/// This property is available after the WebSocket connection has been established.
/// </remarks>
/// <value> /// <value>
/// A <see cref="WebSocketSessionManager"/> that manages the sessions to the /// A <see cref="WebSocketSessionManager"/> that provides the access to the sessions.
/// WebSocket service.
/// </value> /// </value>
protected WebSocketSessionManager Sessions { protected WebSocketSessionManager Sessions {
get { get {
@ -107,10 +103,10 @@ namespace WebSocketSharp.Server
#region Public Properties #region Public Properties
/// <summary> /// <summary>
/// Gets the WebSocket connection request information. /// Gets the information in the WebSocket connection request.
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="WebSocketContext"/> that represents the WebSocket connection /// A <see cref="WebSocketContext"/> that provides the access to the WebSocket connection
/// request. /// request.
/// </value> /// </value>
public WebSocketContext Context { public WebSocketContext Context {
@ -120,26 +116,26 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Gets the unique ID of the current <see cref="WebSocketService"/> instance. /// Gets the unique ID of the current session.
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="string"/> that represents the unique ID of the current /// A <see cref="string"/> that represents the unique ID of the current session.
/// <see cref="WebSocketService"/> instance.
/// </value> /// </value>
public string ID { public string ID {
get; private set; get; private set;
} }
/// <summary> /// <summary>
/// Gets or sets the subprotocol used on the WebSocket connection. /// Gets or sets the subprotocol of the <see cref="WebSocket"/> used in the current session.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// Set operation of this property is available before the connection has /// Set operation of this property is available before the WebSocket connection has been
/// been established. /// established.
/// </remarks> /// </remarks>
/// <value> /// <value>
/// <para> /// <para>
/// A <see cref="string"/> that represents the subprotocol if any. /// A <see cref="string"/> that represents the subprotocol of the <see cref="WebSocket"/>
/// used in the current session if any.
/// </para> /// </para>
/// <para> /// <para>
/// The value to set must be a token defined in /// The value to set must be a token defined in
@ -166,12 +162,10 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Gets the time that the current <see cref="WebSocketService"/> instance /// Gets the time that the current session has started.
/// has been started.
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="DateTime"/> that represents the time that the current /// A <see cref="DateTime"/> that represents the time that the current session has started.
/// <see cref="WebSocketService"/> instance has been started.
/// </value> /// </value>
public DateTime StartTime { public DateTime StartTime {
get { get {
@ -180,11 +174,11 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Gets the state of the WebSocket connection. /// Gets the state of the <see cref="WebSocket"/> used in the current session.
/// </summary> /// </summary>
/// <value> /// <value>
/// One of the <see cref="WebSocketState"/> values that indicate the state of /// One of the <see cref="WebSocketState"/> enum values, indicates the state of the
/// the WebSocket connection. /// <see cref="WebSocket"/> used in the current session.
/// </value> /// </value>
public WebSocketState State { public WebSocketState State {
get { get {
@ -255,8 +249,7 @@ namespace WebSocketSharp.Server
#region Protected Methods #region Protected Methods
/// <summary> /// <summary>
/// Calls the <see cref="OnError"/> method with the specified /// Calls the <see cref="OnError"/> method with the specified <paramref name="message"/>.
/// <paramref name="message"/>.
/// </summary> /// </summary>
/// <param name="message"> /// <param name="message">
/// A <see cref="string"/> that represents the error message. /// A <see cref="string"/> that represents the error message.
@ -268,49 +261,47 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Is called when the WebSocket connection has been closed. /// Called when the WebSocket connection used in the current session has been closed.
/// </summary> /// </summary>
/// <param name="e"> /// <param name="e">
/// A <see cref="CloseEventArgs"/> that contains the event data associated /// A <see cref="CloseEventArgs"/> that represents the event data received by
/// with an inner <see cref="WebSocket.OnClose"/> event. /// a <see cref="WebSocket.OnClose"/> event.
/// </param> /// </param>
protected virtual void OnClose (CloseEventArgs e) protected virtual void OnClose (CloseEventArgs e)
{ {
} }
/// <summary> /// <summary>
/// Is called when the inner <see cref="WebSocket"/> or current /// Called when the current session gets an error.
/// <see cref="WebSocketService"/> gets an error.
/// </summary> /// </summary>
/// <param name="e"> /// <param name="e">
/// An <see cref="ErrorEventArgs"/> that contains the event data associated /// A <see cref="ErrorEventArgs"/> that represents the event data received by
/// with an inner <see cref="WebSocket.OnError"/> event. /// a <see cref="WebSocket.OnError"/> event.
/// </param> /// </param>
protected virtual void OnError (ErrorEventArgs e) protected virtual void OnError (ErrorEventArgs e)
{ {
} }
/// <summary> /// <summary>
/// Is called when the inner <see cref="WebSocket"/> receives a data frame. /// Called when the current session receives a message.
/// </summary> /// </summary>
/// <param name="e"> /// <param name="e">
/// A <see cref="MessageEventArgs"/> that contains the event data associated /// A <see cref="MessageEventArgs"/> that represents the event data received by
/// with an inner <see cref="WebSocket.OnMessage"/> event. /// a <see cref="WebSocket.OnMessage"/> event.
/// </param> /// </param>
protected virtual void OnMessage (MessageEventArgs e) protected virtual void OnMessage (MessageEventArgs e)
{ {
} }
/// <summary> /// <summary>
/// Is called when the WebSocket connection has been established. /// Called when the WebSocket connection used in the current session has been established.
/// </summary> /// </summary>
protected virtual void OnOpen () protected virtual void OnOpen ()
{ {
} }
/// <summary> /// <summary>
/// Sends a binary <paramref name="data"/> to the client on the current /// Sends a binary <paramref name="data"/> to the client on the current session.
/// session in the WebSocket service.
/// </summary> /// </summary>
/// <param name="data"> /// <param name="data">
/// An array of <see cref="byte"/> that represents the binary data to send. /// An array of <see cref="byte"/> that represents the binary data to send.
@ -322,8 +313,8 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Sends the specified <paramref name="file"/> as a binary data /// Sends the specified <paramref name="file"/> as a binary data to the client on the current
/// to the client on the current session in the WebSocket service. /// session.
/// </summary> /// </summary>
/// <param name="file"> /// <param name="file">
/// A <see cref="FileInfo"/> that represents the file to send. /// A <see cref="FileInfo"/> that represents the file to send.
@ -335,8 +326,7 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Sends a text <paramref name="data"/> to the client on the current /// Sends a text <paramref name="data"/> to the client on the current session.
/// session in the WebSocket service.
/// </summary> /// </summary>
/// <param name="data"> /// <param name="data">
/// A <see cref="string"/> that represents the text data to send. /// A <see cref="string"/> that represents the text data to send.
@ -348,8 +338,7 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Sends a binary <paramref name="data"/> asynchronously to the client /// Sends a binary <paramref name="data"/> asynchronously to the client on the current session.
/// on the current session in the WebSocket service.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method doesn't wait for the send to be complete. /// This method doesn't wait for the send to be complete.
@ -358,9 +347,9 @@ namespace WebSocketSharp.Server
/// An array of <see cref="byte"/> that represents the binary data to send. /// An array of <see cref="byte"/> that represents the binary data to send.
/// </param> /// </param>
/// <param name="completed"> /// <param name="completed">
/// An Action&lt;bool&gt; delegate that references the method(s) called when /// An Action&lt;bool&gt; delegate that references the method(s) called when the send is
/// the send is complete. A <see cref="bool"/> passed to this delegate is /// complete. A <see cref="bool"/> passed to this delegate is <c>true</c> if the send is
/// <c>true</c> if the send is complete successfully; otherwise, <c>false</c>. /// complete successfully; otherwise, <c>false</c>.
/// </param> /// </param>
protected void SendAsync (byte [] data, Action<bool> completed) protected void SendAsync (byte [] data, Action<bool> completed)
{ {
@ -369,9 +358,8 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Sends the specified <paramref name="file"/> as a binary data /// Sends the specified <paramref name="file"/> as a binary data asynchronously to the client
/// asynchronously to the client on the current session in the WebSocket /// on the current session.
/// service.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method doesn't wait for the send to be complete. /// This method doesn't wait for the send to be complete.
@ -380,9 +368,9 @@ namespace WebSocketSharp.Server
/// A <see cref="FileInfo"/> that represents the file to send. /// A <see cref="FileInfo"/> that represents the file to send.
/// </param> /// </param>
/// <param name="completed"> /// <param name="completed">
/// An Action&lt;bool&gt; delegate that references the method(s) called when /// An Action&lt;bool&gt; delegate that references the method(s) called when the send is
/// the send is complete. A <see cref="bool"/> passed to this delegate is /// complete. A <see cref="bool"/> passed to this delegate is <c>true</c> if the send is
/// <c>true</c> if the send is complete successfully; otherwise, <c>false</c>. /// complete successfully; otherwise, <c>false</c>.
/// </param> /// </param>
protected void SendAsync (FileInfo file, Action<bool> completed) protected void SendAsync (FileInfo file, Action<bool> completed)
{ {
@ -391,8 +379,7 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Sends a text <paramref name="data"/> asynchronously to the client /// Sends a text <paramref name="data"/> asynchronously to the client on the current session.
/// on the current session in the WebSocket service.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method doesn't wait for the send to be complete. /// This method doesn't wait for the send to be complete.
@ -401,9 +388,9 @@ namespace WebSocketSharp.Server
/// A <see cref="string"/> that represents the text data to send. /// A <see cref="string"/> that represents the text data to send.
/// </param> /// </param>
/// <param name="completed"> /// <param name="completed">
/// An Action&lt;bool&gt; delegate that references the method(s) called when /// An Action&lt;bool&gt; delegate that references the method(s) called when the send is
/// the send is complete. A <see cref="bool"/> passed to this delegate is /// complete. A <see cref="bool"/> passed to this delegate is <c>true</c> if the send is
/// <c>true</c> if the send is complete successfully; otherwise, <c>false</c>. /// complete successfully; otherwise, <c>false</c>.
/// </param> /// </param>
protected void SendAsync (string data, Action<bool> completed) protected void SendAsync (string data, Action<bool> completed)
{ {
@ -412,8 +399,8 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Sends a binary data from the specified <see cref="Stream"/> asynchronously /// Sends a binary data from the specified <see cref="Stream"/> asynchronously to the client on
/// to the client on the current session in the WebSocket service. /// the current session.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method doesn't wait for the send to be complete. /// This method doesn't wait for the send to be complete.
@ -425,9 +412,9 @@ namespace WebSocketSharp.Server
/// An <see cref="int"/> that represents the number of bytes to send. /// An <see cref="int"/> that represents the number of bytes to send.
/// </param> /// </param>
/// <param name="completed"> /// <param name="completed">
/// An Action&lt;bool&gt; delegate that references the method(s) called when /// An Action&lt;bool&gt; delegate that references the method(s) called when the send is
/// the send is complete. A <see cref="bool"/> passed to this delegate is /// complete. A <see cref="bool"/> passed to this delegate is <c>true</c> if the send is
/// <c>true</c> if the send is complete successfully; otherwise, <c>false</c>. /// complete successfully; otherwise, <c>false</c>.
/// </param> /// </param>
protected void SendAsync (Stream stream, int length, Action<bool> completed) protected void SendAsync (Stream stream, int length, Action<bool> completed)
{ {
@ -436,26 +423,23 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Validates the HTTP Cookies used in the WebSocket connection request. /// Used to validate the HTTP cookies included in the WebSocket connection request.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method is called when the inner <see cref="WebSocket"/> validates /// This method is called when the <see cref="WebSocket"/> used in the current session
/// the WebSocket connection request. /// validates the WebSocket connection request.
/// </remarks> /// </remarks>
/// <returns> /// <returns>
/// <c>true</c> if the cookies is valid; otherwise, <c>false</c>. This method /// <c>true</c> if the cookies are valid; otherwise, <c>false</c>. This method returns
/// returns <c>true</c> as default. /// <c>true</c> as default.
/// </returns> /// </returns>
/// <param name="request"> /// <param name="request">
/// A <see cref="CookieCollection"/> that contains the collection of the /// A <see cref="CookieCollection"/> that contains the cookies to validate.
/// cookies to validate.
/// </param> /// </param>
/// <param name="response"> /// <param name="response">
/// A <see cref="CookieCollection"/> that receives the cookies to send to the /// A <see cref="CookieCollection"/> that receives the cookies to send to the client.
/// client.
/// </param> /// </param>
protected virtual bool ValidateCookies ( protected virtual bool ValidateCookies (CookieCollection request, CookieCollection response)
CookieCollection request, CookieCollection response)
{ {
return true; return true;
} }