Bug 1229589 - Implements an efficient version of TCPSocket by removing stale input streams. r=jdm
authorAmit Chandra <amitforfriends_dns@yahoo.com>
Wed, 25 May 2016 12:18:58 -0700
changeset 371078 a4d93c6954d1e05478eb434072ab35de8577905e
parent 371077 7909d0735869e171d3daef18be07afef8abb2add
child 371079 e81b62dcc3326c77339a93e46223cc3cf81cba36
push id19232
push userbmo:gps@mozilla.com
push dateWed, 25 May 2016 22:36:54 +0000
reviewersjdm
bugs1229589
milestone49.0a1
Bug 1229589 - Implements an efficient version of TCPSocket by removing stale input streams. r=jdm
dom/network/TCPSocket.cpp
dom/network/TCPSocket.h
--- a/dom/network/TCPSocket.cpp
+++ b/dom/network/TCPSocket.cpp
@@ -402,17 +402,31 @@ TCPSocket::EnsureCopying()
   RefPtr<CopierCallbacks> callbacks = new CopierCallbacks(this);
   return mMultiplexStreamCopier->AsyncCopy(callbacks, nullptr);
 }
 
 void
 TCPSocket::NotifyCopyComplete(nsresult aStatus)
 {
   mAsyncCopierActive = false;
-  mMultiplexStream->RemoveStream(0);
+  
+  uint32_t countRemaining;
+  nsresult rvRemaining = mMultiplexStream->GetCount(&countRemaining);
+  NS_ENSURE_SUCCESS_VOID(rvRemaining);
+
+  while (countRemaining--) {
+    mMultiplexStream->RemoveStream(0);
+  }
+
+  while (!mPendingDataWhileCopierActive.IsEmpty()) {
+      nsCOMPtr<nsIInputStream> stream = mPendingDataWhileCopierActive[0];
+      mMultiplexStream->AppendStream(stream);
+      mPendingDataWhileCopierActive.RemoveElementAt(0);
+  }
+  
   if (mSocketBridgeParent) {
     mozilla::Unused << mSocketBridgeParent->SendUpdateBufferedAmount(BufferedAmount(),
                                                                      mTrackingNumber);
   }
 
   if (NS_FAILED(aStatus)) {
     MaybeReportErrorAndCloseIfOpen(aStatus);
     return;
@@ -900,16 +914,19 @@ TCPSocket::Send(nsIInputStream* aStream,
     mBufferedAmount = newBufferedAmount;
     return !bufferFull;
   }
 
   if (mWaitingForStartTLS) {
     // When we are waiting for starttls, newStream is added to pendingData
     // and will be appended to multiplexStream after tls had been set up.
     mPendingDataAfterStartTLS.AppendElement(aStream);
+  } else if (mAsyncCopierActive) {
+    // While the AsyncCopier is still active..
+    mPendingDataWhileCopierActive.AppendElement(aStream);
   } else {
     mMultiplexStream->AppendStream(aStream);
   }
 
   EnsureCopying();
 
 #ifdef MOZ_WIDGET_GONK
   // Collect transmitted amount for network statistics.
--- a/dom/network/TCPSocket.h
+++ b/dom/network/TCPSocket.h
@@ -234,16 +234,19 @@ private:
   uint32_t mTrackingNumber;
 
   // True if this socket has been upgraded to secure after the initial connection,
   // but the actual upgrade is waiting for an in-progress copy operation to complete.
   bool mWaitingForStartTLS;
   // The buffered data awaiting the TLS upgrade to finish.
   nsTArray<nsCOMPtr<nsIInputStream>> mPendingDataAfterStartTLS;
 
+  // The data to be sent while AsyncCopier is still active.
+  nsTArray<nsCOMPtr<nsIInputStream>> mPendingDataWhileCopierActive;
+
   bool mObserversActive;
 
 #ifdef MOZ_WIDGET_GONK
   // Number of bytes sent.
   uint32_t mTxBytes;
   // Number of bytes received.
   uint32_t mRxBytes;
   // The app that owns this socket.