1 /* vim:set ts=4 sw=4 sts=4 ci et: */
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 nsSocketTransportService2_h__
40 #define nsSocketTransportService2_h__
42 #include "nsPISocketTransportService.h"
43 #include "nsIThreadInternal.h"
44 #include "nsThreadUtils.h"
45 #include "nsEventQueue.h"
51 #include "nsASocketHandler.h"
53 //-----------------------------------------------------------------------------
55 #if defined(PR_LOGGING)
57 // set NSPR_LOG_MODULES=nsSocketTransport:5
59 extern PRLogModuleInfo
*gSocketTransportLog
;
61 #define LOG(args) PR_LOG(gSocketTransportLog, PR_LOG_DEBUG, args)
62 #define LOG_ENABLED() PR_LOG_TEST(gSocketTransportLog, PR_LOG_DEBUG)
64 //-----------------------------------------------------------------------------
66 #define NS_SOCKET_MAX_COUNT 50
67 #define NS_SOCKET_POLL_TIMEOUT PR_INTERVAL_NO_TIMEOUT
69 //-----------------------------------------------------------------------------
71 class nsSocketTransportService
: public nsPISocketTransportService
72 , public nsIEventTarget
73 , public nsIThreadObserver
78 NS_DECL_NSPISOCKETTRANSPORTSERVICE
79 NS_DECL_NSISOCKETTRANSPORTSERVICE
80 NS_DECL_NSIEVENTTARGET
81 NS_DECL_NSITHREADOBSERVER
84 nsSocketTransportService();
87 // the number of sockets that can be attached at any given time is
88 // limited. this is done because some operating systems (e.g., Win9x)
89 // limit the number of sockets that can be created by an application.
90 // AttachSocket will fail if the limit is exceeded. consumers should
91 // call CanAttachSocket and check the result before creating a socket.
93 PRBool
CanAttachSocket() {
94 return mActiveCount
+ mIdleCount
< NS_SOCKET_MAX_COUNT
;
99 virtual ~nsSocketTransportService();
103 //-------------------------------------------------------------------------
105 //-------------------------------------------------------------------------
107 nsCOMPtr
<nsIThread
> mThread
; // protected by mLock
108 PRFileDesc
*mThreadEvent
;
109 // protected by mLock. mThreadEvent may change
110 // if the old pollable event is broken. only
111 // the socket thread may change mThreadEvent;
112 // it needs to lock mLock only when it changes
113 // mThreadEvent. other threads don't change
114 // mThreadEvent; they need to lock mLock
115 // whenever they access mThreadEvent.
116 PRBool mAutodialEnabled
;
117 // pref to control autodial code
119 // Returns mThread, protecting the get-and-addref with mLock
120 already_AddRefed
<nsIThread
> GetThreadSafely();
122 //-------------------------------------------------------------------------
123 // initialization and shutdown (any thread)
124 //-------------------------------------------------------------------------
127 PRPackedBool mInitialized
;
128 PRPackedBool mShuttingDown
;
129 // indicates whether we are currently in the
130 // process of shutting down
132 //-------------------------------------------------------------------------
133 // socket lists (socket thread only)
135 // only "active" sockets are on the poll list. the active list is kept
136 // in sync with the poll list such that:
138 // mActiveList[k].mFD == mPollList[k+1].fd
141 //-------------------------------------------------------------------------
146 nsASocketHandler
*mHandler
;
147 PRUint16 mElapsedTime
; // time elapsed w/o activity
150 SocketContext mActiveList
[ NS_SOCKET_MAX_COUNT
];
151 SocketContext mIdleList
[ NS_SOCKET_MAX_COUNT
];
153 PRUint32 mActiveCount
;
156 nsresult
DetachSocket(SocketContext
*);
157 nsresult
AddToIdleList(SocketContext
*);
158 nsresult
AddToPollList(SocketContext
*);
159 void RemoveFromIdleList(SocketContext
*);
160 void RemoveFromPollList(SocketContext
*);
161 void MoveToIdleList(SocketContext
*sock
);
162 void MoveToPollList(SocketContext
*sock
);
164 //-------------------------------------------------------------------------
165 // poll list (socket thread only)
167 // first element of the poll list is mThreadEvent (or null if the pollable
168 // event cannot be created).
169 //-------------------------------------------------------------------------
171 PRPollDesc mPollList
[ NS_SOCKET_MAX_COUNT
+ 1 ];
173 PRIntervalTime
PollTimeout(); // computes ideal poll timeout
174 nsresult
DoPollIteration(PRBool wait
);
175 // perfoms a single poll iteration
176 PRInt32
Poll(PRBool wait
, PRUint32
*interval
);
177 // calls PR_Poll. the out param
178 // interval indicates the poll
179 // duration in seconds.
181 //-------------------------------------------------------------------------
182 // pending socket queue - see NotifyWhenCanAttachSocket
183 //-------------------------------------------------------------------------
185 nsEventQueue mPendingSocketQ
; // queue of nsIRunnable objects
188 extern nsSocketTransportService
*gSocketTransportService
;
189 extern PRThread
*gSocketThread
;
191 #endif // !nsSocketTransportService_h__