ipcClient.h revision 677833bc953b6cb418c701facbdcf4aa18d6c44e
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync/* ***** BEGIN LICENSE BLOCK *****
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync * Version: MPL 1.1/GPL 2.0/LGPL 2.1
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync *
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync * The contents of this file are subject to the Mozilla Public License Version
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync * 1.1 (the "License"); you may not use this file except in compliance with
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync * the License. You may obtain a copy of the License at
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync * http://www.mozilla.org/MPL/
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync *
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync * Software distributed under the License is distributed on an "AS IS" basis,
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync * for the specific language governing rights and limitations under the
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync * License.
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync *
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync * The Original Code is Mozilla IPC.
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync *
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync * The Initial Developer of the Original Code is
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync * Netscape Communications Corporation.
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync * Portions created by the Initial Developer are Copyright (C) 2002
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync * the Initial Developer. All Rights Reserved.
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync *
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync * Contributor(s):
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync * Darin Fisher <darin@netscape.com>
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync *
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync * Alternatively, the contents of this file may be used under the terms of
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync * either the GNU General Public License Version 2 or later (the "GPL"), or
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync * in which case the provisions of the GPL or the LGPL are applicable instead
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync * of those above. If you wish to allow use of your version of this file only
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync * under the terms of either the GPL or the LGPL, and not to allow others to
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync * use your version of this file under the terms of the MPL, indicate your
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync * decision by deleting the provisions above and replace them with the notice
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync * and other provisions required by the GPL or the LGPL. If you do not delete
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync * the provisions above, a recipient may use your version of this file under
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync * the terms of any one of the MPL, the GPL or the LGPL.
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync *
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync * ***** END LICENSE BLOCK ***** */
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync#ifndef ipcClientUnix_h__
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync#define ipcClientUnix_h__
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync#include "prio.h"
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync#include "ipcMessageQ.h"
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync#include "ipcStringList.h"
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync#include "ipcIDList.h"
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync#ifdef XP_WIN
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync#include <windows.h>
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync#endif
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync//-----------------------------------------------------------------------------
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync// ipcClient
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync//
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync// NOTE: this class is an implementation detail of the IPC daemon. IPC daemon
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync// modules (other than the built-in IPCM module) must not access methods on
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync// this class directly. use the API provided via ipcd.h instead.
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync//-----------------------------------------------------------------------------
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsyncclass ipcClient
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync{
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsyncpublic:
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync void Init();
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync void Finalize();
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync PRUint32 ID() const { return mID; }
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync void AddName(const char *name);
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync void DelName(const char *name);
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync PRBool HasName(const char *name) const { return mNames.Find(name) != NULL; }
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync void AddTarget(const nsID &target);
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync void DelTarget(const nsID &target);
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync PRBool HasTarget(const nsID &target) const { return mTargets.Find(target) != NULL; }
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync // list iterators
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync const ipcStringNode *Names() const { return mNames.First(); }
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync const ipcIDNode *Targets() const { return mTargets.First(); }
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync // returns primary client name (the one specified in the "client hello" message)
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync const char *PrimaryName() const { return mNames.First() ? mNames.First()->Value() : NULL; }
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync void SetExpectsSyncReply(PRBool val) { mExpectsSyncReply = val; }
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync PRBool GetExpectsSyncReply() const { return mExpectsSyncReply; }
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync#ifdef XP_WIN
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync PRUint32 PID() const { return mPID; }
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync void SetPID(PRUint32 pid) { mPID = pid; }
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync HWND Hwnd() const { return mHwnd; }
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync void SetHwnd(HWND hwnd) { mHwnd = hwnd; }
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync#endif
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync#if defined(XP_UNIX) || defined(XP_OS2)
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync //
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync // called to process a client file descriptor. the value of pollFlags
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync // indicates the state of the socket.
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync //
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync // returns:
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync // 0 - to cancel client connection
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync // PR_POLL_READ - to poll for a readable socket
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync // PR_POLL_WRITE - to poll for a writable socket
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync // (both flags) - to poll for either a readable or writable socket
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync //
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync // the socket is non-blocking.
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync //
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync int Process(PRFileDesc *sockFD, int pollFlags);
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync //
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync // on success or failure, this function takes ownership of |msg| and will
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync // delete it when appropriate.
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync //
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync void EnqueueOutboundMsg(ipcMessage *msg) { mOutMsgQ.Append(msg); }
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync#endif
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsyncprivate:
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync static PRUint32 gLastID;
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync PRUint32 mID;
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync ipcStringList mNames;
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync ipcIDList mTargets;
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync PRBool mExpectsSyncReply;
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync#ifdef XP_WIN
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync // on windows, we store the PID of the client process to help us determine
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync // the client from which a message originated. each message has the PID
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync // encoded in it.
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync PRUint32 mPID;
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync // the hwnd of the client's message window.
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync HWND mHwnd;
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync#endif
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync#if defined(XP_UNIX) || defined(XP_OS2)
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync ipcMessage mInMsg; // buffer for incoming message
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync ipcMessageQ mOutMsgQ; // outgoing message queue
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync // keep track of the amount of the first message sent
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync PRUint32 mSendOffset;
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync // utility function for writing out messages.
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync int WriteMsgs(PRFileDesc *fd);
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync#endif
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync};
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync#endif // !ipcClientUnix_h__
37136b5ecb07042e5ba50f86849a79d1cba5d5f1vboxsync