netwerk/base/src/nsSocketTransportService2.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* vim:set ts=4 sw=4 sts=4 ci et: */
     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/. */
     6 #ifndef nsSocketTransportService2_h__
     7 #define nsSocketTransportService2_h__
     9 #include "nsPISocketTransportService.h"
    10 #include "nsIThreadInternal.h"
    11 #include "nsIRunnable.h"
    12 #include "nsEventQueue.h"
    13 #include "nsCOMPtr.h"
    14 #include "prinrval.h"
    15 #include "prlog.h"
    16 #include "prinit.h"
    17 #include "nsIObserver.h"
    18 #include "mozilla/Mutex.h"
    19 #include "mozilla/net/DashboardTypes.h"
    21 class nsASocketHandler;
    22 struct PRPollDesc;
    24 //-----------------------------------------------------------------------------
    26 #if defined(PR_LOGGING)
    27 //
    28 // set NSPR_LOG_MODULES=nsSocketTransport:5
    29 //
    30 extern PRLogModuleInfo *gSocketTransportLog;
    31 #endif
    32 #define SOCKET_LOG(args)     PR_LOG(gSocketTransportLog, PR_LOG_DEBUG, args)
    33 #define SOCKET_LOG_ENABLED() PR_LOG_TEST(gSocketTransportLog, PR_LOG_DEBUG)
    35 //-----------------------------------------------------------------------------
    37 #define NS_SOCKET_POLL_TIMEOUT PR_INTERVAL_NO_TIMEOUT
    39 //-----------------------------------------------------------------------------
    41 namespace mozilla {
    42 namespace net {
    43 // These maximums are borrowed from the linux kernel.
    44 static const int32_t kMaxTCPKeepIdle  = 32767; // ~9 hours.
    45 static const int32_t kMaxTCPKeepIntvl = 32767;
    46 static const int32_t kMaxTCPKeepCount   = 127;
    47 static const int32_t kDefaultTCPKeepCount =
    48 #if defined (XP_WIN)
    49                                               10; // Hardcoded in Windows.
    50 #elif defined (XP_MACOSX)
    51                                               8;  // Hardcoded in OSX.
    52 #else
    53                                               4;  // Specifiable in Linux.
    54 #endif
    55 }
    56 }
    58 //-----------------------------------------------------------------------------
    60 class nsSocketTransportService : public nsPISocketTransportService
    61                                , public nsIEventTarget
    62                                , public nsIThreadObserver
    63                                , public nsIRunnable
    64                                , public nsIObserver
    65 {
    66     typedef mozilla::Mutex Mutex;
    68 public:
    69     NS_DECL_THREADSAFE_ISUPPORTS
    70     NS_DECL_NSPISOCKETTRANSPORTSERVICE
    71     NS_DECL_NSISOCKETTRANSPORTSERVICE
    72     NS_DECL_NSIEVENTTARGET
    73     NS_DECL_NSITHREADOBSERVER
    74     NS_DECL_NSIRUNNABLE
    75     NS_DECL_NSIOBSERVER 
    77     nsSocketTransportService();
    79     // Max Socket count may need to get initialized/used by nsHttpHandler
    80     // before this class is initialized.
    81     static uint32_t gMaxCount;
    82     static PRCallOnceType gMaxCountInitOnce;
    83     static PRStatus DiscoverMaxCount();
    85     //
    86     // the number of sockets that can be attached at any given time is
    87     // limited.  this is done because some operating systems (e.g., Win9x)
    88     // limit the number of sockets that can be created by an application.
    89     // AttachSocket will fail if the limit is exceeded.  consumers should
    90     // call CanAttachSocket and check the result before creating a socket.
    91     //
    92     bool CanAttachSocket() {
    93         return mActiveCount + mIdleCount < gMaxCount;
    94     }
    96     // Called by the networking dashboard on the socket thread only
    97     // Fills the passed array with socket information
    98     void GetSocketConnections(nsTArray<mozilla::net::SocketInfo> *);
    99     uint64_t GetSentBytes() { return mSentBytesCount; }
   100     uint64_t GetReceivedBytes() { return mReceivedBytesCount; }
   102     // Returns true if keepalives are enabled in prefs.
   103     bool IsKeepaliveEnabled() { return mKeepaliveEnabledPref; }
   104 protected:
   106     virtual ~nsSocketTransportService();
   108 private:
   110     //-------------------------------------------------------------------------
   111     // misc (any thread)
   112     //-------------------------------------------------------------------------
   114     nsCOMPtr<nsIThread> mThread;    // protected by mLock
   115     PRFileDesc *mThreadEvent;
   116                             // protected by mLock.  mThreadEvent may change
   117                             // if the old pollable event is broken.  only
   118                             // the socket thread may change mThreadEvent;
   119                             // it needs to lock mLock only when it changes
   120                             // mThreadEvent.  other threads don't change
   121                             // mThreadEvent; they need to lock mLock
   122                             // whenever they access mThreadEvent.
   123     bool        mAutodialEnabled;
   124                             // pref to control autodial code
   126     // Returns mThread, protecting the get-and-addref with mLock
   127     already_AddRefed<nsIThread> GetThreadSafely();
   129     //-------------------------------------------------------------------------
   130     // initialization and shutdown (any thread)
   131     //-------------------------------------------------------------------------
   133     Mutex         mLock;
   134     bool          mInitialized;
   135     bool          mShuttingDown;
   136                             // indicates whether we are currently in the
   137                             // process of shutting down
   138     bool          mOffline;
   139     bool          mGoingOffline;
   141     // Detaches all sockets.
   142     void Reset(bool aGuardLocals);
   144     //-------------------------------------------------------------------------
   145     // socket lists (socket thread only)
   146     //
   147     // only "active" sockets are on the poll list.  the active list is kept
   148     // in sync with the poll list such that:
   149     //
   150     //   mActiveList[k].mFD == mPollList[k+1].fd
   151     //
   152     // where k=0,1,2,...
   153     //-------------------------------------------------------------------------
   155     struct SocketContext
   156     {
   157         PRFileDesc       *mFD;
   158         nsASocketHandler *mHandler;
   159         uint16_t          mElapsedTime;  // time elapsed w/o activity
   160     };
   162     SocketContext *mActiveList;                   /* mListSize entries */
   163     SocketContext *mIdleList;                     /* mListSize entries */
   165     uint32_t mActiveListSize;
   166     uint32_t mIdleListSize;
   167     uint32_t mActiveCount;
   168     uint32_t mIdleCount;
   170     nsresult DetachSocket(SocketContext *, SocketContext *);
   171     nsresult AddToIdleList(SocketContext *);
   172     nsresult AddToPollList(SocketContext *);
   173     void RemoveFromIdleList(SocketContext *);
   174     void RemoveFromPollList(SocketContext *);
   175     void MoveToIdleList(SocketContext *sock);
   176     void MoveToPollList(SocketContext *sock);
   178     bool GrowActiveList();
   179     bool GrowIdleList();
   180     void   InitMaxCount();
   182     // Total bytes number transfered through all the sockets except active ones
   183     uint64_t mSentBytesCount;
   184     uint64_t mReceivedBytesCount;
   185     //-------------------------------------------------------------------------
   186     // poll list (socket thread only)
   187     //
   188     // first element of the poll list is mThreadEvent (or null if the pollable
   189     // event cannot be created).
   190     //-------------------------------------------------------------------------
   192     PRPollDesc *mPollList;                        /* mListSize + 1 entries */
   194     PRIntervalTime PollTimeout();            // computes ideal poll timeout
   195     nsresult       DoPollIteration(bool wait);
   196                                              // perfoms a single poll iteration
   197     int32_t        Poll(bool wait, uint32_t *interval);
   198                                              // calls PR_Poll.  the out param
   199                                              // interval indicates the poll
   200                                              // duration in seconds.
   202     //-------------------------------------------------------------------------
   203     // pending socket queue - see NotifyWhenCanAttachSocket
   204     //-------------------------------------------------------------------------
   206     nsEventQueue mPendingSocketQ; // queue of nsIRunnable objects
   208     // Preference Monitor for SendBufferSize and Keepalive prefs.
   209     nsresult    UpdatePrefs();
   210     int32_t     mSendBufferSize;
   211     // Number of seconds of connection is idle before first keepalive ping.
   212     int32_t     mKeepaliveIdleTimeS;
   213     // Number of seconds between retries should keepalive pings fail.
   214     int32_t     mKeepaliveRetryIntervalS;
   215     // Number of keepalive probes to send.
   216     int32_t     mKeepaliveProbeCount;
   217     // True if TCP keepalive is enabled globally.
   218     bool        mKeepaliveEnabledPref;
   220     void OnKeepaliveEnabledPrefChange();
   221     void NotifyKeepaliveEnabledPrefChange(SocketContext *sock);
   223     // Socket thread only for dynamically adjusting max socket size
   224 #if defined(XP_WIN)
   225     void ProbeMaxCount();
   226 #endif
   227     bool mProbedMaxCount;
   229     void AnalyzeConnection(nsTArray<mozilla::net::SocketInfo> *data,
   230                            SocketContext *context, bool aActive);
   232     void ClosePrivateConnections();
   233     void DetachSocketWithGuard(bool aGuardLocals,
   234                                SocketContext *socketList,
   235                                int32_t index);
   236 };
   238 extern nsSocketTransportService *gSocketTransportService;
   239 extern PRThread                 *gSocketThread;
   241 #endif // !nsSocketTransportService_h__

mercurial