Back to index

lightning-sunbird  0.9+nobinonly
ipcClient.h
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla IPC.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 2002
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Darin Fisher <darin@netscape.com>
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef ipcClientUnix_h__
00039 #define ipcClientUnix_h__
00040 
00041 #include "prio.h"
00042 #include "ipcMessageQ.h"
00043 #include "ipcStringList.h"
00044 #include "ipcIDList.h"
00045 
00046 #ifdef XP_WIN
00047 #include <windows.h>
00048 #endif
00049 
00050 //-----------------------------------------------------------------------------
00051 // ipcClient
00052 //
00053 // NOTE: this class is an implementation detail of the IPC daemon. IPC daemon
00054 // modules (other than the built-in IPCM module) must not access methods on
00055 // this class directly. use the API provided via ipcd.h instead.
00056 //-----------------------------------------------------------------------------
00057 
00058 class ipcClient
00059 {
00060 public:
00061     void Init();
00062     void Finalize();
00063 
00064     PRUint32 ID() const { return mID; }
00065 
00066     void   AddName(const char *name);
00067     void   DelName(const char *name);
00068     PRBool HasName(const char *name) const { return mNames.Find(name) != NULL; }
00069 
00070     void   AddTarget(const nsID &target);
00071     void   DelTarget(const nsID &target);
00072     PRBool HasTarget(const nsID &target) const { return mTargets.Find(target) != NULL; }
00073 
00074     // list iterators
00075     const ipcStringNode *Names()   const { return mNames.First(); }
00076     const ipcIDNode     *Targets() const { return mTargets.First(); }
00077 
00078     // returns primary client name (the one specified in the "client hello" message)
00079     const char *PrimaryName() const { return mNames.First() ? mNames.First()->Value() : NULL; }
00080 
00081     void   SetExpectsSyncReply(PRBool val) { mExpectsSyncReply = val; }
00082     PRBool GetExpectsSyncReply() const     { return mExpectsSyncReply; }
00083 
00084 #ifdef XP_WIN
00085     PRUint32 PID() const { return mPID; }
00086     void SetPID(PRUint32 pid) { mPID = pid; }
00087 
00088     HWND Hwnd() const { return mHwnd; }
00089     void SetHwnd(HWND hwnd) { mHwnd = hwnd; }
00090 #endif
00091 
00092 #if defined(XP_UNIX) || defined(XP_OS2)
00093     //
00094     // called to process a client file descriptor.  the value of pollFlags
00095     // indicates the state of the socket.
00096     //
00097     // returns:
00098     //   0             - to cancel client connection  
00099     //   PR_POLL_READ  - to poll for a readable socket
00100     //   PR_POLL_WRITE - to poll for a writable socket
00101     //   (both flags)  - to poll for either a readable or writable socket
00102     //
00103     // the socket is non-blocking.
00104     // 
00105     int Process(PRFileDesc *sockFD, int pollFlags);
00106 
00107     //
00108     // on success or failure, this function takes ownership of |msg| and will
00109     // delete it when appropriate.
00110     //
00111     void EnqueueOutboundMsg(ipcMessage *msg) { mOutMsgQ.Append(msg); }
00112 #endif
00113 
00114 private:
00115     static PRUint32 gLastID;
00116 
00117     PRUint32      mID;
00118     ipcStringList mNames;
00119     ipcIDList     mTargets;
00120     PRBool        mExpectsSyncReply;
00121 
00122 #ifdef XP_WIN
00123     // on windows, we store the PID of the client process to help us determine
00124     // the client from which a message originated.  each message has the PID
00125     // encoded in it.
00126     PRUint32      mPID;
00127     
00128     // the hwnd of the client's message window.
00129     HWND          mHwnd;
00130 #endif
00131 
00132 #if defined(XP_UNIX) || defined(XP_OS2)
00133     ipcMessage    mInMsg;    // buffer for incoming message
00134     ipcMessageQ   mOutMsgQ;  // outgoing message queue
00135 
00136     // keep track of the amount of the first message sent
00137     PRUint32      mSendOffset;
00138 
00139     // utility function for writing out messages.
00140     int WriteMsgs(PRFileDesc *fd);
00141 #endif
00142 };
00143 
00144 #endif // !ipcClientUnix_h__