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)
{
return BitConverter.ToUInt16 (src.ToHostOrder (srcOrder), 0);

View File

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

View File

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