1 /* vim:set ts=4 sw=4 sts=4 et cin: */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2002
20 * the Initial Developer. All Rights Reserved.
23 * Darin Fisher <darin@netscape.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #ifndef nsHttpConnectionMgr_h__
40 #define nsHttpConnectionMgr_h__
42 #include "nsHttpConnectionInfo.h"
43 #include "nsHttpConnection.h"
44 #include "nsHttpTransaction.h"
45 #include "nsVoidArray.h"
46 #include "nsThreadUtils.h"
47 #include "nsHashtable.h"
48 #include "nsAutoPtr.h"
53 //-----------------------------------------------------------------------------
55 class nsHttpConnectionMgr
62 MAX_CONNECTIONS_PER_HOST
,
63 MAX_CONNECTIONS_PER_PROXY
,
64 MAX_PERSISTENT_CONNECTIONS_PER_HOST
,
65 MAX_PERSISTENT_CONNECTIONS_PER_PROXY
,
67 MAX_PIPELINED_REQUESTS
70 //-------------------------------------------------------------------------
71 // NOTE: functions below may only be called on the main thread.
72 //-------------------------------------------------------------------------
74 nsHttpConnectionMgr();
76 nsresult
Init(PRUint16 maxConnections
,
77 PRUint16 maxConnectionsPerHost
,
78 PRUint16 maxConnectionsPerProxy
,
79 PRUint16 maxPersistentConnectionsPerHost
,
80 PRUint16 maxPersistentConnectionsPerProxy
,
81 PRUint16 maxRequestDelay
,
82 PRUint16 maxPipelinedRequests
);
85 //-------------------------------------------------------------------------
86 // NOTE: functions below may be called on any thread.
87 //-------------------------------------------------------------------------
91 return PR_AtomicIncrement(&mRef
);
96 nsrefcnt n
= PR_AtomicDecrement(&mRef
);
102 // adds a transaction to the list of managed transactions.
103 nsresult
AddTransaction(nsHttpTransaction
*, PRInt32 priority
);
105 // called to reschedule the given transaction. it must already have been
106 // added to the connection manager via AddTransaction.
107 nsresult
RescheduleTransaction(nsHttpTransaction
*, PRInt32 priority
);
109 // cancels a transaction w/ the given reason.
110 nsresult
CancelTransaction(nsHttpTransaction
*, nsresult reason
);
112 // called to force the connection manager to prune its list of idle
114 nsresult
PruneDeadConnections();
116 // called to get a reference to the socket transport service. the socket
117 // transport service is not available when the connection manager is down.
118 nsresult
GetSocketThreadTarget(nsIEventTarget
**);
120 // called when a connection is done processing a transaction. if the
121 // connection can be reused then it will be added to the idle list, else
122 // it will be closed.
123 nsresult
ReclaimConnection(nsHttpConnection
*conn
);
125 // called to update a parameter after the connection manager has already
127 nsresult
UpdateParam(nsParamName name
, PRUint16 value
);
129 //-------------------------------------------------------------------------
130 // NOTE: functions below may be called only on the socket thread.
131 //-------------------------------------------------------------------------
133 // removes the next transaction for the specified connection from the
134 // pending transaction queue.
135 void AddTransactionToPipeline(nsHttpPipeline
*);
137 // called to force the transaction queue to be processed once more, giving
138 // preference to the specified connection.
139 nsresult
ProcessPendingQ(nsHttpConnectionInfo
*);
142 virtual ~nsHttpConnectionMgr();
146 // mCT maps connection info hash key to nsConnectionEntry object, which
147 // contains list of active and idle connections as well as the list of
148 // pending transactions.
150 struct nsConnectionEntry
152 nsConnectionEntry(nsHttpConnectionInfo
*ci
)
155 NS_ADDREF(mConnInfo
);
157 ~nsConnectionEntry() { NS_RELEASE(mConnInfo
); }
159 nsHttpConnectionInfo
*mConnInfo
;
160 nsVoidArray mPendingQ
; // pending transaction queue
161 nsVoidArray mActiveConns
; // active connections
162 nsVoidArray mIdleConns
; // idle persistent connections
165 // nsConnectionHandle
167 // thin wrapper around a real connection, used to keep track of references
168 // to the connection to determine when the connection may be reused. the
169 // transaction (or pipeline) owns a reference to this handle. this extra
170 // layer of indirection greatly simplifies consumer code, avoiding the
171 // need for consumer code to know when to give the connection back to the
172 // connection manager.
174 class nsConnectionHandle
: public nsAHttpConnection
178 NS_DECL_NSAHTTPCONNECTION
180 nsConnectionHandle(nsHttpConnection
*conn
) { NS_ADDREF(mConn
= conn
); }
181 virtual ~nsConnectionHandle();
183 nsHttpConnection
*mConn
;
186 //-------------------------------------------------------------------------
187 // NOTE: these members may be accessed from any thread (use mMonitor)
188 //-------------------------------------------------------------------------
192 nsCOMPtr
<nsIEventTarget
> mSocketThreadTarget
;
196 PRUint16 mMaxConnsPerHost
;
197 PRUint16 mMaxConnsPerProxy
;
198 PRUint16 mMaxPersistConnsPerHost
;
199 PRUint16 mMaxPersistConnsPerProxy
;
200 PRUint16 mMaxRequestDelay
; // in seconds
201 PRUint16 mMaxPipelinedRequests
;
203 //-------------------------------------------------------------------------
204 // NOTE: these members are only accessed on the socket transport thread
205 //-------------------------------------------------------------------------
207 static PRIntn
ProcessOneTransactionCB(nsHashKey
*, void *, void *);
208 static PRIntn
PurgeOneIdleConnectionCB(nsHashKey
*, void *, void *);
209 static PRIntn
PruneDeadConnectionsCB(nsHashKey
*, void *, void *);
210 static PRIntn
ShutdownPassCB(nsHashKey
*, void *, void *);
212 PRBool
ProcessPendingQForEntry(nsConnectionEntry
*);
213 PRBool
AtActiveConnectionLimit(nsConnectionEntry
*, PRUint8 caps
);
214 void GetConnection(nsConnectionEntry
*, PRUint8 caps
, nsHttpConnection
**);
215 nsresult
DispatchTransaction(nsConnectionEntry
*, nsAHttpTransaction
*,
216 PRUint8 caps
, nsHttpConnection
*);
217 PRBool
BuildPipeline(nsConnectionEntry
*, nsAHttpTransaction
*, nsHttpPipeline
**);
218 nsresult
ProcessNewTransaction(nsHttpTransaction
*);
220 // message handlers have this signature
221 typedef void (nsHttpConnectionMgr:: *nsConnEventHandler
)(PRInt32
, void *);
225 // subclass of nsRunnable used to marshall events to the socket transport
226 // thread. this class is used to implement PostEvent.
229 friend class nsConnEvent
;
230 class nsConnEvent
: public nsRunnable
233 nsConnEvent(nsHttpConnectionMgr
*mgr
,
234 nsConnEventHandler handler
,
247 (mMgr
->*mHandler
)(mIParam
, mVParam
);
252 virtual ~nsConnEvent()
257 nsHttpConnectionMgr
*mMgr
;
258 nsConnEventHandler mHandler
;
263 nsresult
PostEvent(nsConnEventHandler handler
,
265 void *vparam
= nsnull
);
268 void OnMsgShutdown (PRInt32
, void *);
269 void OnMsgNewTransaction (PRInt32
, void *);
270 void OnMsgReschedTransaction (PRInt32
, void *);
271 void OnMsgCancelTransaction (PRInt32
, void *);
272 void OnMsgProcessPendingQ (PRInt32
, void *);
273 void OnMsgPruneDeadConnections (PRInt32
, void *);
274 void OnMsgReclaimConnection (PRInt32
, void *);
275 void OnMsgUpdateParam (PRInt32
, void *);
278 PRUint16 mNumActiveConns
;
279 PRUint16 mNumIdleConns
;
282 // the connection table
284 // this table is indexed by connection key. each entry is a
285 // ConnectionEntry object.
290 #endif // !nsHttpConnectionMgr_h__