diff --git a/websocket-sharp/Ext.cs b/websocket-sharp/Ext.cs index 95737fe0..4d781d32 100644 --- a/websocket-sharp/Ext.cs +++ b/websocket-sharp/Ext.cs @@ -211,24 +211,14 @@ namespace WebSocketSharp : 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 - ? "This operation has already been done." - : null; - } - - 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." + return (!connecting && state == WebSocketState.Connecting) || + (!open && state == WebSocketState.Open) || + (!closing && state == WebSocketState.Closing) || + (!closed && state == WebSocketState.Closed) + ? "This operation isn't available in: " + state : null; } @@ -241,13 +231,6 @@ namespace WebSocketSharp : 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) { return protocols.Contains ( diff --git a/websocket-sharp/WebSocket.cs b/websocket-sharp/WebSocket.cs index d874713c..536c9126 100644 --- a/websocket-sharp/WebSocket.cs +++ b/websocket-sharp/WebSocket.cs @@ -596,7 +596,7 @@ namespace WebSocketSharp private bool accept () { lock (_forConn) { - var msg = _readyState.CheckIfCanAccept (); + var msg = _readyState.CheckIfAvailable (true, false, false, false); if (msg != null) { _logger.Error (msg); error ("An error has occurred in accepting.", null); @@ -659,14 +659,14 @@ namespace WebSocketSharp { return _client ? "This operation isn't available in the client." - : _readyState.CheckIfCanAccept (); + : _readyState.CheckIfAvailable (true, false, false, false); } private string checkIfCanConnect () { return !_client ? "This operation isn't available in the server." - : _readyState.CheckIfCanConnect (); + : _readyState.CheckIfAvailable (true, false, false, true); } // As server @@ -791,7 +791,7 @@ namespace WebSocketSharp private bool connect () { lock (_forConn) { - var msg = _readyState.CheckIfCanConnect (); + var msg = _readyState.CheckIfAvailable (true, false, false, true); if (msg != null) { _logger.Error (msg); error ("An error has occurred in connecting.", null); @@ -1806,7 +1806,7 @@ namespace WebSocketSharp /// public void Close () { - var msg = _readyState.CheckIfCanClose (); + var msg = _readyState.CheckIfAvailable (true, true, false, false); if (msg != null) { _logger.Error (msg); error ("An error has occurred in closing the connection.", null); @@ -1830,7 +1830,9 @@ namespace WebSocketSharp /// 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) { _logger.Error (msg); error ("An error has occurred in closing the connection.", null); @@ -1857,7 +1859,9 @@ namespace WebSocketSharp /// 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) { _logger.Error (msg); error ("An error has occurred in closing the connection.", null); @@ -1891,7 +1895,9 @@ namespace WebSocketSharp /// 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) { _logger.Error (msg); error ("An error has occurred in closing the connection.", null); @@ -1925,7 +1931,9 @@ namespace WebSocketSharp /// 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) { _logger.Error (msg); error ("An error has occurred in closing the connection.", null); @@ -1950,7 +1958,7 @@ namespace WebSocketSharp /// public void CloseAsync () { - var msg = _readyState.CheckIfCanClose (); + var msg = _readyState.CheckIfAvailable (true, true, false, false); if (msg != null) { _logger.Error (msg); error ("An error has occurred in closing the connection.", null); @@ -1979,7 +1987,9 @@ namespace WebSocketSharp /// 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) { _logger.Error (msg); error ("An error has occurred in closing the connection.", null); @@ -2009,7 +2019,9 @@ namespace WebSocketSharp /// 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) { _logger.Error (msg); error ("An error has occurred in closing the connection.", null); @@ -2048,7 +2060,9 @@ namespace WebSocketSharp /// 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) { _logger.Error (msg); error ("An error has occurred in closing the connection.", null); @@ -2088,7 +2102,9 @@ namespace WebSocketSharp /// 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) { _logger.Error (msg); error ("An error has occurred in closing the connection.", null); @@ -2206,7 +2222,9 @@ namespace WebSocketSharp /// public void Send (byte[] data) { - var msg = _readyState.CheckIfCanSend () ?? data.CheckIfValidSendData (); + var msg = _readyState.CheckIfAvailable (false, true, false, false) ?? + data.CheckIfValidSendData (); + if (msg != null) { _logger.Error (msg); error ("An error has occurred in sending the data.", null); @@ -2225,7 +2243,9 @@ namespace WebSocketSharp /// public void Send (FileInfo file) { - var msg = _readyState.CheckIfCanSend () ?? file.CheckIfValidSendData (); + var msg = _readyState.CheckIfAvailable (false, true, false, false) ?? + file.CheckIfValidSendData (); + if (msg != null) { _logger.Error (msg); error ("An error has occurred in sending the data.", null); @@ -2244,7 +2264,9 @@ namespace WebSocketSharp /// public void Send (string data) { - var msg = _readyState.CheckIfCanSend () ?? data.CheckIfValidSendData (); + var msg = _readyState.CheckIfAvailable (false, true, false, false) ?? + data.CheckIfValidSendData (); + if (msg != null) { _logger.Error (msg); error ("An error has occurred in sending the data.", null); @@ -2271,7 +2293,9 @@ namespace WebSocketSharp /// public void SendAsync (byte[] data, Action completed) { - var msg = _readyState.CheckIfCanSend () ?? data.CheckIfValidSendData (); + var msg = _readyState.CheckIfAvailable (false, true, false, false) ?? + data.CheckIfValidSendData (); + if (msg != null) { _logger.Error (msg); error ("An error has occurred in sending the data.", null); @@ -2299,7 +2323,9 @@ namespace WebSocketSharp /// public void SendAsync (FileInfo file, Action completed) { - var msg = _readyState.CheckIfCanSend () ?? file.CheckIfValidSendData (); + var msg = _readyState.CheckIfAvailable (false, true, false, false) ?? + file.CheckIfValidSendData (); + if (msg != null) { _logger.Error (msg); error ("An error has occurred in sending the data.", null); @@ -2326,7 +2352,9 @@ namespace WebSocketSharp /// public void SendAsync (string data, Action completed) { - var msg = _readyState.CheckIfCanSend () ?? data.CheckIfValidSendData (); + var msg = _readyState.CheckIfAvailable (false, true, false, false) ?? + data.CheckIfValidSendData (); + if (msg != null) { _logger.Error (msg); error ("An error has occurred in sending the data.", null); @@ -2357,7 +2385,7 @@ namespace WebSocketSharp /// public void SendAsync (Stream stream, int length, Action completed) { - var msg = _readyState.CheckIfCanSend () ?? + var msg = _readyState.CheckIfAvailable (false, true, false, false) ?? stream.CheckIfCanRead () ?? (length < 1 ? "'length' is less than 1." : null);