2007-03-22 10:30:00 -07:00
|
|
|
/* vim:set ts=4 sw=4 sts=4 ci et: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#ifndef nsSocketTransportService2_h__
|
|
|
|
#define nsSocketTransportService2_h__
|
|
|
|
|
|
|
|
#include "nsPISocketTransportService.h"
|
|
|
|
#include "nsIThreadInternal.h"
|
2013-09-19 06:54:39 -07:00
|
|
|
#include "nsIRunnable.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsEventQueue.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "prinrval.h"
|
2015-05-19 11:15:34 -07:00
|
|
|
#include "mozilla/Logging.h"
|
2011-04-13 05:59:29 -07:00
|
|
|
#include "prinit.h"
|
2009-02-09 09:31:44 -08:00
|
|
|
#include "nsIObserver.h"
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-03-31 21:29:02 -07:00
|
|
|
#include "mozilla/Mutex.h"
|
2012-12-10 06:13:55 -08:00
|
|
|
#include "mozilla/net/DashboardTypes.h"
|
2015-03-04 05:17:00 -08:00
|
|
|
#include "mozilla/Atomics.h"
|
2015-03-12 02:25:13 -07:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-09-21 20:04:57 -07:00
|
|
|
class nsASocketHandler;
|
2013-09-22 20:35:05 -07:00
|
|
|
struct PRPollDesc;
|
2013-09-21 20:04:57 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//
|
|
|
|
// set NSPR_LOG_MODULES=nsSocketTransport:5
|
|
|
|
//
|
|
|
|
extern PRLogModuleInfo *gSocketTransportLog;
|
2015-06-03 15:25:57 -07:00
|
|
|
#define SOCKET_LOG(args) MOZ_LOG(gSocketTransportLog, mozilla::LogLevel::Debug, args)
|
|
|
|
#define SOCKET_LOG_ENABLED() MOZ_LOG_TEST(gSocketTransportLog, mozilla::LogLevel::Debug)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2015-05-29 07:13:39 -07:00
|
|
|
//
|
|
|
|
// set NSPR_LOG_MODULES=UDPSocket:5
|
|
|
|
//
|
|
|
|
extern PRLogModuleInfo *gUDPSocketLog;
|
2015-06-03 15:25:57 -07:00
|
|
|
#define UDPSOCKET_LOG(args) MOZ_LOG(gUDPSocketLog, mozilla::LogLevel::Debug, args)
|
|
|
|
#define UDPSOCKET_LOG_ENABLED() MOZ_LOG_TEST(gUDPSocketLog, mozilla::LogLevel::Debug)
|
2015-05-29 07:13:39 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#define NS_SOCKET_POLL_TIMEOUT PR_INTERVAL_NO_TIMEOUT
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-02-06 11:51:38 -08:00
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
// These maximums are borrowed from the linux kernel.
|
|
|
|
static const int32_t kMaxTCPKeepIdle = 32767; // ~9 hours.
|
|
|
|
static const int32_t kMaxTCPKeepIntvl = 32767;
|
|
|
|
static const int32_t kMaxTCPKeepCount = 127;
|
|
|
|
static const int32_t kDefaultTCPKeepCount =
|
|
|
|
#if defined (XP_WIN)
|
|
|
|
10; // Hardcoded in Windows.
|
|
|
|
#elif defined (XP_MACOSX)
|
|
|
|
8; // Hardcoded in OSX.
|
|
|
|
#else
|
|
|
|
4; // Specifiable in Linux.
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
class nsSocketTransportService final : public nsPISocketTransportService
|
2015-03-27 11:52:19 -07:00
|
|
|
, public nsIEventTarget
|
|
|
|
, public nsIThreadObserver
|
|
|
|
, public nsIRunnable
|
|
|
|
, public nsIObserver
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-03-31 21:29:02 -07:00
|
|
|
typedef mozilla::Mutex Mutex;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
public:
|
2013-07-18 19:24:13 -07:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_DECL_NSPISOCKETTRANSPORTSERVICE
|
|
|
|
NS_DECL_NSISOCKETTRANSPORTSERVICE
|
2015-04-09 08:31:59 -07:00
|
|
|
NS_DECL_NSIROUTEDSOCKETTRANSPORTSERVICE
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_DECL_NSIEVENTTARGET
|
|
|
|
NS_DECL_NSITHREADOBSERVER
|
|
|
|
NS_DECL_NSIRUNNABLE
|
2009-02-09 09:31:44 -08:00
|
|
|
NS_DECL_NSIOBSERVER
|
2015-07-09 20:21:46 -07:00
|
|
|
// missing from NS_DECL_NSIEVENTTARGET because MSVC
|
|
|
|
nsresult Dispatch(nsIRunnable* aEvent, uint32_t aFlags) {
|
|
|
|
return Dispatch(nsCOMPtr<nsIRunnable>(aEvent).forget(), aFlags);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsSocketTransportService();
|
|
|
|
|
2011-04-13 05:59:29 -07:00
|
|
|
// Max Socket count may need to get initialized/used by nsHttpHandler
|
|
|
|
// before this class is initialized.
|
2012-08-22 08:56:38 -07:00
|
|
|
static uint32_t gMaxCount;
|
2011-04-13 05:59:29 -07:00
|
|
|
static PRCallOnceType gMaxCountInitOnce;
|
|
|
|
static PRStatus DiscoverMaxCount();
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//
|
|
|
|
// the number of sockets that can be attached at any given time is
|
|
|
|
// limited. this is done because some operating systems (e.g., Win9x)
|
|
|
|
// limit the number of sockets that can be created by an application.
|
|
|
|
// AttachSocket will fail if the limit is exceeded. consumers should
|
|
|
|
// call CanAttachSocket and check the result before creating a socket.
|
|
|
|
//
|
2011-09-28 23:19:26 -07:00
|
|
|
bool CanAttachSocket() {
|
2011-04-13 05:59:29 -07:00
|
|
|
return mActiveCount + mIdleCount < gMaxCount;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-07-29 09:08:03 -07:00
|
|
|
// Called by the networking dashboard on the socket thread only
|
2012-12-10 06:13:55 -08:00
|
|
|
// Fills the passed array with socket information
|
|
|
|
void GetSocketConnections(nsTArray<mozilla::net::SocketInfo> *);
|
2013-07-29 09:08:03 -07:00
|
|
|
uint64_t GetSentBytes() { return mSentBytesCount; }
|
|
|
|
uint64_t GetReceivedBytes() { return mReceivedBytesCount; }
|
2014-02-06 11:51:38 -08:00
|
|
|
|
|
|
|
// Returns true if keepalives are enabled in prefs.
|
|
|
|
bool IsKeepaliveEnabled() { return mKeepaliveEnabledPref; }
|
2015-07-01 04:51:00 -07:00
|
|
|
|
|
|
|
bool IsTelemetryEnabled() { return mTelemetryEnabledPref; }
|
2007-03-22 10:30:00 -07:00
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual ~nsSocketTransportService();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// misc (any thread)
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
2007-12-04 18:18:58 -08:00
|
|
|
nsCOMPtr<nsIThread> mThread; // protected by mLock
|
2007-03-22 10:30:00 -07:00
|
|
|
PRFileDesc *mThreadEvent;
|
2007-08-26 13:14:57 -07:00
|
|
|
// protected by mLock. mThreadEvent may change
|
2007-05-30 10:30:39 -07:00
|
|
|
// if the old pollable event is broken. only
|
2007-08-26 13:14:57 -07:00
|
|
|
// the socket thread may change mThreadEvent;
|
|
|
|
// it needs to lock mLock only when it changes
|
|
|
|
// mThreadEvent. other threads don't change
|
|
|
|
// mThreadEvent; they need to lock mLock
|
|
|
|
// whenever they access mThreadEvent.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mAutodialEnabled;
|
2007-03-22 10:30:00 -07:00
|
|
|
// pref to control autodial code
|
|
|
|
|
2007-12-04 18:18:58 -08:00
|
|
|
// Returns mThread, protecting the get-and-addref with mLock
|
|
|
|
already_AddRefed<nsIThread> GetThreadSafely();
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// initialization and shutdown (any thread)
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-03-31 21:29:02 -07:00
|
|
|
Mutex mLock;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mInitialized;
|
|
|
|
bool mShuttingDown;
|
2007-03-22 10:30:00 -07:00
|
|
|
// indicates whether we are currently in the
|
|
|
|
// process of shutting down
|
2012-09-17 16:45:10 -07:00
|
|
|
bool mOffline;
|
|
|
|
bool mGoingOffline;
|
|
|
|
|
|
|
|
// Detaches all sockets.
|
|
|
|
void Reset(bool aGuardLocals);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// socket lists (socket thread only)
|
|
|
|
//
|
|
|
|
// only "active" sockets are on the poll list. the active list is kept
|
|
|
|
// in sync with the poll list such that:
|
|
|
|
//
|
|
|
|
// mActiveList[k].mFD == mPollList[k+1].fd
|
|
|
|
//
|
|
|
|
// where k=0,1,2,...
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
struct SocketContext
|
|
|
|
{
|
|
|
|
PRFileDesc *mFD;
|
|
|
|
nsASocketHandler *mHandler;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint16_t mElapsedTime; // time elapsed w/o activity
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2011-04-13 05:59:29 -07:00
|
|
|
SocketContext *mActiveList; /* mListSize entries */
|
|
|
|
SocketContext *mIdleList; /* mListSize entries */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mActiveListSize;
|
|
|
|
uint32_t mIdleListSize;
|
|
|
|
uint32_t mActiveCount;
|
|
|
|
uint32_t mIdleCount;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-04-13 05:59:29 -07:00
|
|
|
nsresult DetachSocket(SocketContext *, SocketContext *);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult AddToIdleList(SocketContext *);
|
|
|
|
nsresult AddToPollList(SocketContext *);
|
|
|
|
void RemoveFromIdleList(SocketContext *);
|
|
|
|
void RemoveFromPollList(SocketContext *);
|
|
|
|
void MoveToIdleList(SocketContext *sock);
|
|
|
|
void MoveToPollList(SocketContext *sock);
|
2011-04-13 05:59:29 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool GrowActiveList();
|
|
|
|
bool GrowIdleList();
|
2011-04-13 05:59:29 -07:00
|
|
|
void InitMaxCount();
|
2013-07-29 09:08:03 -07:00
|
|
|
|
|
|
|
// Total bytes number transfered through all the sockets except active ones
|
|
|
|
uint64_t mSentBytesCount;
|
|
|
|
uint64_t mReceivedBytesCount;
|
2007-03-22 10:30:00 -07:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// poll list (socket thread only)
|
|
|
|
//
|
|
|
|
// first element of the poll list is mThreadEvent (or null if the pollable
|
|
|
|
// event cannot be created).
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
2011-04-13 05:59:29 -07:00
|
|
|
PRPollDesc *mPollList; /* mListSize + 1 entries */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
PRIntervalTime PollTimeout(); // computes ideal poll timeout
|
2015-03-12 02:25:13 -07:00
|
|
|
nsresult DoPollIteration(bool wait,
|
|
|
|
mozilla::TimeDuration *pollDuration);
|
2007-03-22 10:30:00 -07:00
|
|
|
// perfoms a single poll iteration
|
2015-03-12 02:25:13 -07:00
|
|
|
int32_t Poll(bool wait,
|
|
|
|
uint32_t *interval,
|
|
|
|
mozilla::TimeDuration *pollDuration);
|
2007-03-22 10:30:00 -07:00
|
|
|
// calls PR_Poll. the out param
|
|
|
|
// interval indicates the poll
|
|
|
|
// duration in seconds.
|
2015-03-12 02:25:13 -07:00
|
|
|
// pollDuration is used only for
|
|
|
|
// telemetry
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// pending socket queue - see NotifyWhenCanAttachSocket
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsEventQueue mPendingSocketQ; // queue of nsIRunnable objects
|
2009-02-09 09:31:44 -08:00
|
|
|
|
2014-02-06 11:51:38 -08:00
|
|
|
// Preference Monitor for SendBufferSize and Keepalive prefs.
|
2009-02-09 09:31:44 -08:00
|
|
|
nsresult UpdatePrefs();
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t mSendBufferSize;
|
2014-02-06 11:51:38 -08:00
|
|
|
// Number of seconds of connection is idle before first keepalive ping.
|
|
|
|
int32_t mKeepaliveIdleTimeS;
|
|
|
|
// Number of seconds between retries should keepalive pings fail.
|
|
|
|
int32_t mKeepaliveRetryIntervalS;
|
|
|
|
// Number of keepalive probes to send.
|
|
|
|
int32_t mKeepaliveProbeCount;
|
|
|
|
// True if TCP keepalive is enabled globally.
|
|
|
|
bool mKeepaliveEnabledPref;
|
|
|
|
|
2015-03-04 05:17:00 -08:00
|
|
|
bool mServeMultipleEventsPerPollIter;
|
|
|
|
mozilla::Atomic<bool> mServingPendingQueue;
|
|
|
|
int32_t mMaxTimePerPollIter;
|
2015-03-12 02:25:13 -07:00
|
|
|
bool mTelemetryEnabledPref;
|
2015-03-04 05:17:00 -08:00
|
|
|
|
2014-02-06 11:51:38 -08:00
|
|
|
void OnKeepaliveEnabledPrefChange();
|
|
|
|
void NotifyKeepaliveEnabledPrefChange(SocketContext *sock);
|
2011-10-25 08:36:49 -07:00
|
|
|
|
|
|
|
// Socket thread only for dynamically adjusting max socket size
|
|
|
|
#if defined(XP_WIN)
|
|
|
|
void ProbeMaxCount();
|
|
|
|
#endif
|
|
|
|
bool mProbedMaxCount;
|
2012-12-10 06:13:55 -08:00
|
|
|
|
|
|
|
void AnalyzeConnection(nsTArray<mozilla::net::SocketInfo> *data,
|
|
|
|
SocketContext *context, bool aActive);
|
2012-12-07 14:50:43 -08:00
|
|
|
|
|
|
|
void ClosePrivateConnections();
|
2013-04-09 17:46:25 -07:00
|
|
|
void DetachSocketWithGuard(bool aGuardLocals,
|
|
|
|
SocketContext *socketList,
|
|
|
|
int32_t index);
|
2015-03-04 05:17:00 -08:00
|
|
|
|
|
|
|
void MarkTheLastElementOfPendingQueue();
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
extern nsSocketTransportService *gSocketTransportService;
|
|
|
|
extern PRThread *gSocketThread;
|
|
|
|
|
|
|
|
#endif // !nsSocketTransportService_h__
|