Bug 1229589 - Implements an efficient version of TCPSocket by removing stale input streams. r=jdm
--- 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.