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"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsEventQueue.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "pldhash.h"
|
|
|
|
#include "prinrval.h"
|
|
|
|
#include "prlog.h"
|
2011-04-13 05:59:29 -07:00
|
|
|
#include "prinit.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "prio.h"
|
2008-02-26 12:39:50 -08:00
|
|
|
#include "nsASocketHandler.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"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
//
|
|
|
|
// set NSPR_LOG_MODULES=nsSocketTransport:5
|
|
|
|
//
|
|
|
|
extern PRLogModuleInfo *gSocketTransportLog;
|
|
|
|
#endif
|
2010-10-28 10:09:08 -07:00
|
|
|
#define SOCKET_LOG(args) PR_LOG(gSocketTransportLog, PR_LOG_DEBUG, args)
|
|
|
|
#define SOCKET_LOG_ENABLED() PR_LOG_TEST(gSocketTransportLog, PR_LOG_DEBUG)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#define NS_SOCKET_POLL_TIMEOUT PR_INTERVAL_NO_TIMEOUT
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class nsSocketTransportService : public nsPISocketTransportService
|
|
|
|
, public nsIEventTarget
|
|
|
|
, public nsIThreadObserver
|
|
|
|
, public nsIRunnable
|
2009-02-09 09:31:44 -08:00
|
|
|
, 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
|
|
|
|
NS_DECL_NSIEVENTTARGET
|
|
|
|
NS_DECL_NSITHREADOBSERVER
|
|
|
|
NS_DECL_NSIRUNNABLE
|
2009-02-09 09:31:44 -08:00
|
|
|
NS_DECL_NSIOBSERVER
|
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
|
|
|
}
|
|
|
|
|
2012-12-10 06:13:55 -08:00
|
|
|
// Called by the networking dashboard
|
|
|
|
// Fills the passed array with socket information
|
|
|
|
void GetSocketConnections(nsTArray<mozilla::net::SocketInfo> *);
|
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();
|
2011-04-11 10:28:41 -07:00
|
|
|
|
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
|
2011-09-28 23:19:26 -07:00
|
|
|
nsresult DoPollIteration(bool wait);
|
2007-03-22 10:30:00 -07:00
|
|
|
// perfoms a single poll iteration
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t Poll(bool wait, uint32_t *interval);
|
2007-03-22 10:30:00 -07:00
|
|
|
// calls PR_Poll. the out param
|
|
|
|
// interval indicates the poll
|
|
|
|
// duration in seconds.
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// pending socket queue - see NotifyWhenCanAttachSocket
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsEventQueue mPendingSocketQ; // queue of nsIRunnable objects
|
2009-02-09 09:31:44 -08:00
|
|
|
|
|
|
|
// Preference Monitor for SendBufferSize
|
|
|
|
nsresult UpdatePrefs();
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t mSendBufferSize;
|
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);
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
extern nsSocketTransportService *gSocketTransportService;
|
|
|
|
extern PRThread *gSocketThread;
|
|
|
|
|
|
|
|
#endif // !nsSocketTransportService_h__
|