TCPConnection can never be deteted if they fail to connect.

Since the TCPConnection has never been connected, they are not scheduled for ping hence will never be detected.

Also fix the case when reconnect fails, as it has become READABLE before, it also will not be deleted.

BUG=webrtc:4936
R=pthatcher@webrtc.org

Review URL: https://codereview.webrtc.org/1307083002 .

Cr-Commit-Position: refs/heads/master@{#9782}
diff --git a/webrtc/p2p/base/tcpport.cc b/webrtc/p2p/base/tcpport.cc
index 7dc416f..91b9944 100644
--- a/webrtc/p2p/base/tcpport.cc
+++ b/webrtc/p2p/base/tcpport.cc
@@ -29,32 +29,32 @@
  *  Data could only be sent in state 3. Sening data during state 2 & 6 will get
  *  EWOULDBLOCK, 4 & 5 EPIPE.
  *
- *                          7 -------------+
- *                          |Connected: N  |
- *         Timeout          |Writable:  N  |     Timeout
- *     +------------------->|Connection is |<----------------+
- *     |                    |Dead          |                 |
- *     |                    +--------------+                 |
- *     |                               ^                     |
- *     |            OnClose            |                     |
- *     |    +-----------------------+  |                     |
- *     |    |                       |  |Timeout              |
- *     |    v                       |  |                     |
- *   4 +----------+          5 -----+--+--+           6 -----+-----+
- *   |Connected: N|Send() or |Connected: N|           |Connected: Y|
- *   |Writable:  Y|Ping()    |Writable:  Y|OnConnect  |Writable:  Y|
- *   |PendingTCP:N+--------> |PendingTCP:Y+---------> |PendingTCP:N|
- *   |PretendWri:Y|          |PretendWri:Y|           |PretendWri:Y|
- *   +-----+------+          +------------+           +---+--+-----+
- *     ^   ^                                              |  |
- *     |   |                     OnClose                  |  |
- *     |   +----------------------------------------------+  |
- *     |                                                     |
- *     |                              Stun Binding Completed |
- *     |                                                     |
- *     |                    OnClose                          |
- *     +------------------------------------------------+    |
- *                                                      |    v
+ *         OS Timeout         7 -------------+
+ *   +----------------------->|Connected: N  |
+ *   |                        |Writable:  N  |     Timeout
+ *   |       Timeout          |Connection is |<----------------+
+ *   |   +------------------->|Dead          |                 |
+ *   |   |                    +--------------+                 |
+ *   |   |                               ^                     |
+ *   |   |            OnClose            |                     |
+ *   |   |    +-----------------------+  |                     |
+ *   |   |    |                       |  |Timeout              |
+ *   |   |    v                       |  |                     |
+ *   | 4 +----------+          5 -----+--+--+           6 -----+-----+
+ *   | |Connected: N|Send() or |Connected: N|           |Connected: Y|
+ *   | |Writable:  Y|Ping()    |Writable:  Y|OnConnect  |Writable:  Y|
+ *   | |PendingTCP:N+--------> |PendingTCP:Y+---------> |PendingTCP:N|
+ *   | |PretendWri:Y|          |PretendWri:Y|           |PretendWri:Y|
+ *   | +-----+------+          +------------+           +---+--+-----+
+ *   |   ^   ^                                              |  |
+ *   |   |   |                     OnClose                  |  |
+ *   |   |   +----------------------------------------------+  |
+ *   |   |                                                     |
+ *   |   |                              Stun Binding Completed |
+ *   |   |                                                     |
+ *   |   |                    OnClose                          |
+ *   |   +------------------------------------------------+    |
+ *   |                                                    |    v
  *  1 -----------+           2 -----------+Stun      3 -----------+
  *  |Connected: N|           |Connected: Y|Binding   |Connected: Y|
  *  |Writable:  N|OnConnect  |Writable:  N|Completed |Writable:  Y|
@@ -384,7 +384,7 @@
                             << socket_ip.ToSensitiveString()
                             << ", different from the local candidate IP "
                             << port()->ip().ToSensitiveString();
-    socket_->Close();
+    OnClose(socket, 0);
   }
 }
 
@@ -396,6 +396,9 @@
   // packet it can't send.
   if (connected()) {
     set_connected(false);
+
+    // Prevent the connection from being destroyed by redundant SignalClose
+    // events.
     pretending_to_be_writable_ = true;
 
     // We don't attempt reconnect right here. This is to avoid a case where the
@@ -403,6 +406,12 @@
     // when the connection is used to Send() or Ping().
     port()->thread()->PostDelayed(reconnection_timeout(), this,
                                   MSG_TCPCONNECTION_DELAYED_ONCLOSE);
+  } else if (!pretending_to_be_writable_) {
+    // OnClose could be called when the underneath socket times out during the
+    // initial connect() (i.e. |pretending_to_be_writable_| is false) . We have
+    // to manually destroy here as this connection, as never connected, will not
+    // be scheduled for ping to trigger destroy.
+    Destroy();
   }
 }
 
@@ -413,7 +422,7 @@
       // seconds, it's time to tear this down. This is the case for the original
       // TCP connection on passive side during a reconnect.
       if (pretending_to_be_writable_) {
-        set_write_state(STATE_WRITE_TIMEOUT);
+        Destroy();
       }
       break;
     default: