Modified a few for the WebSocket.Connect and ConnectAsync methods

This commit is contained in:
sta 2015-08-08 15:39:31 +09:00
parent 8efe37eb2f
commit 5ceb4c4311
2 changed files with 26 additions and 10 deletions

View File

@ -208,6 +208,13 @@ namespace WebSocketSharp
: null; : null;
} }
internal static string CheckIfCanConnect (this WebSocketState state)
{
return state == WebSocketState.Open || state == WebSocketState.Closing
? "This operation has already been done."
: null;
}
internal static string CheckIfCanRead (this Stream stream) internal static string CheckIfCanRead (this Stream stream)
{ {
return stream == null return stream == null

View File

@ -664,9 +664,9 @@ namespace WebSocketSharp
private string checkIfCanConnect () private string checkIfCanConnect ()
{ {
return !_client && _readyState == WebSocketState.Closed return !_client
? "Connect isn't available to reconnect as a server." ? "This operation isn't available in the server."
: _readyState.CheckIfConnectable (); : _readyState.CheckIfCanConnect ();
} }
// As server // As server
@ -787,26 +787,27 @@ namespace WebSocketSharp
return ret; return ret;
} }
// As client
private bool connect () private bool connect ()
{ {
lock (_forConn) { lock (_forConn) {
var msg = _readyState.CheckIfConnectable (); var msg = _readyState.CheckIfCanConnect ();
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
error ("An error has occurred in handshaking.", null); error ("An error has occurred in connecting.", null);
return false; return false;
} }
try { try {
_readyState = WebSocketState.Connecting; _readyState = WebSocketState.Connecting;
if (_client ? doHandshake () : acceptHandshake ()) { if (doHandshake ()) {
_readyState = WebSocketState.Open; _readyState = WebSocketState.Open;
return true; return true;
} }
} }
catch (Exception ex) { catch (Exception ex) {
processException (ex, "An exception has occurred while handshaking."); processException (ex, "An exception has occurred while connecting.");
} }
return false; return false;
@ -2107,12 +2108,15 @@ namespace WebSocketSharp
/// <summary> /// <summary>
/// Establishes a WebSocket connection. /// Establishes a WebSocket connection.
/// </summary> /// </summary>
/// <remarks>
/// This method isn't available in the server.
/// </remarks>
public void Connect () public void Connect ()
{ {
var msg = checkIfCanConnect (); var msg = checkIfCanConnect ();
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
error ("An error has occurred in handshaking.", null); error ("An error has occurred in connecting.", null);
return; return;
} }
@ -2125,14 +2129,19 @@ namespace WebSocketSharp
/// Establishes a WebSocket connection asynchronously. /// Establishes a WebSocket connection asynchronously.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method doesn't wait for the connect to be complete. /// <para>
/// This method doesn't wait for the connect to be complete.
/// </para>
/// <para>
/// This method isn't available in the server.
/// </para>
/// </remarks> /// </remarks>
public void ConnectAsync () public void ConnectAsync ()
{ {
var msg = checkIfCanConnect (); var msg = checkIfCanConnect ();
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
error ("An error has occurred in handshaking.", null); error ("An error has occurred in connecting.", null);
return; return;
} }