Refactored IWebSocketSession.cs, WebSocketService.cs

This commit is contained in:
sta 2014-01-12 02:41:49 +09:00
parent 816f737821
commit 82015cdb1a
2 changed files with 70 additions and 61 deletions

View File

@ -4,8 +4,8 @@
* *
* The MIT License * The MIT License
* *
* Copyright (c) 2013 sta.blockhead * Copyright (c) 2013-2014 sta.blockhead
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
@ -15,7 +15,7 @@
* *
* The above copyright notice and this permission notice shall be included in * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software. * all copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@ -32,7 +32,7 @@ using WebSocketSharp.Net.WebSockets;
namespace WebSocketSharp.Server namespace WebSocketSharp.Server
{ {
/// <summary> /// <summary>
/// Exposes the properties for the session to the WebSocket service. /// Exposes the access to the session to the WebSocket service.
/// </summary> /// </summary>
public interface IWebSocketSession public interface IWebSocketSession
{ {
@ -40,15 +40,16 @@ namespace WebSocketSharp.Server
/// Gets the WebSocket connection request information. /// Gets the WebSocket connection request information.
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="WebSocketContext"/> that contains the WebSocket connection request information. /// A <see cref="WebSocketContext"/> that represents the WebSocket connection
/// request.
/// </value> /// </value>
WebSocketContext Context { get; } WebSocketContext Context { get; }
/// <summary> /// <summary>
/// Gets the unique ID of the session to the WebSocket service. /// Gets the unique ID of the session.
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="string"/> that contains the unique ID of the session. /// A <see cref="string"/> that represents the unique ID of the session.
/// </value> /// </value>
string ID { get; } string ID { get; }
@ -56,7 +57,8 @@ namespace WebSocketSharp.Server
/// Gets the time that the session has been started. /// Gets the time that the session has been started.
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="DateTime"/> that represents the time that the session has been started. /// A <see cref="DateTime"/> that represents the time that the session has
/// been started.
/// </value> /// </value>
DateTime StartTime { get; } DateTime StartTime { get; }
@ -64,7 +66,8 @@ namespace WebSocketSharp.Server
/// Gets the state of the WebSocket connection. /// Gets the state of the WebSocket connection.
/// </summary> /// </summary>
/// <value> /// <value>
/// One of the <see cref="WebSocketState"/> values. /// One of the <see cref="WebSocketState"/> values that indicate the state of
/// the WebSocket connection.
/// </value> /// </value>
WebSocketState State { get; } WebSocketState State { get; }
} }

View File

@ -27,18 +27,15 @@
#endregion #endregion
using System; using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO; using System.IO;
using System.Text;
using System.Threading;
using WebSocketSharp.Net; using WebSocketSharp.Net;
using WebSocketSharp.Net.WebSockets; using WebSocketSharp.Net.WebSockets;
namespace WebSocketSharp.Server namespace WebSocketSharp.Server
{ {
/// <summary> /// <summary>
/// Provides the basic functions of the WebSocket service used by the WebSocket service host. /// Provides the basic functions of the WebSocket service provided by the
/// <see cref="HttpServer"/> or <see cref="WebSocketServer"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// The WebSocketService class is an abstract class. /// The WebSocketService class is an abstract class.
@ -54,12 +51,12 @@ namespace WebSocketSharp.Server
#endregion #endregion
#region Public Constructors #region Protected Constructors
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="WebSocketService"/> class. /// Initializes a new instance of the <see cref="WebSocketService"/> class.
/// </summary> /// </summary>
public WebSocketService () protected WebSocketService ()
{ {
_start = DateTime.MaxValue; _start = DateTime.MaxValue;
} }
@ -72,8 +69,8 @@ namespace WebSocketSharp.Server
/// Gets or sets the logging functions. /// Gets or sets the logging functions.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// If you want to change the current logger to the service own logger, you set this property /// If you want to change the current logger to the service own logger, you
/// to a new <see cref="Logger"/> instance that you created. /// 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.
@ -95,7 +92,8 @@ namespace WebSocketSharp.Server
/// Gets the manager of the sessions to the WebSocket service. /// Gets the manager of the sessions to the WebSocket service.
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="WebSocketSessionManager"/> that manages the sessions to the WebSocket service. /// A <see cref="WebSocketSessionManager"/> that manages the sessions to the
/// WebSocket service.
/// </value> /// </value>
protected WebSocketSessionManager Sessions { protected WebSocketSessionManager Sessions {
get { get {
@ -111,7 +109,8 @@ namespace WebSocketSharp.Server
/// Gets the WebSocket connection request information. /// Gets the WebSocket connection request information.
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="WebSocketContext"/> that contains the WebSocket connection request information. /// A <see cref="WebSocketContext"/> that represents the WebSocket connection
/// request.
/// </value> /// </value>
public WebSocketContext Context { public WebSocketContext Context {
get { get {
@ -123,18 +122,20 @@ namespace WebSocketSharp.Server
/// Gets the unique ID of the current <see cref="WebSocketService"/> instance. /// Gets the unique ID of the current <see cref="WebSocketService"/> instance.
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="string"/> that contains the unique ID. /// A <see cref="string"/> that represents the unique ID of the current
/// <see cref="WebSocketService"/> instance.
/// </value> /// </value>
public string ID { public string ID {
get; private set; get; private set;
} }
/// <summary> /// <summary>
/// Gets the time that the current <see cref="WebSocketService"/> instance has been started. /// Gets the time that the current <see cref="WebSocketService"/> instance
/// has been started.
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="DateTime"/> that represents the time that the current <see cref="WebSocketService"/> /// A <see cref="DateTime"/> that represents the time that the current
/// instance has been started. /// <see cref="WebSocketService"/> instance has been started.
/// </value> /// </value>
public DateTime StartTime { public DateTime StartTime {
get { get {
@ -146,7 +147,8 @@ namespace WebSocketSharp.Server
/// Gets the state of the WebSocket connection. /// Gets the state of the WebSocket connection.
/// </summary> /// </summary>
/// <value> /// <value>
/// One of the <see cref="WebSocketState"/> values. /// One of the <see cref="WebSocketState"/> values that indicate the state of
/// the WebSocket connection.
/// </value> /// </value>
public WebSocketState State { public WebSocketState State {
get { get {
@ -214,14 +216,15 @@ namespace WebSocketSharp.Server
#region Protected Methods #region Protected Methods
/// <summary> /// <summary>
/// Calls the <see cref="OnError"/> method with the specified <paramref name="message"/>. /// Calls the <see cref="OnError"/> method with the specified
/// <paramref name="message"/>.
/// </summary> /// </summary>
/// <param name="message"> /// <param name="message">
/// A <see cref="string"/> that contains an error message. /// A <see cref="string"/> that represents the error message.
/// </param> /// </param>
protected void Error (string message) protected void Error (string message)
{ {
if (!message.IsNullOrEmpty ()) if (message != null && message.Length > 0)
OnError (new ErrorEventArgs (message)); OnError (new ErrorEventArgs (message));
} }
@ -229,20 +232,20 @@ namespace WebSocketSharp.Server
/// Is called when the WebSocket connection has been closed. /// Is called when the WebSocket connection has been closed.
/// </summary> /// </summary>
/// <param name="e"> /// <param name="e">
/// A <see cref="CloseEventArgs"/> that contains an event data associated with /// A <see cref="CloseEventArgs"/> that contains the event data associated
/// an inner <see cref="WebSocket.OnClose"/> event. /// with an inner <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 <see cref="WebSocketService"/> /// Is called when the inner <see cref="WebSocket"/> or current
/// gets an error. /// <see cref="WebSocketService"/> gets an error.
/// </summary> /// </summary>
/// <param name="e"> /// <param name="e">
/// An <see cref="ErrorEventArgs"/> that contains an event data associated with /// An <see cref="ErrorEventArgs"/> that contains the event data associated
/// an inner <see cref="WebSocket.OnError"/> event. /// with an inner <see cref="WebSocket.OnError"/> event.
/// </param> /// </param>
protected virtual void OnError (ErrorEventArgs e) protected virtual void OnError (ErrorEventArgs e)
{ {
@ -252,8 +255,8 @@ namespace WebSocketSharp.Server
/// Is called when the inner <see cref="WebSocket"/> receives a data frame. /// Is called when the inner <see cref="WebSocket"/> receives a data frame.
/// </summary> /// </summary>
/// <param name="e"> /// <param name="e">
/// A <see cref="MessageEventArgs"/> that contains an event data associated with /// A <see cref="MessageEventArgs"/> that contains the event data associated
/// an inner <see cref="WebSocket.OnMessage"/> event. /// with an inner <see cref="WebSocket.OnMessage"/> event.
/// </param> /// </param>
protected virtual void OnMessage (MessageEventArgs e) protected virtual void OnMessage (MessageEventArgs e)
{ {
@ -267,11 +270,12 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Sends a Ping to the client of the current <see cref="WebSocketService"/> instance. /// Sends a Ping to the client of the current <see cref="WebSocketService"/>
/// instance.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// <c>true</c> if the current <see cref="WebSocketService"/> instance receives a Pong /// <c>true</c> if the current <see cref="WebSocketService"/> instance
/// from the client in a time; otherwise, <c>false</c>. /// receives a Pong from the client in a time; otherwise, <c>false</c>.
/// </returns> /// </returns>
protected bool Ping () protected bool Ping ()
{ {
@ -281,15 +285,15 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Sends a Ping with the specified <paramref name="message"/> to the client of /// Sends a Ping with the specified <paramref name="message"/> to the client
/// the current <see cref="WebSocketService"/> instance. /// of the current <see cref="WebSocketService"/> instance.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// <c>true</c> if the current <see cref="WebSocketService"/> instance receives a Pong /// <c>true</c> if the current <see cref="WebSocketService"/> instance
/// from the client in a time; otherwise, <c>false</c>. /// receives a Pong from the client in a time; otherwise, <c>false</c>.
/// </returns> /// </returns>
/// <param name="message"> /// <param name="message">
/// A <see cref="string"/> that contains a message to send. /// A <see cref="string"/> that represents the message to send.
/// </param> /// </param>
protected bool Ping (string message) protected bool Ping (string message)
{ {
@ -390,7 +394,7 @@ namespace WebSocketSharp.Server
/// This method doesn't wait for the send to be complete. /// This method doesn't wait for the send to be complete.
/// </remarks> /// </remarks>
/// <param name="data"> /// <param name="data">
/// A <see cref="string"/> that contains 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
@ -439,14 +443,14 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Stops the current <see cref="WebSocketService"/> instance with the specified /// Stops the current <see cref="WebSocketService"/> instance with the
/// <see cref="ushort"/> and <see cref="string"/>. /// specified <see cref="ushort"/> and <see cref="string"/>.
/// </summary> /// </summary>
/// <param name="code"> /// <param name="code">
/// A <see cref="ushort"/> that contains a status code indicating the reason for stop. /// A <see cref="ushort"/> that represents the status code for stop.
/// </param> /// </param>
/// <param name="reason"> /// <param name="reason">
/// A <see cref="string"/> that contains the reason for stop. /// A <see cref="string"/> that represents the reason for stop.
/// </param> /// </param>
protected void Stop (ushort code, string reason) protected void Stop (ushort code, string reason)
{ {
@ -455,15 +459,15 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Stops the current <see cref="WebSocketService"/> instance with the specified /// Stops the current <see cref="WebSocketService"/> instance with the
/// <see cref="CloseStatusCode"/> and <see cref="string"/>. /// specified <see cref="CloseStatusCode"/> and <see cref="string"/>.
/// </summary> /// </summary>
/// <param name="code"> /// <param name="code">
/// One of the <see cref="CloseStatusCode"/> values that indicates a status code /// One of the <see cref="CloseStatusCode"/> values that indicate the status
/// indicating the reason for stop. /// codes for stop.
/// </param> /// </param>
/// <param name="reason"> /// <param name="reason">
/// A <see cref="string"/> that contains the reason for stop. /// A <see cref="string"/> that represents the reason for stop.
/// </param> /// </param>
protected void Stop (CloseStatusCode code, string reason) protected void Stop (CloseStatusCode code, string reason)
{ {
@ -472,24 +476,26 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Validates the cookies used in the WebSocket connection request. /// Validates the HTTP Cookies used 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 inner <see cref="WebSocket"/> validates
/// the WebSocket connection request. /// the WebSocket connection request.
/// </remarks> /// </remarks>
/// <returns> /// <returns>
/// <c>true</c> if the cookies is valid; otherwise, <c>false</c>. /// <c>true</c> if the cookies is valid; otherwise, <c>false</c>. This method
/// The default returns <c>true</c>. /// returns <c>true</c> as default.
/// </returns> /// </returns>
/// <param name="request"> /// <param name="request">
/// A <see cref="CookieCollection"/> that contains a collection of the HTTP Cookies /// A <see cref="CookieCollection"/> that contains the collection of the
/// to validate. /// cookies to validate.
/// </param> /// </param>
/// <param name="response"> /// <param name="response">
/// A <see cref="CookieCollection"/> that receives the HTTP Cookies to send to the client. /// A <see cref="CookieCollection"/> that receives the cookies to send to the
/// client.
/// </param> /// </param>
protected virtual bool ValidateCookies (CookieCollection request, CookieCollection response) protected virtual bool ValidateCookies (
CookieCollection request, CookieCollection response)
{ {
return true; return true;
} }