2006-05-10 21:30:15 +04:00
|
|
|
/* vim:set ts=4 sw=4 sts=4 ci et: */
|
2012-05-21 15:12:37 +04: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/. */
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
#ifndef nsSocketTransportService2_h__
|
|
|
|
#define nsSocketTransportService2_h__
|
|
|
|
|
2005-04-15 03:18:34 +04:00
|
|
|
#include "nsPISocketTransportService.h"
|
2006-05-10 21:30:15 +04:00
|
|
|
#include "nsIThreadInternal.h"
|
2013-09-19 17:54:39 +04:00
|
|
|
#include "nsIRunnable.h"
|
2006-05-10 21:30:15 +04:00
|
|
|
#include "nsEventQueue.h"
|
2003-04-09 02:18:10 +04:00
|
|
|
#include "nsCOMPtr.h"
|
2003-01-18 04:27:53 +03:00
|
|
|
#include "prinrval.h"
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2011-04-13 16:59:29 +04:00
|
|
|
#include "prinit.h"
|
2009-02-09 20:31:44 +03:00
|
|
|
#include "nsIObserver.h"
|
2016-05-23 19:20:58 +03:00
|
|
|
#include "mozilla/LinkedList.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-04-01 08:29:02 +04:00
|
|
|
#include "mozilla/Mutex.h"
|
2012-12-10 18:13:55 +04:00
|
|
|
#include "mozilla/net/DashboardTypes.h"
|
2015-03-04 16:17:00 +03:00
|
|
|
#include "mozilla/Atomics.h"
|
2015-03-12 12:25:13 +03:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2016-03-19 06:25:00 +03:00
|
|
|
#include "nsITimer.h"
|
2016-03-22 18:02:39 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
|
|
|
#include "PollableEvent.h"
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2013-09-22 07:04:57 +04:00
|
|
|
class nsASocketHandler;
|
2013-09-23 07:35:05 +04:00
|
|
|
struct PRPollDesc;
|
2016-05-11 12:31:01 +03:00
|
|
|
class nsIPrefBranch;
|
2013-09-22 07:04:57 +04:00
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2016-05-19 05:02:57 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
//
|
2016-05-26 02:13:54 +03:00
|
|
|
// set MOZ_LOG=nsSocketTransport:5
|
2003-01-18 04:27:53 +03:00
|
|
|
//
|
2016-05-19 05:02:57 +03:00
|
|
|
extern LazyLogModule gSocketTransportLog;
|
|
|
|
#define SOCKET_LOG(args) MOZ_LOG(gSocketTransportLog, LogLevel::Debug, args)
|
|
|
|
#define SOCKET_LOG_ENABLED() MOZ_LOG_TEST(gSocketTransportLog, LogLevel::Debug)
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2015-05-29 17:13:39 +03:00
|
|
|
//
|
2016-05-26 02:13:54 +03:00
|
|
|
// set MOZ_LOG=UDPSocket:5
|
2015-05-29 17:13:39 +03:00
|
|
|
//
|
2016-05-19 05:02:57 +03:00
|
|
|
extern LazyLogModule gUDPSocketLog;
|
|
|
|
#define UDPSOCKET_LOG(args) MOZ_LOG(gUDPSocketLog, LogLevel::Debug, args)
|
|
|
|
#define UDPSOCKET_LOG_ENABLED() MOZ_LOG_TEST(gUDPSocketLog, LogLevel::Debug)
|
2015-05-29 17:13:39 +03:00
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#define NS_SOCKET_POLL_TIMEOUT PR_INTERVAL_NO_TIMEOUT
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-02-06 23:51:38 +04:00
|
|
|
// 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
|
|
|
|
|
2016-05-23 19:20:58 +03:00
|
|
|
class LinkedRunnableEvent final : public LinkedListElement<LinkedRunnableEvent>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit LinkedRunnableEvent(nsIRunnable *event) : mEvent(event) {}
|
|
|
|
~LinkedRunnableEvent() {}
|
|
|
|
|
|
|
|
already_AddRefed<nsIRunnable> TakeEvent()
|
|
|
|
{
|
|
|
|
return mEvent.forget();
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIRunnable> mEvent;
|
|
|
|
};
|
|
|
|
|
2014-02-06 23:51:38 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class nsSocketTransportService final : public nsPISocketTransportService
|
2015-03-27 21:52:19 +03:00
|
|
|
, public nsIEventTarget
|
|
|
|
, public nsIThreadObserver
|
|
|
|
, public nsIRunnable
|
|
|
|
, public nsIObserver
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
|
|
|
public:
|
2013-07-19 06:24:13 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2005-04-15 03:18:34 +04:00
|
|
|
NS_DECL_NSPISOCKETTRANSPORTSERVICE
|
2003-01-18 04:27:53 +03:00
|
|
|
NS_DECL_NSISOCKETTRANSPORTSERVICE
|
2015-04-09 18:31:59 +03:00
|
|
|
NS_DECL_NSIROUTEDSOCKETTRANSPORTSERVICE
|
2003-10-06 05:46:31 +04:00
|
|
|
NS_DECL_NSIEVENTTARGET
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_DECL_NSITHREADOBSERVER
|
2003-01-18 04:27:53 +03:00
|
|
|
NS_DECL_NSIRUNNABLE
|
2009-02-09 20:31:44 +03:00
|
|
|
NS_DECL_NSIOBSERVER
|
2016-01-12 07:18:47 +03:00
|
|
|
using nsIEventTarget::Dispatch;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
nsSocketTransportService();
|
|
|
|
|
2011-04-13 16:59:29 +04:00
|
|
|
// Max Socket count may need to get initialized/used by nsHttpHandler
|
|
|
|
// before this class is initialized.
|
2012-08-22 19:56:38 +04:00
|
|
|
static uint32_t gMaxCount;
|
2011-04-13 16:59:29 +04:00
|
|
|
static PRCallOnceType gMaxCountInitOnce;
|
|
|
|
static PRStatus DiscoverMaxCount();
|
|
|
|
|
2016-03-24 03:44:28 +03:00
|
|
|
bool CanAttachSocket();
|
2003-04-09 02:18:10 +04:00
|
|
|
|
2013-07-29 20:08:03 +04:00
|
|
|
// Called by the networking dashboard on the socket thread only
|
2012-12-10 18:13:55 +04:00
|
|
|
// Fills the passed array with socket information
|
2016-05-19 05:02:57 +03:00
|
|
|
void GetSocketConnections(nsTArray<SocketInfo> *);
|
2013-07-29 20:08:03 +04:00
|
|
|
uint64_t GetSentBytes() { return mSentBytesCount; }
|
|
|
|
uint64_t GetReceivedBytes() { return mReceivedBytesCount; }
|
2014-02-06 23:51:38 +04:00
|
|
|
|
|
|
|
// Returns true if keepalives are enabled in prefs.
|
|
|
|
bool IsKeepaliveEnabled() { return mKeepaliveEnabledPref; }
|
2015-07-01 14:51:00 +03:00
|
|
|
|
2016-03-19 06:25:00 +03:00
|
|
|
bool IsTelemetryEnabledAndNotSleepPhase() { return mTelemetryEnabledPref &&
|
|
|
|
!mSleepPhase; }
|
2016-01-18 10:20:00 +03:00
|
|
|
PRIntervalTime MaxTimeForPrClosePref() {return mMaxTimeForPrClosePref; }
|
2003-11-15 08:52:32 +03:00
|
|
|
protected:
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
virtual ~nsSocketTransportService();
|
|
|
|
|
2003-11-15 08:52:32 +03:00
|
|
|
private:
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// misc (any thread)
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
2016-05-19 05:02:57 +03:00
|
|
|
nsCOMPtr<nsIThread> mThread; // protected by mLock
|
|
|
|
UniquePtr<PollableEvent> mPollableEvent;
|
2003-01-22 03:09:05 +03:00
|
|
|
|
2007-12-05 05:18:58 +03:00
|
|
|
// Returns mThread, protecting the get-and-addref with mLock
|
|
|
|
already_AddRefed<nsIThread> GetThreadSafely();
|
|
|
|
|
2005-06-09 18:09:23 +04:00
|
|
|
//-------------------------------------------------------------------------
|
2006-05-10 21:30:15 +04:00
|
|
|
// initialization and shutdown (any thread)
|
2005-06-09 18:09:23 +04: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-04-01 08:29:02 +04:00
|
|
|
Mutex mLock;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mInitialized;
|
|
|
|
bool mShuttingDown;
|
2006-05-10 21:30:15 +04:00
|
|
|
// indicates whether we are currently in the
|
|
|
|
// process of shutting down
|
2012-09-18 03:45:10 +04:00
|
|
|
bool mOffline;
|
|
|
|
bool mGoingOffline;
|
|
|
|
|
|
|
|
// Detaches all sockets.
|
|
|
|
void Reset(bool aGuardLocals);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2016-09-19 20:29:59 +03:00
|
|
|
nsresult ShutdownThread();
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// socket lists (socket thread only)
|
|
|
|
//
|
|
|
|
// only "active" sockets are on the poll list. the active list is kept
|
2003-02-23 08:07:34 +03:00
|
|
|
// in sync with the poll list such that:
|
|
|
|
//
|
|
|
|
// mActiveList[k].mFD == mPollList[k+1].fd
|
|
|
|
//
|
|
|
|
// where k=0,1,2,...
|
2003-01-18 04:27:53 +03:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
struct SocketContext
|
|
|
|
{
|
|
|
|
PRFileDesc *mFD;
|
|
|
|
nsASocketHandler *mHandler;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint16_t mElapsedTime; // time elapsed w/o activity
|
2003-01-18 04:27:53 +03:00
|
|
|
};
|
|
|
|
|
2011-04-13 16:59:29 +04:00
|
|
|
SocketContext *mActiveList; /* mListSize entries */
|
|
|
|
SocketContext *mIdleList; /* mListSize entries */
|
2016-03-22 18:02:39 +03:00
|
|
|
nsIThread *mRawThread;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mActiveListSize;
|
|
|
|
uint32_t mIdleListSize;
|
|
|
|
uint32_t mActiveCount;
|
|
|
|
uint32_t mIdleCount;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2011-04-13 16:59:29 +04:00
|
|
|
nsresult DetachSocket(SocketContext *, SocketContext *);
|
2003-01-18 04:27:53 +03:00
|
|
|
nsresult AddToIdleList(SocketContext *);
|
|
|
|
nsresult AddToPollList(SocketContext *);
|
|
|
|
void RemoveFromIdleList(SocketContext *);
|
|
|
|
void RemoveFromPollList(SocketContext *);
|
2003-02-23 08:07:34 +03:00
|
|
|
void MoveToIdleList(SocketContext *sock);
|
|
|
|
void MoveToPollList(SocketContext *sock);
|
2011-04-13 16:59:29 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool GrowActiveList();
|
|
|
|
bool GrowIdleList();
|
2011-04-13 16:59:29 +04:00
|
|
|
void InitMaxCount();
|
2013-07-29 20:08:03 +04:00
|
|
|
|
|
|
|
// Total bytes number transfered through all the sockets except active ones
|
|
|
|
uint64_t mSentBytesCount;
|
|
|
|
uint64_t mReceivedBytesCount;
|
2003-01-18 04:27:53 +03:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// poll list (socket thread only)
|
|
|
|
//
|
2016-03-22 18:02:39 +03:00
|
|
|
// first element of the poll list is mPollableEvent (or null if the pollable
|
2003-02-23 08:07:34 +03:00
|
|
|
// event cannot be created).
|
2003-01-18 04:27:53 +03:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
2011-04-13 16:59:29 +04:00
|
|
|
PRPollDesc *mPollList; /* mListSize + 1 entries */
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2005-01-26 05:13:14 +03:00
|
|
|
PRIntervalTime PollTimeout(); // computes ideal poll timeout
|
2016-05-19 05:02:57 +03:00
|
|
|
nsresult DoPollIteration(TimeDuration *pollDuration);
|
2006-05-10 21:30:15 +04:00
|
|
|
// perfoms a single poll iteration
|
2016-03-22 18:02:39 +03:00
|
|
|
int32_t Poll(uint32_t *interval,
|
2016-05-19 05:02:57 +03:00
|
|
|
TimeDuration *pollDuration);
|
2006-05-10 21:30:15 +04:00
|
|
|
// calls PR_Poll. the out param
|
2005-01-26 05:13:14 +03:00
|
|
|
// interval indicates the poll
|
|
|
|
// duration in seconds.
|
2015-03-12 12:25:13 +03:00
|
|
|
// pollDuration is used only for
|
|
|
|
// telemetry
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2003-04-09 02:18:10 +04:00
|
|
|
//-------------------------------------------------------------------------
|
2003-10-06 05:46:31 +04:00
|
|
|
// pending socket queue - see NotifyWhenCanAttachSocket
|
2003-04-09 02:18:10 +04:00
|
|
|
//-------------------------------------------------------------------------
|
2016-05-23 19:20:58 +03:00
|
|
|
AutoCleanLinkedList<LinkedRunnableEvent> mPendingSocketQueue;
|
2009-02-09 20:31:44 +03:00
|
|
|
|
2014-02-06 23:51:38 +04:00
|
|
|
// Preference Monitor for SendBufferSize and Keepalive prefs.
|
2009-02-09 20:31:44 +03:00
|
|
|
nsresult UpdatePrefs();
|
2016-05-11 12:31:01 +03:00
|
|
|
void UpdateSendBufferPref(nsIPrefBranch *);
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t mSendBufferSize;
|
2014-02-06 23:51:38 +04: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;
|
|
|
|
|
2016-05-19 05:02:57 +03:00
|
|
|
Atomic<bool> mServingPendingQueue;
|
|
|
|
Atomic<int32_t, Relaxed> mMaxTimePerPollIter;
|
|
|
|
Atomic<bool, Relaxed> mTelemetryEnabledPref;
|
|
|
|
Atomic<PRIntervalTime, Relaxed> mMaxTimeForPrClosePref;
|
2015-03-04 16:17:00 +03:00
|
|
|
|
2016-03-19 06:25:00 +03:00
|
|
|
// Between a computer going to sleep and waking up the PR_*** telemetry
|
|
|
|
// will be corrupted - so do not record it.
|
2016-05-19 05:02:57 +03:00
|
|
|
Atomic<bool, Relaxed> mSleepPhase;
|
|
|
|
nsCOMPtr<nsITimer> mAfterWakeUpTimer;
|
2016-03-19 06:25:00 +03:00
|
|
|
|
2014-02-06 23:51:38 +04:00
|
|
|
void OnKeepaliveEnabledPrefChange();
|
|
|
|
void NotifyKeepaliveEnabledPrefChange(SocketContext *sock);
|
2011-10-25 19:36:49 +04:00
|
|
|
|
|
|
|
// Socket thread only for dynamically adjusting max socket size
|
|
|
|
#if defined(XP_WIN)
|
|
|
|
void ProbeMaxCount();
|
|
|
|
#endif
|
|
|
|
bool mProbedMaxCount;
|
2012-12-10 18:13:55 +04:00
|
|
|
|
2016-05-19 05:02:57 +03:00
|
|
|
void AnalyzeConnection(nsTArray<SocketInfo> *data,
|
2012-12-10 18:13:55 +04:00
|
|
|
SocketContext *context, bool aActive);
|
2012-12-08 02:50:43 +04:00
|
|
|
|
|
|
|
void ClosePrivateConnections();
|
2013-04-10 04:46:25 +04:00
|
|
|
void DetachSocketWithGuard(bool aGuardLocals,
|
|
|
|
SocketContext *socketList,
|
|
|
|
int32_t index);
|
2015-03-04 16:17:00 +03:00
|
|
|
|
|
|
|
void MarkTheLastElementOfPendingQueue();
|
2016-09-13 11:43:00 +03:00
|
|
|
|
|
|
|
#if defined(XP_WIN)
|
|
|
|
Atomic<bool> mPolling;
|
|
|
|
nsCOMPtr<nsITimer> mPollRepairTimer;
|
|
|
|
void StartPollWatchdog();
|
|
|
|
void DoPollRepair();
|
|
|
|
void StartPolling();
|
|
|
|
void EndPolling();
|
|
|
|
#endif
|
2003-01-18 04:27:53 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
extern nsSocketTransportService *gSocketTransportService;
|
2017-04-27 21:34:42 +03:00
|
|
|
bool OnSocketThread();
|
2016-05-19 05:02:57 +03:00
|
|
|
|
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
#endif // !nsSocketTransportService_h__
|