1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/base/src/Tickler.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,130 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef mozilla_net_Tickler_h 1.10 +#define mozilla_net_Tickler_h 1.11 + 1.12 +// The tickler sends a regular 0 byte UDP heartbeat out to a 1.13 +// particular address for a short time after it has been touched. This 1.14 +// is used on some mobile wifi chipsets to mitigate Power Save Polling 1.15 +// (PSP) Mode when we are anticipating a response packet 1.16 +// soon. Typically PSP adds 100ms of latency to a read event because 1.17 +// the packet delivery is not triggered until the 802.11 beacon is 1.18 +// delivered to the host (100ms is the standard Access Point 1.19 +// configuration for the beacon interval.) Requesting a frequent 1.20 +// transmission and getting a CTS frame from the AP at least that 1.21 +// frequently allows for low latency receives when we have reason to 1.22 +// expect them (e.g a SYN-ACK). 1.23 +// 1.24 +// The tickler is used to allow RTT based phases of web transport to 1.25 +// complete quickly when on wifi - ARP, DNS, TCP handshake, SSL 1.26 +// handshake, HTTP headers, and the TCP slow start phase. The 1.27 +// transaction is given up to 400 miliseconds by default to get 1.28 +// through those phases before the tickler is disabled. 1.29 +// 1.30 +// The tickler only applies to wifi on mobile right now. Hopefully it 1.31 +// can also be restricted to particular handset models in the future. 1.32 + 1.33 +#if defined(ANDROID) && !defined(MOZ_B2G) 1.34 +#define MOZ_USE_WIFI_TICKLER 1.35 +#endif 1.36 + 1.37 +#include "mozilla/Attributes.h" 1.38 +#include "nsISupports.h" 1.39 +#include <stdint.h> 1.40 + 1.41 +#ifdef MOZ_USE_WIFI_TICKLER 1.42 +#include "mozilla/Mutex.h" 1.43 +#include "mozilla/TimeStamp.h" 1.44 +#include "nsAutoPtr.h" 1.45 +#include "nsISupports.h" 1.46 +#include "nsIThread.h" 1.47 +#include "nsITimer.h" 1.48 +#include "nsWeakReference.h" 1.49 +#include "prio.h" 1.50 + 1.51 +class nsIPrefBranch; 1.52 +#endif 1.53 + 1.54 +namespace mozilla { 1.55 +namespace net { 1.56 + 1.57 +#ifdef MOZ_USE_WIFI_TICKLER 1.58 + 1.59 +// 8f769ed6-207c-4af9-9f7e-9e832da3754e 1.60 +#define NS_TICKLER_IID \ 1.61 +{ 0x8f769ed6, 0x207c, 0x4af9, \ 1.62 + { 0x9f, 0x7e, 0x9e, 0x83, 0x2d, 0xa3, 0x75, 0x4e } } 1.63 + 1.64 +class Tickler MOZ_FINAL : public nsSupportsWeakReference 1.65 +{ 1.66 +public: 1.67 + NS_DECL_THREADSAFE_ISUPPORTS 1.68 + NS_DECLARE_STATIC_IID_ACCESSOR(NS_TICKLER_IID) 1.69 + 1.70 + // These methods are main thread only 1.71 + Tickler(); 1.72 + ~Tickler(); 1.73 + void Cancel(); 1.74 + nsresult Init(); 1.75 + void SetIPV4Address(uint32_t address); 1.76 + void SetIPV4Port(uint16_t port); 1.77 + 1.78 + // Tickle the tickler to (re-)start the activity. 1.79 + // May call from any thread 1.80 + void Tickle(); 1.81 + 1.82 +private: 1.83 + friend class TicklerTimer; 1.84 + Mutex mLock; 1.85 + nsCOMPtr<nsIThread> mThread; 1.86 + nsCOMPtr<nsITimer> mTimer; 1.87 + nsCOMPtr<nsIPrefBranch> mPrefs; 1.88 + 1.89 + bool mActive; 1.90 + bool mCanceled; 1.91 + bool mEnabled; 1.92 + uint32_t mDelay; 1.93 + TimeDuration mDuration; 1.94 + PRFileDesc* mFD; 1.95 + 1.96 + TimeStamp mLastTickle; 1.97 + PRNetAddr mAddr; 1.98 + 1.99 + // These functions may be called from any thread 1.100 + void PostCheckTickler(); 1.101 + void MaybeStartTickler(); 1.102 + void MaybeStartTicklerUnlocked(); 1.103 + 1.104 + // Tickler thread only 1.105 + void CheckTickler(); 1.106 + void StartTickler(); 1.107 + void StopTickler(); 1.108 +}; 1.109 + 1.110 +NS_DEFINE_STATIC_IID_ACCESSOR(Tickler, NS_TICKLER_IID) 1.111 + 1.112 +#else // not defined MOZ_USE_WIFI_TICKLER 1.113 + 1.114 +class Tickler MOZ_FINAL : public nsISupports 1.115 +{ 1.116 +public: 1.117 + NS_DECL_THREADSAFE_ISUPPORTS 1.118 + 1.119 + Tickler() { } 1.120 + ~Tickler() { } 1.121 + nsresult Init() { return NS_ERROR_NOT_IMPLEMENTED; } 1.122 + void Cancel() { } 1.123 + void SetIPV4Address(uint32_t) { }; 1.124 + void SetIPV4Port(uint16_t) { } 1.125 + void Tickle() { } 1.126 +}; 1.127 + 1.128 +#endif // defined MOZ_USE_WIFI_TICKLER 1.129 + 1.130 +} // namespace mozilla::net 1.131 +} // namespace mozilla 1.132 + 1.133 +#endif // mozilla_net_Tickler_h