Added the internal Ext.CheckIfAvailable (WebSocketState, bool, bool, bool, bool) method

This commit is contained in:
sta 2015-08-15 16:28:46 +09:00
parent 999e777517
commit 18776d1a22
2 changed files with 56 additions and 45 deletions

View File

@ -211,24 +211,14 @@ namespace WebSocketSharp
: null; : null;
} }
internal static string CheckIfCanAccept (this WebSocketState state) internal static string CheckIfAvailable (
this WebSocketState state, bool connecting, bool open, bool closing, bool closed)
{ {
return state != WebSocketState.Connecting return (!connecting && state == WebSocketState.Connecting) ||
? "This operation has already been done." (!open && state == WebSocketState.Open) ||
: null; (!closing && state == WebSocketState.Closing) ||
} (!closed && state == WebSocketState.Closed)
? "This operation isn't available in: " + state
internal static string CheckIfCanClose (this WebSocketState state)
{
return state == WebSocketState.Closing || state == WebSocketState.Closed
? String.Format ("This operation isn't available ({0}).", state)
: null;
}
internal static string CheckIfCanConnect (this WebSocketState state)
{
return state == WebSocketState.Open || state == WebSocketState.Closing
? "This operation has already been done."
: null; : null;
} }
@ -241,13 +231,6 @@ namespace WebSocketSharp
: null; : null;
} }
internal static string CheckIfCanSend (this WebSocketState state)
{
return state != WebSocketState.Open
? String.Format ("This operation isn't available ({0}).", state)
: null;
}
internal static string CheckIfValidProtocols (this string[] protocols) internal static string CheckIfValidProtocols (this string[] protocols)
{ {
return protocols.Contains ( return protocols.Contains (

View File

@ -596,7 +596,7 @@ namespace WebSocketSharp
private bool accept () private bool accept ()
{ {
lock (_forConn) { lock (_forConn) {
var msg = _readyState.CheckIfCanAccept (); var msg = _readyState.CheckIfAvailable (true, false, false, false);
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
error ("An error has occurred in accepting.", null); error ("An error has occurred in accepting.", null);
@ -659,14 +659,14 @@ namespace WebSocketSharp
{ {
return _client return _client
? "This operation isn't available in the client." ? "This operation isn't available in the client."
: _readyState.CheckIfCanAccept (); : _readyState.CheckIfAvailable (true, false, false, false);
} }
private string checkIfCanConnect () private string checkIfCanConnect ()
{ {
return !_client return !_client
? "This operation isn't available in the server." ? "This operation isn't available in the server."
: _readyState.CheckIfCanConnect (); : _readyState.CheckIfAvailable (true, false, false, true);
} }
// As server // As server
@ -791,7 +791,7 @@ namespace WebSocketSharp
private bool connect () private bool connect ()
{ {
lock (_forConn) { lock (_forConn) {
var msg = _readyState.CheckIfCanConnect (); var msg = _readyState.CheckIfAvailable (true, false, false, true);
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
error ("An error has occurred in connecting.", null); error ("An error has occurred in connecting.", null);
@ -1806,7 +1806,7 @@ namespace WebSocketSharp
/// </summary> /// </summary>
public void Close () public void Close ()
{ {
var msg = _readyState.CheckIfCanClose (); var msg = _readyState.CheckIfAvailable (true, true, false, false);
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
error ("An error has occurred in closing the connection.", null); error ("An error has occurred in closing the connection.", null);
@ -1830,7 +1830,9 @@ namespace WebSocketSharp
/// </param> /// </param>
public void Close (ushort code) public void Close (ushort code)
{ {
var msg = _readyState.CheckIfCanClose () ?? CheckCloseParameters (code, null, _client); var msg = _readyState.CheckIfAvailable (true, true, false, false) ??
CheckCloseParameters (code, null, _client);
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
error ("An error has occurred in closing the connection.", null); error ("An error has occurred in closing the connection.", null);
@ -1857,7 +1859,9 @@ namespace WebSocketSharp
/// </param> /// </param>
public void Close (CloseStatusCode code) public void Close (CloseStatusCode code)
{ {
var msg = _readyState.CheckIfCanClose () ?? CheckCloseParameters (code, null, _client); var msg = _readyState.CheckIfAvailable (true, true, false, false) ??
CheckCloseParameters (code, null, _client);
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
error ("An error has occurred in closing the connection.", null); error ("An error has occurred in closing the connection.", null);
@ -1891,7 +1895,9 @@ namespace WebSocketSharp
/// </param> /// </param>
public void Close (ushort code, string reason) public void Close (ushort code, string reason)
{ {
var msg = _readyState.CheckIfCanClose () ?? CheckCloseParameters (code, reason, _client); var msg = _readyState.CheckIfAvailable (true, true, false, false) ??
CheckCloseParameters (code, reason, _client);
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
error ("An error has occurred in closing the connection.", null); error ("An error has occurred in closing the connection.", null);
@ -1925,7 +1931,9 @@ namespace WebSocketSharp
/// </param> /// </param>
public void Close (CloseStatusCode code, string reason) public void Close (CloseStatusCode code, string reason)
{ {
var msg = _readyState.CheckIfCanClose () ?? CheckCloseParameters (code, reason, _client); var msg = _readyState.CheckIfAvailable (true, true, false, false) ??
CheckCloseParameters (code, reason, _client);
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
error ("An error has occurred in closing the connection.", null); error ("An error has occurred in closing the connection.", null);
@ -1950,7 +1958,7 @@ namespace WebSocketSharp
/// </remarks> /// </remarks>
public void CloseAsync () public void CloseAsync ()
{ {
var msg = _readyState.CheckIfCanClose (); var msg = _readyState.CheckIfAvailable (true, true, false, false);
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
error ("An error has occurred in closing the connection.", null); error ("An error has occurred in closing the connection.", null);
@ -1979,7 +1987,9 @@ namespace WebSocketSharp
/// </param> /// </param>
public void CloseAsync (ushort code) public void CloseAsync (ushort code)
{ {
var msg = _readyState.CheckIfCanClose () ?? CheckCloseParameters (code, null, _client); var msg = _readyState.CheckIfAvailable (true, true, false, false) ??
CheckCloseParameters (code, null, _client);
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
error ("An error has occurred in closing the connection.", null); error ("An error has occurred in closing the connection.", null);
@ -2009,7 +2019,9 @@ namespace WebSocketSharp
/// </param> /// </param>
public void CloseAsync (CloseStatusCode code) public void CloseAsync (CloseStatusCode code)
{ {
var msg = _readyState.CheckIfCanClose () ?? CheckCloseParameters (code, null, _client); var msg = _readyState.CheckIfAvailable (true, true, false, false) ??
CheckCloseParameters (code, null, _client);
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
error ("An error has occurred in closing the connection.", null); error ("An error has occurred in closing the connection.", null);
@ -2048,7 +2060,9 @@ namespace WebSocketSharp
/// </param> /// </param>
public void CloseAsync (ushort code, string reason) public void CloseAsync (ushort code, string reason)
{ {
var msg = _readyState.CheckIfCanClose () ?? CheckCloseParameters (code, reason, _client); var msg = _readyState.CheckIfAvailable (true, true, false, false) ??
CheckCloseParameters (code, reason, _client);
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
error ("An error has occurred in closing the connection.", null); error ("An error has occurred in closing the connection.", null);
@ -2088,7 +2102,9 @@ namespace WebSocketSharp
/// </param> /// </param>
public void CloseAsync (CloseStatusCode code, string reason) public void CloseAsync (CloseStatusCode code, string reason)
{ {
var msg = _readyState.CheckIfCanClose () ?? CheckCloseParameters (code, reason, _client); var msg = _readyState.CheckIfAvailable (true, true, false, false) ??
CheckCloseParameters (code, reason, _client);
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
error ("An error has occurred in closing the connection.", null); error ("An error has occurred in closing the connection.", null);
@ -2206,7 +2222,9 @@ namespace WebSocketSharp
/// </param> /// </param>
public void Send (byte[] data) public void Send (byte[] data)
{ {
var msg = _readyState.CheckIfCanSend () ?? data.CheckIfValidSendData (); var msg = _readyState.CheckIfAvailable (false, true, false, false) ??
data.CheckIfValidSendData ();
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
error ("An error has occurred in sending the data.", null); error ("An error has occurred in sending the data.", null);
@ -2225,7 +2243,9 @@ namespace WebSocketSharp
/// </param> /// </param>
public void Send (FileInfo file) public void Send (FileInfo file)
{ {
var msg = _readyState.CheckIfCanSend () ?? file.CheckIfValidSendData (); var msg = _readyState.CheckIfAvailable (false, true, false, false) ??
file.CheckIfValidSendData ();
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
error ("An error has occurred in sending the data.", null); error ("An error has occurred in sending the data.", null);
@ -2244,7 +2264,9 @@ namespace WebSocketSharp
/// </param> /// </param>
public void Send (string data) public void Send (string data)
{ {
var msg = _readyState.CheckIfCanSend () ?? data.CheckIfValidSendData (); var msg = _readyState.CheckIfAvailable (false, true, false, false) ??
data.CheckIfValidSendData ();
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
error ("An error has occurred in sending the data.", null); error ("An error has occurred in sending the data.", null);
@ -2271,7 +2293,9 @@ namespace WebSocketSharp
/// </param> /// </param>
public void SendAsync (byte[] data, Action<bool> completed) public void SendAsync (byte[] data, Action<bool> completed)
{ {
var msg = _readyState.CheckIfCanSend () ?? data.CheckIfValidSendData (); var msg = _readyState.CheckIfAvailable (false, true, false, false) ??
data.CheckIfValidSendData ();
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
error ("An error has occurred in sending the data.", null); error ("An error has occurred in sending the data.", null);
@ -2299,7 +2323,9 @@ namespace WebSocketSharp
/// </param> /// </param>
public void SendAsync (FileInfo file, Action<bool> completed) public void SendAsync (FileInfo file, Action<bool> completed)
{ {
var msg = _readyState.CheckIfCanSend () ?? file.CheckIfValidSendData (); var msg = _readyState.CheckIfAvailable (false, true, false, false) ??
file.CheckIfValidSendData ();
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
error ("An error has occurred in sending the data.", null); error ("An error has occurred in sending the data.", null);
@ -2326,7 +2352,9 @@ namespace WebSocketSharp
/// </param> /// </param>
public void SendAsync (string data, Action<bool> completed) public void SendAsync (string data, Action<bool> completed)
{ {
var msg = _readyState.CheckIfCanSend () ?? data.CheckIfValidSendData (); var msg = _readyState.CheckIfAvailable (false, true, false, false) ??
data.CheckIfValidSendData ();
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
error ("An error has occurred in sending the data.", null); error ("An error has occurred in sending the data.", null);
@ -2357,7 +2385,7 @@ namespace WebSocketSharp
/// </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.CheckIfCanSend () ?? var msg = _readyState.CheckIfAvailable (false, true, false, false) ??
stream.CheckIfCanRead () ?? stream.CheckIfCanRead () ??
(length < 1 ? "'length' is less than 1." : null); (length < 1 ? "'length' is less than 1." : null);