Refactored WebSocketServiceManager.cs, WebSocketSessionManager.cs

This commit is contained in:
sta 2014-02-20 16:34:28 +09:00
parent 348aff642d
commit 1548112965
3 changed files with 46 additions and 44 deletions

View File

@ -817,6 +817,11 @@ namespace WebSocketSharp
} }
} }
internal static List<TSource> ToList<TSource> (this IEnumerable<TSource> source)
{
return new List<TSource> (source);
}
internal static ushort ToUInt16 (this byte [] src, ByteOrder srcOrder) internal static ushort ToUInt16 (this byte [] src, ByteOrder srcOrder)
{ {
return BitConverter.ToUInt16 (src.ToHostOrder (srcOrder), 0); return BitConverter.ToUInt16 (src.ToHostOrder (srcOrder), 0);

View File

@ -87,24 +87,26 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Gets the collection of the WebSocket service hosts. /// Gets the collection of every information in the Websocket services provided by the server.
/// </summary> /// </summary>
/// <value> /// <value>
/// An IEnumerable&lt;WebSocketServiceHost&gt; that contains the collection of the WebSocket /// An IEnumerable&lt;WebSocketServiceHost&gt; that contains the collection of every
/// service hosts. /// information in the Websocket services.
/// </value> /// </value>
public IEnumerable<WebSocketServiceHost> Hosts { public IEnumerable<WebSocketServiceHost> Hosts {
get { get {
return copyHosts ().Values; lock (_sync) {
return _hosts.Values.ToList ();
}
} }
} }
/// <summary> /// <summary>
/// Gets a WebSocket service host with the specified <paramref name="path"/>. /// Gets the information in a WebSocket service with the specified <paramref name="path"/>.
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="WebSocketServiceHost"/> instance that represents the WebSocket service host if /// A <see cref="WebSocketServiceHost"/> instance that provides the access to the WebSocket
/// it's successfully found; otherwise, <see langword="null"/>. /// service if it's successfully found; otherwise, <see langword="null"/>.
/// </value> /// </value>
/// <param name="path"> /// <param name="path">
/// A <see cref="string"/> that represents the absolute path to the WebSocket service to find. /// A <see cref="string"/> that represents the absolute path to the WebSocket service to find.
@ -152,7 +154,9 @@ namespace WebSocketSharp.Server
/// </value> /// </value>
public IEnumerable<string> Paths { public IEnumerable<string> Paths {
get { get {
return copyHosts ().Keys; lock (_sync) {
return _hosts.Keys.ToList ();
}
} }
} }
@ -253,13 +257,6 @@ namespace WebSocketSharp.Server
return result; return result;
} }
private Dictionary<string, WebSocketServiceHost> copyHosts ()
{
lock (_sync) {
return new Dictionary<string, WebSocketServiceHost> (_hosts);
}
}
#endregion #endregion
#region Internal Methods #region Internal Methods
@ -328,7 +325,6 @@ namespace WebSocketSharp.Server
host.Sessions.Stop (args, frameAsBytes); host.Sessions.Stop (args, frameAsBytes);
_hosts.Clear (); _hosts.Clear ();
_state = ServerState.STOP; _state = ServerState.STOP;
} }
} }
@ -555,7 +551,8 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Tries to get a WebSocket service host with the specified <paramref name="path"/>. /// Tries to get the information in a WebSocket service with the specified
/// <paramref name="path"/>.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// <c>true</c> if the WebSocket service is successfully found; otherwise, <c>false</c>. /// <c>true</c> if the WebSocket service is successfully found; otherwise, <c>false</c>.
@ -564,9 +561,9 @@ namespace WebSocketSharp.Server
/// A <see cref="string"/> that represents the absolute path to the WebSocket service to find. /// A <see cref="string"/> that represents the absolute path to the WebSocket service to find.
/// </param> /// </param>
/// <param name="host"> /// <param name="host">
/// When this method returns, a <see cref="WebSocketServiceHost"/> instance that represents the /// When this method returns, a <see cref="WebSocketServiceHost"/> instance that
/// WebSocket service host if it's successfully found; otherwise, <see langword="null"/>. This /// provides the access to the WebSocket service if it's successfully found;
/// parameter is passed uninitialized. /// otherwise, <see langword="null"/>. This parameter is passed uninitialized.
/// </param> /// </param>
public bool TryGetServiceHost (string path, out WebSocketServiceHost host) public bool TryGetServiceHost (string path, out WebSocketServiceHost host)
{ {

View File

@ -42,7 +42,7 @@ namespace WebSocketSharp.Server
{ {
#region Private Static Fields #region Private Static Fields
private static readonly List<IWebSocketSession> _emptySessions; private static readonly Dictionary<string, IWebSocketSession> _emptySessions;
#endregion #endregion
@ -63,7 +63,7 @@ namespace WebSocketSharp.Server
static WebSocketSessionManager () static WebSocketSessionManager ()
{ {
_emptySessions = new List<IWebSocketSession> (); _emptySessions = new Dictionary<string, IWebSocketSession> ();
} }
#endregion #endregion
@ -138,7 +138,12 @@ namespace WebSocketSharp.Server
/// </value> /// </value>
public IEnumerable<string> IDs { public IEnumerable<string> IDs {
get { get {
return copySessions ().Keys; if (_state == ServerState.SHUTDOWN)
return _emptySessions.Keys;
lock (_sync) {
return _sessions.Keys.ToList ();
}
} }
} }
@ -158,11 +163,11 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Gets a session information with the specified <paramref name="id"/> in the WebSocket /// Gets the information in a session with the specified <paramref name="id"/> in the WebSocket
/// service. /// service.
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="IWebSocketSession"/> instance that represents the session information if it's /// A <see cref="IWebSocketSession"/> instance that provides the access to the session if it's
/// successfully found; otherwise, <see langword="null"/>. /// successfully found; otherwise, <see langword="null"/>.
/// </value> /// </value>
/// <param name="id"> /// <param name="id">
@ -200,18 +205,20 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Gets the collection of the session informations in the Websocket service. /// Gets the collection of every information in the sessions in the Websocket service.
/// </summary> /// </summary>
/// <value> /// <value>
/// An IEnumerable&lt;IWebSocketSession&gt; that contains the collection of the session /// An IEnumerable&lt;IWebSocketSession&gt; that contains the collection of every information
/// informations. /// in the sessions.
/// </value> /// </value>
public IEnumerable<IWebSocketSession> Sessions { public IEnumerable<IWebSocketSession> Sessions {
get { get {
if (_state == ServerState.SHUTDOWN) if (_state == ServerState.SHUTDOWN)
return _emptySessions; return _emptySessions.Values;
return copySessions ().Values; lock (_sync) {
return _sessions.Values.ToList ();
}
} }
} }
@ -266,13 +273,6 @@ namespace WebSocketSharp.Server
state => broadcast (opcode, stream, completed)); state => broadcast (opcode, stream, completed));
} }
private Dictionary<string, IWebSocketSession> copySessions ()
{
lock (_sync) {
return new Dictionary<string, IWebSocketSession> (_sessions);
}
}
private static string createID () private static string createID ()
{ {
return Guid.NewGuid ().ToString ("N"); return Guid.NewGuid ().ToString ("N");
@ -380,7 +380,7 @@ namespace WebSocketSharp.Server
_state = ServerState.SHUTDOWN; _state = ServerState.SHUTDOWN;
_sweepTimer.Enabled = false; _sweepTimer.Enabled = false;
foreach (var session in copySessions ().Values) foreach (var session in _sessions.Values.ToList ())
session.Context.WebSocket.Close (args, frame, 1000); session.Context.WebSocket.Close (args, frame, 1000);
_state = ServerState.STOP; _state = ServerState.STOP;
@ -609,7 +609,7 @@ namespace WebSocketSharp.Server
/// A <see cref="string"/> that represents the ID of the session to close. /// A <see cref="string"/> that represents the ID of the session to close.
/// </param> /// </param>
/// <param name="code"> /// <param name="code">
/// A <see cref="ushort"/> that indicates the status code for closure. /// A <see cref="ushort"/> that represents the status code indicating the reason for closure.
/// </param> /// </param>
/// <param name="reason"> /// <param name="reason">
/// A <see cref="string"/> that represents the reason for closure. /// A <see cref="string"/> that represents the reason for closure.
@ -629,8 +629,8 @@ namespace WebSocketSharp.Server
/// A <see cref="string"/> that represents the ID of the session to close. /// A <see cref="string"/> that represents the ID of the session to close.
/// </param> /// </param>
/// <param name="code"> /// <param name="code">
/// One of the <see cref="CloseStatusCode"/> enum values, indicates the status code for /// One of the <see cref="CloseStatusCode"/> enum values, represents the status code indicating
/// closure. /// the reason for closure.
/// </param> /// </param>
/// <param name="reason"> /// <param name="reason">
/// A <see cref="string"/> that represents the reason for closure. /// A <see cref="string"/> that represents the reason for closure.
@ -826,7 +826,7 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Tries to get a session information with the specified <paramref name="id"/> in the /// Tries to get the information in a session with the specified <paramref name="id"/> in the
/// WebSocket service. /// WebSocket service.
/// </summary> /// </summary>
/// <returns> /// <returns>
@ -836,8 +836,8 @@ namespace WebSocketSharp.Server
/// A <see cref="string"/> that represents the ID of the session to find. /// A <see cref="string"/> that represents the ID of the session to find.
/// </param> /// </param>
/// <param name="session"> /// <param name="session">
/// When this method returns, a <see cref="IWebSocketSession"/> instance that represents the /// When this method returns, a <see cref="IWebSocketSession"/> instance that provides the
/// session information if it's successfully found; otherwise, <see langword="null"/>. This /// access to the session if it's successfully found; otherwise, <see langword="null"/>. This
/// parameter is passed uninitialized. /// parameter is passed uninitialized.
/// </param> /// </param>
public bool TryGetSession (string id, out IWebSocketSession session) public bool TryGetSession (string id, out IWebSocketSession session)