Refactored WebSocketServiceHostManager.cs

This commit is contained in:
sta 2014-01-10 15:45:04 +09:00
parent 418ec8bc2f
commit d188670fb2

View File

@ -37,7 +37,7 @@ using WebSocketSharp.Net;
namespace WebSocketSharp.Server namespace WebSocketSharp.Server
{ {
/// <summary> /// <summary>
/// Manages the WebSocket services provided by the <see cref="HttpServer"/> and /// Manages the WebSocket services provided by the <see cref="HttpServer"/> or
/// <see cref="WebSocketServer"/>. /// <see cref="WebSocketServer"/>.
/// </summary> /// </summary>
public class WebSocketServiceHostManager public class WebSocketServiceHostManager
@ -73,15 +73,15 @@ namespace WebSocketSharp.Server
#region Public Properties #region Public Properties
/// <summary> /// <summary>
/// Gets the number of the WebSocket services provided by the WebSocket server. /// Gets the number of the WebSocket services provided by the server.
/// </summary> /// </summary>
/// <value> /// <value>
/// An <see cref="int"/> that contains the number of the WebSocket services. /// An <see cref="int"/> that represents the number of the WebSocket services
/// provided by the server.
/// </value> /// </value>
public int Count { public int Count {
get { get {
lock (_sync) lock (_sync) {
{
return _serviceHosts.Count; return _serviceHosts.Count;
} }
} }
@ -91,11 +91,13 @@ namespace WebSocketSharp.Server
/// Gets a WebSocket service host with the specified <paramref name="servicePath"/>. /// Gets a WebSocket service host with the specified <paramref name="servicePath"/>.
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="WebSocketServiceHost"/> instance that represents the service host /// A <see cref="WebSocketServiceHost"/> instance that represents the
/// if the service is successfully found; otherwise, <see langword="null"/>. /// WebSocket service host if it's successfully found; otherwise,
/// <see langword="null"/>.
/// </value> /// </value>
/// <param name="servicePath"> /// <param name="servicePath">
/// A <see cref="string"/> that contains an absolute path to the service to find. /// A <see cref="string"/> that represents the absolute path to the WebSocket
/// service to find.
/// </param> /// </param>
public WebSocketServiceHost this [string servicePath] { public WebSocketServiceHost this [string servicePath] {
get { get {
@ -107,12 +109,12 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Gets a value indicating whether the manager cleans up periodically the every inactive session /// Gets a value indicating whether the manager cleans up periodically the
/// to the WebSocket services provided by the WebSocket server. /// every inactive session to the WebSocket services provided by the server.
/// </summary> /// </summary>
/// <value> /// <value>
/// <c>true</c> if the manager cleans up periodically the every inactive session to the WebSocket /// <c>true</c> if the manager cleans up periodically the every inactive
/// services; otherwise, <c>false</c>. /// session to the WebSocket services; otherwise, <c>false</c>.
/// </value> /// </value>
public bool KeepClean { public bool KeepClean {
get { get {
@ -120,8 +122,7 @@ namespace WebSocketSharp.Server
} }
internal set { internal set {
lock (_sync) lock (_sync) {
{
if (!(value ^ _keepClean)) if (!(value ^ _keepClean))
return; return;
@ -133,48 +134,47 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Gets the collection of the WebSocket service hosts managed by the WebSocket server. /// Gets the collection of the WebSocket service hosts managed 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
/// service hosts. /// the WebSocket service hosts.
/// </value> /// </value>
public IEnumerable<WebSocketServiceHost> ServiceHosts { public IEnumerable<WebSocketServiceHost> ServiceHosts {
get { get {
lock (_sync) lock (_sync) {
{
return _serviceHosts.Values.ToList (); return _serviceHosts.Values.ToList ();
} }
} }
} }
/// <summary> /// <summary>
/// Gets the collection of every path to the WebSocket services provided by the WebSocket server. /// Gets the collection of every path to the WebSocket services provided by
/// the server.
/// </summary> /// </summary>
/// <value> /// <value>
/// An IEnumerable&lt;string&gt; that contains the collection of every path to the WebSocket services. /// An IEnumerable&lt;string&gt; that contains the collection of every path to
/// the WebSocket services.
/// </value> /// </value>
public IEnumerable<string> ServicePaths { public IEnumerable<string> ServicePaths {
get { get {
lock (_sync) lock (_sync) {
{
return _serviceHosts.Keys.ToList (); return _serviceHosts.Keys.ToList ();
} }
} }
} }
/// <summary> /// <summary>
/// Gets the number of the sessions to the every WebSocket service /// Gets the number of the sessions to the every WebSocket service provided by
/// provided by the WebSocket server. /// the server.
/// </summary> /// </summary>
/// <value> /// <value>
/// An <see cref="int"/> that contains the session count of the WebSocket server. /// An <see cref="int"/> that represents the session count of the server.
/// </value> /// </value>
public int SessionCount { public int SessionCount {
get { get {
var count = 0; var count = 0;
foreach (var host in ServiceHosts) foreach (var host in ServiceHosts) {
{
if (_state != ServerState.START) if (_state != ServerState.START)
break; break;
@ -193,8 +193,7 @@ namespace WebSocketSharp.Server
{ {
var cache = new Dictionary<CompressionMethod, byte []> (); var cache = new Dictionary<CompressionMethod, byte []> ();
try { try {
foreach (var host in ServiceHosts) foreach (var host in ServiceHosts) {
{
if (_state != ServerState.START) if (_state != ServerState.START)
break; break;
@ -216,8 +215,7 @@ namespace WebSocketSharp.Server
{ {
var cache = new Dictionary<CompressionMethod, Stream> (); var cache = new Dictionary<CompressionMethod, Stream> ();
try { try {
foreach (var host in ServiceHosts) foreach (var host in ServiceHosts) {
{
if (_state != ServerState.START) if (_state != ServerState.START)
break; break;
@ -240,33 +238,26 @@ namespace WebSocketSharp.Server
private void broadcastAsync (Opcode opcode, byte [] data, Action completed) private void broadcastAsync (Opcode opcode, byte [] data, Action completed)
{ {
WaitCallback callback = state => ThreadPool.QueueUserWorkItem (
{ state => broadcast (opcode, data, completed));
broadcast (opcode, data, completed);
};
ThreadPool.QueueUserWorkItem (callback);
} }
private void broadcastAsync (Opcode opcode, Stream stream, Action completed) private void broadcastAsync (Opcode opcode, Stream stream, Action completed)
{ {
WaitCallback callback = state => ThreadPool.QueueUserWorkItem (
{ state => broadcast (opcode, stream, completed));
broadcast (opcode, stream, completed);
};
ThreadPool.QueueUserWorkItem (callback);
} }
private Dictionary<string, Dictionary<string, bool>> broadping (byte [] frameAsBytes, int timeOut) private Dictionary<string, Dictionary<string, bool>> broadping (
byte [] frameAsBytes, int timeOut)
{ {
var result = new Dictionary<string, Dictionary<string, bool>> (); var result = new Dictionary<string, Dictionary<string, bool>> ();
foreach (var host in ServiceHosts) foreach (var host in ServiceHosts) {
{
if (_state != ServerState.START) if (_state != ServerState.START)
break; break;
result.Add (host.ServicePath, host.Sessions.Broadping (frameAsBytes, timeOut)); result.Add (
host.ServicePath, host.Sessions.Broadping (frameAsBytes, timeOut));
} }
return result; return result;
@ -278,11 +269,9 @@ namespace WebSocketSharp.Server
internal void Add (string servicePath, WebSocketServiceHost serviceHost) internal void Add (string servicePath, WebSocketServiceHost serviceHost)
{ {
lock (_sync) lock (_sync) {
{
WebSocketServiceHost host; WebSocketServiceHost host;
if (_serviceHosts.TryGetValue (servicePath, out host)) if (_serviceHosts.TryGetValue (servicePath, out host)) {
{
_logger.Error ( _logger.Error (
"A WebSocket service with the specified path already exists.\npath: " + servicePath); "A WebSocket service with the specified path already exists.\npath: " + servicePath);
return; return;
@ -298,11 +287,10 @@ namespace WebSocketSharp.Server
internal bool Remove (string servicePath) internal bool Remove (string servicePath)
{ {
servicePath = HttpUtility.UrlDecode (servicePath).TrimEndSlash (); servicePath = HttpUtility.UrlDecode (servicePath).TrimEndSlash ();
WebSocketServiceHost host; WebSocketServiceHost host;
lock (_sync) lock (_sync) {
{ if (!_serviceHosts.TryGetValue (servicePath, out host)) {
if (!_serviceHosts.TryGetValue (servicePath, out host))
{
_logger.Error ( _logger.Error (
"A WebSocket service with the specified path not found.\npath: " + servicePath); "A WebSocket service with the specified path not found.\npath: " + servicePath);
return false; return false;
@ -312,15 +300,15 @@ namespace WebSocketSharp.Server
} }
if (host.Sessions.State == ServerState.START) if (host.Sessions.State == ServerState.START)
host.Sessions.Stop (((ushort) CloseStatusCode.AWAY).ToByteArrayInternally (ByteOrder.BIG), true); host.Sessions.Stop (
((ushort) CloseStatusCode.AWAY).ToByteArrayInternally (ByteOrder.BIG), true);
return true; return true;
} }
internal void Start () internal void Start ()
{ {
lock (_sync) lock (_sync) {
{
foreach (var host in _serviceHosts.Values) foreach (var host in _serviceHosts.Values)
host.Sessions.Start (); host.Sessions.Start ();
@ -330,15 +318,15 @@ namespace WebSocketSharp.Server
internal void Stop (byte [] data, bool send) internal void Stop (byte [] data, bool send)
{ {
lock (_sync) lock (_sync) {
{
_state = ServerState.SHUTDOWN; _state = ServerState.SHUTDOWN;
var payload = new PayloadData (data); var payload = new PayloadData (data);
var args = new CloseEventArgs (payload); var args = new CloseEventArgs (payload);
var frameAsBytes = send var frameAsBytes =
? WsFrame.CreateCloseFrame (Mask.UNMASK, payload).ToByteArray () send
: null; ? WsFrame.CreateCloseFrame (Mask.UNMASK, payload).ToByteArray ()
: null;
foreach (var host in _serviceHosts.Values) foreach (var host in _serviceHosts.Values)
host.Sessions.Stop (args, frameAsBytes); host.Sessions.Stop (args, frameAsBytes);
@ -349,17 +337,19 @@ namespace WebSocketSharp.Server
} }
} }
internal bool TryGetServiceHostInternally (string servicePath, out WebSocketServiceHost serviceHost) internal bool TryGetServiceHostInternally (
string servicePath, out WebSocketServiceHost serviceHost)
{ {
servicePath = HttpUtility.UrlDecode (servicePath).TrimEndSlash (); servicePath = HttpUtility.UrlDecode (servicePath).TrimEndSlash ();
bool result; bool result;
lock (_sync) lock (_sync) {
{
result = _serviceHosts.TryGetValue (servicePath, out serviceHost); result = _serviceHosts.TryGetValue (servicePath, out serviceHost);
} }
if (!result) if (!result)
_logger.Error ("A WebSocket service with the specified path not found.\npath: " + servicePath); _logger.Error (
"A WebSocket service with the specified path not found.\npath: " + servicePath);
return result; return result;
} }
@ -369,54 +359,66 @@ namespace WebSocketSharp.Server
#region Public Methods #region Public Methods
/// <summary> /// <summary>
/// Broadcasts a binary <paramref name="data"/> to all clients of the WebSocket services /// Broadcasts a binary <paramref name="data"/> to all clients of the
/// provided by a WebSocket server. /// WebSocket services provided by the server.
/// </summary> /// </summary>
/// <remarks>
/// This method does not wait for the broadcast to be complete.
/// </remarks>
/// <param name="data"> /// <param name="data">
/// An array of <see cref="byte"/> that contains a binary data to broadcast. /// An array of <see cref="byte"/> that contains the binary data to broadcast.
/// </param> /// </param>
public void Broadcast (byte [] data) public void Broadcast (byte [] data)
{ {
Broadcast (data, null); var msg = _state.CheckIfStarted () ?? data.CheckIfValidSendData ();
if (msg != null) {
_logger.Error (msg);
return;
}
if (data.LongLength <= WebSocket.FragmentLength)
broadcast (Opcode.BINARY, data, null);
else
broadcast (Opcode.BINARY, new MemoryStream (data), null);
} }
/// <summary> /// <summary>
/// Broadcasts a text <paramref name="data"/> to all clients of the WebSocket services /// Broadcasts a text <paramref name="data"/> to all clients of the WebSocket
/// provided by a WebSocket server. /// services provided by the server.
/// </summary> /// </summary>
/// <remarks>
/// This method does not wait for the broadcast to be complete.
/// </remarks>
/// <param name="data"> /// <param name="data">
/// A <see cref="string"/> that contains a text data to broadcast. /// A <see cref="string"/> that represents the text data to broadcast.
/// </param> /// </param>
public void Broadcast (string data) public void Broadcast (string data)
{ {
Broadcast (data, null); var msg = _state.CheckIfStarted () ?? data.CheckIfValidSendData ();
if (msg != null) {
_logger.Error (msg);
return;
}
var rawData = Encoding.UTF8.GetBytes (data);
if (rawData.LongLength <= WebSocket.FragmentLength)
broadcast (Opcode.TEXT, rawData, null);
else
broadcast (Opcode.TEXT, new MemoryStream (rawData), null);
} }
/// <summary> /// <summary>
/// Broadcasts a binary <paramref name="data"/> to all clients of the WebSocket services /// Broadcasts a binary <paramref name="data"/> asynchronously to all clients
/// provided by a WebSocket server. /// of the WebSocket services provided by the server.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method does not wait for the broadcast to be complete. /// This method doesn't wait for the broadcast to be complete.
/// </remarks> /// </remarks>
/// <param name="data"> /// <param name="data">
/// An array of <see cref="byte"/> that contains a binary data to broadcast. /// An array of <see cref="byte"/> that contains the binary data to broadcast.
/// </param> /// </param>
/// <param name="completed"> /// <param name="completed">
/// A <see cref="Action"/> delegate that references the method(s) called when /// A <see cref="Action"/> delegate that references the method(s) called when
/// the broadcast is complete. /// the broadcast is complete.
/// </param> /// </param>
public void Broadcast (byte [] data, Action completed) public void BroadcastAsync (byte [] data, Action completed)
{ {
var msg = _state.CheckIfStarted () ?? data.CheckIfValidSendData (); var msg = _state.CheckIfStarted () ?? data.CheckIfValidSendData ();
if (msg != null) if (msg != null) {
{
_logger.Error (msg); _logger.Error (msg);
return; return;
} }
@ -428,24 +430,23 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Broadcasts a text <paramref name="data"/> to all clients of the WebSocket services /// Broadcasts a text <paramref name="data"/> asynchronously to all clients of
/// provided by a WebSocket server. /// the WebSocket services provided by the server.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method does not wait for the broadcast to be complete. /// This method doesn't wait for the broadcast to be complete.
/// </remarks> /// </remarks>
/// <param name="data"> /// <param name="data">
/// A <see cref="string"/> that contains a text data to broadcast. /// A <see cref="string"/> that represents the text data to broadcast.
/// </param> /// </param>
/// <param name="completed"> /// <param name="completed">
/// A <see cref="Action"/> delegate that references the method(s) called when /// A <see cref="Action"/> delegate that references the method(s) called when
/// the broadcast is complete. /// the broadcast is complete.
/// </param> /// </param>
public void Broadcast (string data, Action completed) public void BroadcastAsync (string data, Action completed)
{ {
var msg = _state.CheckIfStarted () ?? data.CheckIfValidSendData (); var msg = _state.CheckIfStarted () ?? data.CheckIfValidSendData ();
if (msg != null) if (msg != null) {
{
_logger.Error (msg); _logger.Error (msg);
return; return;
} }
@ -458,92 +459,74 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Broadcasts a binary data from the specified <see cref="Stream"/> to all clients /// Broadcasts a binary data from the specified <see cref="Stream"/>
/// of the WebSocket services provided by a WebSocket server. /// asynchronously to all clients of the WebSocket services provided by the
/// server.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method does not wait for the broadcast to be complete. /// This method doesn't wait for the broadcast to be complete.
/// </remarks> /// </remarks>
/// <param name="stream"> /// <param name="stream">
/// A <see cref="Stream"/> object from which contains a binary data to broadcast. /// A <see cref="Stream"/> object from which contains the binary data to
/// broadcast.
/// </param> /// </param>
/// <param name="length"> /// <param name="length">
/// An <see cref="int"/> that contains the number of bytes to broadcast. /// An <see cref="int"/> that represents the number of bytes to broadcast.
/// </param>
public void Broadcast (Stream stream, int length)
{
Broadcast (stream, length, null);
}
/// <summary>
/// Broadcasts a binary data from the specified <see cref="Stream"/> to all clients
/// of the WebSocket services provided by a WebSocket server.
/// </summary>
/// <remarks>
/// This method does not wait for the broadcast to be complete.
/// </remarks>
/// <param name="stream">
/// A <see cref="Stream"/> object from which contains a binary data to broadcast.
/// </param>
/// <param name="length">
/// An <see cref="int"/> that contains the number of bytes to broadcast.
/// </param> /// </param>
/// <param name="completed"> /// <param name="completed">
/// A <see cref="Action"/> delegate that references the method(s) called when /// A <see cref="Action"/> delegate that references the method(s) called when
/// the broadcast is complete. /// the broadcast is complete.
/// </param> /// </param>
public void Broadcast (Stream stream, int length, Action completed) public void BroadcastAsync (Stream stream, int length, Action completed)
{ {
var msg = _state.CheckIfStarted () ?? var msg = _state.CheckIfStarted () ??
stream.CheckIfCanRead () ?? stream.CheckIfCanRead () ??
(length < 1 ? "'length' must be greater than 0." : null); (length < 1 ? "'length' must be greater than 0." : null);
if (msg != null) if (msg != null) {
{
_logger.Error (msg); _logger.Error (msg);
return; return;
} }
stream.ReadBytesAsync ( stream.ReadBytesAsync (
length, length,
data => data => {
{
var len = data.Length; var len = data.Length;
if (len == 0) if (len == 0) {
{
_logger.Error ("A data cannot be read from 'stream'."); _logger.Error ("A data cannot be read from 'stream'.");
return; return;
} }
if (len < length) if (len < length)
_logger.Warn (String.Format ( _logger.Warn (
"A data with 'length' cannot be read from 'stream'.\nexpected: {0} actual: {1}", String.Format (
length, "A data with 'length' cannot be read from 'stream'.\nexpected: {0} actual: {1}",
len)); length,
len));
if (len <= WebSocket.FragmentLength) if (len <= WebSocket.FragmentLength)
broadcast (Opcode.BINARY, data, completed); broadcast (Opcode.BINARY, data, completed);
else else
broadcast (Opcode.BINARY, new MemoryStream (data), completed); broadcast (Opcode.BINARY, new MemoryStream (data), completed);
}, },
ex => ex => {
{
_logger.Fatal (ex.ToString ()); _logger.Fatal (ex.ToString ());
}); });
} }
/// <summary> /// <summary>
/// Broadcasts a binary <paramref name="data"/> to all clients of a WebSocket service /// Broadcasts a binary <paramref name="data"/> to all clients of the
/// with the specified <paramref name="servicePath"/>. /// WebSocket service with the specified <paramref name="servicePath"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method does not wait for the broadcast to be complete. /// This method doesn't wait for the broadcast to be complete.
/// </remarks> /// </remarks>
/// <param name="servicePath"> /// <param name="servicePath">
/// A <see cref="string"/> that contains an 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="data"> /// <param name="data">
/// An array of <see cref="byte"/> that contains a binary data to broadcast. /// An array of <see cref="byte"/> that contains the binary data to broadcast.
/// </param> /// </param>
public void BroadcastTo (string servicePath, byte [] data) public void BroadcastTo (string servicePath, byte [] data)
{ {
@ -551,17 +534,18 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Broadcasts a text <paramref name="data"/> to all clients of a WebSocket service /// Broadcasts a text <paramref name="data"/> to all clients of the WebSocket
/// with the specified <paramref name="servicePath"/>. /// service with the specified <paramref name="servicePath"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method does not wait for the broadcast to be complete. /// This method doesn't wait for the broadcast to be complete.
/// </remarks> /// </remarks>
/// <param name="servicePath"> /// <param name="servicePath">
/// A <see cref="string"/> that contains an 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="data"> /// <param name="data">
/// A <see cref="string"/> that contains a text data to broadcast. /// A <see cref="string"/> that represents the text data to broadcast.
/// </param> /// </param>
public void BroadcastTo (string servicePath, string data) public void BroadcastTo (string servicePath, string data)
{ {
@ -569,17 +553,18 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Broadcasts a binary <paramref name="data"/> to all clients of a WebSocket service /// Broadcasts a binary <paramref name="data"/> to all clients of the
/// with the specified <paramref name="servicePath"/>. /// WebSocket service with the specified <paramref name="servicePath"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method does not wait for the broadcast to be complete. /// This method doesn't wait for the broadcast to be complete.
/// </remarks> /// </remarks>
/// <param name="servicePath"> /// <param name="servicePath">
/// A <see cref="string"/> that contains an 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="data"> /// <param name="data">
/// An array of <see cref="byte"/> that contains a binary data to broadcast. /// An array of <see cref="byte"/> that contains the binary data to broadcast.
/// </param> /// </param>
/// <param name="completed"> /// <param name="completed">
/// A <see cref="Action"/> delegate that references the method(s) called when /// A <see cref="Action"/> delegate that references the method(s) called when
@ -593,17 +578,18 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Broadcasts a text <paramref name="data"/> to all clients of a WebSocket service /// Broadcasts a text <paramref name="data"/> to all clients of the WebSocket
/// with the specified <paramref name="servicePath"/>. /// service with the specified <paramref name="servicePath"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method does not wait for the broadcast to be complete. /// This method doesn't wait for the broadcast to be complete.
/// </remarks> /// </remarks>
/// <param name="servicePath"> /// <param name="servicePath">
/// A <see cref="string"/> that contains an 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="data"> /// <param name="data">
/// A <see cref="string"/> that contains a text data to broadcast. /// A <see cref="string"/> that represents the text data to broadcast.
/// </param> /// </param>
/// <param name="completed"> /// <param name="completed">
/// A <see cref="Action"/> delegate that references the method(s) called when /// A <see cref="Action"/> delegate that references the method(s) called when
@ -617,41 +603,22 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Broadcasts a binary data from the specified <see cref="Stream"/> to all clients /// Broadcasts a binary data from the specified <see cref="Stream"/> to all
/// of a WebSocket service with the specified <paramref name="servicePath"/>. /// clients of the WebSocket service with the specified <paramref name="servicePath"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method does not wait for the broadcast to be complete. /// This method doesn't wait for the broadcast to be complete.
/// </remarks> /// </remarks>
/// <param name="servicePath"> /// <param name="servicePath">
/// A <see cref="string"/> that contains an 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="stream"> /// <param name="stream">
/// A <see cref="Stream"/> object from which contains a binary data to broadcast. /// A <see cref="Stream"/> object from which contains the binary data to
/// broadcast.
/// </param> /// </param>
/// <param name="length"> /// <param name="length">
/// An <see cref="int"/> that contains the number of bytes to broadcast. /// An <see cref="int"/> that represents the number of bytes to broadcast.
/// </param>
public void BroadcastTo (string servicePath, Stream stream, int length)
{
BroadcastTo (servicePath, stream, length, null);
}
/// <summary>
/// Broadcasts a binary data from the specified <see cref="Stream"/> to all clients
/// of a WebSocket service with the specified <paramref name="servicePath"/>.
/// </summary>
/// <remarks>
/// This method does not wait for the broadcast to be complete.
/// </remarks>
/// <param name="servicePath">
/// A <see cref="string"/> that contains an absolute path to the WebSocket service to find.
/// </param>
/// <param name="stream">
/// A <see cref="Stream"/> object from which contains a binary data to broadcast.
/// </param>
/// <param name="length">
/// An <see cref="int"/> that contains the number of bytes to broadcast.
/// </param> /// </param>
/// <param name="completed"> /// <param name="completed">
/// A <see cref="Action"/> delegate that references the method(s) called when /// A <see cref="Action"/> delegate that references the method(s) called when
@ -666,18 +633,19 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Sends Pings to all clients of the WebSocket services provided by a WebSocket server. /// Sends Pings to all clients of the WebSocket services provided by the
/// server.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// A Dictionary&lt;string, Dictionary&lt;string, bool&gt;&gt; that contains the collection of /// A Dictionary&lt;string, Dictionary&lt;string, bool&gt;&gt; that contains
/// service paths and pairs of session ID and value indicating whether each WebSocket service /// the collection of service paths and pairs of session ID and value
/// received a Pong from each client in a time. /// indicating whether each WebSocket service received a Pong from each client
/// in a time.
/// </returns> /// </returns>
public Dictionary<string, Dictionary<string, bool>> Broadping () public Dictionary<string, Dictionary<string, bool>> Broadping ()
{ {
var msg = _state.CheckIfStarted (); var msg = _state.CheckIfStarted ();
if (msg != null) if (msg != null) {
{
_logger.Error (msg); _logger.Error (msg);
return null; return null;
} }
@ -686,17 +654,17 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Sends Pings with the specified <paramref name="message"/> to all clients of the WebSocket /// Sends Pings with the specified <paramref name="message"/> to all clients
/// services provided by a WebSocket server. /// of the WebSocket services provided by the server.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// A Dictionary&lt;string, Dictionary&lt;string, bool&gt;&gt; that contains the collection of /// A Dictionary&lt;string, Dictionary&lt;string, bool&gt;&gt; that contains
/// service paths and pairs of session ID and value indicating whether each WebSocket service /// the collection of service paths and pairs of session ID and value
/// received a Pong from each client in a time. /// indicating whether each WebSocket service received a Pong from each client
/// If <paramref name="message"/> is invalid, returns <see langword="null"/>. /// in a time. If <paramref name="message"/> is invalid, returns <see langword="null"/>.
/// </returns> /// </returns>
/// <param name="message"> /// <param name="message">
/// A <see cref="string"/> that contains a message to broadcast. /// A <see cref="string"/> that represents the message to broadcast.
/// </param> /// </param>
public Dictionary<string, Dictionary<string, bool>> Broadping (string message) public Dictionary<string, Dictionary<string, bool>> Broadping (string message)
{ {
@ -707,26 +675,28 @@ namespace WebSocketSharp.Server
var msg = _state.CheckIfStarted () ?? var msg = _state.CheckIfStarted () ??
(data = Encoding.UTF8.GetBytes (message)).CheckIfValidPingData (); (data = Encoding.UTF8.GetBytes (message)).CheckIfValidPingData ();
if (msg != null) if (msg != null) {
{
_logger.Error (msg); _logger.Error (msg);
return null; return null;
} }
return broadping (WsFrame.CreatePingFrame (Mask.UNMASK, data).ToByteArray (), 1000); return broadping (
WsFrame.CreatePingFrame (Mask.UNMASK, data).ToByteArray (), 1000);
} }
/// <summary> /// <summary>
/// Sends Pings to all clients of a WebSocket service with /// Sends Pings to all clients of the WebSocket service with the specified
/// the specified <paramref name="servicePath"/>. /// <paramref name="servicePath"/>.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// A Dictionary&lt;string, bool&gt; that contains the collection of pairs of session ID and /// A Dictionary&lt;string, bool&gt; that contains the collection of pairs of
/// value indicating whether the WebSocket service received a Pong from each client in a time. /// session ID and value indicating whether the WebSocket service received a
/// If the WebSocket service not found, returns <see langword="null"/>. /// Pong from each client in a time. If the WebSocket service not found,
/// returns <see langword="null"/>.
/// </returns> /// </returns>
/// <param name="servicePath"> /// <param name="servicePath">
/// A <see cref="string"/> that contains an 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>
public Dictionary<string, bool> BroadpingTo (string servicePath) public Dictionary<string, bool> BroadpingTo (string servicePath)
{ {
@ -738,20 +708,23 @@ namespace WebSocketSharp.Server
/// <summary> /// <summary>
/// Sends Pings with the specified <paramref name="message"/> to all clients /// Sends Pings with the specified <paramref name="message"/> to all clients
/// of a WebSocket service with the specified <paramref name="servicePath"/>. /// of the WebSocket service with the specified <paramref name="servicePath"/>.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// A Dictionary&lt;string, bool&gt; that contains the collection of pairs of session ID and /// A Dictionary&lt;string, bool&gt; that contains the collection of pairs of
/// value indicating whether the WebSocket service received a Pong from each client in a time. /// session ID and value indicating whether the WebSocket service received a
/// If the WebSocket service not found, returns <see langword="null"/>. /// Pong from each client in a time. If the WebSocket service not found,
/// returns <see langword="null"/>.
/// </returns> /// </returns>
/// <param name="servicePath"> /// <param name="servicePath">
/// A <see cref="string"/> that contains an 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="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>
public Dictionary<string, bool> BroadpingTo (string servicePath, string message) public Dictionary<string, bool> BroadpingTo (
string servicePath, string message)
{ {
WebSocketServiceHost host; WebSocketServiceHost host;
return TryGetServiceHost (servicePath, out host) return TryGetServiceHost (servicePath, out host)
@ -764,10 +737,11 @@ namespace WebSocketSharp.Server
/// <paramref name="id"/>. /// <paramref name="id"/>.
/// </summary> /// </summary>
/// <param name="servicePath"> /// <param name="servicePath">
/// A <see cref="string"/> that contains an absolute path to a WebSocket service to find. /// A <see cref="string"/> that represents the absolute path to the WebSocket
/// service to find.
/// </param> /// </param>
/// <param name="id"> /// <param name="id">
/// A <see cref="string"/> that contains a session ID to find. /// A <see cref="string"/> that represents the session ID to find.
/// </param> /// </param>
public void CloseSession (string servicePath, string id) public void CloseSession (string servicePath, string id)
{ {
@ -777,22 +751,24 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Closes the session with the specified <paramref name="servicePath"/>, <paramref name="id"/>, /// Closes the session with the specified <paramref name="servicePath"/>,
/// <paramref name="code"/> and <paramref name="reason"/>. /// <paramref name="id"/>, <paramref name="code"/> and <paramref name="reason"/>.
/// </summary> /// </summary>
/// <param name="servicePath"> /// <param name="servicePath">
/// A <see cref="string"/> that contains an absolute path to a WebSocket service to find. /// A <see cref="string"/> that represents the absolute path to the WebSocket
/// service to find.
/// </param> /// </param>
/// <param name="id"> /// <param name="id">
/// A <see cref="string"/> that contains a session ID to find. /// A <see cref="string"/> that represents the session ID to find.
/// </param> /// </param>
/// <param name="code"> /// <param name="code">
/// A <see cref="ushort"/> that contains a status code indicating the reason for closure. /// A <see cref="ushort"/> that indicates the status code for closure.
/// </param> /// </param>
/// <param name="reason"> /// <param name="reason">
/// A <see cref="string"/> that contains the reason for closure. /// A <see cref="string"/> that represents the reason for closure.
/// </param> /// </param>
public void CloseSession (string servicePath, string id, ushort code, string reason) public void CloseSession (
string servicePath, string id, ushort code, string reason)
{ {
WebSocketServiceHost host; WebSocketServiceHost host;
if (TryGetServiceHost (servicePath, out host)) if (TryGetServiceHost (servicePath, out host))
@ -800,22 +776,25 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Closes the session with the specified <paramref name="servicePath"/>, <paramref name="id"/>, /// Closes the session with the specified <paramref name="servicePath"/>,
/// <paramref name="code"/> and <paramref name="reason"/>. /// <paramref name="id"/>, <paramref name="code"/> and <paramref name="reason"/>.
/// </summary> /// </summary>
/// <param name="servicePath"> /// <param name="servicePath">
/// A <see cref="string"/> that contains an absolute path to a WebSocket service to find. /// A <see cref="string"/> that represents the absolute path to the WebSocket
/// service to find.
/// </param> /// </param>
/// <param name="id"> /// <param name="id">
/// A <see cref="string"/> that contains a session ID to find. /// A <see cref="string"/> that represents the session ID to find.
/// </param> /// </param>
/// <param name="code"> /// <param name="code">
/// One of the <see cref="CloseStatusCode"/> values that indicate the status codes for closure. /// One of the <see cref="CloseStatusCode"/> values that indicate the status
/// codes for closure.
/// </param> /// </param>
/// <param name="reason"> /// <param name="reason">
/// A <see cref="string"/> that contains the reason for closure. /// A <see cref="string"/> that represents the reason for closure.
/// </param> /// </param>
public void CloseSession (string servicePath, string id, CloseStatusCode code, string reason) public void CloseSession (
string servicePath, string id, CloseStatusCode code, string reason)
{ {
WebSocketServiceHost host; WebSocketServiceHost host;
if (TryGetServiceHost (servicePath, out host)) if (TryGetServiceHost (servicePath, out host))
@ -823,48 +802,53 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Sends a Ping to the client associated with the specified <paramref name="servicePath"/> /// Sends a Ping to the client associated with the specified
/// and <paramref name="id"/>. /// <paramref name="servicePath"/> and <paramref name="id"/>.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// <c>true</c> if the WebSocket service with <paramref name="servicePath"/> receives /// <c>true</c> if the WebSocket service with <paramref name="servicePath"/>
/// a Pong 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="servicePath"> /// <param name="servicePath">
/// A <see cref="string"/> that contains an 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="id"> /// <param name="id">
/// A <see cref="string"/> that contains a session ID that represents the destination /// A <see cref="string"/> that represents the session ID that represents the
/// for the Ping. /// destination for the Ping.
/// </param> /// </param>
public bool PingTo (string servicePath, string id) public bool PingTo (string servicePath, string id)
{ {
WebSocketServiceHost host; WebSocketServiceHost host;
return TryGetServiceHost (servicePath, out host) && host.Sessions.PingTo (id); return TryGetServiceHost (servicePath, out host) &&
host.Sessions.PingTo (id);
} }
/// <summary> /// <summary>
/// Sends a Ping with the specified <paramref name="message"/> to the client associated /// Sends a Ping with the specified <paramref name="message"/> to the client
/// with the specified <paramref name="servicePath"/> and <paramref name="id"/>. /// associated with the specified <paramref name="servicePath"/> and
/// <paramref name="id"/>.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// <c>true</c> if the WebSocket service with <paramref name="servicePath"/> receives /// <c>true</c> if the WebSocket service with <paramref name="servicePath"/>
/// a Pong 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="servicePath"> /// <param name="servicePath">
/// A <see cref="string"/> that contains an 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="id"> /// <param name="id">
/// A <see cref="string"/> that contains a session ID that represents the destination /// A <see cref="string"/> that represents the session ID that represents the
/// for the Ping. /// destination for the Ping.
/// </param> /// </param>
/// <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>
public bool PingTo (string servicePath, string id, string message) public bool PingTo (string servicePath, string id, string message)
{ {
WebSocketServiceHost host; WebSocketServiceHost host;
return TryGetServiceHost (servicePath, out host) && host.Sessions.PingTo (id, message); return TryGetServiceHost (servicePath, out host) &&
host.Sessions.PingTo (id, message);
} }
/// <summary> /// <summary>
@ -1014,24 +998,27 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Tries to get a WebSocket service host with the specified <paramref name="servicePath"/>. /// Tries to get a WebSocket service host with the specified
/// <paramref name="servicePath"/>.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// <c>true</c> if the service is successfully found; otherwise, <c>false</c>. /// <c>true</c> if the WebSocket service host is successfully found;
/// otherwise, <c>false</c>.
/// </returns> /// </returns>
/// <param name="servicePath"> /// <param name="servicePath">
/// A <see cref="string"/> that contains an absolute path to the service to find. /// A <see cref="string"/> that represents the absolute path to the WebSocket
/// service to find.
/// </param> /// </param>
/// <param name="serviceHost"> /// <param name="serviceHost">
/// When this method returns, a <see cref="WebSocketServiceHost"/> instance that represents /// When this method returns, a <see cref="WebSocketServiceHost"/> instance
/// the service host if the service is successfully found; otherwise, <see langword="null"/>. /// that represents the WebSocket service host if it's successfully found;
/// This parameter is passed uninitialized. /// otherwise, <see langword="null"/>. This parameter is passed uninitialized.
/// </param> /// </param>
public bool TryGetServiceHost (string servicePath, out WebSocketServiceHost serviceHost) public bool TryGetServiceHost (
string servicePath, out WebSocketServiceHost serviceHost)
{ {
var msg = _state.CheckIfStarted () ?? servicePath.CheckIfValidServicePath (); var msg = _state.CheckIfStarted () ?? servicePath.CheckIfValidServicePath ();
if (msg != null) if (msg != null) {
{
_logger.Error (msg); _logger.Error (msg);
serviceHost = null; serviceHost = null;