Modified checking if can send

This commit is contained in:
sta 2014-02-02 16:28:20 +09:00
parent 31463022ee
commit 31c3ece37f
7 changed files with 198 additions and 184 deletions

View File

@ -223,18 +223,24 @@ namespace WebSocketSharp
: null; : null;
} }
internal static string CheckIfStarted (this ServerState state) internal static string CheckIfStart (this ServerState state)
{ {
return state != ServerState.START return state == ServerState.READY
? "Any of not started, on shutdown or stopped." ? "The server hasn't started yet."
: null; : state == ServerState.SHUTDOWN
? "The server is shutting down."
: state == ServerState.STOP
? "The server has already stopped."
: null;
} }
internal static string CheckIfStopped (this ServerState state) internal static string CheckIfStopped (this ServerState state)
{ {
return state == ServerState.START || state == ServerState.SHUTDOWN return state == ServerState.START
? "Already started or on shutdown." ? "The server has already started."
: null; : state == ServerState.SHUTDOWN
? "The server is shutting down."
: null;
} }
internal static string CheckIfValidCloseStatusCode (this ushort code) internal static string CheckIfValidCloseStatusCode (this ushort code)
@ -259,6 +265,13 @@ namespace WebSocketSharp
: null; : null;
} }
internal static string CheckIfValidSendData (this FileInfo file)
{
return file == null
? "'file' must not be null."
: null;
}
internal static string CheckIfValidSendData (this string data) internal static string CheckIfValidSendData (this string data)
{ {
return data == null return data == null

View File

@ -537,7 +537,7 @@ namespace WebSocketSharp.Server
private string checkIfCanStop (Func<string> checkParams) private string checkIfCanStop (Func<string> checkParams)
{ {
return _state.CheckIfStarted () ?? checkParams (); return _state.CheckIfStart () ?? checkParams ();
} }
private string checkIfCertExists () private string checkIfCertExists ()
@ -742,7 +742,7 @@ namespace WebSocketSharp.Server
public void Stop () public void Stop ()
{ {
lock (_sync) { lock (_sync) {
var msg = _state.CheckIfStarted (); var msg = _state.CheckIfStart ();
if (msg != null) { if (msg != null) {
_logger.Error (String.Format ("{0}\nstate: {1}", msg, _state)); _logger.Error (String.Format ("{0}\nstate: {1}", msg, _state));
return; return;

View File

@ -551,7 +551,7 @@ namespace WebSocketSharp.Server
private string checkIfCanStop (Func<string> checkParams) private string checkIfCanStop (Func<string> checkParams)
{ {
return _state.CheckIfStarted () ?? checkParams (); return _state.CheckIfStart () ?? checkParams ();
} }
private string checkIfCertExists () private string checkIfCertExists ()
@ -758,7 +758,7 @@ namespace WebSocketSharp.Server
public void Stop () public void Stop ()
{ {
lock (_sync) { lock (_sync) {
var msg = _state.CheckIfStarted (); var msg = _state.CheckIfStart ();
if (msg != null) { if (msg != null) {
_logger.Error (String.Format ("{0}\nstate: {1}", msg, _state)); _logger.Error (String.Format ("{0}\nstate: {1}", msg, _state));
return; return;

View File

@ -270,11 +270,11 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Sends a binary <paramref name="data"/> to the client of the current /// Sends a binary <paramref name="data"/> to the client on the current
/// <see cref="WebSocketService"/> instance. /// session in the WebSocket service.
/// </summary> /// </summary>
/// <param name="data"> /// <param name="data">
/// An array of <see cref="byte"/> that contains the binary data to send. /// An array of <see cref="byte"/> that represents the binary data to send.
/// </param> /// </param>
protected void Send (byte [] data) protected void Send (byte [] data)
{ {
@ -283,11 +283,11 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Sends a binary data from the specified <see cref="FileInfo"/> to /// Sends the specified <paramref name="file"/> as a binary data
/// the client of the current <see cref="WebSocketService"/> instance. /// to the client on the current session in the WebSocket service.
/// </summary> /// </summary>
/// <param name="file"> /// <param name="file">
/// A <see cref="FileInfo"/> from which contains the binary data to send. /// A <see cref="FileInfo"/> that represents the file to send.
/// </param> /// </param>
protected void Send (FileInfo file) protected void Send (FileInfo file)
{ {
@ -296,8 +296,8 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Sends a text <paramref name="data"/> to the client of the current /// Sends a text <paramref name="data"/> to the client on the current
/// <see cref="WebSocketService"/> instance. /// session in the WebSocket service.
/// </summary> /// </summary>
/// <param name="data"> /// <param name="data">
/// A <see cref="string"/> that represents the text data to send. /// A <see cref="string"/> that represents the text data to send.
@ -309,20 +309,19 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Sends a binary <paramref name="data"/> asynchronously to the client of the /// Sends a binary <paramref name="data"/> asynchronously to the client
/// current <see cref="WebSocketService"/> instance. /// on the current session in the WebSocket service.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method doesn't wait for the send to be complete. /// This method doesn't wait for the send to be complete.
/// </remarks> /// </remarks>
/// <param name="data"> /// <param name="data">
/// An array of <see cref="byte"/> that contains the binary data to send. /// An array of <see cref="byte"/> that represents the binary data to send.
/// </param> /// </param>
/// <param name="completed"> /// <param name="completed">
/// An Action&lt;bool&gt; delegate that references the method(s) called when /// An Action&lt;bool&gt; delegate that references the method(s) called when
/// the send is complete. /// the send is complete. A <see cref="bool"/> passed to this delegate is
/// A <see cref="bool"/> passed to this delegate is <c>true</c> if the send is /// <c>true</c> if the send is complete successfully; otherwise, <c>false</c>.
/// complete successfully; otherwise, <c>false</c>.
/// </param> /// </param>
protected void SendAsync (byte [] data, Action<bool> completed) protected void SendAsync (byte [] data, Action<bool> completed)
{ {
@ -331,21 +330,20 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Sends a binary data from the specified <see cref="FileInfo"/> /// Sends the specified <paramref name="file"/> as a binary data
/// asynchronously to the client of the current <see cref="WebSocketService"/> /// asynchronously to the client on the current session in the WebSocket
/// instance. /// service.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method doesn't wait for the send to be complete. /// This method doesn't wait for the send to be complete.
/// </remarks> /// </remarks>
/// <param name="file"> /// <param name="file">
/// A <see cref="FileInfo"/> from which contains the binary data to send. /// A <see cref="FileInfo"/> that represents the file to send.
/// </param> /// </param>
/// <param name="completed"> /// <param name="completed">
/// An Action&lt;bool&gt; delegate that references the method(s) called when /// An Action&lt;bool&gt; delegate that references the method(s) called when
/// the send is complete. /// the send is complete. A <see cref="bool"/> passed to this delegate is
/// A <see cref="bool"/> passed to this delegate is <c>true</c> if the send is /// <c>true</c> if the send is complete successfully; otherwise, <c>false</c>.
/// complete successfully; otherwise, <c>false</c>.
/// </param> /// </param>
protected void SendAsync (FileInfo file, Action<bool> completed) protected void SendAsync (FileInfo file, Action<bool> completed)
{ {
@ -354,8 +352,8 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Sends a text <paramref name="data"/> asynchronously to the client of the /// Sends a text <paramref name="data"/> asynchronously to the client
/// current <see cref="WebSocketService"/> instance. /// on the current session in the WebSocket service.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method doesn't wait for the send to be complete. /// This method doesn't wait for the send to be complete.
@ -365,9 +363,8 @@ namespace WebSocketSharp.Server
/// </param> /// </param>
/// <param name="completed"> /// <param name="completed">
/// An Action&lt;bool&gt; delegate that references the method(s) called when /// An Action&lt;bool&gt; delegate that references the method(s) called when
/// the send is complete. /// the send is complete. A <see cref="bool"/> passed to this delegate is
/// A <see cref="bool"/> passed to this delegate is <c>true</c> if the send is /// <c>true</c> if the send is complete successfully; otherwise, <c>false</c>.
/// complete successfully; otherwise, <c>false</c>.
/// </param> /// </param>
protected void SendAsync (string data, Action<bool> completed) protected void SendAsync (string data, Action<bool> completed)
{ {
@ -377,22 +374,21 @@ namespace WebSocketSharp.Server
/// <summary> /// <summary>
/// Sends a binary data from the specified <see cref="Stream"/> asynchronously /// Sends a binary data from the specified <see cref="Stream"/> asynchronously
/// to the client of the current <see cref="WebSocketService"/> instance. /// to the client on the current session in the WebSocket service.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method doesn't wait for the send to be complete. /// This method doesn't wait for the send to be complete.
/// </remarks> /// </remarks>
/// <param name="stream"> /// <param name="stream">
/// A <see cref="Stream"/> object from which contains the binary data to send. /// A <see cref="Stream"/> from which contains the binary data to send.
/// </param> /// </param>
/// <param name="length"> /// <param name="length">
/// An <see cref="int"/> that represents the number of bytes to send. /// An <see cref="int"/> that represents the number of bytes to send.
/// </param> /// </param>
/// <param name="completed"> /// <param name="completed">
/// An Action&lt;bool&gt; delegate that references the method(s) called when /// An Action&lt;bool&gt; delegate that references the method(s) called when
/// the send is complete. /// the send is complete. A <see cref="bool"/> passed to this delegate is
/// A <see cref="bool"/> passed to this delegate is <c>true</c> if the send is /// <c>true</c> if the send is complete successfully; otherwise, <c>false</c>.
/// complete successfully; otherwise, <c>false</c>.
/// </param> /// </param>
protected void SendAsync (Stream stream, int length, Action<bool> completed) protected void SendAsync (Stream stream, int length, Action<bool> completed)
{ {

View File

@ -263,6 +263,11 @@ namespace WebSocketSharp.Server
return result; return result;
} }
private string checkIfCanSend (Func<string> checkParams)
{
return _state.CheckIfStart () ?? checkParams ();
}
#endregion #endregion
#region Internal Methods #region Internal Methods
@ -359,15 +364,16 @@ namespace WebSocketSharp.Server
#region Public Methods #region Public Methods
/// <summary> /// <summary>
/// Broadcasts a binary <paramref name="data"/> to all clients of the /// Broadcasts a binary <paramref name="data"/> to all clients
/// WebSocket services provided by the server. /// of the WebSocket services provided by the server.
/// </summary> /// </summary>
/// <param name="data"> /// <param name="data">
/// An array of <see cref="byte"/> that contains the binary data to broadcast. /// An array of <see cref="byte"/> that represents the binary data
/// to broadcast.
/// </param> /// </param>
public void Broadcast (byte [] data) public void Broadcast (byte [] data)
{ {
var msg = _state.CheckIfStarted () ?? data.CheckIfValidSendData (); var msg = checkIfCanSend (() => data.CheckIfValidSendData ());
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
return; return;
@ -380,15 +386,15 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Broadcasts a text <paramref name="data"/> to all clients of the WebSocket /// Broadcasts a text <paramref name="data"/> to all clients
/// services provided by the server. /// of the WebSocket services 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 = _state.CheckIfStarted () ?? data.CheckIfValidSendData (); var msg = checkIfCanSend (() => data.CheckIfValidSendData ());
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
return; return;
@ -409,7 +415,8 @@ namespace WebSocketSharp.Server
/// 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 contains the binary data to broadcast. /// An array of <see cref="byte"/> that represents 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
@ -417,7 +424,7 @@ namespace WebSocketSharp.Server
/// </param> /// </param>
public void BroadcastAsync (byte [] data, Action completed) public void BroadcastAsync (byte [] data, Action completed)
{ {
var msg = _state.CheckIfStarted () ?? data.CheckIfValidSendData (); var msg = checkIfCanSend (() => data.CheckIfValidSendData ());
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
return; return;
@ -430,8 +437,8 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Broadcasts a text <paramref name="data"/> asynchronously to all clients of /// Broadcasts a text <paramref name="data"/> asynchronously to all clients
/// the WebSocket services provided by the server. /// of the WebSocket 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.
@ -445,7 +452,7 @@ namespace WebSocketSharp.Server
/// </param> /// </param>
public void BroadcastAsync (string data, Action completed) public void BroadcastAsync (string data, Action completed)
{ {
var msg = _state.CheckIfStarted () ?? data.CheckIfValidSendData (); var msg = checkIfCanSend (() => data.CheckIfValidSendData ());
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
return; return;
@ -460,15 +467,14 @@ 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 all clients of the WebSocket services provided by the /// asynchronously to all clients of the WebSocket services provided
/// 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.
/// </remarks> /// </remarks>
/// <param name="stream"> /// <param name="stream">
/// A <see cref="Stream"/> object from which contains the binary data to /// A <see cref="Stream"/> from which contains the binary data to broadcast.
/// broadcast.
/// </param> /// </param>
/// <param name="length"> /// <param name="length">
/// An <see cref="int"/> that represents the number of bytes to broadcast. /// An <see cref="int"/> that represents the number of bytes to broadcast.
@ -479,9 +485,9 @@ namespace WebSocketSharp.Server
/// </param> /// </param>
public void BroadcastAsync (Stream stream, int length, Action completed) public void BroadcastAsync (Stream stream, int length, Action completed)
{ {
var msg = _state.CheckIfStarted () ?? var msg = checkIfCanSend (
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);
@ -521,7 +527,8 @@ namespace WebSocketSharp.Server
/// service to find. /// service to find.
/// </param> /// </param>
/// <param name="data"> /// <param name="data">
/// An array of <see cref="byte"/> that contains the binary data to broadcast. /// An array of <see cref="byte"/> that represents the binary data
/// to broadcast.
/// </param> /// </param>
public void BroadcastTo (string servicePath, byte [] data) public void BroadcastTo (string servicePath, byte [] data)
{ {
@ -531,8 +538,8 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Broadcasts a text <paramref name="data"/> to all clients of the WebSocket /// Broadcasts a text <paramref name="data"/> to all clients of the
/// service with the specified <paramref name="servicePath"/>. /// WebSocket service with the specified <paramref name="servicePath"/>.
/// </summary> /// </summary>
/// <param name="servicePath"> /// <param name="servicePath">
/// A <see cref="string"/> that represents the absolute path to the WebSocket /// A <see cref="string"/> that represents the absolute path to the WebSocket
@ -560,7 +567,8 @@ namespace WebSocketSharp.Server
/// service to find. /// service to find.
/// </param> /// </param>
/// <param name="data"> /// <param name="data">
/// An array of <see cref="byte"/> that contains the binary data to broadcast. /// An array of <see cref="byte"/> that represents 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
@ -575,8 +583,8 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Broadcasts a text <paramref name="data"/> asynchronously to all clients of /// Broadcasts a text <paramref name="data"/> asynchronously to all clients
/// the WebSocket service with the specified <paramref name="servicePath"/>. /// of the WebSocket service with the specified <paramref name="servicePath"/>.
/// </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.
@ -602,8 +610,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 all clients of the WebSocket service with the specified /// asynchronously to all clients of the WebSocket service with the
/// <paramref name="servicePath"/>. /// specified <paramref name="servicePath"/>.
/// </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.
@ -613,8 +621,7 @@ namespace WebSocketSharp.Server
/// service to find. /// service to find.
/// </param> /// </param>
/// <param name="stream"> /// <param name="stream">
/// A <see cref="Stream"/> object from which contains the binary data to /// A <see cref="Stream"/> from which contains the binary data to broadcast.
/// broadcast.
/// </param> /// </param>
/// <param name="length"> /// <param name="length">
/// An <see cref="int"/> that represents the number of bytes to broadcast. /// An <see cref="int"/> that represents the number of bytes to broadcast.
@ -643,7 +650,7 @@ namespace WebSocketSharp.Server
/// </returns> /// </returns>
public Dictionary<string, Dictionary<string, bool>> Broadping () public Dictionary<string, Dictionary<string, bool>> Broadping ()
{ {
var msg = _state.CheckIfStarted (); var msg = _state.CheckIfStart ();
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
return null; return null;
@ -671,8 +678,9 @@ namespace WebSocketSharp.Server
return Broadping (); return Broadping ();
byte [] data = null; byte [] data = null;
var msg = _state.CheckIfStarted () ?? var msg = checkIfCanSend (
(data = Encoding.UTF8.GetBytes (message)).CheckIfValidControlData ("message"); () => (data = Encoding.UTF8.GetBytes (message))
.CheckIfValidControlData ("message"));
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
@ -851,19 +859,19 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Sends a binary <paramref name="data"/> to the client associated with the /// Sends a binary <paramref name="data"/> to the client on the session with
/// specified <paramref name="servicePath"/> and <paramref name="id"/>. /// the specified <paramref name="id"/>, in the WebSocket service with the
/// specified <paramref name="servicePath"/>.
/// </summary> /// </summary>
/// <param name="servicePath"> /// <param name="servicePath">
/// 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>
/// <param name="id"> /// <param name="id">
/// A <see cref="string"/> that represents the ID of the session to send the /// A <see cref="string"/> that represents the ID of the session to find.
/// data to.
/// </param> /// </param>
/// <param name="data"> /// <param name="data">
/// An array of <see cref="byte"/> that contains the binary data to send. /// An array of <see cref="byte"/> that represents the binary data to send.
/// </param> /// </param>
public void SendTo (string servicePath, string id, byte [] data) public void SendTo (string servicePath, string id, byte [] data)
{ {
@ -873,16 +881,16 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Sends a text <paramref name="data"/> to the client associated with the /// Sends a text <paramref name="data"/> to the client on the session with
/// specified <paramref name="servicePath"/> and <paramref name="id"/>. /// the specified <paramref name="id"/>, in the WebSocket service with the
/// specified <paramref name="servicePath"/>.
/// </summary> /// </summary>
/// <param name="servicePath"> /// <param name="servicePath">
/// 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>
/// <param name="id"> /// <param name="id">
/// A <see cref="string"/> that represents the ID of the session to send the /// A <see cref="string"/> that represents the ID of the session to find.
/// data to.
/// </param> /// </param>
/// <param name="data"> /// <param name="data">
/// A <see cref="string"/> that represents the text data to send. /// A <see cref="string"/> that represents the text data to send.
@ -895,9 +903,9 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Sends a binary <paramref name="data"/> asynchronously to the client /// Sends a binary <paramref name="data"/> asynchronously to the client on the
/// associated with the specified <paramref name="servicePath"/> and /// session with the specified <paramref name="id"/>, in the WebSocket service
/// <paramref name="id"/>. /// with the specified <paramref name="servicePath"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method doesn't wait for the send to be complete. /// This method doesn't wait for the send to be complete.
@ -907,11 +915,10 @@ namespace WebSocketSharp.Server
/// service to find. /// service to find.
/// </param> /// </param>
/// <param name="id"> /// <param name="id">
/// A <see cref="string"/> that represents the ID of the session to send the /// A <see cref="string"/> that represents the ID of the session to find.
/// data to.
/// </param> /// </param>
/// <param name="data"> /// <param name="data">
/// An array of <see cref="byte"/> that contains the binary data to send. /// An array of <see cref="byte"/> that represents the binary data to send.
/// </param> /// </param>
/// <param name="completed"> /// <param name="completed">
/// An Action&lt;bool&gt; delegate that references the method(s) called when /// An Action&lt;bool&gt; delegate that references the method(s) called when
@ -928,9 +935,9 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Sends a text <paramref name="data"/> asynchronously to the client /// Sends a text <paramref name="data"/> asynchronously to the client on the
/// associated with the specified <paramref name="servicePath"/> and /// session with the specified <paramref name="id"/>, in the WebSocket service
/// <paramref name="id"/>. /// with the specified <paramref name="servicePath"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method doesn't wait for the send to be complete. /// This method doesn't wait for the send to be complete.
@ -940,8 +947,7 @@ namespace WebSocketSharp.Server
/// service to find. /// service to find.
/// </param> /// </param>
/// <param name="id"> /// <param name="id">
/// A <see cref="string"/> that represents the ID of the session to send the /// A <see cref="string"/> that represents the ID of the session to find.
/// data to.
/// </param> /// </param>
/// <param name="data"> /// <param name="data">
/// A <see cref="string"/> that represents the text data to send. /// A <see cref="string"/> that represents the text data to send.
@ -962,8 +968,8 @@ namespace WebSocketSharp.Server
/// <summary> /// <summary>
/// Sends a binary data from the specified <see cref="Stream"/> asynchronously /// Sends a binary data from the specified <see cref="Stream"/> asynchronously
/// to the client associated with the specified <paramref name="servicePath"/> /// to the client on the session with the specified <paramref name="id"/>, in
/// and <paramref name="id"/>. /// the WebSocket service with the specified <paramref name="servicePath"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method doesn't wait for the send to be complete. /// This method doesn't wait for the send to be complete.
@ -973,11 +979,10 @@ namespace WebSocketSharp.Server
/// service to find. /// service to find.
/// </param> /// </param>
/// <param name="id"> /// <param name="id">
/// A <see cref="string"/> that represents the ID of the session to send the /// A <see cref="string"/> that represents the ID of the session to find.
/// data to.
/// </param> /// </param>
/// <param name="stream"> /// <param name="stream">
/// A <see cref="Stream"/> object from which contains the binary data to send. /// A <see cref="Stream"/> from which contains the binary data to send.
/// </param> /// </param>
/// <param name="length"> /// <param name="length">
/// An <see cref="int"/> that represents the number of bytes to send. /// An <see cref="int"/> that represents the number of bytes to send.
@ -1016,7 +1021,7 @@ namespace WebSocketSharp.Server
public bool TryGetServiceHost ( public bool TryGetServiceHost (
string servicePath, out WebSocketServiceHost serviceHost) string servicePath, out WebSocketServiceHost serviceHost)
{ {
var msg = _state.CheckIfStarted () ?? servicePath.CheckIfValidServicePath (); var msg = _state.CheckIfStart () ?? servicePath.CheckIfValidServicePath ();
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
serviceHost = null; serviceHost = null;

View File

@ -276,6 +276,11 @@ namespace WebSocketSharp.Server
state => broadcast (opcode, stream, completed)); state => broadcast (opcode, stream, completed));
} }
private string checkIfCanSend (Func<string> checkParams)
{
return _state.CheckIfStart () ?? checkParams ();
}
private static string createID () private static string createID ()
{ {
return Guid.NewGuid ().ToString ("N"); return Guid.NewGuid ().ToString ("N");
@ -398,15 +403,16 @@ namespace WebSocketSharp.Server
#region Public Methods #region Public Methods
/// <summary> /// <summary>
/// Broadcasts a binary <paramref name="data"/> to all clients of the /// Broadcasts a binary <paramref name="data"/> to all clients
/// WebSocket service. /// of the WebSocket service.
/// </summary> /// </summary>
/// <param name="data"> /// <param name="data">
/// An array of <see cref="byte"/> that contains the binary data to broadcast. /// An array of <see cref="byte"/> that represents the binary data
/// to broadcast.
/// </param> /// </param>
public void Broadcast (byte [] data) public void Broadcast (byte [] data)
{ {
var msg = _state.CheckIfStarted () ?? data.CheckIfValidSendData (); var msg = checkIfCanSend (() => data.CheckIfValidSendData ());
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
return; return;
@ -419,15 +425,15 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Broadcasts a text <paramref name="data"/> to all clients of the WebSocket /// Broadcasts a text <paramref name="data"/> to all clients
/// service. /// of the WebSocket service.
/// </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 = _state.CheckIfStarted () ?? data.CheckIfValidSendData (); var msg = checkIfCanSend (() => data.CheckIfValidSendData ());
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
return; return;
@ -448,7 +454,8 @@ namespace WebSocketSharp.Server
/// 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 contains the binary data to broadcast. /// An array of <see cref="byte"/> that represents 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
@ -456,7 +463,7 @@ namespace WebSocketSharp.Server
/// </param> /// </param>
public void BroadcastAsync (byte [] data, Action completed) public void BroadcastAsync (byte [] data, Action completed)
{ {
var msg = _state.CheckIfStarted () ?? data.CheckIfValidSendData (); var msg = checkIfCanSend (() => data.CheckIfValidSendData ());
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
return; return;
@ -469,8 +476,8 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Broadcasts a text <paramref name="data"/> asynchronously to all clients of /// Broadcasts a text <paramref name="data"/> asynchronously to all clients
/// the WebSocket service. /// of the WebSocket service.
/// </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.
@ -484,7 +491,7 @@ namespace WebSocketSharp.Server
/// </param> /// </param>
public void BroadcastAsync (string data, Action completed) public void BroadcastAsync (string data, Action completed)
{ {
var msg = _state.CheckIfStarted () ?? data.CheckIfValidSendData (); var msg = checkIfCanSend (() => data.CheckIfValidSendData ());
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
return; return;
@ -505,8 +512,7 @@ namespace WebSocketSharp.Server
/// 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="stream"> /// <param name="stream">
/// A <see cref="Stream"/> object from which contains the binary data to /// A <see cref="Stream"/> from which contains the binary data to broadcast.
/// broadcast.
/// </param> /// </param>
/// <param name="length"> /// <param name="length">
/// An <see cref="int"/> that represents the number of bytes to broadcast. /// An <see cref="int"/> that represents the number of bytes to broadcast.
@ -517,9 +523,9 @@ namespace WebSocketSharp.Server
/// </param> /// </param>
public void BroadcastAsync (Stream stream, int length, Action completed) public void BroadcastAsync (Stream stream, int length, Action completed)
{ {
var msg = _state.CheckIfStarted () ?? var msg = checkIfCanSend (
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);
@ -560,7 +566,7 @@ namespace WebSocketSharp.Server
/// </returns> /// </returns>
public Dictionary<string, bool> Broadping () public Dictionary<string, bool> Broadping ()
{ {
var msg = _state.CheckIfStarted (); var msg = _state.CheckIfStart ();
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
return null; return null;
@ -587,8 +593,9 @@ namespace WebSocketSharp.Server
return Broadping (); return Broadping ();
byte [] data = null; byte [] data = null;
var msg = _state.CheckIfStarted () ?? var msg = checkIfCanSend (
(data = Encoding.UTF8.GetBytes (message)).CheckIfValidControlData ("message"); () => (data = Encoding.UTF8.GetBytes (message))
.CheckIfValidControlData ("message"));
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
@ -695,15 +702,14 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Sends a binary <paramref name="data"/> to the client associated with the /// Sends a binary <paramref name="data"/> to the client on the session
/// specified <paramref name="id"/>. /// with the specified <paramref name="id"/>.
/// </summary> /// </summary>
/// <param name="id"> /// <param name="id">
/// A <see cref="string"/> that represents the ID of the session to send the /// A <see cref="string"/> that represents the ID of the session to find.
/// data to.
/// </param> /// </param>
/// <param name="data"> /// <param name="data">
/// An array of <see cref="byte"/> that contains the binary data to send. /// An array of <see cref="byte"/> that represents the binary data to send.
/// </param> /// </param>
public void SendTo (string id, byte [] data) public void SendTo (string id, byte [] data)
{ {
@ -713,11 +719,11 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Sends a text <paramref name="data"/> to the client associated with the /// Sends a text <paramref name="data"/> to the client on the session
/// specified <paramref name="id"/>. /// with the specified <paramref name="id"/>.
/// </summary> /// </summary>
/// <param name="id"> /// <param name="id">
/// A <see cref="string"/> that represents the ID of the session to send the /// A <see cref="string"/> that represents the ID of the session to find.
/// data to. /// data to.
/// </param> /// </param>
/// <param name="data"> /// <param name="data">
@ -732,17 +738,16 @@ namespace WebSocketSharp.Server
/// <summary> /// <summary>
/// Sends a binary <paramref name="data"/> asynchronously to the client /// Sends a binary <paramref name="data"/> asynchronously to the client
/// associated with the specified <paramref name="id"/>. /// on the session with the specified <paramref name="id"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method doesn't wait for the send to be complete. /// This method doesn't wait for the send to be complete.
/// </remarks> /// </remarks>
/// <param name="id"> /// <param name="id">
/// A <see cref="string"/> that represents the ID of the session to send the /// A <see cref="string"/> that represents the ID of the session to find.
/// data to.
/// </param> /// </param>
/// <param name="data"> /// <param name="data">
/// An array of <see cref="byte"/> that contains the binary data to send. /// An array of <see cref="byte"/> that represents the binary data to send.
/// </param> /// </param>
/// <param name="completed"> /// <param name="completed">
/// An Action&lt;bool&gt; delegate that references the method(s) called when /// An Action&lt;bool&gt; delegate that references the method(s) called when
@ -759,14 +764,13 @@ namespace WebSocketSharp.Server
/// <summary> /// <summary>
/// Sends a text <paramref name="data"/> asynchronously to the client /// Sends a text <paramref name="data"/> asynchronously to the client
/// associated with the specified <paramref name="id"/>. /// on the session with the specified <paramref name="id"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method doesn't wait for the send to be complete. /// This method doesn't wait for the send to be complete.
/// </remarks> /// </remarks>
/// <param name="id"> /// <param name="id">
/// A <see cref="string"/> that represents the ID of the session to send the /// A <see cref="string"/> that represents the ID of the session to find.
/// data to.
/// </param> /// </param>
/// <param name="data"> /// <param name="data">
/// A <see cref="string"/> that represents the text data to send. /// A <see cref="string"/> that represents the text data to send.
@ -786,17 +790,16 @@ namespace WebSocketSharp.Server
/// <summary> /// <summary>
/// Sends a binary data from the specified <see cref="Stream"/> asynchronously /// Sends a binary data from the specified <see cref="Stream"/> asynchronously
/// to the client associated with the specified <paramref name="id"/>. /// to the client on the session with the specified <paramref name="id"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method doesn't wait for the send to be complete. /// This method doesn't wait for the send to be complete.
/// </remarks> /// </remarks>
/// <param name="id"> /// <param name="id">
/// A <see cref="string"/> that represents the ID of the session to send the /// A <see cref="string"/> that represents the ID of the session to find.
/// data to.
/// </param> /// </param>
/// <param name="stream"> /// <param name="stream">
/// A <see cref="Stream"/> object from which contains the binary data to send. /// A <see cref="Stream"/> from which contains the binary data to send.
/// </param> /// </param>
/// <param name="length"> /// <param name="length">
/// An <see cref="int"/> that represents the number of bytes to send. /// An <see cref="int"/> that represents the number of bytes to send.
@ -864,7 +867,7 @@ namespace WebSocketSharp.Server
/// </param> /// </param>
public bool TryGetSession (string id, out IWebSocketSession session) public bool TryGetSession (string id, out IWebSocketSession session)
{ {
var msg = _state.CheckIfStarted () ?? id.CheckIfValidSessionID (); var msg = _state.CheckIfStart () ?? id.CheckIfValidSessionID ();
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
session = null; session = null;

View File

@ -650,6 +650,11 @@ namespace WebSocketSharp
: _readyState.CheckIfConnectable (); : _readyState.CheckIfConnectable ();
} }
private string checkIfCanSend (Func<string> checkParams)
{
return _readyState.CheckIfOpen () ?? checkParams ();
}
// As server // As server
private string checkIfValidHandshakeRequest (WebSocketContext context) private string checkIfValidHandshakeRequest (WebSocketContext context)
{ {
@ -1054,7 +1059,7 @@ namespace WebSocketSharp
return res; return res;
} }
private bool send (byte [] frameAsBytes) private bool send (byte [] frame)
{ {
lock (_forConn) { lock (_forConn) {
if (_readyState != WebSocketState.OPEN) { if (_readyState != WebSocketState.OPEN) {
@ -1065,7 +1070,7 @@ namespace WebSocketSharp
return false; return false;
} }
return _stream.Write (frameAsBytes); return _stream.Write (frame);
} }
} }
@ -1860,11 +1865,11 @@ namespace WebSocketSharp
/// Sends a binary <paramref name="data"/> using the WebSocket connection. /// Sends a binary <paramref name="data"/> using the WebSocket connection.
/// </summary> /// </summary>
/// <param name="data"> /// <param name="data">
/// An array of <see cref="byte"/> that contains the binary data to send. /// An array of <see cref="byte"/> that represents the binary data to send.
/// </param> /// </param>
public void Send (byte [] data) public void Send (byte [] data)
{ {
var msg = _readyState.CheckIfOpen () ?? data.CheckIfValidSendData (); var msg = checkIfCanSend (() => data.CheckIfValidSendData ());
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
error (msg); error (msg);
@ -1884,17 +1889,15 @@ namespace WebSocketSharp
} }
/// <summary> /// <summary>
/// Sends a binary data from the specified <see cref="FileInfo"/> using the /// Sends the specified <paramref name="file"/> as a binary data
/// WebSocket connection. /// using the WebSocket connection.
/// </summary> /// </summary>
/// <param name="file"> /// <param name="file">
/// A <see cref="FileInfo"/> from which contains the binary data to send. /// A <see cref="FileInfo"/> that represents the file to send.
/// </param> /// </param>
public void Send (FileInfo file) public void Send (FileInfo file)
{ {
var msg = _readyState.CheckIfOpen () ?? var msg = checkIfCanSend (() => file.CheckIfValidSendData ());
(file == null ? "'file' must not be null." : null);
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
error (msg); error (msg);
@ -1913,7 +1916,7 @@ namespace WebSocketSharp
/// </param> /// </param>
public void Send (string data) public void Send (string data)
{ {
var msg = _readyState.CheckIfOpen () ?? data.CheckIfValidSendData (); var msg = checkIfCanSend (() => data.CheckIfValidSendData ());
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
error (msg); error (msg);
@ -1929,24 +1932,23 @@ namespace WebSocketSharp
} }
/// <summary> /// <summary>
/// Sends a binary <paramref name="data"/> asynchronously using the WebSocket /// Sends a binary <paramref name="data"/> asynchronously
/// connection. /// using the WebSocket connection.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method doesn't wait for the send to be complete. /// This method doesn't wait for the send to be complete.
/// </remarks> /// </remarks>
/// <param name="data"> /// <param name="data">
/// An array of <see cref="byte"/> that contains the binary data to send. /// An array of <see cref="byte"/> that represents the binary data to send.
/// </param> /// </param>
/// <param name="completed"> /// <param name="completed">
/// An Action&lt;bool&gt; delegate that references the method(s) called when /// An Action&lt;bool&gt; delegate that references the method(s) called when
/// the send is complete. /// the send is complete. A <see cref="bool"/> passed to this delegate is
/// A <see cref="bool"/> passed to this delegate is <c>true</c> if the send is /// <c>true</c> if the send is complete successfully; otherwise, <c>false</c>.
/// complete successfully; otherwise, <c>false</c>.
/// </param> /// </param>
public void SendAsync (byte [] data, Action<bool> completed) public void SendAsync (byte [] data, Action<bool> completed)
{ {
var msg = _readyState.CheckIfOpen () ?? data.CheckIfValidSendData (); var msg = checkIfCanSend (() => data.CheckIfValidSendData ());
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
error (msg); error (msg);
@ -1967,26 +1969,23 @@ namespace WebSocketSharp
} }
/// <summary> /// <summary>
/// Sends a binary data from the specified <see cref="FileInfo"/> /// Sends the specified <paramref name="file"/> as a binary data
/// asynchronously using the WebSocket connection. /// asynchronously using the WebSocket connection.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method doesn't wait for the send to be complete. /// This method doesn't wait for the send to be complete.
/// </remarks> /// </remarks>
/// <param name="file"> /// <param name="file">
/// A <see cref="FileInfo"/> from which contains the binary data to send. /// A <see cref="FileInfo"/> that represents the file to send.
/// </param> /// </param>
/// <param name="completed"> /// <param name="completed">
/// An Action&lt;bool&gt; delegate that references the method(s) called when /// An Action&lt;bool&gt; delegate that references the method(s) called when
/// the send is complete. /// the send is complete. A <see cref="bool"/> passed to this delegate is
/// A <see cref="bool"/> passed to this delegate is <c>true</c> if the send is /// <c>true</c> if the send is complete successfully; otherwise, <c>false</c>.
/// complete successfully; otherwise, <c>false</c>.
/// </param> /// </param>
public void SendAsync (FileInfo file, Action<bool> completed) public void SendAsync (FileInfo file, Action<bool> completed)
{ {
var msg = _readyState.CheckIfOpen () ?? var msg = checkIfCanSend (() => file.CheckIfValidSendData ());
(file == null ? "'file' must not be null." : null);
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
error (msg); error (msg);
@ -1998,8 +1997,8 @@ namespace WebSocketSharp
} }
/// <summary> /// <summary>
/// Sends a text <paramref name="data"/> asynchronously using the WebSocket /// Sends a text <paramref name="data"/> asynchronously
/// connection. /// using the WebSocket connection.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method doesn't wait for the send to be complete. /// This method doesn't wait for the send to be complete.
@ -2009,13 +2008,12 @@ namespace WebSocketSharp
/// </param> /// </param>
/// <param name="completed"> /// <param name="completed">
/// An Action&lt;bool&gt; delegate that references the method(s) called when /// An Action&lt;bool&gt; delegate that references the method(s) called when
/// the send is complete. /// the send is complete. A <see cref="bool"/> passed to this delegate is
/// A <see cref="bool"/> passed to this delegate is <c>true</c> if the send is /// <c>true</c> if the send is complete successfully; otherwise, <c>false</c>.
/// complete successfully; otherwise, <c>false</c>.
/// </param> /// </param>
public void SendAsync (string data, Action<bool> completed) public void SendAsync (string data, Action<bool> completed)
{ {
var msg = _readyState.CheckIfOpen () ?? data.CheckIfValidSendData (); var msg = checkIfCanSend (() => data.CheckIfValidSendData ());
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
error (msg); error (msg);
@ -2031,29 +2029,28 @@ namespace WebSocketSharp
} }
/// <summary> /// <summary>
/// Sends a binary data from the specified <see cref="Stream"/> asynchronously /// Sends a binary data from the specified <see cref="Stream"/>
/// using the WebSocket connection. /// asynchronously using the WebSocket connection.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method doesn't wait for the send to be complete. /// This method doesn't wait for the send to be complete.
/// </remarks> /// </remarks>
/// <param name="stream"> /// <param name="stream">
/// A <see cref="Stream"/> object from which contains the binary data to send. /// A <see cref="Stream"/> from which contains the binary data to send.
/// </param> /// </param>
/// <param name="length"> /// <param name="length">
/// An <see cref="int"/> that represents the number of bytes to send. /// An <see cref="int"/> that represents the number of bytes to send.
/// </param> /// </param>
/// <param name="completed"> /// <param name="completed">
/// An Action&lt;bool&gt; delegate that references the method(s) called when /// An Action&lt;bool&gt; delegate that references the method(s) called when
/// the send is complete. /// the send is complete. A <see cref="bool"/> passed to this delegate is
/// A <see cref="bool"/> passed to this delegate is <c>true</c> if the send is /// <c>true</c> if the send is complete successfully; otherwise, <c>false</c>.
/// complete successfully; otherwise, <c>false</c>.
/// </param> /// </param>
public void SendAsync (Stream stream, int length, Action<bool> completed) public void SendAsync (Stream stream, int length, Action<bool> completed)
{ {
var msg = _readyState.CheckIfOpen () ?? var msg = checkIfCanSend (
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);
@ -2067,9 +2064,9 @@ namespace WebSocketSharp
data => { data => {
var len = data.Length; var len = data.Length;
if (len == 0) { if (len == 0) {
var err = "A data cannot be read from 'stream'."; msg = "A data cannot be read from 'stream'.";
_logger.Error (err); _logger.Error (msg);
error (err); error (msg);
return; return;
} }