Tickler.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_net_Tickler_h
  6. #define mozilla_net_Tickler_h
  7. // The tickler sends a regular 0 byte UDP heartbeat out to a
  8. // particular address for a short time after it has been touched. This
  9. // is used on some mobile wifi chipsets to mitigate Power Save Polling
  10. // (PSP) Mode when we are anticipating a response packet
  11. // soon. Typically PSP adds 100ms of latency to a read event because
  12. // the packet delivery is not triggered until the 802.11 beacon is
  13. // delivered to the host (100ms is the standard Access Point
  14. // configuration for the beacon interval.) Requesting a frequent
  15. // transmission and getting a CTS frame from the AP at least that
  16. // frequently allows for low latency receives when we have reason to
  17. // expect them (e.g a SYN-ACK).
  18. //
  19. // The tickler is used to allow RTT based phases of web transport to
  20. // complete quickly when on wifi - ARP, DNS, TCP handshake, SSL
  21. // handshake, HTTP headers, and the TCP slow start phase. The
  22. // transaction is given up to 400 miliseconds by default to get
  23. // through those phases before the tickler is disabled.
  24. //
  25. // The tickler only applies to wifi on mobile right now. Hopefully it
  26. // can also be restricted to particular handset models in the future.
  27. #include "mozilla/Attributes.h"
  28. #include "nsISupports.h"
  29. #include <stdint.h>
  30. #ifdef MOZ_USE_WIFI_TICKLER
  31. #include "mozilla/Mutex.h"
  32. #include "mozilla/TimeStamp.h"
  33. #include "nsAutoPtr.h"
  34. #include "nsISupports.h"
  35. #include "nsIThread.h"
  36. #include "nsITimer.h"
  37. #include "nsWeakReference.h"
  38. #include "prio.h"
  39. class nsIPrefBranch;
  40. #endif
  41. namespace mozilla {
  42. namespace net {
  43. #ifdef MOZ_USE_WIFI_TICKLER
  44. // 8f769ed6-207c-4af9-9f7e-9e832da3754e
  45. #define NS_TICKLER_IID \
  46. { 0x8f769ed6, 0x207c, 0x4af9, \
  47. { 0x9f, 0x7e, 0x9e, 0x83, 0x2d, 0xa3, 0x75, 0x4e } }
  48. class Tickler final : public nsSupportsWeakReference
  49. {
  50. public:
  51. NS_DECL_THREADSAFE_ISUPPORTS
  52. NS_DECLARE_STATIC_IID_ACCESSOR(NS_TICKLER_IID)
  53. // These methods are main thread only
  54. Tickler();
  55. void Cancel();
  56. nsresult Init();
  57. void SetIPV4Address(uint32_t address);
  58. void SetIPV4Port(uint16_t port);
  59. // Tickle the tickler to (re-)start the activity.
  60. // May call from any thread
  61. void Tickle();
  62. private:
  63. ~Tickler();
  64. friend class TicklerTimer;
  65. Mutex mLock;
  66. nsCOMPtr<nsIThread> mThread;
  67. nsCOMPtr<nsITimer> mTimer;
  68. nsCOMPtr<nsIPrefBranch> mPrefs;
  69. bool mActive;
  70. bool mCanceled;
  71. bool mEnabled;
  72. uint32_t mDelay;
  73. TimeDuration mDuration;
  74. PRFileDesc* mFD;
  75. TimeStamp mLastTickle;
  76. PRNetAddr mAddr;
  77. // These functions may be called from any thread
  78. void PostCheckTickler();
  79. void MaybeStartTickler();
  80. void MaybeStartTicklerUnlocked();
  81. // Tickler thread only
  82. void CheckTickler();
  83. void StartTickler();
  84. void StopTickler();
  85. };
  86. NS_DEFINE_STATIC_IID_ACCESSOR(Tickler, NS_TICKLER_IID)
  87. #else // not defined MOZ_USE_WIFI_TICKLER
  88. class Tickler final : public nsISupports
  89. {
  90. ~Tickler() { }
  91. public:
  92. NS_DECL_THREADSAFE_ISUPPORTS
  93. Tickler() { }
  94. nsresult Init() { return NS_ERROR_NOT_IMPLEMENTED; }
  95. void Cancel() { }
  96. void SetIPV4Address(uint32_t) { };
  97. void SetIPV4Port(uint16_t) { }
  98. void Tickle() { }
  99. };
  100. #endif // defined MOZ_USE_WIFI_TICKLER
  101. } // namespace net
  102. } // namespace mozilla
  103. #endif // mozilla_net_Tickler_h