From cd035247dbb040148a5269e591c98620168ae87c Mon Sep 17 00:00:00 2001 From: Eric Chou Date: Thu, 20 Dec 2012 18:36:58 +0800 Subject: [PATCH] Bug 811683 - Increase speed of sending file, r=gyeh --- dom/bluetooth/BluetoothOppManager.cpp | 32 ++++++++++++++++++--------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/dom/bluetooth/BluetoothOppManager.cpp b/dom/bluetooth/BluetoothOppManager.cpp index 590fd507b27..5ac29620616 100644 --- a/dom/bluetooth/BluetoothOppManager.cpp +++ b/dom/bluetooth/BluetoothOppManager.cpp @@ -17,6 +17,7 @@ #include "mozilla/RefPtr.h" #include "mozilla/Services.h" #include "mozilla/StaticPtr.h" +#include "nsAutoPtr.h" #include "nsCExternalHandlerService.h" #include "nsIObserver.h" #include "nsIObserverService.h" @@ -76,6 +77,13 @@ public: namespace { // Sending system message "bluetooth-opp-update-progress" every 50kb static const uint32_t kUpdateProgressBase = 50 * 1024; + +/* + * The format of the header of an PUT request is + * [opcode:1][packet length:2][headerId:1][header length:2] + */ +static const uint32_t kPutRequestHeaderSize = 6; + StaticRefPtr sInstance; StaticRefPtr sOppObserver; @@ -114,24 +122,24 @@ BluetoothOppManagerObserver::Observe(nsISupports* aSubject, class ReadFileTask : public nsRunnable { public: - ReadFileTask(nsIInputStream* aInputStream) : mInputStream(aInputStream) + ReadFileTask(nsIInputStream* aInputStream, + uint32_t aRemoteMaxPacketSize) : mInputStream(aInputStream) { MOZ_ASSERT(NS_IsMainThread()); + + mAvailablePacketSize = aRemoteMaxPacketSize - kPutRequestHeaderSize; } NS_IMETHOD Run() { MOZ_ASSERT(!NS_IsMainThread()); - /* - * 255 is the Minimum OBEX Packet Length (See section 3.3.1.4, - * IrOBEX ver 1.2) - */ - char buf[255]; uint32_t numRead; + nsAutoArrayPtr buf; + buf = new char[mAvailablePacketSize]; // function inputstream->Read() only works on non-main thread - nsresult rv = mInputStream->Read(buf, sizeof(buf), &numRead); + nsresult rv = mInputStream->Read(buf.get(), mAvailablePacketSize, &numRead); if (NS_FAILED(rv)) { // Needs error handling here return NS_ERROR_FAILURE; @@ -141,7 +149,7 @@ public: if (sSentFileLength + numRead >= sFileLength) { sWaitingToSendPutFinal = true; } - sInstance->SendPutRequest((uint8_t*)buf, numRead); + sInstance->SendPutRequest((uint8_t*)buf.get(), numRead); sSentFileLength += numRead; } @@ -150,6 +158,7 @@ public: private: nsCOMPtr mInputStream; + uint32_t mAvailablePacketSize; }; BluetoothOppManager::BluetoothOppManager() : mConnected(false) @@ -829,7 +838,8 @@ BluetoothOppManager::ClientDataHandler(UnixSocketRawData* aMessage) } } - nsRefPtr task = new ReadFileTask(mInputStream); + nsRefPtr task = new ReadFileTask(mInputStream, + mRemoteMaxPacketLength); rv = mReadFileThread->Dispatch(task, NS_DISPATCH_NORMAL); if (NS_FAILED(rv)) { NS_WARNING("Cannot dispatch read file task!"); @@ -915,8 +925,7 @@ void BluetoothOppManager::SendPutRequest(uint8_t* aFileBody, int aFileBodyLength) { - int index = 3; - int packetLeftSpace = mRemoteMaxPacketLength - index - 3; + int packetLeftSpace = mRemoteMaxPacketLength - kPutRequestHeaderSize; if (!mConnected) return; if (aFileBodyLength > packetLeftSpace) { @@ -928,6 +937,7 @@ BluetoothOppManager::SendPutRequest(uint8_t* aFileBody, // [opcode:1][length:2][Headers:var] uint8_t* req = new uint8_t[mRemoteMaxPacketLength]; + int index = 3; index += AppendHeaderBody(&req[index], aFileBody, aFileBodyLength); SetObexPacketInfo(req, ObexRequestCode::Put, index);