Refactored WebSocketServiceHostManager.cs

This commit is contained in:
sta 2014-02-13 14:38:13 +09:00
parent f69977a2fe
commit d07265dd8e
3 changed files with 123 additions and 575 deletions

View File

@ -50,7 +50,7 @@ namespace Example2
Console.WriteLine ( Console.WriteLine (
"A WebSocket server listening on port: {0} service paths:", wssv.Port); "A WebSocket server listening on port: {0} service paths:", wssv.Port);
foreach (var path in wssv.WebSocketServices.ServicePaths) foreach (var path in wssv.WebSocketServices.Paths)
Console.WriteLine (" {0}", path); Console.WriteLine (" {0}", path);
} }

View File

@ -55,7 +55,7 @@ namespace Example3
"An HTTP server listening on port: {0} WebSocket service paths:", "An HTTP server listening on port: {0} WebSocket service paths:",
_httpsv.Port); _httpsv.Port);
foreach (var path in _httpsv.WebSocketServices.ServicePaths) foreach (var path in _httpsv.WebSocketServices.Paths)
Console.WriteLine (" {0}", path); Console.WriteLine (" {0}", path);
} }

View File

@ -29,7 +29,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using WebSocketSharp.Net; using WebSocketSharp.Net;
@ -44,9 +43,9 @@ namespace WebSocketSharp.Server
{ {
#region Private Fields #region Private Fields
private Dictionary<string, WebSocketServiceHost> _hosts;
private volatile bool _keepClean; private volatile bool _keepClean;
private Logger _logger; private Logger _logger;
private Dictionary<string, WebSocketServiceHost> _serviceHosts;
private volatile ServerState _state; private volatile ServerState _state;
private object _sync; private object _sync;
@ -62,8 +61,8 @@ namespace WebSocketSharp.Server
internal WebSocketServiceHostManager (Logger logger) internal WebSocketServiceHostManager (Logger logger)
{ {
_logger = logger; _logger = logger;
_hosts = new Dictionary<string, WebSocketServiceHost> ();
_keepClean = true; _keepClean = true;
_serviceHosts = new Dictionary<string, WebSocketServiceHost> ();
_state = ServerState.READY; _state = ServerState.READY;
_sync = new object (); _sync = new object ();
} }
@ -76,45 +75,55 @@ namespace WebSocketSharp.Server
/// Gets the number of the WebSocket services provided by the server. /// Gets the number of the WebSocket services provided by the server.
/// </summary> /// </summary>
/// <value> /// <value>
/// An <see cref="int"/> that represents 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 _hosts.Count;
} }
} }
} }
/// <summary> /// <summary>
/// Gets a WebSocket service host with the specified <paramref name="servicePath"/>. /// Gets the collection of the WebSocket service hosts.
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="WebSocketServiceHost"/> instance that represents the /// An IEnumerable&lt;WebSocketServiceHost&gt; that contains the collection of the WebSocket
/// WebSocket service host if it's successfully found; otherwise, /// service hosts.
/// <see langword="null"/>.
/// </value> /// </value>
/// <param name="servicePath"> public IEnumerable<WebSocketServiceHost> Hosts {
/// A <see cref="string"/> that represents the absolute path to the WebSocket get {
/// service to find. return copyHosts ().Values;
}
}
/// <summary>
/// Gets a WebSocket service host 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"/>.
/// </value>
/// <param name="path">
/// 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 path] {
get { get {
WebSocketServiceHost host; WebSocketServiceHost host;
TryGetServiceHost (servicePath, out host); TryGetServiceHost (path, out host);
return host; return host;
} }
} }
/// <summary> /// <summary>
/// Gets a value indicating whether the manager cleans up periodically the /// Gets a value indicating whether the manager cleans up periodically the inactive sessions in
/// every inactive session to the WebSocket services provided by the server. /// the WebSocket services provided by the server.
/// </summary> /// </summary>
/// <value> /// <value>
/// <c>true</c> if the manager cleans up periodically the every inactive /// <c>true</c> if the manager cleans up the inactive sessions every 60 seconds; otherwise,
/// session to the WebSocket services; otherwise, <c>false</c>. /// <c>false</c>.
/// </value> /// </value>
public bool KeepClean { public bool KeepClean {
get { get {
@ -127,54 +136,35 @@ namespace WebSocketSharp.Server
return; return;
_keepClean = value; _keepClean = value;
foreach (var host in _serviceHosts.Values) foreach (var host in _hosts.Values)
host.KeepClean = value; host.KeepClean = value;
} }
} }
} }
/// <summary> /// <summary>
/// Gets the collection of the WebSocket service hosts managed by the manager. /// Gets the collection of every path to the WebSocket services provided by the server.
/// </summary> /// </summary>
/// <value> /// <value>
/// An IEnumerable&lt;WebSocketServiceHost&gt; that contains the collection of /// An IEnumerable&lt;string&gt; that contains the collection of every path to the WebSocket
/// the WebSocket service hosts. /// services.
/// </value> /// </value>
public IEnumerable<WebSocketServiceHost> ServiceHosts { public IEnumerable<string> Paths {
get { get {
lock (_sync) { return copyHosts ().Keys;
return _serviceHosts.Values.ToList ();
}
} }
} }
/// <summary> /// <summary>
/// Gets the collection of every path to the WebSocket services provided by /// Gets the number of the WebSocket sessions in the server.
/// the server.
/// </summary> /// </summary>
/// <value> /// <value>
/// An IEnumerable&lt;string&gt; that contains the collection of every path to /// An <see cref="int"/> that represents the number of the sessions in the server.
/// the WebSocket services.
/// </value>
public IEnumerable<string> ServicePaths {
get {
lock (_sync) {
return _serviceHosts.Keys.ToList ();
}
}
}
/// <summary>
/// Gets the number of the WebSocket sessions to the server.
/// </summary>
/// <value>
/// An <see cref="int"/> that represents the number of the sessions to 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 Hosts) {
if (_state != ServerState.START) if (_state != ServerState.START)
break; break;
@ -193,7 +183,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 Hosts) {
if (_state != ServerState.START) if (_state != ServerState.START)
break; break;
@ -215,7 +205,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 Hosts) {
if (_state != ServerState.START) if (_state != ServerState.START)
break; break;
@ -249,59 +239,60 @@ namespace WebSocketSharp.Server
} }
private Dictionary<string, Dictionary<string, bool>> broadping ( private Dictionary<string, Dictionary<string, bool>> broadping (
byte [] frameAsBytes, int timeOut) byte [] frame, int millisecondsTimeout)
{ {
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 Hosts) {
if (_state != ServerState.START) if (_state != ServerState.START)
break; break;
result.Add ( result.Add (host.ServicePath, host.Sessions.Broadping (frame, millisecondsTimeout));
host.ServicePath, host.Sessions.Broadping (frameAsBytes, timeOut));
} }
return result; return result;
} }
private string checkIfCanSend (Func<string> checkParams) private Dictionary<string, WebSocketServiceHost> copyHosts ()
{ {
return _state.CheckIfStart () ?? checkParams (); lock (_sync) {
return new Dictionary<string, WebSocketServiceHost> (_hosts);
}
} }
#endregion #endregion
#region Internal Methods #region Internal Methods
internal void Add (string servicePath, WebSocketServiceHost serviceHost) internal void Add (string path, WebSocketServiceHost host)
{ {
lock (_sync) { lock (_sync) {
WebSocketServiceHost host; WebSocketServiceHost find;
if (_serviceHosts.TryGetValue (servicePath, out host)) { if (_hosts.TryGetValue (path, out find)) {
_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: " + path);
return; return;
} }
if (_state == ServerState.START) if (_state == ServerState.START)
serviceHost.Sessions.Start (); host.Sessions.Start ();
_serviceHosts.Add (servicePath, serviceHost); _hosts.Add (path, host);
} }
} }
internal bool Remove (string servicePath) internal bool Remove (string path)
{ {
servicePath = HttpUtility.UrlDecode (servicePath).TrimEndSlash (); path = HttpUtility.UrlDecode (path).TrimEndSlash ();
WebSocketServiceHost host; WebSocketServiceHost host;
lock (_sync) { lock (_sync) {
if (!_serviceHosts.TryGetValue (servicePath, out host)) { if (!_hosts.TryGetValue (path, out host)) {
_logger.Error ( _logger.Error ("A WebSocket service with the specified path not found.\npath: " + path);
"A WebSocket service with the specified path not found.\npath: " + servicePath);
return false; return false;
} }
_serviceHosts.Remove (servicePath); _hosts.Remove (path);
} }
if (host.Sessions.State == ServerState.START) if (host.Sessions.State == ServerState.START)
@ -314,7 +305,7 @@ namespace WebSocketSharp.Server
internal void Start () internal void Start ()
{ {
lock (_sync) { lock (_sync) {
foreach (var host in _serviceHosts.Values) foreach (var host in _hosts.Values)
host.Sessions.Start (); host.Sessions.Start ();
_state = ServerState.START; _state = ServerState.START;
@ -328,33 +319,30 @@ namespace WebSocketSharp.Server
var payload = new PayloadData (data); var payload = new PayloadData (data);
var args = new CloseEventArgs (payload); var args = new CloseEventArgs (payload);
var frameAsBytes = var frameAsBytes = send
send ? WsFrame.CreateCloseFrame (Mask.UNMASK, payload).ToByteArray ()
? WsFrame.CreateCloseFrame (Mask.UNMASK, payload).ToByteArray () : null;
: null;
foreach (var host in _serviceHosts.Values) foreach (var host in _hosts.Values)
host.Sessions.Stop (args, frameAsBytes); host.Sessions.Stop (args, frameAsBytes);
_serviceHosts.Clear (); _hosts.Clear ();
_state = ServerState.STOP; _state = ServerState.STOP;
} }
} }
internal bool TryGetServiceHostInternally ( internal bool TryGetServiceHostInternally (string path, out WebSocketServiceHost host)
string servicePath, out WebSocketServiceHost serviceHost)
{ {
servicePath = HttpUtility.UrlDecode (servicePath).TrimEndSlash (); path = HttpUtility.UrlDecode (path).TrimEndSlash ();
bool result; bool result;
lock (_sync) { lock (_sync) {
result = _serviceHosts.TryGetValue (servicePath, out serviceHost); result = _hosts.TryGetValue (path, out host);
} }
if (!result) if (!result)
_logger.Error ( _logger.Error ("A WebSocket service with the specified path not found.\npath: " + path);
"A WebSocket service with the specified path not found.\npath: " + servicePath);
return result; return result;
} }
@ -364,16 +352,15 @@ namespace WebSocketSharp.Server
#region Public Methods #region Public Methods
/// <summary> /// <summary>
/// Broadcasts a binary <paramref name="data"/> to all clients /// Broadcasts a binary <paramref name="data"/> to every client in the WebSocket services
/// of the WebSocket services provided by the server. /// provided by the server.
/// </summary> /// </summary>
/// <param name="data"> /// <param name="data">
/// An array of <see cref="byte"/> that represents the binary data /// An array of <see cref="byte"/> that represents the binary data to broadcast.
/// to broadcast.
/// </param> /// </param>
public void Broadcast (byte [] data) public void Broadcast (byte [] data)
{ {
var msg = checkIfCanSend (() => data.CheckIfValidSendData ()); var msg = _state.CheckIfStart () ?? data.CheckIfValidSendData ();
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
return; return;
@ -386,15 +373,15 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Broadcasts a text <paramref name="data"/> to all clients /// Broadcasts a text <paramref name="data"/> to every client in the WebSocket services
/// of the WebSocket services provided by the server. /// provided by the server.
/// </summary> /// </summary>
/// <param name="data"> /// <param name="data">
/// A <see cref="string"/> that represents the 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)
{ {
var msg = checkIfCanSend (() => data.CheckIfValidSendData ()); var msg = _state.CheckIfStart () ?? data.CheckIfValidSendData ();
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
return; return;
@ -408,23 +395,22 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Broadcasts a binary <paramref name="data"/> asynchronously to all clients /// Broadcasts a binary <paramref name="data"/> asynchronously to every client in the WebSocket
/// of the WebSocket services provided by the server. /// services provided by the server.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method doesn't 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 represents the binary data /// An array of <see cref="byte"/> that represents the binary data to broadcast.
/// 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
/// the broadcast is complete. /// complete.
/// </param> /// </param>
public void BroadcastAsync (byte [] data, Action completed) public void BroadcastAsync (byte [] data, Action completed)
{ {
var msg = checkIfCanSend (() => data.CheckIfValidSendData ()); var msg = _state.CheckIfStart () ?? data.CheckIfValidSendData ();
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
return; return;
@ -437,8 +423,8 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Broadcasts a text <paramref name="data"/> asynchronously to all clients /// Broadcasts a text <paramref name="data"/> asynchronously to every client in the WebSocket
/// of the WebSocket services provided by the server. /// services provided by the server.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method doesn't wait for the broadcast to be complete. /// This method doesn't wait for the broadcast to be complete.
@ -447,12 +433,12 @@ namespace WebSocketSharp.Server
/// A <see cref="string"/> that represents the 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
/// the broadcast is complete. /// complete.
/// </param> /// </param>
public void BroadcastAsync (string data, Action completed) public void BroadcastAsync (string data, Action completed)
{ {
var msg = checkIfCanSend (() => data.CheckIfValidSendData ()); var msg = _state.CheckIfStart () ?? data.CheckIfValidSendData ();
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
return; return;
@ -466,9 +452,8 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Broadcasts a binary data from the specified <see cref="Stream"/> /// Broadcasts a binary data from the specified <see cref="Stream"/> asynchronously to every
/// asynchronously to all clients of the WebSocket services provided /// client in the WebSocket services provided by the server.
/// by the server.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method doesn't wait for the broadcast to be complete. /// This method doesn't wait for the broadcast to be complete.
@ -480,14 +465,14 @@ namespace WebSocketSharp.Server
/// An <see cref="int"/> that represents the number of bytes to broadcast. /// An <see cref="int"/> that represents 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
/// the broadcast is complete. /// complete.
/// </param> /// </param>
public void BroadcastAsync (Stream stream, int length, Action completed) public void BroadcastAsync (Stream stream, int length, Action completed)
{ {
var msg = checkIfCanSend ( var msg = _state.CheckIfStart () ??
() => 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);
@ -519,134 +504,13 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Broadcasts a binary <paramref name="data"/> to all clients of the /// Sends a Ping to every client in the WebSocket services provided by the server.
/// WebSocket service with the specified <paramref name="servicePath"/>.
/// </summary>
/// <param name="servicePath">
/// A <see cref="string"/> that represents the absolute path to the WebSocket
/// service to find.
/// </param>
/// <param name="data">
/// An array of <see cref="byte"/> that represents the binary data
/// to broadcast.
/// </param>
public void BroadcastTo (string servicePath, byte [] data)
{
WebSocketServiceHost host;
if (TryGetServiceHost (servicePath, out host))
host.Sessions.Broadcast (data);
}
/// <summary>
/// Broadcasts a text <paramref name="data"/> to all clients of the
/// WebSocket service with the specified <paramref name="servicePath"/>.
/// </summary>
/// <param name="servicePath">
/// A <see cref="string"/> that represents the absolute path to the WebSocket
/// service to find.
/// </param>
/// <param name="data">
/// A <see cref="string"/> that represents the text data to broadcast.
/// </param>
public void BroadcastTo (string servicePath, string data)
{
WebSocketServiceHost host;
if (TryGetServiceHost (servicePath, out host))
host.Sessions.Broadcast (data);
}
/// <summary>
/// Broadcasts a binary <paramref name="data"/> asynchronously to all clients
/// of the WebSocket service with the specified <paramref name="servicePath"/>.
/// </summary>
/// <remarks>
/// This method doesn't wait for the broadcast to be complete.
/// </remarks>
/// <param name="servicePath">
/// A <see cref="string"/> that represents the absolute path to the WebSocket
/// service to find.
/// </param>
/// <param name="data">
/// An array of <see cref="byte"/> that represents the binary data
/// to broadcast.
/// </param>
/// <param name="completed">
/// A <see cref="Action"/> delegate that references the method(s) called when
/// the broadcast is complete.
/// </param>
public void BroadcastToAsync (
string servicePath, byte [] data, Action completed)
{
WebSocketServiceHost host;
if (TryGetServiceHost (servicePath, out host))
host.Sessions.BroadcastAsync (data, completed);
}
/// <summary>
/// Broadcasts a text <paramref name="data"/> asynchronously to all clients
/// of the WebSocket service with the specified <paramref name="servicePath"/>.
/// </summary>
/// <remarks>
/// This method doesn't wait for the broadcast to be complete.
/// </remarks>
/// <param name="servicePath">
/// A <see cref="string"/> that represents the absolute path to the WebSocket
/// service to find.
/// </param>
/// <param name="data">
/// A <see cref="string"/> that represents the text data to broadcast.
/// </param>
/// <param name="completed">
/// A <see cref="Action"/> delegate that references the method(s) called when
/// the broadcast is complete.
/// </param>
public void BroadcastToAsync (
string servicePath, string data, Action completed)
{
WebSocketServiceHost host;
if (TryGetServiceHost (servicePath, out host))
host.Sessions.BroadcastAsync (data, completed);
}
/// <summary>
/// Broadcasts a binary data from the specified <see cref="Stream"/>
/// asynchronously to all clients of the WebSocket service with the
/// specified <paramref name="servicePath"/>.
/// </summary>
/// <remarks>
/// This method doesn't wait for the broadcast to be complete.
/// </remarks>
/// <param name="servicePath">
/// A <see cref="string"/> that represents the absolute path to the WebSocket
/// service to find.
/// </param>
/// <param name="stream">
/// A <see cref="Stream"/> from which contains the binary data to broadcast.
/// </param>
/// <param name="length">
/// An <see cref="int"/> that represents the number of bytes to broadcast.
/// </param>
/// <param name="completed">
/// A <see cref="Action"/> delegate that references the method(s) called when
/// the broadcast is complete.
/// </param>
public void BroadcastToAsync (
string servicePath, Stream stream, int length, Action completed)
{
WebSocketServiceHost host;
if (TryGetServiceHost (servicePath, out host))
host.Sessions.BroadcastAsync (stream, length, completed);
}
/// <summary>
/// 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 /// A Dictionary&lt;string, Dictionary&lt;string, bool&gt;&gt; that contains the collection of
/// the collection of service paths and pairs of session ID and value /// pairs of service path and collection of pairs of session ID and value indicating whether
/// indicating whether each WebSocket service received a Pong from each client /// the manager received a Pong from every client in a time. If this method isn't available,
/// in a time. /// returns <see langword="null"/>.
/// </returns> /// </returns>
public Dictionary<string, Dictionary<string, bool>> Broadping () public Dictionary<string, Dictionary<string, bool>> Broadping ()
{ {
@ -660,17 +524,17 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Sends Pings with the specified <paramref name="message"/> to all clients /// Sends a Ping with the specified <paramref name="message"/> to every client in the WebSocket
/// of the WebSocket services provided by the server. /// services provided by the server.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// A Dictionary&lt;string, Dictionary&lt;string, bool&gt;&gt; that contains /// A Dictionary&lt;string, Dictionary&lt;string, bool&gt;&gt; that contains the collection of
/// the collection of service paths and pairs of session ID and value /// pairs of service path and collection of pairs of session ID and value indicating whether
/// indicating whether each WebSocket service received a Pong from each client /// the manager received a Pong from every client in a time. If this method isn't available or
/// in a time. If <paramref name="message"/> is invalid, returns <see langword="null"/>. /// <paramref name="message"/> is invalid, returns <see langword="null"/>.
/// </returns> /// </returns>
/// <param name="message"> /// <param name="message">
/// A <see cref="string"/> that represents the message to broadcast. /// A <see cref="string"/> that represents the message to send.
/// </param> /// </param>
public Dictionary<string, Dictionary<string, bool>> Broadping (string message) public Dictionary<string, Dictionary<string, bool>> Broadping (string message)
{ {
@ -678,358 +542,42 @@ namespace WebSocketSharp.Server
return Broadping (); return Broadping ();
byte [] data = null; byte [] data = null;
var msg = checkIfCanSend ( var msg = _state.CheckIfStart () ??
() => (data = Encoding.UTF8.GetBytes (message)) (data = Encoding.UTF8.GetBytes (message)).CheckIfValidControlData ("message");
.CheckIfValidControlData ("message"));
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
return null; return null;
} }
return broadping ( return broadping (WsFrame.CreatePingFrame (Mask.UNMASK, data).ToByteArray (), 1000);
WsFrame.CreatePingFrame (Mask.UNMASK, data).ToByteArray (), 1000);
} }
/// <summary> /// <summary>
/// Sends Pings to all clients of the WebSocket service with the specified /// Tries to get a WebSocket service host with the specified <paramref name="path"/>.
/// <paramref name="servicePath"/>.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// A Dictionary&lt;string, bool&gt; that contains the collection of pairs of /// <c>true</c> if the WebSocket service is successfully found; otherwise, <c>false</c>.
/// session ID and value indicating whether the WebSocket service received a
/// Pong from each client in a time. If the WebSocket service not found,
/// returns <see langword="null"/>.
/// </returns> /// </returns>
/// <param name="servicePath"> /// <param name="path">
/// A <see cref="string"/> that represents the absolute path to the WebSocket /// A <see cref="string"/> that represents the absolute path to the WebSocket service to find.
/// service to find.
/// </param> /// </param>
public Dictionary<string, bool> BroadpingTo (string servicePath) /// <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.
/// </param>
public bool TryGetServiceHost (string path, out WebSocketServiceHost host)
{ {
WebSocketServiceHost host; var msg = _state.CheckIfStart () ?? path.CheckIfValidServicePath ();
return TryGetServiceHost (servicePath, out host)
? host.Sessions.Broadping ()
: null;
}
/// <summary>
/// Sends Pings with the specified <paramref name="message"/> to all clients
/// of the WebSocket service with the specified <paramref name="servicePath"/>.
/// </summary>
/// <returns>
/// A Dictionary&lt;string, bool&gt; 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. If the WebSocket service not found,
/// returns <see langword="null"/>.
/// </returns>
/// <param name="servicePath">
/// A <see cref="string"/> that represents the absolute path to the WebSocket
/// service to find.
/// </param>
/// <param name="message">
/// A <see cref="string"/> that represents the message to send.
/// </param>
public Dictionary<string, bool> BroadpingTo (
string servicePath, string message)
{
WebSocketServiceHost host;
return TryGetServiceHost (servicePath, out host)
? host.Sessions.Broadping (message)
: null;
}
/// <summary>
/// Closes the session with the specified <paramref name="servicePath"/> and
/// <paramref name="id"/>.
/// </summary>
/// <param name="servicePath">
/// A <see cref="string"/> that represents the absolute path to the WebSocket
/// service to find.
/// </param>
/// <param name="id">
/// A <see cref="string"/> that represents the ID of the session to close.
/// </param>
public void CloseSession (string servicePath, string id)
{
WebSocketServiceHost host;
if (TryGetServiceHost (servicePath, out host))
host.Sessions.CloseSession (id);
}
/// <summary>
/// Closes the session with the specified <paramref name="servicePath"/>,
/// <paramref name="id"/>, <paramref name="code"/> and <paramref name="reason"/>.
/// </summary>
/// <param name="servicePath">
/// A <see cref="string"/> that represents the absolute path to the WebSocket
/// service to find.
/// </param>
/// <param name="id">
/// A <see cref="string"/> that represents the ID of the session to close.
/// </param>
/// <param name="code">
/// A <see cref="ushort"/> that represents the status code for closure.
/// </param>
/// <param name="reason">
/// A <see cref="string"/> that represents the reason for closure.
/// </param>
public void CloseSession (
string servicePath, string id, ushort code, string reason)
{
WebSocketServiceHost host;
if (TryGetServiceHost (servicePath, out host))
host.Sessions.CloseSession (id, code, reason);
}
/// <summary>
/// Closes the session with the specified <paramref name="servicePath"/>,
/// <paramref name="id"/>, <paramref name="code"/> and <paramref name="reason"/>.
/// </summary>
/// <param name="servicePath">
/// A <see cref="string"/> that represents the absolute path to the WebSocket
/// service to find.
/// </param>
/// <param name="id">
/// A <see cref="string"/> that represents the ID of the session to close.
/// </param>
/// <param name="code">
/// One of the <see cref="CloseStatusCode"/> values that indicate the status
/// codes for closure.
/// </param>
/// <param name="reason">
/// A <see cref="string"/> that represents the reason for closure.
/// </param>
public void CloseSession (
string servicePath, string id, CloseStatusCode code, string reason)
{
WebSocketServiceHost host;
if (TryGetServiceHost (servicePath, out host))
host.Sessions.CloseSession (id, code, reason);
}
/// <summary>
/// Sends a Ping to the client associated with the specified
/// <paramref name="servicePath"/> and <paramref name="id"/>.
/// </summary>
/// <returns>
/// <c>true</c> if the WebSocket service with <paramref name="servicePath"/>
/// receives a Pong from the client in a time; otherwise, <c>false</c>.
/// </returns>
/// <param name="servicePath">
/// A <see cref="string"/> that represents the absolute path to the WebSocket
/// service to find.
/// </param>
/// <param name="id">
/// A <see cref="string"/> that represents the ID of the session to send the
/// Ping to.
/// </param>
public bool PingTo (string servicePath, string id)
{
WebSocketServiceHost host;
return TryGetServiceHost (servicePath, out host) &&
host.Sessions.PingTo (id);
}
/// <summary>
/// Sends a Ping with the specified <paramref name="message"/> to the client
/// associated with the specified <paramref name="servicePath"/> and
/// <paramref name="id"/>.
/// </summary>
/// <returns>
/// <c>true</c> if the WebSocket service with <paramref name="servicePath"/>
/// receives a Pong from the client in a time; otherwise, <c>false</c>.
/// </returns>
/// <param name="servicePath">
/// A <see cref="string"/> that represents the absolute path to the WebSocket
/// service to find.
/// </param>
/// <param name="id">
/// A <see cref="string"/> that represents the ID of the session to send the
/// Ping to.
/// </param>
/// <param name="message">
/// A <see cref="string"/> that represents the message to send.
/// </param>
public bool PingTo (string servicePath, string id, string message)
{
WebSocketServiceHost host;
return TryGetServiceHost (servicePath, out host) &&
host.Sessions.PingTo (id, message);
}
/// <summary>
/// Sends a binary <paramref name="data"/> to the client on the session with
/// the specified <paramref name="id"/>, in the WebSocket service with the
/// specified <paramref name="servicePath"/>.
/// </summary>
/// <param name="servicePath">
/// A <see cref="string"/> that represents the absolute path to the WebSocket
/// service to find.
/// </param>
/// <param name="id">
/// A <see cref="string"/> that represents the ID of the session to find.
/// </param>
/// <param name="data">
/// An array of <see cref="byte"/> that represents the binary data to send.
/// </param>
public void SendTo (string servicePath, string id, byte [] data)
{
WebSocketServiceHost host;
if (TryGetServiceHost (servicePath, out host))
host.Sessions.SendTo (id, data);
}
/// <summary>
/// Sends a text <paramref name="data"/> to the client on the session with
/// the specified <paramref name="id"/>, in the WebSocket service with the
/// specified <paramref name="servicePath"/>.
/// </summary>
/// <param name="servicePath">
/// A <see cref="string"/> that represents the absolute path to the WebSocket
/// service to find.
/// </param>
/// <param name="id">
/// A <see cref="string"/> that represents the ID of the session to find.
/// </param>
/// <param name="data">
/// A <see cref="string"/> that represents the text data to send.
/// </param>
public void SendTo (string servicePath, string id, string data)
{
WebSocketServiceHost host;
if (TryGetServiceHost (servicePath, out host))
host.Sessions.SendTo (id, data);
}
/// <summary>
/// Sends a binary <paramref name="data"/> asynchronously to the client on the
/// session with the specified <paramref name="id"/>, in the WebSocket service
/// with the specified <paramref name="servicePath"/>.
/// </summary>
/// <remarks>
/// This method doesn't wait for the send to be complete.
/// </remarks>
/// <param name="servicePath">
/// A <see cref="string"/> that represents the absolute path to the WebSocket
/// service to find.
/// </param>
/// <param name="id">
/// A <see cref="string"/> that represents the ID of the session to find.
/// </param>
/// <param name="data">
/// An array of <see cref="byte"/> that represents the binary data to send.
/// </param>
/// <param name="completed">
/// An Action&lt;bool&gt; delegate that references the method(s) called when
/// the send is complete.
/// A <see cref="bool"/> passed to this delegate is <c>true</c> if the send is
/// complete successfully; otherwise, <c>false</c>.
/// </param>
public void SendToAsync (
string servicePath, string id, byte [] data, Action<bool> completed)
{
WebSocketServiceHost host;
if (TryGetServiceHost (servicePath, out host))
host.Sessions.SendToAsync (id, data, completed);
}
/// <summary>
/// Sends a text <paramref name="data"/> asynchronously to the client on the
/// session with the specified <paramref name="id"/>, in the WebSocket service
/// with the specified <paramref name="servicePath"/>.
/// </summary>
/// <remarks>
/// This method doesn't wait for the send to be complete.
/// </remarks>
/// <param name="servicePath">
/// A <see cref="string"/> that represents the absolute path to the WebSocket
/// service to find.
/// </param>
/// <param name="id">
/// A <see cref="string"/> that represents the ID of the session to find.
/// </param>
/// <param name="data">
/// A <see cref="string"/> that represents the text data to send.
/// </param>
/// <param name="completed">
/// An Action&lt;bool&gt; delegate that references the method(s) called when
/// the send is complete.
/// A <see cref="bool"/> passed to this delegate is <c>true</c> if the send is
/// complete successfully; otherwise, <c>false</c>.
/// </param>
public void SendToAsync (
string servicePath, string id, string data, Action<bool> completed)
{
WebSocketServiceHost host;
if (TryGetServiceHost (servicePath, out host))
host.Sessions.SendToAsync (id, data, completed);
}
/// <summary>
/// Sends a binary data from the specified <see cref="Stream"/> asynchronously
/// to the client on the session with the specified <paramref name="id"/>, in
/// the WebSocket service with the specified <paramref name="servicePath"/>.
/// </summary>
/// <remarks>
/// This method doesn't wait for the send to be complete.
/// </remarks>
/// <param name="servicePath">
/// A <see cref="string"/> that represents the absolute path to the WebSocket
/// service to find.
/// </param>
/// <param name="id">
/// A <see cref="string"/> that represents the ID of the session to find.
/// </param>
/// <param name="stream">
/// A <see cref="Stream"/> from which contains the binary data to send.
/// </param>
/// <param name="length">
/// An <see cref="int"/> that represents the number of bytes to send.
/// </param>
/// <param name="completed">
/// An Action&lt;bool&gt; delegate that references the method(s) called when
/// the send is complete.
/// A <see cref="bool"/> passed to this delegate is <c>true</c> if the send is
/// complete successfully; otherwise, <c>false</c>.
/// </param>
public void SendToAsync (
string servicePath, string id, Stream stream, int length, Action<bool> completed)
{
WebSocketServiceHost host;
if (TryGetServiceHost (servicePath, out host))
host.Sessions.SendToAsync (id, stream, length, completed);
}
/// <summary>
/// Tries to get a WebSocket service host with the specified
/// <paramref name="servicePath"/>.
/// </summary>
/// <returns>
/// <c>true</c> if the WebSocket service host is successfully found;
/// otherwise, <c>false</c>.
/// </returns>
/// <param name="servicePath">
/// A <see cref="string"/> that represents the absolute path to the WebSocket
/// service to find.
/// </param>
/// <param name="serviceHost">
/// 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.
/// </param>
public bool TryGetServiceHost (
string servicePath, out WebSocketServiceHost serviceHost)
{
var msg = _state.CheckIfStart () ?? servicePath.CheckIfValidServicePath ();
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
serviceHost = null; host = null;
return false; return false;
} }
return TryGetServiceHostInternally (servicePath, out serviceHost); return TryGetServiceHostInternally (path, out host);
} }
#endregion #endregion