Renamed IServiceHost.cs to IWebSocketServiceHost.cs

This commit is contained in:
sta 2013-08-29 11:58:03 +09:00
parent b3fa68d8ce
commit 9daaa33111
8 changed files with 202 additions and 234 deletions

View File

@ -387,7 +387,7 @@ namespace WebSocketSharp.Server
var wsContext = context.AcceptWebSocket (); var wsContext = context.AcceptWebSocket ();
var path = wsContext.Path.UrlDecode (); var path = wsContext.Path.UrlDecode ();
IServiceHost host; IWebSocketServiceHost host;
if (!_serviceHosts.TryGetServiceHost (path, out host)) if (!_serviceHosts.TryGetServiceHost (path, out host))
{ {
context.Response.StatusCode = (int) HttpStatusCode.NotImplemented; context.Response.StatusCode = (int) HttpStatusCode.NotImplemented;

View File

@ -1,6 +1,6 @@
#region License #region License
/* /*
* IServiceHost.cs * IWebSocketServiceHost.cs
* *
* The MIT License * The MIT License
* *
@ -35,9 +35,7 @@ namespace WebSocketSharp.Server
/// <summary> /// <summary>
/// Exposes the methods and properties for the WebSocket service host. /// Exposes the methods and properties for the WebSocket service host.
/// </summary> /// </summary>
/// <remarks> public interface IWebSocketServiceHost
/// </remarks>
public interface IServiceHost
{ {
/// <summary> /// <summary>
/// Gets the connection count to the WebSocket service host. /// Gets the connection count to the WebSocket service host.
@ -82,11 +80,12 @@ namespace WebSocketSharp.Server
void Broadcast (string data); void Broadcast (string data);
/// <summary> /// <summary>
/// Sends Pings with the specified <see cref="string"/> to all clients of the WebSocket service host. /// Sends Pings with the specified <paramref name="message"/> to all clients of
/// the WebSocket service host.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// A Dictionary&lt;string, bool&gt; that contains the collection of session IDs and values /// A Dictionary&lt;string, bool&gt; that contains the collection of pairs of session ID and value
/// indicating whether the WebSocket service host received the Pongs from each clients in a time. /// indicating whether the WebSocket service host received the Pong from each client in a time.
/// </returns> /// </returns>
/// <param name="message"> /// <param name="message">
/// A <see cref="string"/> that contains a message to send. /// A <see cref="string"/> that contains a message to send.
@ -94,50 +93,48 @@ namespace WebSocketSharp.Server
Dictionary<string, bool> Broadping (string message); Dictionary<string, bool> Broadping (string message);
/// <summary> /// <summary>
/// Sends a Ping with the specified <see cref="string"/> to the client associated with /// Sends a Ping with the specified <paramref name="message"/> to the client associated with
/// the specified ID. /// the specified ID.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// <c>true</c> if the WebSocket service host receives a Pong from the client in a time; /// <c>true</c> if the WebSocket service host receives a Pong from the client in a time;
/// otherwise, <c>false</c>. /// otherwise, <c>false</c>.
/// </returns> /// </returns>
/// <param name="id">
/// A <see cref="string"/> that contains an ID that represents the destination for the Ping.
/// </param>
/// <param name="message"> /// <param name="message">
/// A <see cref="string"/> that contains a message to send. /// A <see cref="string"/> that contains a message to send.
/// </param> /// </param>
bool PingTo (string id, string message); /// <param name="id">
/// A <see cref="string"/> that contains an ID that represents the destination for the Ping.
/// </param>
bool PingTo (string message, string id);
/// <summary> /// <summary>
/// Sends a binary data to the client associated with the specified ID. /// Sends a binary data to the client associated with the specified ID.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// <c>true</c> if the client associated with <paramref name="id"/> is successfully found; /// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
/// otherwise, <c>false</c>.
/// </returns> /// </returns>
/// <param name="id">
/// A <see cref="string"/> that contains an ID that represents the destination for the data.
/// </param>
/// <param name="data"> /// <param name="data">
/// An array of <see cref="byte"/> that contains a binary data to send. /// An array of <see cref="byte"/> that contains a binary data to send.
/// </param> /// </param>
bool SendTo (string id, byte [] data); /// <param name="id">
/// A <see cref="string"/> that contains an ID that represents the destination for the data.
/// </param>
bool SendTo (byte [] data, string id);
/// <summary> /// <summary>
/// Sends a text data to the client associated with the specified ID. /// Sends a text data to the client associated with the specified ID.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// <c>true</c> if the client associated with <paramref name="id"/> is successfully found; /// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
/// otherwise, <c>false</c>.
/// </returns> /// </returns>
/// <param name="id">
/// A <see cref="string"/> that contains an ID that represents the destination for the data.
/// </param>
/// <param name="data"> /// <param name="data">
/// A <see cref="string"/> that contains a text data to send. /// A <see cref="string"/> that contains a text data to send.
/// </param> /// </param>
bool SendTo (string id, string data); /// <param name="id">
/// A <see cref="string"/> that contains an ID that represents the destination for the data.
/// </param>
bool SendTo (string data, string id);
/// <summary> /// <summary>
/// Starts the WebSocket service host. /// Starts the WebSocket service host.

View File

@ -226,7 +226,7 @@ namespace WebSocketSharp.Server
var path = context.Path.UrlDecode (); var path = context.Path.UrlDecode ();
websocket.Log = Log; websocket.Log = Log;
IServiceHost host; IWebSocketServiceHost host;
if (!_serviceHosts.TryGetServiceHost (path, out host)) if (!_serviceHosts.TryGetServiceHost (path, out host))
{ {
websocket.Close (HttpStatusCode.NotImplemented); websocket.Close (HttpStatusCode.NotImplemented);

View File

@ -349,8 +349,8 @@ namespace WebSocketSharp.Server
/// in the <see cref="WebSocketService.Sessions"/>. /// in the <see cref="WebSocketService.Sessions"/>.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// A Dictionary&lt;string, bool&gt; that contains the collection of IDs and values indicating /// A Dictionary&lt;string, bool&gt; that contains the collection of pairs of session ID and value
/// whether the each <see cref="WebSocketService"/> instances received a Pong in a time. /// indicating whether the each <see cref="WebSocketService"/> instance received a Pong in a time.
/// </returns> /// </returns>
public virtual Dictionary<string, bool> Broadping () public virtual Dictionary<string, bool> Broadping ()
{ {
@ -360,12 +360,12 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Sends Pings with the specified <see cref="string"/> to the clients of every <see cref="WebSocketService"/> /// Sends Pings with the specified <paramref name="message"/> to the clients of every <see cref="WebSocketService"/>
/// instances in the <see cref="WebSocketService.Sessions"/>. /// instances in the <see cref="WebSocketService.Sessions"/>.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// A Dictionary&lt;string, bool&gt; that contains the collection of IDs and values /// A Dictionary&lt;string, bool&gt; that contains the collection of pairs of session ID and value
/// indicating whether the each <see cref="WebSocketService"/> instances received a Pong in a time. /// indicating whether the each <see cref="WebSocketService"/> instance received a Pong in a time.
/// </returns> /// </returns>
/// <param name="message"> /// <param name="message">
/// A <see cref="string"/> that contains a message to send. /// A <see cref="string"/> that contains a message to send.
@ -406,7 +406,7 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Sends a Ping with the specified <see cref="string"/> to the client of /// Sends a Ping with the specified <paramref name="message"/> to the client of
/// the current <see cref="WebSocketService"/> instance. /// the current <see cref="WebSocketService"/> instance.
/// </summary> /// </summary>
/// <returns> /// <returns>
@ -428,54 +428,46 @@ namespace WebSocketSharp.Server
/// with the specified ID. /// with the specified ID.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// <c>true</c> if the <see cref="WebSocketService"/> instance receives a Pong in a time; /// <c>true</c> if the <see cref="WebSocketService"/> instance with <paramref name="id"/> receives
/// otherwise, <c>false</c>. /// a Pong in a time; otherwise, <c>false</c>.
/// </returns> /// </returns>
/// <param name="id"> /// <param name="id">
/// A <see cref="string"/> that contains an ID that represents the destination for the Ping. /// A <see cref="string"/> that contains an ID that represents the destination for the Ping.
/// </param> /// </param>
public virtual bool PingTo (string id) public virtual bool PingTo (string id)
{ {
return PingTo (id, String.Empty); return PingTo (String.Empty, id);
} }
/// <summary> /// <summary>
/// Sends a Ping with the specified <see cref="string"/> to the client of the <see cref="WebSocketService"/> /// Sends a Ping with the specified <paramref name="message"/> to the client of
/// instance with the specified ID. /// the <see cref="WebSocketService"/> instance with the specified ID.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// <c>true</c> if the <see cref="WebSocketService"/> instance receives a Pong in a time; /// <c>true</c> if the <see cref="WebSocketService"/> instance with <paramref name="id"/> receives
/// otherwise, <c>false</c>. /// a Pong in a time; otherwise, <c>false</c>.
/// </returns> /// </returns>
/// <param name="id">
/// A <see cref="string"/> that contains an ID that represents the destination for the Ping.
/// </param>
/// <param name="message"> /// <param name="message">
/// A <see cref="string"/> that contains a message to send. /// A <see cref="string"/> that contains a message to send.
/// </param> /// </param>
public virtual bool PingTo (string id, string message) /// <param name="id">
/// A <see cref="string"/> that contains an ID that represents the destination for the Ping.
/// </param>
public virtual bool PingTo (string message, string id)
{ {
if (!IsBound) if (!IsBound)
return false; return false;
if (message == null) if (id.IsNullOrEmpty ())
message = String.Empty;
var msg = id.IsNullOrEmpty ()
? "'id' must not be null or empty."
: Encoding.UTF8.GetBytes (message).Length > 125
? "The payload length of a Ping frame must be 125 bytes or less."
: String.Empty;
if (msg.Length > 0)
{ {
var msg = "'id' must not be null or empty.";
Log.Error (msg); Log.Error (msg);
Error (msg); Error (msg);
return false; return false;
} }
return _sessions.PingTo (id, message); return _sessions.PingTo (message, id);
} }
/// <summary> /// <summary>
@ -507,26 +499,26 @@ namespace WebSocketSharp.Server
/// with the specified ID. /// with the specified ID.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// <c>true</c> if the client is successfully found; otherwise, <c>false</c>. /// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
/// </returns> /// </returns>
/// <param name="id">
/// A <see cref="string"/> that contains an ID that represents the destination for the data.
/// </param>
/// <param name="data"> /// <param name="data">
/// An array of <see cref="byte"/> that contains a binary data to send. /// An array of <see cref="byte"/> that contains a binary data to send.
/// </param> /// </param>
public virtual bool SendTo (string id, byte [] data) /// <param name="id">
/// A <see cref="string"/> that contains an ID that represents the destination for the data.
/// </param>
public virtual bool SendTo (byte [] data, string id)
{ {
if (!IsBound) if (!IsBound)
return false; return false;
var msg = id.IsNullOrEmpty () var msg = data == null
? "'id' must not be null or empty." ? "'data' must not be null."
: data == null : id.IsNullOrEmpty ()
? "'data' must not be null." ? "'id' must not be null or empty."
: String.Empty; : null;
if (msg.Length > 0) if (msg != null)
{ {
Log.Error (msg); Log.Error (msg);
Error (msg); Error (msg);
@ -534,7 +526,7 @@ namespace WebSocketSharp.Server
return false; return false;
} }
return _sessions.SendTo (id, data); return _sessions.SendTo (data, id);
} }
/// <summary> /// <summary>
@ -542,26 +534,26 @@ namespace WebSocketSharp.Server
/// with the specified ID. /// with the specified ID.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// <c>true</c> if the client is successfully found; otherwise, <c>false</c>. /// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
/// </returns> /// </returns>
/// <param name="id">
/// A <see cref="string"/> that contains an ID that represents the destination for the data.
/// </param>
/// <param name="data"> /// <param name="data">
/// A <see cref="string"/> that contains a text data to send. /// A <see cref="string"/> that contains a text data to send.
/// </param> /// </param>
public virtual bool SendTo (string id, string data) /// <param name="id">
/// A <see cref="string"/> that contains an ID that represents the destination for the data.
/// </param>
public virtual bool SendTo (string data, string id)
{ {
if (!IsBound) if (!IsBound)
return false; return false;
var msg = id.IsNullOrEmpty () var msg = data == null
? "'id' must not be null or empty." ? "'data' must not be null."
: data == null : id.IsNullOrEmpty ()
? "'data' must not be null." ? "'id' must not be null or empty."
: String.Empty; : null;
if (msg.Length > 0) if (msg != null)
{ {
Log.Error (msg); Log.Error (msg);
Error (msg); Error (msg);
@ -569,11 +561,11 @@ namespace WebSocketSharp.Server
return false; return false;
} }
return _sessions.SendTo (id, data); return _sessions.SendTo (data, id);
} }
/// <summary> /// <summary>
/// Starts a <see cref="WebSocketService"/> instance. /// Starts the current <see cref="WebSocketService"/> instance.
/// </summary> /// </summary>
public void Start () public void Start ()
{ {

View File

@ -47,7 +47,7 @@ namespace WebSocketSharp.Server
/// The type of the WebSocket service that the server provides. /// The type of the WebSocket service that the server provides.
/// The T must inherit the <see cref="WebSocketService"/> class. /// The T must inherit the <see cref="WebSocketService"/> class.
/// </typeparam> /// </typeparam>
public class WebSocketServiceHost<T> : WebSocketServerBase, IServiceHost public class WebSocketServiceHost<T> : WebSocketServerBase, IWebSocketServiceHost
where T : WebSocketService, new () where T : WebSocketService, new ()
{ {
#region Private Fields #region Private Fields
@ -267,20 +267,20 @@ namespace WebSocketSharp.Server
/// </param> /// </param>
protected override void AcceptWebSocket (TcpListenerWebSocketContext context) protected override void AcceptWebSocket (TcpListenerWebSocketContext context)
{ {
var ws = context.WebSocket; var websocket = context.WebSocket;
var path = context.Path.UrlDecode (); var path = context.Path.UrlDecode ();
ws.Log = Log; websocket.Log = Log;
if (path != Uri.GetAbsolutePath ().UrlDecode ()) if (path != Uri.GetAbsolutePath ().UrlDecode ())
{ {
ws.Close (HttpStatusCode.NotImplemented); websocket.Close (HttpStatusCode.NotImplemented);
return; return;
} }
if (Uri.IsAbsoluteUri) if (Uri.IsAbsoluteUri)
ws.Url = Uri; websocket.Url = Uri;
((IServiceHost) this).BindWebSocket (context); ((IWebSocketServiceHost) this).BindWebSocket (context);
} }
#endregion #endregion
@ -322,11 +322,11 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Sends Pings with the specified <see cref="string"/> to all clients. /// Sends Pings with the specified <paramref name="message"/> to all clients.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// A Dictionary&lt;string, bool&gt; that contains the collection of session IDs and values /// A Dictionary&lt;string, bool&gt; that contains the collection of pairs of session ID and value
/// indicating whether the service host received the Pongs from each clients in a time. /// indicating whether the service host received the Pong from each client in a time.
/// </returns> /// </returns>
/// <param name="message"> /// <param name="message">
/// A <see cref="string"/> that contains a message to send. /// A <see cref="string"/> that contains a message to send.
@ -347,58 +347,48 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Sends a Ping with the specified <see cref="string"/> to the client associated with /// Sends a Ping with the specified <paramref name="message"/> to the client associated with
/// the specified ID. /// the specified ID.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// <c>true</c> if the service host receives a Pong from the client in a time; otherwise, <c>false</c>. /// <c>true</c> if the service host receives a Pong from the client in a time; otherwise, <c>false</c>.
/// </returns> /// </returns>
/// <param name="id">
/// A <see cref="string"/> that contains an ID that represents the destination for the Ping.
/// </param>
/// <param name="message"> /// <param name="message">
/// A <see cref="string"/> that contains a message to send. /// A <see cref="string"/> that contains a message to send.
/// </param> /// </param>
public bool PingTo (string id, string message) /// <param name="id">
/// A <see cref="string"/> that contains an ID that represents the destination for the Ping.
/// </param>
public bool PingTo (string message, string id)
{ {
if (message == null) if (id.IsNullOrEmpty ())
message = String.Empty;
var msg = id.IsNullOrEmpty ()
? "'id' must not be null or empty."
: Encoding.UTF8.GetBytes (message).Length > 125
? "The payload length of a Ping frame must be 125 bytes or less."
: null;
if (msg != null)
{ {
Log.Error (msg); Log.Error ("'id' must not be null or empty.");
return false; return false;
} }
return _sessions.PingTo (id, message); return _sessions.PingTo (message, id);
} }
/// <summary> /// <summary>
/// Sends a binary data to the client associated with the specified ID. /// Sends a binary data to the client associated with the specified ID.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// <c>true</c> if the client associated with <paramref name="id"/> is successfully found; /// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
/// otherwise, <c>false</c>.
/// </returns> /// </returns>
/// <param name="id">
/// A <see cref="string"/> that contains an ID that represents the destination for the data.
/// </param>
/// <param name="data"> /// <param name="data">
/// An array of <see cref="byte"/> that contains a binary data to send. /// An array of <see cref="byte"/> that contains a binary data to send.
/// </param> /// </param>
public bool SendTo (string id, byte [] data) /// <param name="id">
/// A <see cref="string"/> that contains an ID that represents the destination for the data.
/// </param>
public bool SendTo (byte [] data, string id)
{ {
var msg = id.IsNullOrEmpty () var msg = data == null
? "'data' must not be null."
: id.IsNullOrEmpty ()
? "'id' must not be null or empty." ? "'id' must not be null or empty."
: data == null : null;
? "'data' must not be null."
: null;
if (msg != null) if (msg != null)
{ {
@ -406,29 +396,28 @@ namespace WebSocketSharp.Server
return false; return false;
} }
return _sessions.SendTo (id, data); return _sessions.SendTo (data, id);
} }
/// <summary> /// <summary>
/// Sends a text data to the client associated with the specified ID. /// Sends a text data to the client associated with the specified ID.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// <c>true</c> if the client associated with <paramref name="id"/> is successfully found; /// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
/// otherwise, <c>false</c>.
/// </returns> /// </returns>
/// <param name="id">
/// A <see cref="string"/> that contains an ID that represents the destination for the data.
/// </param>
/// <param name="data"> /// <param name="data">
/// A <see cref="string"/> that contains a text data to send. /// A <see cref="string"/> that contains a text data to send.
/// </param> /// </param>
public bool SendTo (string id, string data) /// <param name="id">
/// A <see cref="string"/> that contains an ID that represents the destination for the data.
/// </param>
public bool SendTo (string data, string id)
{ {
var msg = id.IsNullOrEmpty () var msg = data == null
? "'data' must not be null."
: id.IsNullOrEmpty ()
? "'id' must not be null or empty." ? "'id' must not be null or empty."
: data == null : null;
? "'data' must not be null."
: null;
if (msg != null) if (msg != null)
{ {
@ -436,7 +425,7 @@ namespace WebSocketSharp.Server
return false; return false;
} }
return _sessions.SendTo (id, data); return _sessions.SendTo (data, id);
} }
/// <summary> /// <summary>
@ -494,7 +483,7 @@ namespace WebSocketSharp.Server
/// <param name="context"> /// <param name="context">
/// A <see cref="WebSocketContext"/> that contains the WebSocket connection request objects to bind. /// A <see cref="WebSocketContext"/> that contains the WebSocket connection request objects to bind.
/// </param> /// </param>
void IServiceHost.BindWebSocket (WebSocketContext context) void IWebSocketServiceHost.BindWebSocket (WebSocketContext context)
{ {
T service = new T (); T service = new T ();
service.Bind (context, _sessions); service.Bind (context, _sessions);
@ -507,7 +496,7 @@ namespace WebSocketSharp.Server
/// <param name="data"> /// <param name="data">
/// An array of <see cref="byte"/> to broadcast. /// An array of <see cref="byte"/> to broadcast.
/// </param> /// </param>
void IServiceHost.Broadcast (byte [] data) void IWebSocketServiceHost.Broadcast (byte [] data)
{ {
_sessions.Broadcast (data); _sessions.Broadcast (data);
} }
@ -518,78 +507,76 @@ namespace WebSocketSharp.Server
/// <param name="data"> /// <param name="data">
/// A <see cref="string"/> to broadcast. /// A <see cref="string"/> to broadcast.
/// </param> /// </param>
void IServiceHost.Broadcast (string data) void IWebSocketServiceHost.Broadcast (string data)
{ {
_sessions.Broadcast (data); _sessions.Broadcast (data);
} }
/// <summary> /// <summary>
/// Sends Pings with the specified <see cref="string"/> to all clients. /// Sends Pings with the specified <paramref name="message"/> to all clients.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// A Dictionary&lt;string, bool&gt; that contains the collection of session IDs and values /// A Dictionary&lt;string, bool&gt; that contains the collection of pairs of session ID and value
/// indicating whether the service host received the Pongs from each clients in a time. /// indicating whether the service host received the Pong from each client in a time.
/// </returns> /// </returns>
/// <param name="message"> /// <param name="message">
/// A <see cref="string"/> that contains a message to send. /// A <see cref="string"/> that contains a message to send.
/// </param> /// </param>
Dictionary<string, bool> IServiceHost.Broadping (string message) Dictionary<string, bool> IWebSocketServiceHost.Broadping (string message)
{ {
return _sessions.Broadping (message); return _sessions.Broadping (message);
} }
/// <summary> /// <summary>
/// Sends a Ping with the specified <see cref="string"/> to the client associated with /// Sends a Ping with the specified <paramref name="message"/> to the client associated with
/// the specified ID. /// the specified ID.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// <c>true</c> if the service host receives a Pong from the client in a time; otherwise, <c>false</c>. /// <c>true</c> if the service host receives a Pong from the client in a time; otherwise, <c>false</c>.
/// </returns> /// </returns>
/// <param name="id">
/// A <see cref="string"/> that contains an ID that represents the destination for the Ping.
/// </param>
/// <param name="message"> /// <param name="message">
/// A <see cref="string"/> that contains a message to send. /// A <see cref="string"/> that contains a message to send.
/// </param> /// </param>
bool IServiceHost.PingTo (string id, string message) /// <param name="id">
/// A <see cref="string"/> that contains an ID that represents the destination for the Ping.
/// </param>
bool IWebSocketServiceHost.PingTo (string message, string id)
{ {
return _sessions.PingTo (id, message); return _sessions.PingTo (message, id);
} }
/// <summary> /// <summary>
/// Sends a binary data to the client associated with the specified ID. /// Sends a binary data to the client associated with the specified ID.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// <c>true</c> if the client associated with <paramref name="id"/> is successfully found; /// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
/// otherwise, <c>false</c>.
/// </returns> /// </returns>
/// <param name="id">
/// A <see cref="string"/> that contains an ID that represents the destination for the data.
/// </param>
/// <param name="data"> /// <param name="data">
/// An array of <see cref="byte"/> that contains a binary data to send. /// An array of <see cref="byte"/> that contains a binary data to send.
/// </param> /// </param>
bool IServiceHost.SendTo (string id, byte [] data) /// <param name="id">
/// A <see cref="string"/> that contains an ID that represents the destination for the data.
/// </param>
bool IWebSocketServiceHost.SendTo (byte [] data, string id)
{ {
return _sessions.SendTo (id, data); return _sessions.SendTo (data, id);
} }
/// <summary> /// <summary>
/// Sends a text data to the client associated with the specified ID. /// Sends a text data to the client associated with the specified ID.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// <c>true</c> if the client associated with <paramref name="id"/> is successfully found; /// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
/// otherwise, <c>false</c>.
/// </returns> /// </returns>
/// <param name="id">
/// A <see cref="string"/> that contains an ID that represents the destination for the data.
/// </param>
/// <param name="data"> /// <param name="data">
/// A <see cref="string"/> that contains a text data to send. /// A <see cref="string"/> that contains a text data to send.
/// </param> /// </param>
bool IServiceHost.SendTo (string id, string data) /// <param name="id">
/// A <see cref="string"/> that contains an ID that represents the destination for the data.
/// </param>
bool IWebSocketServiceHost.SendTo (string data, string id)
{ {
return _sessions.SendTo (id, data); return _sessions.SendTo (data, id);
} }
/// <summary> /// <summary>
@ -602,7 +589,7 @@ namespace WebSocketSharp.Server
/// <param name="reason"> /// <param name="reason">
/// A <see cref="string"/> that contains the reason for stop. /// A <see cref="string"/> that contains the reason for stop.
/// </param> /// </param>
void IServiceHost.Stop (ushort code, string reason) void IWebSocketServiceHost.Stop (ushort code, string reason)
{ {
base.Stop (); base.Stop ();
_sessions.Stop (code, reason); _sessions.Stop (code, reason);

View File

@ -39,10 +39,10 @@ namespace WebSocketSharp.Server
{ {
#region Private Fields #region Private Fields
private volatile bool _keepClean; private volatile bool _keepClean;
private Logger _logger; private Logger _logger;
private Dictionary<string, IServiceHost> _serviceHosts; private Dictionary<string, IWebSocketServiceHost> _serviceHosts;
private object _sync; private object _sync;
#endregion #endregion
@ -57,7 +57,7 @@ namespace WebSocketSharp.Server
{ {
_logger = logger; _logger = logger;
_keepClean = true; _keepClean = true;
_serviceHosts = new Dictionary<string, IServiceHost> (); _serviceHosts = new Dictionary<string, IWebSocketServiceHost> ();
_sync = new object (); _sync = new object ();
} }
@ -133,7 +133,7 @@ namespace WebSocketSharp.Server
} }
} }
internal IEnumerable<IServiceHost> ServiceHosts { internal IEnumerable<IWebSocketServiceHost> ServiceHosts {
get { get {
lock (_sync) lock (_sync)
{ {
@ -146,11 +146,11 @@ namespace WebSocketSharp.Server
#region Private Methods #region Private Methods
private Dictionary<string, IServiceHost> copy () private Dictionary<string, IWebSocketServiceHost> copy ()
{ {
lock (_sync) lock (_sync)
{ {
return new Dictionary<string, IServiceHost> (_serviceHosts); return new Dictionary<string, IWebSocketServiceHost> (_serviceHosts);
} }
} }
@ -158,15 +158,15 @@ namespace WebSocketSharp.Server
#region Internal Methods #region Internal Methods
internal void Add (string servicePath, IServiceHost serviceHost) internal void Add (string servicePath, IWebSocketServiceHost serviceHost)
{ {
lock (_sync) lock (_sync)
{ {
IServiceHost host; IWebSocketServiceHost host;
if (_serviceHosts.TryGetValue (servicePath, out host)) if (_serviceHosts.TryGetValue (servicePath, out host))
{ {
_logger.Error ( _logger.Error (
"The WebSocket service host with the specified path already exists.\npath: " + servicePath); "The WebSocket service with the specified path already exists.\npath: " + servicePath);
return; return;
} }
@ -176,13 +176,13 @@ namespace WebSocketSharp.Server
internal bool Remove (string servicePath) internal bool Remove (string servicePath)
{ {
IServiceHost host; IWebSocketServiceHost host;
lock (_sync) lock (_sync)
{ {
if (!_serviceHosts.TryGetValue (servicePath, out host)) if (!_serviceHosts.TryGetValue (servicePath, out host))
{ {
_logger.Error ( _logger.Error (
"The WebSocket service host with the specified path not found.\npath: " + servicePath); "The WebSocket service with the specified path not found.\npath: " + servicePath);
return false; return false;
} }
@ -215,7 +215,7 @@ namespace WebSocketSharp.Server
} }
} }
internal bool TryGetServiceHost (string servicePath, out IServiceHost serviceHost) internal bool TryGetServiceHost (string servicePath, out IWebSocketServiceHost serviceHost)
{ {
lock (_sync) lock (_sync)
{ {
@ -290,7 +290,7 @@ namespace WebSocketSharp.Server
return false; return false;
} }
IServiceHost host; IWebSocketServiceHost host;
if (!TryGetServiceHost (servicePath, out host)) if (!TryGetServiceHost (servicePath, out host))
{ {
_logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath); _logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath);
@ -328,7 +328,7 @@ namespace WebSocketSharp.Server
return false; return false;
} }
IServiceHost host; IWebSocketServiceHost host;
if (!TryGetServiceHost (servicePath, out host)) if (!TryGetServiceHost (servicePath, out host))
{ {
_logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath); _logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath);
@ -401,7 +401,7 @@ namespace WebSocketSharp.Server
return null; return null;
} }
IServiceHost host; IWebSocketServiceHost host;
if (!TryGetServiceHost (servicePath, out host)) if (!TryGetServiceHost (servicePath, out host))
{ {
_logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath); _logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath);
@ -429,7 +429,7 @@ namespace WebSocketSharp.Server
return -1; return -1;
} }
IServiceHost host; IWebSocketServiceHost host;
if (!TryGetServiceHost (servicePath, out host)) if (!TryGetServiceHost (servicePath, out host))
{ {
_logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath); _logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath);
@ -458,16 +458,11 @@ namespace WebSocketSharp.Server
/// </param> /// </param>
public bool PingTo (string message, string id, string servicePath) public bool PingTo (string message, string id, string servicePath)
{ {
if (message == null) var msg = id.IsNullOrEmpty ()
message = String.Empty; ? "'id' must not be null or empty."
: servicePath.IsNullOrEmpty ()
var msg = Encoding.UTF8.GetBytes (message).Length > 125 ? "'servicePath' must not be null or empty."
? "The payload length of a Ping frame must be 125 bytes or less." : null;
: id.IsNullOrEmpty ()
? "'id' must not be null or empty."
: servicePath.IsNullOrEmpty ()
? "'servicePath' must not be null or empty."
: null;
if (msg != null) if (msg != null)
{ {
@ -475,14 +470,14 @@ namespace WebSocketSharp.Server
return false; return false;
} }
IServiceHost host; IWebSocketServiceHost host;
if (!TryGetServiceHost (servicePath, out host)) if (!TryGetServiceHost (servicePath, out host))
{ {
_logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath); _logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath);
return false; return false;
} }
return host.PingTo (id, message); return host.PingTo (message, id);
} }
/// <summary> /// <summary>
@ -517,14 +512,14 @@ namespace WebSocketSharp.Server
return false; return false;
} }
IServiceHost host; IWebSocketServiceHost host;
if (!TryGetServiceHost (servicePath, out host)) if (!TryGetServiceHost (servicePath, out host))
{ {
_logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath); _logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath);
return false; return false;
} }
return host.SendTo (id, data); return host.SendTo (data, id);
} }
/// <summary> /// <summary>
@ -559,14 +554,14 @@ namespace WebSocketSharp.Server
return false; return false;
} }
IServiceHost host; IWebSocketServiceHost host;
if (!TryGetServiceHost (servicePath, out host)) if (!TryGetServiceHost (servicePath, out host))
{ {
_logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath); _logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath);
return false; return false;
} }
return host.SendTo (id, data); return host.SendTo (data, id);
} }
#endregion #endregion

View File

@ -361,12 +361,12 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Sends Pings with the specified <see cref="string"/> to the clients of every /// Sends Pings with the specified <paramref name="message"/> to the clients of every
/// <see cref="WebSocketService"/> instances managed by the <see cref="WebSocketServiceManager"/>. /// <see cref="WebSocketService"/> instances managed by the <see cref="WebSocketServiceManager"/>.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// A Dictionary&lt;string, bool&gt; that contains the collection of IDs and values indicating /// A Dictionary&lt;string, bool&gt; that contains the collection of pairs of session ID and value
/// whether each <see cref="WebSocketService"/> instances received a Pong in a time. /// indicating whether the each <see cref="WebSocketService"/> instance received a Pong in a time.
/// </returns> /// </returns>
/// <param name="message"> /// <param name="message">
/// A <see cref="string"/> that contains a message to send. /// A <see cref="string"/> that contains a message to send.
@ -381,28 +381,30 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Sends a Ping with the specified <see cref="string"/> to the client of the <see cref="WebSocketService"/> /// Sends a Ping with the specified <paramref name="message"/> to the client of
/// instance with the specified ID. /// the <see cref="WebSocketService"/> instance with the specified ID.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// <c>true</c> if the <see cref="WebSocketService"/> instance receives a Pong in a time; /// <c>true</c> if the <see cref="WebSocketService"/> instance with <paramref name="id"/> receives
/// otherwise, <c>false</c>. /// a Pong in a time; otherwise, <c>false</c>.
/// </returns> /// </returns>
/// <param name="id">
/// A <see cref="string"/> that contains an ID that represents the destination for the Ping.
/// </param>
/// <param name="message"> /// <param name="message">
/// A <see cref="string"/> that contains a message to send. /// A <see cref="string"/> that contains a message to send.
/// </param> /// </param>
internal bool PingTo (string id, string message) /// <param name="id">
/// A <see cref="string"/> that contains an ID that represents the destination for the Ping.
/// </param>
internal bool PingTo (string message, string id)
{ {
WebSocketService service; WebSocketService service;
if (TryGetServiceInstance (id, out service)) if (!TryGetServiceInstance (id, out service))
return service.Ping (message); {
_logger.Error (
"The WebSocket service instance with the specified ID not found.\nID: " + id);
return false;
}
_logger.Error ( return service.Ping (message);
"The WebSocket service instance with the specified ID not found.\nID: " + id);
return false;
} }
internal bool Remove (string id) internal bool Remove (string id)
@ -418,27 +420,26 @@ namespace WebSocketSharp.Server
/// with the specified ID. /// with the specified ID.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// <c>true</c> if the <see cref="WebSocketService"/> instance with <paramref name="id"/> /// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
/// is successfully found; otherwise, <c>false</c>.
/// </returns> /// </returns>
/// <param name="id">
/// A <see cref="string"/> that contains an ID that represents the destination for the data.
/// </param>
/// <param name="data"> /// <param name="data">
/// An array of <see cref="byte"/> that contains a binary data to send. /// An array of <see cref="byte"/> that contains a binary data to send.
/// </param> /// </param>
internal bool SendTo (string id, byte [] data) /// <param name="id">
/// A <see cref="string"/> that contains an ID that represents the destination for the data.
/// </param>
internal bool SendTo (byte [] data, string id)
{ {
WebSocketService service; WebSocketService service;
if (TryGetServiceInstance (id, out service)) if (!TryGetServiceInstance (id, out service))
{ {
service.Send (data); _logger.Error (
return true; "The WebSocket service instance with the specified ID not found.\nID: " + id);
return false;
} }
_logger.Error ( service.Send (data);
"The WebSocket service instance with the specified ID not found.\nID: " + id); return true;
return false;
} }
/// <summary> /// <summary>
@ -446,27 +447,26 @@ namespace WebSocketSharp.Server
/// with the specified ID. /// with the specified ID.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// <c>true</c> if the <see cref="WebSocketService"/> instance with <paramref name="id"/> /// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
/// is successfully found; otherwise, <c>false</c>.
/// </returns> /// </returns>
/// <param name="id">
/// A <see cref="string"/> that contains an ID that represents the destination for the data.
/// </param>
/// <param name="data"> /// <param name="data">
/// A <see cref="string"/> that contains a text data to send. /// A <see cref="string"/> that contains a text data to send.
/// </param> /// </param>
internal bool SendTo (string id, string data) /// <param name="id">
/// A <see cref="string"/> that contains an ID that represents the destination for the data.
/// </param>
internal bool SendTo (string data, string id)
{ {
WebSocketService service; WebSocketService service;
if (TryGetServiceInstance (id, out service)) if (!TryGetServiceInstance (id, out service))
{ {
service.Send (data); _logger.Error (
return true; "The WebSocket service instance with the specified ID not found.\nID: " + id);
return false;
} }
_logger.Error ( service.Send (data);
"The WebSocket service instance with the specified ID not found.\nID: " + id); return true;
return false;
} }
internal void Stop () internal void Stop ()
@ -499,10 +499,7 @@ namespace WebSocketSharp.Server
lock (_sync) lock (_sync)
{ {
if (_stopped) if (_stopped)
{ break;
_sweeping = false;
return;
}
WebSocketService service; WebSocketService service;
if (_services.TryGetValue (id, out service)) if (_services.TryGetValue (id, out service))

View File

@ -100,7 +100,6 @@
<Compile Include="Net\HttpStatusCode.cs" /> <Compile Include="Net\HttpStatusCode.cs" />
<Compile Include="Server\WebSocketServerBase.cs" /> <Compile Include="Server\WebSocketServerBase.cs" />
<Compile Include="Net\Security\SslStream.cs" /> <Compile Include="Net\Security\SslStream.cs" />
<Compile Include="Server\IServiceHost.cs" />
<Compile Include="Server\WebSocketServiceHost.cs" /> <Compile Include="Server\WebSocketServiceHost.cs" />
<Compile Include="CloseStatusCode.cs" /> <Compile Include="CloseStatusCode.cs" />
<Compile Include="Fin.cs" /> <Compile Include="Fin.cs" />
@ -128,6 +127,7 @@
<Compile Include="HandshakeRequest.cs" /> <Compile Include="HandshakeRequest.cs" />
<Compile Include="HandshakeResponse.cs" /> <Compile Include="HandshakeResponse.cs" />
<Compile Include="Server\WebSocketServiceHostManager.cs" /> <Compile Include="Server\WebSocketServiceHostManager.cs" />
<Compile Include="Server\IWebSocketServiceHost.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup> <ItemGroup>