diff --git a/websocket-sharp/Server/WebSocketSessionManager.cs b/websocket-sharp/Server/WebSocketSessionManager.cs
index 28d72313..066d3a60 100644
--- a/websocket-sharp/Server/WebSocketSessionManager.cs
+++ b/websocket-sharp/Server/WebSocketSessionManager.cs
@@ -29,7 +29,6 @@
using System;
using System.Collections.Generic;
using System.IO;
-using System.Linq;
using System.Text;
using System.Threading;
using System.Timers;
@@ -37,7 +36,7 @@ using System.Timers;
namespace WebSocketSharp.Server
{
///
- /// Manages the sessions to a Websocket service.
+ /// Manages the sessions in a Websocket service.
///
public class WebSocketSessionManager
{
@@ -83,10 +82,9 @@ namespace WebSocketSharp.Server
_keepClean = true;
_sessions = new Dictionary ();
_state = ServerState.READY;
- _sweeping = false;
_sync = new object ();
- setSweepTimer (60 * 1000);
+ setSweepTimer (60000);
}
#endregion
@@ -104,23 +102,22 @@ namespace WebSocketSharp.Server
#region Public Properties
///
- /// Gets the collection of every ID of the active sessions to the Websocket
- /// service.
+ /// Gets the collection of every ID of the active sessions in the Websocket service.
///
///
- /// An IEnumerable<string> that contains the collection of every ID of
- /// the active sessions.
+ /// An IEnumerable<string> that contains the collection of every ID of the active
+ /// sessions.
///
public IEnumerable ActiveIDs {
get {
- return from result in Broadping (WsFrame.EmptyUnmaskPingData, 1000)
- where result.Value
- select result.Key;
+ foreach (var result in Broadping (WsFrame.EmptyUnmaskPingData, 1000))
+ if (result.Value)
+ yield return result.Key;
}
}
///
- /// Gets the number of the sessions to the Websocket service.
+ /// Gets the number of the sessions in the Websocket service.
///
///
/// An that represents the number of the sessions.
@@ -134,46 +131,42 @@ namespace WebSocketSharp.Server
}
///
- /// Gets the collection of every ID of the sessions to the Websocket service.
+ /// Gets the collection of every ID of the sessions in the Websocket service.
///
///
- /// An IEnumerable<string> that contains the collection of every ID of
- /// the sessions.
+ /// An IEnumerable<string> that contains the collection of every ID of the sessions.
///
public IEnumerable IDs {
get {
- lock (_sync) {
- return _sessions.Keys.ToList ();
- }
+ return copySessions ().Keys;
}
}
///
- /// Gets the collection of every ID of the inactive sessions to the Websocket
- /// service.
+ /// Gets the collection of every ID of the inactive sessions in the Websocket service.
///
///
- /// An IEnumerable<string> that contains the collection of every ID of
- /// the inactive sessions.
+ /// An IEnumerable<string> that contains the collection of every ID of the inactive
+ /// sessions.
///
public IEnumerable InactiveIDs {
get {
- return from result in Broadping (WsFrame.EmptyUnmaskPingData, 1000)
- where !result.Value
- select result.Key;
+ foreach (var result in Broadping (WsFrame.EmptyUnmaskPingData, 1000))
+ if (!result.Value)
+ yield return result.Key;
}
}
///
- /// Gets a WebSocket session information with the specified .
+ /// Gets a session information with the specified in the WebSocket
+ /// service.
///
///
- /// A instance that represents the session if
- /// it's successfully found; otherwise, .
+ /// A instance that represents the session information if it's
+ /// successfully found; otherwise, .
///
///
- /// A that represents the ID of the WebSocket session to
- /// get.
+ /// A that represents the ID of the session to find.
///
public IWebSocketSession this [string id] {
get {
@@ -185,12 +178,11 @@ namespace WebSocketSharp.Server
}
///
- /// Gets a value indicating whether the manager cleans up the inactive
- /// sessions periodically.
+ /// Gets a value indicating whether the manager cleans up the inactive sessions periodically.
///
///
- /// true if the manager cleans up the inactive sessions every 60
- /// seconds; otherwise, false.
+ /// true if the manager cleans up the inactive sessions every 60 seconds; otherwise,
+ /// false.
///
public bool KeepClean {
get {
@@ -208,20 +200,18 @@ namespace WebSocketSharp.Server
}
///
- /// Gets the collection of the session informations to the Websocket service.
+ /// Gets the collection of the session informations in the Websocket service.
///
///
- /// An IEnumerable<IWebSocketSession> that contains the collection of
- /// the session informations to the Websocket service.
+ /// An IEnumerable<IWebSocketSession> that contains the collection of the session
+ /// informations.
///
public IEnumerable Sessions {
get {
if (_state == ServerState.SHUTDOWN)
return _emptySessions;
- lock (_sync) {
- return _sessions.Values.ToList ();
- }
+ return copySessions ().Values;
}
}
@@ -276,9 +266,11 @@ namespace WebSocketSharp.Server
state => broadcast (opcode, stream, completed));
}
- private string checkIfCanSend (Func checkParams)
+ private Dictionary copySessions ()
{
- return _state.CheckIfStart () ?? checkParams ();
+ lock (_sync) {
+ return new Dictionary (_sessions);
+ }
}
private static string createID ()
@@ -300,8 +292,7 @@ namespace WebSocketSharp.Server
}
if (!result)
- _logger.Error (
- "A WebSocket session with the specified ID not found.\nID: " + id);
+ _logger.Error ("A session with the specified ID not found.\nID: " + id);
return result;
}
@@ -345,8 +336,7 @@ namespace WebSocketSharp.Server
}
}
- internal Dictionary Broadping (
- byte [] frameAsBytes, int timeOut)
+ internal Dictionary Broadping (byte [] frame, int millisecondsTimeout)
{
var result = new Dictionary ();
foreach (var session in Sessions) {
@@ -354,7 +344,7 @@ namespace WebSocketSharp.Server
break;
result.Add (
- session.ID, session.Context.WebSocket.Ping (frameAsBytes, timeOut));
+ session.ID, session.Context.WebSocket.Ping (frame, millisecondsTimeout));
}
return result;
@@ -377,22 +367,21 @@ namespace WebSocketSharp.Server
{
var payload = new PayloadData (data);
var args = new CloseEventArgs (payload);
- var frameAsBytes =
- send
- ? WsFrame.CreateCloseFrame (Mask.UNMASK, payload).ToByteArray ()
- : null;
+ var frameAsBytes = send
+ ? WsFrame.CreateCloseFrame (Mask.UNMASK, payload).ToByteArray ()
+ : null;
Stop (args, frameAsBytes);
}
- internal void Stop (CloseEventArgs args, byte [] frameAsBytes)
+ internal void Stop (CloseEventArgs args, byte [] frame)
{
lock (_sync) {
_state = ServerState.SHUTDOWN;
_sweepTimer.Enabled = false;
- foreach (var session in _sessions.Values.ToList ())
- session.Context.WebSocket.Close (args, frameAsBytes, 1000);
+ foreach (var session in copySessions ().Values)
+ session.Context.WebSocket.Close (args, frame, 1000);
_state = ServerState.STOP;
}
@@ -403,16 +392,14 @@ namespace WebSocketSharp.Server
#region Public Methods
///
- /// Broadcasts a binary to all clients
- /// of the WebSocket service.
+ /// Broadcasts a binary to every client in the WebSocket service.
///
///
- /// An array of that represents the binary data
- /// to broadcast.
+ /// An array of that represents the binary data to broadcast.
///
public void Broadcast (byte [] data)
{
- var msg = checkIfCanSend (() => data.CheckIfValidSendData ());
+ var msg = _state.CheckIfStart () ?? data.CheckIfValidSendData ();
if (msg != null) {
_logger.Error (msg);
return;
@@ -425,15 +412,14 @@ namespace WebSocketSharp.Server
}
///
- /// Broadcasts a text to all clients
- /// of the WebSocket service.
+ /// Broadcasts a text to every client in the WebSocket service.
///
///
/// A that represents the text data to broadcast.
///
public void Broadcast (string data)
{
- var msg = checkIfCanSend (() => data.CheckIfValidSendData ());
+ var msg = _state.CheckIfStart () ?? data.CheckIfValidSendData ();
if (msg != null) {
_logger.Error (msg);
return;
@@ -447,23 +433,22 @@ namespace WebSocketSharp.Server
}
///
- /// Broadcasts a binary asynchronously to all clients
- /// of the WebSocket service.
+ /// Broadcasts a binary asynchronously to every client in the WebSocket
+ /// service.
///
///
/// This method doesn't wait for the broadcast to be complete.
///
///
- /// An array of that represents the binary data
- /// to broadcast.
+ /// An array of that represents the binary data to broadcast.
///
///
- /// A delegate that references the method(s) called when
- /// the broadcast is complete.
+ /// A delegate that references the method(s) called when the broadcast is
+ /// complete.
///
public void BroadcastAsync (byte [] data, Action completed)
{
- var msg = checkIfCanSend (() => data.CheckIfValidSendData ());
+ var msg = _state.CheckIfStart () ?? data.CheckIfValidSendData ();
if (msg != null) {
_logger.Error (msg);
return;
@@ -476,8 +461,8 @@ namespace WebSocketSharp.Server
}
///
- /// Broadcasts a text asynchronously to all clients
- /// of the WebSocket service.
+ /// Broadcasts a text asynchronously to every client in the WebSocket
+ /// service.
///
///
/// This method doesn't wait for the broadcast to be complete.
@@ -486,12 +471,12 @@ namespace WebSocketSharp.Server
/// A that represents the text data to broadcast.
///
///
- /// A delegate that references the method(s) called when
- /// the broadcast is complete.
+ /// A delegate that references the method(s) called when the broadcast is
+ /// complete.
///
public void BroadcastAsync (string data, Action completed)
{
- var msg = checkIfCanSend (() => data.CheckIfValidSendData ());
+ var msg = _state.CheckIfStart () ?? data.CheckIfValidSendData ();
if (msg != null) {
_logger.Error (msg);
return;
@@ -505,8 +490,8 @@ namespace WebSocketSharp.Server
}
///
- /// Broadcasts a binary data from the specified
- /// asynchronously to all clients of the WebSocket service.
+ /// Broadcasts a binary data from the specified asynchronously to every
+ /// client in the WebSocket service.
///
///
/// This method doesn't wait for the broadcast to be complete.
@@ -518,14 +503,14 @@ namespace WebSocketSharp.Server
/// An that represents the number of bytes to broadcast.
///
///
- /// A delegate that references the method(s) called when
- /// the broadcast is complete.
+ /// A delegate that references the method(s) called when the broadcast is
+ /// complete.
///
public void BroadcastAsync (Stream stream, int length, Action completed)
{
- var msg = checkIfCanSend (
- () => stream.CheckIfCanRead () ??
- (length < 1 ? "'length' must be greater than 0." : null));
+ var msg = _state.CheckIfStart () ??
+ stream.CheckIfCanRead () ??
+ (length < 1 ? "'length' must be greater than 0." : null);
if (msg != null) {
_logger.Error (msg);
@@ -557,12 +542,11 @@ namespace WebSocketSharp.Server
}
///
- /// Sends Pings to all clients of the WebSocket service.
+ /// Sends a Ping to every client in the WebSocket service.
///
///
- /// A Dictionary<string, bool> that contains the collection of pairs of
- /// session ID and value indicating whether the WebSocket service received a
- /// Pong from each client in a time.
+ /// A Dictionary<string, bool> that contains the collection of pairs of session ID and
+ /// value indicating whether the manager received a Pong from every client in a time.
///
public Dictionary Broadping ()
{
@@ -576,13 +560,12 @@ namespace WebSocketSharp.Server
}
///
- /// Sends Pings with the specified to all clients
- /// of the WebSocket service.
+ /// Sends a Ping with the specified to every client in the WebSocket
+ /// service.
///
///
- /// A Dictionary<string, bool> that contains the collection of pairs of
- /// session ID and value indicating whether the WebSocket service received a
- /// Pong from each client in a time.
+ /// A Dictionary<string, bool> that contains the collection of pairs of session ID and
+ /// value indicating whether the manager received a Pong from every client in a time.
///
///
/// A that represents the message to send.
@@ -593,9 +576,8 @@ namespace WebSocketSharp.Server
return Broadping ();
byte [] data = null;
- var msg = checkIfCanSend (
- () => (data = Encoding.UTF8.GetBytes (message))
- .CheckIfValidControlData ("message"));
+ var msg = _state.CheckIfStart () ??
+ (data = Encoding.UTF8.GetBytes (message)).CheckIfValidControlData ("message");
if (msg != null) {
_logger.Error (msg);
@@ -620,14 +602,14 @@ namespace WebSocketSharp.Server
}
///
- /// Closes the session with the specified ,
- /// and .
+ /// Closes the session with the specified , , and
+ /// .
///
///
/// A that represents the ID of the session to close.
///
///
- /// A that represents the status code for closure.
+ /// A that indicates the status code for closure.
///
///
/// A that represents the reason for closure.
@@ -640,15 +622,15 @@ namespace WebSocketSharp.Server
}
///
- /// Closes the session with the specified ,
- /// and .
+ /// Closes the session with the specified , , and
+ /// .
///
///
/// A that represents the ID of the session to close.
///
///
- /// One of the values that indicate the status
- /// codes for closure.
+ /// One of the enum values, indicates the status code for
+ /// closure.
///
///
/// A that represents the reason for closure.
@@ -661,16 +643,14 @@ namespace WebSocketSharp.Server
}
///
- /// Sends a Ping to the client associated with the specified
- /// .
+ /// Sends a Ping to the client on the session with the specified .
///
///
- /// true if the WebSocket service receives a Pong from the client in a
- /// time; otherwise, false.
+ /// true if the manager receives a Pong from the client in a time; otherwise,
+ /// false.
///
///
- /// A that represents the ID of the session to send the
- /// Ping to.
+ /// A that represents the ID of the session to find.
///
public bool PingTo (string id)
{
@@ -680,16 +660,15 @@ namespace WebSocketSharp.Server
}
///
- /// Sends a Ping with the specified to the client
- /// associated with the specified .
+ /// Sends a Ping with the specified to the client on the session
+ /// with the specified .
///
///
- /// true if the WebSocket service receives a Pong from the client in a
- /// time; otherwise, false.
+ /// true if the manager receives a Pong from the client in a time; otherwise,
+ /// false.
///
///
- /// A that represents the ID of the session to send the
- /// Ping to.
+ /// A that represents the ID of the session to find.
///
///
/// A that represents the message to send.
@@ -702,8 +681,8 @@ namespace WebSocketSharp.Server
}
///
- /// Sends a binary to the client on the session
- /// with the specified .
+ /// Sends a binary to the client on the session with the specified
+ /// .
///
///
/// A that represents the ID of the session to find.
@@ -719,12 +698,11 @@ namespace WebSocketSharp.Server
}
///
- /// Sends a text to the client on the session
- /// with the specified .
+ /// Sends a text to the client on the session with the specified
+ /// .
///
///
/// A that represents the ID of the session to find.
- /// data to.
///
///
/// A that represents the text data to send.
@@ -737,8 +715,8 @@ namespace WebSocketSharp.Server
}
///
- /// Sends a binary asynchronously to the client
- /// on the session with the specified .
+ /// Sends a binary asynchronously to the client on the session with the
+ /// specified .
///
///
/// This method doesn't wait for the send to be complete.
@@ -750,9 +728,8 @@ namespace WebSocketSharp.Server
/// An array of that represents the binary data to send.
///
///
- /// An Action<bool> delegate that references the method(s) called when
- /// the send is complete.
- /// A passed to this delegate is true if the send is
+ /// An Action<bool> delegate that references the method(s) called when the send is
+ /// complete. A passed to this delegate is true if the send is
/// complete successfully; otherwise, false.
///
public void SendToAsync (string id, byte [] data, Action completed)
@@ -763,8 +740,8 @@ namespace WebSocketSharp.Server
}
///
- /// Sends a text asynchronously to the client
- /// on the session with the specified .
+ /// Sends a text asynchronously to the client on the session with the
+ /// specified .
///
///
/// This method doesn't wait for the send to be complete.
@@ -776,9 +753,8 @@ namespace WebSocketSharp.Server
/// A that represents the text data to send.
///
///
- /// An Action<bool> delegate that references the method(s) called when
- /// the send is complete.
- /// A passed to this delegate is true if the send is
+ /// An Action<bool> delegate that references the method(s) called when the send is
+ /// complete. A passed to this delegate is true if the send is
/// complete successfully; otherwise, false.
///
public void SendToAsync (string id, string data, Action completed)
@@ -789,8 +765,8 @@ namespace WebSocketSharp.Server
}
///
- /// Sends a binary data from the specified asynchronously
- /// to the client on the session with the specified .
+ /// Sends a binary data from the specified asynchronously to the client on
+ /// the session with the specified .
///
///
/// This method doesn't wait for the send to be complete.
@@ -805,9 +781,8 @@ namespace WebSocketSharp.Server
/// An that represents the number of bytes to send.
///
///
- /// An Action<bool> delegate that references the method(s) called when
- /// the send is complete.
- /// A passed to this delegate is true if the send is
+ /// An Action<bool> delegate that references the method(s) called when the send is
+ /// complete. A passed to this delegate is true if the send is
/// complete successfully; otherwise, false.
///
public void SendToAsync (
@@ -819,7 +794,7 @@ namespace WebSocketSharp.Server
}
///
- /// Cleans up the inactive sessions.
+ /// Cleans up the inactive sessions in the WebSocket service.
///
public void Sweep ()
{
@@ -851,19 +826,19 @@ namespace WebSocketSharp.Server
}
///
- /// Tries to get a WebSocket session information with the specified
- /// .
+ /// Tries to get a session information with the specified in the
+ /// WebSocket service.
///
///
/// true if the session is successfully found; otherwise, false.
///
///
- /// A that represents the ID of the session to get.
+ /// A that represents the ID of the session to find.
///
///
- /// When this method returns, a instance that
- /// represents the session if it's successfully found; otherwise,
- /// . This parameter is passed uninitialized.
+ /// When this method returns, a instance that represents the
+ /// session information if it's successfully found; otherwise, . This
+ /// parameter is passed uninitialized.
///
public bool TryGetSession (string id, out IWebSocketSession session)
{