зеркало из https://github.com/mozilla/pjs.git
Moving IPC code from the IPC_BRANCH_20030304 to the trunk. No actual code changes.
r/sr=leaf+darin
This commit is contained in:
Родитель
b6e5489bdb
Коммит
f246267f90
|
@ -0,0 +1 @@
|
|||
Makefile
|
|
@ -0,0 +1,62 @@
|
|||
# vim: noexpandtab ts=4 sw=4
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
#
|
||||
# The contents of this file are subject to the Mozilla Public License Version
|
||||
# 1.1 (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
# http://www.mozilla.org/MPL/
|
||||
#
|
||||
# Software distributed under the License is distributed on an "AS IS" basis,
|
||||
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
# for the specific language governing rights and limitations under the
|
||||
# License.
|
||||
#
|
||||
# The Original Code is Mozilla IPC.
|
||||
#
|
||||
# The Initial Developer of the Original Code is
|
||||
# Netscape Communications Corporation.
|
||||
# Portions created by the Initial Developer are Copyright (C) 2002
|
||||
# the Initial Developer. All Rights Reserved.
|
||||
#
|
||||
# Contributor(s):
|
||||
# Darin Fisher <darin@netscape.com>
|
||||
#
|
||||
# Alternatively, the contents of this file may be used under the terms of
|
||||
# either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
# in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
# of those above. If you wish to allow use of your version of this file only
|
||||
# under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
# use your version of this file under the terms of the MPL, indicate your
|
||||
# decision by deleting the provisions above and replace them with the notice
|
||||
# and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
# the provisions above, a recipient may use your version of this file under
|
||||
# the terms of any one of the MPL, the GPL or the LGPL.
|
||||
#
|
||||
# ***** END LICENSE BLOCK *****
|
||||
|
||||
DEPTH = ../..
|
||||
topsrcdir = @top_srcdir@
|
||||
srcdir = @srcdir@
|
||||
VPATH = @srcdir@
|
||||
|
||||
include $(DEPTH)/config/autoconf.mk
|
||||
|
||||
MODULE = ipcd
|
||||
|
||||
DIRS = \
|
||||
util \
|
||||
shared/src \
|
||||
daemon/public \
|
||||
client/public \
|
||||
extensions \
|
||||
client/src \
|
||||
daemon/src \
|
||||
$(NULL)
|
||||
|
||||
ifdef ENABLE_TESTS
|
||||
DIRS += test
|
||||
endif
|
||||
|
||||
include $(topsrcdir)/config/rules.mk
|
|
@ -0,0 +1 @@
|
|||
Makefile
|
|
@ -0,0 +1,55 @@
|
|||
# vim: noexpandtab ts=4 sw=4
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
#
|
||||
# The contents of this file are subject to the Mozilla Public License Version
|
||||
# 1.1 (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
# http://www.mozilla.org/MPL/
|
||||
#
|
||||
# Software distributed under the License is distributed on an "AS IS" basis,
|
||||
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
# for the specific language governing rights and limitations under the
|
||||
# License.
|
||||
#
|
||||
# The Original Code is Mozilla IPC.
|
||||
#
|
||||
# The Initial Developer of the Original Code is
|
||||
# Netscape Communications Corporation.
|
||||
# Portions created by the Initial Developer are Copyright (C) 2002
|
||||
# the Initial Developer. All Rights Reserved.
|
||||
#
|
||||
# Contributor(s):
|
||||
# Darin Fisher <darin@netscape.com>
|
||||
#
|
||||
# Alternatively, the contents of this file may be used under the terms of
|
||||
# either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
# in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
# of those above. If you wish to allow use of your version of this file only
|
||||
# under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
# use your version of this file under the terms of the MPL, indicate your
|
||||
# decision by deleting the provisions above and replace them with the notice
|
||||
# and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
# the provisions above, a recipient may use your version of this file under
|
||||
# the terms of any one of the MPL, the GPL or the LGPL.
|
||||
#
|
||||
# ***** END LICENSE BLOCK *****
|
||||
|
||||
DEPTH = ../../../..
|
||||
topsrcdir = @top_srcdir@
|
||||
srcdir = @srcdir@
|
||||
VPATH = @srcdir@
|
||||
|
||||
include $(DEPTH)/config/autoconf.mk
|
||||
|
||||
MODULE = ipcd
|
||||
|
||||
XPIDLSRCS = \
|
||||
ipcIService.idl \
|
||||
ipcIMessageObserver.idl \
|
||||
ipcIClientObserver.idl \
|
||||
ipcIClientQueryHandler.idl \
|
||||
$(NULL)
|
||||
|
||||
include $(topsrcdir)/config/rules.mk
|
|
@ -0,0 +1,53 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#ifndef ipcCID_h__
|
||||
#define ipcCID_h__
|
||||
|
||||
#define IPC_SERVICE_CLASSNAME \
|
||||
"ipcService"
|
||||
#define IPC_SERVICE_CONTRACTID \
|
||||
"@mozilla.org/ipc/service;1"
|
||||
#define IPC_SERVICE_CID \
|
||||
{ /* 9f12676a-5168-4a08-beb8-edf8a593a1ca */ \
|
||||
0x9f12676a, \
|
||||
0x5168, \
|
||||
0x4a08, \
|
||||
{0xbe, 0xb8, 0xed, 0xf8, 0xa5, 0x93, 0xa1, 0xca} \
|
||||
}
|
||||
|
||||
#endif // !ipcCID_h__
|
|
@ -0,0 +1,51 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include "nsISupports.idl"
|
||||
|
||||
/**
|
||||
* ipcIClientObserver
|
||||
*/
|
||||
[scriptable, uuid(42283079-030c-4b13-b069-a08b7ad5eab2)]
|
||||
interface ipcIClientObserver : nsISupports
|
||||
{
|
||||
const unsigned long CLIENT_UP = 1;
|
||||
const unsigned long CLIENT_DOWN = 2;
|
||||
|
||||
void onClientStatus(in unsigned long aClientID,
|
||||
in unsigned long aClientStatus);
|
||||
};
|
|
@ -0,0 +1,60 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include "nsISupports.idl"
|
||||
|
||||
/**
|
||||
* ipcIMessageObserver
|
||||
*/
|
||||
[scriptable, uuid(e40a4a3c-2dc1-470e-ab7f-5675fe1f1384)]
|
||||
interface ipcIMessageObserver : nsISupports
|
||||
{
|
||||
/**
|
||||
* @param aTarget
|
||||
* the target of the message, corresponding to the target this
|
||||
* observer was registered under. this parameter is passed to allow
|
||||
* an observer instance to receive messages for more than one target.
|
||||
* @param aData
|
||||
* the data of the message.
|
||||
* @param aDataLen
|
||||
* the data length of the message.
|
||||
*/
|
||||
void onMessageAvailable(in nsIDRef aTarget,
|
||||
[array, const, size_is(aDataLen)]
|
||||
in octet aData,
|
||||
in unsigned long aDataLen);
|
||||
};
|
|
@ -0,0 +1,225 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include "nsISupports.idl"
|
||||
|
||||
interface ipcIMessageObserver;
|
||||
interface ipcIClientObserver;
|
||||
interface ipcIClientQueryHandler;
|
||||
|
||||
/**
|
||||
* ipcIService
|
||||
*
|
||||
* the IPC service provides the means to communicate with an external IPC
|
||||
* daemon and/or other mozilla-based applications on the same physical system.
|
||||
* the IPC daemon hosts modules (some builtin and others dynamically loaded)
|
||||
* with which applications may interact.
|
||||
*
|
||||
* at application startup, the IPC service will attempt to establish a
|
||||
* connection with the IPC daemon. the IPC daemon will be automatically
|
||||
* started if necessary. when a connection has been established, the IPC
|
||||
* service will enumerate the "ipc-startup-category" and broadcast an
|
||||
* "ipc-startup" notification using the observer service.
|
||||
*
|
||||
* when the connection to the IPC daemon is closed, an "ipc-shutdown"
|
||||
* notification will be broadcast.
|
||||
*
|
||||
* each client has a name. the client name need not be unique across all
|
||||
* clients, but it is usually good if it is. the IPC service does not require
|
||||
* unique names. instead, the IPC daemon assigns each client a unique ID that
|
||||
* is good for the current "session." clients can query other clients by name
|
||||
* or by ID. the IPC service supports forwarding messages from one client to
|
||||
* another via the IPC daemon.
|
||||
*
|
||||
* for performance reasons, this system should not be used to transfer large
|
||||
* amounts of data. instead, applications may choose to utilize shared memory,
|
||||
* and rely on the IPC service for synchronization and small message transfer
|
||||
* only.
|
||||
*/
|
||||
[scriptable, uuid(53d3e3a7-528f-4b09-9eab-9416272568c0)]
|
||||
interface ipcIService : nsISupports
|
||||
{
|
||||
/**************************************************************************
|
||||
* properties of this process
|
||||
*/
|
||||
|
||||
/**
|
||||
* returns the "client ID" assigned to this process by the IPC daemon.
|
||||
*
|
||||
* @throws NS_ERROR_NOT_AVAILABLE if no connection to the IPC daemon.
|
||||
*/
|
||||
readonly attribute unsigned long clientID;
|
||||
|
||||
/**
|
||||
* this process can appear under several client names. use the following
|
||||
* methods to add or remove names for this process.
|
||||
*
|
||||
* for example, the mozilla browser might have the primary name "mozilla",
|
||||
* but it could also register itself under the names "browser", "mail",
|
||||
* "news", "addrbook", etc. other IPC clients can then query the IPC
|
||||
* daemon for the client named "mail" in order to talk with a mail program.
|
||||
*
|
||||
* XXX An IPC client name resembles a XPCOM contract ID.
|
||||
*/
|
||||
void addClientName(in string aName);
|
||||
void removeClientName(in string aName);
|
||||
|
||||
|
||||
/**************************************************************************
|
||||
* client query methods
|
||||
*/
|
||||
|
||||
/**
|
||||
* query info about a particular client given its client name. the
|
||||
* observer's onClientInfo method is called with the result of the lookup,
|
||||
* or if there is no client matching the given name, the observer's
|
||||
* onClientDown method will be called instead.
|
||||
*
|
||||
* @param aName
|
||||
* the name of the client being queried.
|
||||
* @param aHandler
|
||||
* the handler to be notified with result.
|
||||
* @param aSync
|
||||
* block the calling thread until the query completes.
|
||||
*
|
||||
* @return integer value identifying this query.
|
||||
*/
|
||||
unsigned long queryClientByName(in string aName,
|
||||
in ipcIClientQueryHandler aHandler,
|
||||
in boolean aSync);
|
||||
|
||||
/**
|
||||
* query info about a particular client given its client ID. the observer's
|
||||
* onClientInfo method is called with the result of the lookup, or if there
|
||||
* is no client matching the given name, the observer's onClientDown method
|
||||
* will be called instead.
|
||||
*
|
||||
* @param aClientID
|
||||
* the ID of the client being queried.
|
||||
* @param aHandler
|
||||
* the handler to be notified with result.
|
||||
* @param aSync
|
||||
* block the calling thread until the query completes.
|
||||
*
|
||||
* @return integer value identifying this query.
|
||||
*/
|
||||
unsigned long queryClientByID(in unsigned long aClientID,
|
||||
in ipcIClientQueryHandler aHandler,
|
||||
in boolean aSync);
|
||||
|
||||
/**
|
||||
* called to cancel a pending query.
|
||||
*
|
||||
* @param aQueryID
|
||||
* the return value from one of the "query" methods.
|
||||
*/
|
||||
void cancelQuery(in unsigned long aQueryID);
|
||||
|
||||
/**
|
||||
* set client observer. observer's onClientUp method is called whenever
|
||||
* a new client comes online, and the observer's onClientDown method is
|
||||
* called whenever a client goes offline.
|
||||
*
|
||||
* @param aObserver
|
||||
* the client observer.
|
||||
*/
|
||||
void setClientObserver(in ipcIClientObserver aObserver);
|
||||
|
||||
// XXX need other functions to enumerate clients, clients implementing targets, etc.
|
||||
|
||||
|
||||
/**************************************************************************
|
||||
* message methods
|
||||
*/
|
||||
|
||||
/**
|
||||
* set a message observer for a particular message target.
|
||||
*
|
||||
* @param aTarget
|
||||
* the message target being observed. any existing observer will
|
||||
* be replaced.
|
||||
* @param aObserver
|
||||
* the message observer to receive incoming messages for the
|
||||
* specified target. pass null to remove the existing observer.
|
||||
*/
|
||||
void setMessageObserver(in nsIDRef aTarget, in ipcIMessageObserver aObserver);
|
||||
|
||||
/**
|
||||
* send message asynchronously to a client or a module in the IPC daemon.
|
||||
* there is no guarantee that the message will be delivered.
|
||||
*
|
||||
* @param aClientID
|
||||
* the client ID of the foreign application that should receive this
|
||||
* message. pass 0 to send a message to a module in the IPC daemon.
|
||||
* @param aTarget
|
||||
* the target of the message. if aClientID is 0, then this is the
|
||||
* ID of the daemon module that should receive this message.
|
||||
* @param aData
|
||||
* the message data.
|
||||
* @param aDataLen
|
||||
* the message length.
|
||||
* @param aSync
|
||||
* block the calling thread until a response to this message is
|
||||
* received.
|
||||
*/
|
||||
void sendMessage(in unsigned long aClientID,
|
||||
in nsIDRef aTarget,
|
||||
[array, const, size_is(aDataLen)]
|
||||
in octet aData,
|
||||
in unsigned long aDataLen,
|
||||
in boolean aSync);
|
||||
};
|
||||
|
||||
%{C++
|
||||
// singleton implementing ipcIService
|
||||
#define IPC_SERVICE_CLASSNAME \
|
||||
"ipcService"
|
||||
#define IPC_SERVICE_CONTRACTID \
|
||||
"@mozilla.org/ipc/service;1"
|
||||
#define IPC_SERVICE_CID \
|
||||
{ /* 9f12676a-5168-4a08-beb8-edf8a593a1ca */ \
|
||||
0x9f12676a, \
|
||||
0x5168, \
|
||||
0x4a08, \
|
||||
{0xbe, 0xb8, 0xed, 0xf8, 0xa5, 0x93, 0xa1, 0xca} \
|
||||
}
|
||||
|
||||
// category and observer event defines
|
||||
#define IPC_SERVICE_STARTUP_CATEGORY "ipc-startup-category"
|
||||
#define IPC_SERVICE_STARTUP_TOPIC "ipc-startup"
|
||||
#define IPC_SERVICE_SHUTDOWN_TOPIC "ipc-shutdown"
|
||||
%}
|
|
@ -0,0 +1 @@
|
|||
Makefile
|
|
@ -0,0 +1,93 @@
|
|||
# vim: noexpandtab ts=4 sw=4
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
#
|
||||
# The contents of this file are subject to the Mozilla Public License Version
|
||||
# 1.1 (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
# http://www.mozilla.org/MPL/
|
||||
#
|
||||
# Software distributed under the License is distributed on an "AS IS" basis,
|
||||
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
# for the specific language governing rights and limitations under the
|
||||
# License.
|
||||
#
|
||||
# The Original Code is Mozilla IPC.
|
||||
#
|
||||
# The Initial Developer of the Original Code is
|
||||
# Netscape Communications Corporation.
|
||||
# Portions created by the Initial Developer are Copyright (C) 2002
|
||||
# the Initial Developer. All Rights Reserved.
|
||||
#
|
||||
# Contributor(s):
|
||||
# Darin Fisher <darin@netscape.com>
|
||||
#
|
||||
# Alternatively, the contents of this file may be used under the terms of
|
||||
# either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
# in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
# of those above. If you wish to allow use of your version of this file only
|
||||
# under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
# use your version of this file under the terms of the MPL, indicate your
|
||||
# decision by deleting the provisions above and replace them with the notice
|
||||
# and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
# the provisions above, a recipient may use your version of this file under
|
||||
# the terms of any one of the MPL, the GPL or the LGPL.
|
||||
#
|
||||
# ***** END LICENSE BLOCK *****
|
||||
|
||||
DEPTH = ../../../..
|
||||
topsrcdir = @top_srcdir@
|
||||
srcdir = @srcdir@
|
||||
VPATH = @srcdir@
|
||||
|
||||
include $(DEPTH)/config/autoconf.mk
|
||||
|
||||
MODULE = ipcd
|
||||
LIBRARY_NAME = ipcdc
|
||||
EXPORT_LIBRARY = 1
|
||||
IS_COMPONENT = 1
|
||||
MODULE_NAME = ipcd
|
||||
|
||||
REQUIRES = \
|
||||
xpcom \
|
||||
string \
|
||||
$(NULL)
|
||||
|
||||
CPPSRCS = \
|
||||
ipcService.cpp \
|
||||
ipcTransport.cpp \
|
||||
ipcModuleFactory.cpp \
|
||||
$(NULL)
|
||||
|
||||
ifeq ($(OS_ARCH),WINNT)
|
||||
CPPSRCS += ipcConnectionWin.cpp
|
||||
else
|
||||
ifeq ($(OS_ARCH),BeOS)
|
||||
CPPSRCS += ipcConnectionStub.cpp
|
||||
else
|
||||
CPPSRCS += ipcConnectionUnix.cpp
|
||||
endif
|
||||
endif
|
||||
|
||||
LOCAL_INCLUDES = \
|
||||
-I$(srcdir)/../../shared/src \
|
||||
-I$(srcdir)/../../extensions/lock/src \
|
||||
-I$(srcdir)/../../extensions/transmngr/src \
|
||||
-I$(srcdir)/../../extensions/transmngr/common \
|
||||
$(NULL)
|
||||
|
||||
SHARED_LIBRARY_LIBS = \
|
||||
$(DIST)/lib/$(LIB_PREFIX)ipcdshared_s.$(LIB_SUFFIX) \
|
||||
$(DIST)/lib/$(LIB_PREFIX)ipcdlock_s.$(LIB_SUFFIX) \
|
||||
$(DIST)/lib/$(LIB_PREFIX)transmngr_s.$(LIB_SUFFIX) \
|
||||
$(DIST)/lib/$(LIB_PREFIX)transmngrcom_s.$(LIB_SUFFIX) \
|
||||
$(NULL)
|
||||
|
||||
EXTRA_DSO_LDOPTS = \
|
||||
$(LIBS_DIR) \
|
||||
$(EXTRA_DSO_LIBS) \
|
||||
$(MOZ_COMPONENT_LIBS) \
|
||||
$(NULL)
|
||||
|
||||
include $(topsrcdir)/config/rules.mk
|
|
@ -0,0 +1,129 @@
|
|||
/* vim:set ts=2 sw=2 et cindent: */
|
||||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is IBM Corporation.
|
||||
* Portions created by IBM Corporation are Copyright (C) 2003
|
||||
* IBM Corporation. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@meer.net>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#ifndef ipcConnection_h__
|
||||
#define ipcConnection_h__
|
||||
|
||||
#include "nscore.h"
|
||||
|
||||
class ipcMessage;
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
/* Platform specific IPC connection API.
|
||||
*/
|
||||
|
||||
/**
|
||||
* IPC_Connect
|
||||
*
|
||||
* This function causes a connection to the IPC daemon to be established.
|
||||
* If a connection already exists, then this function will be ignored.
|
||||
*
|
||||
* @param daemonPath
|
||||
* Specifies the path to the IPC daemon executable.
|
||||
*
|
||||
* NOTE: This function must be called on the main thread.
|
||||
*/
|
||||
nsresult IPC_Connect(const char *daemonPath);
|
||||
|
||||
/**
|
||||
* IPC_Disconnect
|
||||
*
|
||||
* This function causes a connection to the IPC daemon to be closed. Any
|
||||
* unsent messages (IPC_SendMsg puts messages on a queue) will be sent to the
|
||||
* IPC daemon before the connection is closed.
|
||||
*
|
||||
* NOTE: This function must be called on the main thread.
|
||||
*/
|
||||
nsresult IPC_Disconnect();
|
||||
|
||||
/**
|
||||
* IPC_SendMsg
|
||||
*
|
||||
* This function sends a message to the IPC daemon. Typically, the message
|
||||
* is put on a queue, to be delivered asynchronously to the IPC daemon. The
|
||||
* ipcMessage object will be deleted when IPC_SendMsg is done with it. The
|
||||
* caller must not touch |msg| after passing it to IPC_SendMsg.
|
||||
*
|
||||
* IPC_SendMsg cannot be called before IPC_Connect or after IPC_Disconnect.
|
||||
*
|
||||
* NOTE: This function may be called on any thread.
|
||||
*/
|
||||
nsresult IPC_SendMsg(ipcMessage *msg);
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
/* Cross-platform IPC connection methods.
|
||||
*/
|
||||
|
||||
/**
|
||||
* IPC_SpawnDaemon
|
||||
*
|
||||
* This function launches the IPC daemon process. It is called by the platform
|
||||
* specific IPC_Connect implementation. This function may be called on any
|
||||
* thread. It should not return until the daemon process is ready to receive
|
||||
* a client connection or an error occurs.
|
||||
*
|
||||
* @param daemonPath
|
||||
* Specifies the path to the IPC daemon executable.
|
||||
*/
|
||||
nsresult IPC_SpawnDaemon(const char *daemonPath);
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
/* IPC connection callbacks (not implemented by the connection code).
|
||||
*
|
||||
* NOTE: These functions execute on a background thread!!
|
||||
*/
|
||||
|
||||
/**
|
||||
* IPC_OnConnectionEnd
|
||||
*
|
||||
* This function is called whenever the connection to the IPC daemon has been
|
||||
* terminated. If terminated due to an abnormal error, then the error will be
|
||||
* described by the |error| parameter. If |error| is NS_OK, then it means the
|
||||
* connection was closed in response to a call to IPC_Disconnect.
|
||||
*/
|
||||
void IPC_OnConnectionEnd(nsresult error);
|
||||
|
||||
/**
|
||||
* IPC_OnMessageAvailable
|
||||
*
|
||||
* This function is called whenever an incoming message is read from the IPC
|
||||
* daemon. The ipcMessage object, |msg|, must be deleted by the implementation
|
||||
* of IPC_OnMessageAvailable when the object is no longer needed.
|
||||
*/
|
||||
void IPC_OnMessageAvailable(ipcMessage *msg);
|
||||
|
||||
#endif // ipcConnection_h__
|
|
@ -0,0 +1,64 @@
|
|||
/* vim:set ts=2 sw=2 et cindent: */
|
||||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is IBM Corporation.
|
||||
* Portions created by IBM Corporation are Copyright (C) 2003
|
||||
* IBM Corporation. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@meer.net>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include "ipcConnection.h"
|
||||
#include "nsError.h"
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// use this file as a template to add client-side IPC connectivity.
|
||||
//
|
||||
// NOTE: if your platform supports local domain TCP sockets, then you should
|
||||
// be able to make use of ipcConnectionUnix.cpp.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
nsresult
|
||||
IPC_Connect(const char *daemonPath)
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
nsresult
|
||||
IPC_Disconnect()
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
nsresult
|
||||
IPC_SendMsg(ipcMessage *msg)
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
|
@ -0,0 +1,536 @@
|
|||
/* vim:set ts=2 sw=2 et cindent: */
|
||||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is IBM Corporation.
|
||||
* Portions created by IBM Corporation are Copyright (C) 2003
|
||||
* IBM Corporation. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@meer.net>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include "private/pprio.h"
|
||||
#include "prerror.h"
|
||||
#include "prthread.h"
|
||||
#include "prlock.h"
|
||||
#include "prlog.h" // for PR_ASSERT (we don't actually use NSPR logging)
|
||||
#include "prio.h"
|
||||
|
||||
#include "ipcConnection.h"
|
||||
#include "ipcMessageQ.h"
|
||||
#include "ipcConfig.h"
|
||||
#include "ipcLog.h"
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// NOTE: this code does not need to link with anything but NSPR. that is by
|
||||
// design, so it can be easily reused in other projects that want to
|
||||
// talk with mozilla's IPC daemon, but don't want to depend on xpcom.
|
||||
// we depend at most on some xpcom header files, but no xpcom runtime
|
||||
// symbols are used.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
// single user systems, like OS/2, don't need these security checks.
|
||||
#ifndef XP_OS2
|
||||
#define IPC_SKIP_SECURITY_CHECKS
|
||||
#endif
|
||||
|
||||
#ifndef IPC_SKIP_SECURITY_CHECKS
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#endif
|
||||
|
||||
static PRStatus
|
||||
DoSecurityCheck(PRFileDesc *fd, const char *path)
|
||||
{
|
||||
#ifndef IPC_SKIP_SECURITY_CHECKS
|
||||
//
|
||||
// now that we have a connected socket; do some security checks on the
|
||||
// file descriptor.
|
||||
//
|
||||
// (1) make sure owner matches
|
||||
// (2) make sure permissions match expected permissions
|
||||
//
|
||||
// if these conditions aren't met then bail.
|
||||
//
|
||||
int unix_fd = PR_FileDesc2NativeHandle(fd);
|
||||
|
||||
struct stat st;
|
||||
if (fstat(unix_fd, &st) == -1) {
|
||||
LOG(("stat failed"));
|
||||
return PR_FAILURE;
|
||||
}
|
||||
|
||||
if (st.st_uid != getuid() && st.st_uid != geteuid()) {
|
||||
//
|
||||
// on OSX 10.1.5, |fstat| has a bug when passed a file descriptor to
|
||||
// a socket. it incorrectly returns a UID of 0. however, |stat|
|
||||
// succeeds, but using |stat| introduces a race condition.
|
||||
//
|
||||
// XXX come up with a better security check.
|
||||
//
|
||||
if (st.st_uid != 0) {
|
||||
LOG(("userid check failed"));
|
||||
return PR_FAILURE;
|
||||
}
|
||||
if (stat(path, &st) == -1) {
|
||||
LOG(("stat failed"));
|
||||
return PR_FAILURE;
|
||||
}
|
||||
if (st.st_uid != getuid() && st.st_uid != geteuid()) {
|
||||
LOG(("userid check failed"));
|
||||
return PR_FAILURE;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return PR_SUCCESS;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
struct ipcConnectionState
|
||||
{
|
||||
PRLock *lock;
|
||||
PRPollDesc fds[2];
|
||||
ipcMessageQ send_queue;
|
||||
PRUint32 send_offset; // amount of send_queue.First() already written.
|
||||
ipcMessage *in_msg;
|
||||
PRBool shutdown;
|
||||
};
|
||||
|
||||
#define SOCK 0
|
||||
#define POLL 1
|
||||
|
||||
static void
|
||||
ConnDestroy(ipcConnectionState *s)
|
||||
{
|
||||
if (s->lock)
|
||||
PR_DestroyLock(s->lock);
|
||||
|
||||
if (s->fds[SOCK].fd)
|
||||
PR_Close(s->fds[SOCK].fd);
|
||||
|
||||
if (s->fds[POLL].fd)
|
||||
PR_DestroyPollableEvent(s->fds[POLL].fd);
|
||||
|
||||
if (s->in_msg)
|
||||
delete s->in_msg;
|
||||
|
||||
s->send_queue.DeleteAll();
|
||||
delete s;
|
||||
}
|
||||
|
||||
static ipcConnectionState *
|
||||
ConnCreate(PRFileDesc *fd)
|
||||
{
|
||||
ipcConnectionState *s = new ipcConnectionState;
|
||||
if (!s)
|
||||
return NULL;
|
||||
|
||||
s->lock = PR_NewLock();
|
||||
s->fds[SOCK].fd = NULL;
|
||||
s->fds[POLL].fd = PR_NewPollableEvent();
|
||||
s->send_offset = 0;
|
||||
s->in_msg = NULL;
|
||||
|
||||
if (!s->lock || !s->fds[1].fd)
|
||||
{
|
||||
ConnDestroy(s);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// store this only if we are going to succeed.
|
||||
s->fds[SOCK].fd = fd;
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
static nsresult
|
||||
ConnRead(ipcConnectionState *s)
|
||||
{
|
||||
char buf[1024];
|
||||
nsresult rv = NS_OK;
|
||||
PRInt32 n;
|
||||
|
||||
do
|
||||
{
|
||||
n = PR_Read(s->fds[SOCK].fd, buf, sizeof(buf));
|
||||
if (n < 0)
|
||||
{
|
||||
PRErrorCode err = PR_GetError();
|
||||
if (err == PR_WOULD_BLOCK_ERROR)
|
||||
{
|
||||
// socket is empty... we need to go back to polling.
|
||||
break;
|
||||
}
|
||||
LOG(("PR_Read returned failure [err=%d]\n", err));
|
||||
rv = NS_ERROR_UNEXPECTED;
|
||||
}
|
||||
else if (n == 0)
|
||||
{
|
||||
LOG(("PR_Read returned EOF\n"));
|
||||
rv = NS_ERROR_UNEXPECTED;
|
||||
}
|
||||
else
|
||||
{
|
||||
const char *pdata = buf;
|
||||
while (n)
|
||||
{
|
||||
PRUint32 bytesRead;
|
||||
PRBool complete;
|
||||
|
||||
if (!s->in_msg)
|
||||
{
|
||||
s->in_msg = new ipcMessage;
|
||||
if (!s->in_msg)
|
||||
{
|
||||
rv = NS_ERROR_OUT_OF_MEMORY;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (s->in_msg->ReadFrom(pdata, n, &bytesRead, &complete) != PR_SUCCESS)
|
||||
{
|
||||
LOG(("error reading IPC message\n"));
|
||||
rv = NS_ERROR_UNEXPECTED;
|
||||
break;
|
||||
}
|
||||
|
||||
PR_ASSERT(PRUint32(n) >= bytesRead);
|
||||
n -= bytesRead;
|
||||
pdata += bytesRead;
|
||||
|
||||
if (complete)
|
||||
{
|
||||
// protect against weird re-entrancy cases...
|
||||
ipcMessage *m = s->in_msg;
|
||||
s->in_msg = NULL;
|
||||
|
||||
IPC_OnMessageAvailable(m);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
while (NS_SUCCEEDED(rv));
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
static nsresult
|
||||
ConnWrite(ipcConnectionState *s)
|
||||
{
|
||||
nsresult rv = NS_OK;
|
||||
|
||||
PR_Lock(s->lock);
|
||||
|
||||
// write one message and then return.
|
||||
if (s->send_queue.First())
|
||||
{
|
||||
PRInt32 n = PR_Write(s->fds[SOCK].fd,
|
||||
s->send_queue.First()->MsgBuf() + s->send_offset,
|
||||
s->send_queue.First()->MsgLen() - s->send_offset);
|
||||
if (n <= 0)
|
||||
{
|
||||
PRErrorCode err = PR_GetError();
|
||||
if (err == PR_WOULD_BLOCK_ERROR)
|
||||
{
|
||||
// socket is full... we need to go back to polling.
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG(("error writing to socket [err=%d]\n", err));
|
||||
rv = NS_ERROR_UNEXPECTED;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
s->send_offset += n;
|
||||
if (s->send_offset == s->send_queue.First()->MsgLen())
|
||||
{
|
||||
s->send_queue.DeleteFirst();
|
||||
s->send_offset = 0;
|
||||
|
||||
// if the send queue is empty, then we need to stop trying to write.
|
||||
if (s->send_queue.IsEmpty())
|
||||
s->fds[SOCK].in_flags &= ~PR_POLL_WRITE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PR_Unlock(s->lock);
|
||||
return rv;
|
||||
}
|
||||
|
||||
PR_STATIC_CALLBACK(void)
|
||||
ConnThread(void *arg)
|
||||
{
|
||||
PRInt32 num;
|
||||
nsresult rv = NS_OK;
|
||||
|
||||
ipcConnectionState *s = (ipcConnectionState *) arg;
|
||||
|
||||
// we monitor two file descriptors in this thread. the first (at index 0) is
|
||||
// the socket connection with the IPC daemon. the second (at index 1) is the
|
||||
// pollable event we monitor in order to know when to send messages to the
|
||||
// IPC daemon.
|
||||
|
||||
s->fds[SOCK].in_flags = PR_POLL_READ;
|
||||
s->fds[POLL].in_flags = PR_POLL_READ;
|
||||
|
||||
while (NS_SUCCEEDED(rv))
|
||||
{
|
||||
s->fds[SOCK].out_flags = 0;
|
||||
s->fds[POLL].out_flags = 0;
|
||||
|
||||
//
|
||||
// poll on the IPC socket and NSPR pollable event
|
||||
//
|
||||
num = PR_Poll(s->fds, 2, PR_INTERVAL_NO_TIMEOUT);
|
||||
if (num > 0)
|
||||
{
|
||||
// check if something has been added to the send queue. if so, then
|
||||
// acknowledge pollable event (wait should not block), and configure
|
||||
// poll flags to find out when we can write.
|
||||
if (s->fds[POLL].out_flags & PR_POLL_READ)
|
||||
{
|
||||
PR_WaitForPollableEvent(s->fds[POLL].fd);
|
||||
PR_Lock(s->lock);
|
||||
if (!s->send_queue.IsEmpty())
|
||||
s->fds[SOCK].in_flags |= PR_POLL_WRITE;
|
||||
else if (s->shutdown)
|
||||
rv = NS_ERROR_ABORT;
|
||||
PR_Unlock(s->lock);
|
||||
}
|
||||
|
||||
// check if we can read...
|
||||
if (s->fds[SOCK].out_flags & PR_POLL_READ)
|
||||
rv = ConnRead(s);
|
||||
|
||||
// check if we can write...
|
||||
if (s->fds[SOCK].out_flags & PR_POLL_WRITE)
|
||||
rv = ConnWrite(s);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG(("PR_Poll returned an error\n"));
|
||||
rv = NS_ERROR_UNEXPECTED;
|
||||
}
|
||||
}
|
||||
|
||||
// notify termination of the IPC connection
|
||||
if (rv == NS_ERROR_ABORT)
|
||||
rv = NS_OK;
|
||||
IPC_OnConnectionEnd(rv);
|
||||
|
||||
LOG(("IPC thread exiting\n"));
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// IPC connection API
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
static ipcConnectionState *gConnState = NULL;
|
||||
static PRThread *gConnThread = NULL;
|
||||
|
||||
#ifdef DEBUG
|
||||
static PRThread *gMainThread = NULL;
|
||||
#endif
|
||||
|
||||
nsresult
|
||||
TryConnect(PRFileDesc **result)
|
||||
{
|
||||
PRFileDesc *fd;
|
||||
PRNetAddr addr;
|
||||
PRSocketOptionData opt;
|
||||
nsresult rv = NS_ERROR_FAILURE;
|
||||
|
||||
fd = PR_OpenTCPSocket(PR_AF_LOCAL);
|
||||
if (!fd)
|
||||
goto end;
|
||||
|
||||
addr.local.family = PR_AF_LOCAL;
|
||||
IPC_GetDefaultSocketPath(addr.local.path, sizeof(addr.local.path));
|
||||
|
||||
// blocking connect... will fail if no one is listening.
|
||||
if (PR_Connect(fd, &addr, PR_INTERVAL_NO_TIMEOUT) == PR_FAILURE)
|
||||
goto end;
|
||||
|
||||
// make socket non-blocking
|
||||
opt.option = PR_SockOpt_Nonblocking;
|
||||
opt.value.non_blocking = PR_TRUE;
|
||||
PR_SetSocketOption(fd, &opt);
|
||||
|
||||
// do some security checks on connection socket...
|
||||
if (DoSecurityCheck(fd, addr.local.path) != PR_SUCCESS)
|
||||
goto end;
|
||||
|
||||
*result = fd;
|
||||
return NS_OK;
|
||||
|
||||
end:
|
||||
if (fd)
|
||||
PR_Close(fd);
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
nsresult
|
||||
IPC_Connect(const char *daemonPath)
|
||||
{
|
||||
// synchronous connect, spawn daemon if necessary.
|
||||
|
||||
PRFileDesc *fd;
|
||||
nsresult rv = NS_ERROR_FAILURE;
|
||||
|
||||
if (gConnState)
|
||||
return NS_ERROR_ALREADY_INITIALIZED;
|
||||
|
||||
//
|
||||
// here's the connection algorithm: try to connect to an existing daemon.
|
||||
// if the connection fails, then spawn the daemon (wait for it to be ready),
|
||||
// and then retry the connection. it is critical that the socket used to
|
||||
// connect to the daemon not be inherited (this causes problems on RH9 at
|
||||
// least).
|
||||
//
|
||||
|
||||
rv = TryConnect(&fd);
|
||||
if (NS_FAILED(rv))
|
||||
{
|
||||
rv = IPC_SpawnDaemon(daemonPath);
|
||||
if (NS_SUCCEEDED(rv))
|
||||
rv = TryConnect(&fd);
|
||||
}
|
||||
|
||||
if (NS_FAILED(rv))
|
||||
goto end;
|
||||
|
||||
//
|
||||
// ok, we have a connection to the daemon!
|
||||
//
|
||||
|
||||
// build connection state object
|
||||
gConnState = ConnCreate(fd);
|
||||
if (!gConnState)
|
||||
{
|
||||
rv = NS_ERROR_OUT_OF_MEMORY;
|
||||
goto end;
|
||||
}
|
||||
fd = NULL; // connection state now owns the socket
|
||||
|
||||
gConnThread = PR_CreateThread(PR_USER_THREAD,
|
||||
ConnThread,
|
||||
gConnState,
|
||||
PR_PRIORITY_NORMAL,
|
||||
PR_GLOBAL_THREAD,
|
||||
PR_JOINABLE_THREAD,
|
||||
0);
|
||||
if (!gConnThread)
|
||||
{
|
||||
rv = NS_ERROR_OUT_OF_MEMORY;
|
||||
goto end;
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
gMainThread = PR_GetCurrentThread();
|
||||
#endif
|
||||
return NS_OK;
|
||||
|
||||
end:
|
||||
if (gConnState)
|
||||
{
|
||||
ConnDestroy(gConnState);
|
||||
gConnState = NULL;
|
||||
}
|
||||
if (fd)
|
||||
PR_Close(fd);
|
||||
return rv;
|
||||
}
|
||||
|
||||
nsresult
|
||||
IPC_Disconnect()
|
||||
{
|
||||
// Must disconnect on same thread used to connect!
|
||||
PR_ASSERT(gMainThread == PR_GetCurrentThread());
|
||||
|
||||
if (!gConnState || !gConnThread)
|
||||
return NS_ERROR_NOT_INITIALIZED;
|
||||
|
||||
PR_Lock(gConnState->lock);
|
||||
gConnState->shutdown = PR_TRUE;
|
||||
PR_SetPollableEvent(gConnState->fds[POLL].fd);
|
||||
PR_Unlock(gConnState->lock);
|
||||
|
||||
PR_JoinThread(gConnThread);
|
||||
|
||||
ConnDestroy(gConnState);
|
||||
|
||||
gConnState = NULL;
|
||||
gConnThread = NULL;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
IPC_SendMsg(ipcMessage *msg)
|
||||
{
|
||||
if (!gConnState || !gConnThread)
|
||||
return NS_ERROR_NOT_INITIALIZED;
|
||||
|
||||
PR_Lock(gConnState->lock);
|
||||
gConnState->send_queue.Append(msg);
|
||||
PR_SetPollableEvent(gConnState->fds[POLL].fd);
|
||||
PR_Unlock(gConnState->lock);
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifdef TEST_STANDALONE
|
||||
|
||||
void IPC_OnConnectionFault(nsresult rv)
|
||||
{
|
||||
LOG(("IPC_OnConnectionFault [rv=%x]\n", rv));
|
||||
}
|
||||
|
||||
void IPC_OnMessageAvailable(ipcMessage *msg)
|
||||
{
|
||||
LOG(("IPC_OnMessageAvailable\n"));
|
||||
delete msg;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
IPC_InitLog(">>>");
|
||||
IPC_Connect("/builds/moz-trunk/seamonkey-debug-build/dist/bin/mozipcd");
|
||||
IPC_Disconnect();
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,308 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@meer.net>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#include "prprf.h"
|
||||
#include "prmon.h"
|
||||
#include "prthread.h"
|
||||
#include "plevent.h"
|
||||
|
||||
#include "nsIServiceManager.h"
|
||||
#include "nsIEventQueue.h"
|
||||
#include "nsIEventQueueService.h"
|
||||
#include "nsAutoLock.h"
|
||||
|
||||
#include "ipcConfig.h"
|
||||
#include "ipcLog.h"
|
||||
#include "ipcConnection.h"
|
||||
#include "ipcm.h"
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// NOTE: this code does not need to link with anything but NSPR. that is by
|
||||
// design, so it can be easily reused in other projects that want to
|
||||
// talk with mozilla's IPC daemon, but don't want to depend on xpcom.
|
||||
// we depend at most on some xpcom header files, but no xpcom runtime
|
||||
// symbols are used.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// windows message thread
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#define IPC_WM_SENDMSG (WM_USER + 0x1)
|
||||
#define IPC_WM_SHUTDOWN (WM_USER + 0x2)
|
||||
|
||||
static nsresult ipcThreadStatus = NS_OK;
|
||||
static PRThread *ipcThread = NULL;
|
||||
static PRMonitor *ipcMonitor = NULL;
|
||||
static HWND ipcDaemonHwnd = NULL;
|
||||
static HWND ipcLocalHwnd = NULL;
|
||||
static PRBool ipcShutdown = PR_FALSE; // not accessed on message thread!!
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// window proc
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
static LRESULT CALLBACK
|
||||
ipcThreadWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
||||
{
|
||||
LOG(("got message [msg=%x wparam=%x lparam=%x]\n", uMsg, wParam, lParam));
|
||||
|
||||
if (uMsg == WM_COPYDATA) {
|
||||
COPYDATASTRUCT *cd = (COPYDATASTRUCT *) lParam;
|
||||
if (cd && cd->lpData) {
|
||||
ipcMessage *msg = new ipcMessage();
|
||||
PRUint32 bytesRead;
|
||||
PRBool complete;
|
||||
PRStatus rv = msg->ReadFrom((const char *) cd->lpData, cd->cbData,
|
||||
&bytesRead, &complete);
|
||||
if (rv == PR_SUCCESS && complete)
|
||||
IPC_OnMessageAvailable(msg); // takes ownership of msg
|
||||
else {
|
||||
LOG((" unable to deliver message [complete=%u]\n", complete));
|
||||
delete msg;
|
||||
}
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
if (uMsg == IPC_WM_SENDMSG) {
|
||||
ipcMessage *msg = (ipcMessage *) lParam;
|
||||
if (msg) {
|
||||
LOG((" sending message...\n"));
|
||||
COPYDATASTRUCT cd;
|
||||
cd.dwData = GetCurrentProcessId();
|
||||
cd.cbData = (DWORD) msg->MsgLen();
|
||||
cd.lpData = (PVOID) msg->MsgBuf();
|
||||
SendMessageA(ipcDaemonHwnd, WM_COPYDATA, (WPARAM) hWnd, (LPARAM) &cd);
|
||||
LOG((" done.\n"));
|
||||
delete msg;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (uMsg == IPC_WM_SHUTDOWN) {
|
||||
IPC_OnConnectionEnd(NS_OK);
|
||||
PostQuitMessage(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return DefWindowProc(hWnd, uMsg, wParam, lParam);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// ipc thread functions
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
static void
|
||||
ipcThreadFunc(void *arg)
|
||||
{
|
||||
LOG(("entering message thread\n"));
|
||||
|
||||
DWORD pid = GetCurrentProcessId();
|
||||
|
||||
WNDCLASS wc;
|
||||
memset(&wc, 0, sizeof(wc));
|
||||
wc.lpfnWndProc = ipcThreadWindowProc;
|
||||
wc.lpszClassName = IPC_CLIENT_WINDOW_CLASS;
|
||||
RegisterClass(&wc);
|
||||
|
||||
char wName[sizeof(IPC_CLIENT_WINDOW_NAME_PREFIX) + 20];
|
||||
PR_snprintf(wName, sizeof(wName), "%s%u", IPC_CLIENT_WINDOW_NAME_PREFIX, pid);
|
||||
|
||||
ipcLocalHwnd = CreateWindow(IPC_CLIENT_WINDOW_CLASS, wName,
|
||||
0, 0, 0, 10, 10, NULL, NULL, NULL, NULL);
|
||||
|
||||
{
|
||||
nsAutoMonitor mon(ipcMonitor);
|
||||
if (!ipcLocalHwnd)
|
||||
ipcThreadStatus = NS_ERROR_FAILURE;
|
||||
mon.Notify();
|
||||
}
|
||||
|
||||
if (ipcLocalHwnd) {
|
||||
MSG msg;
|
||||
while (GetMessage(&msg, ipcLocalHwnd, 0, 0))
|
||||
DispatchMessage(&msg);
|
||||
|
||||
ipcShutdown = PR_TRUE; // assuming atomic memory write
|
||||
|
||||
DestroyWindow(ipcLocalHwnd);
|
||||
ipcLocalHwnd = NULL;
|
||||
}
|
||||
|
||||
LOG(("exiting message thread\n"));
|
||||
return;
|
||||
}
|
||||
|
||||
static PRStatus
|
||||
ipcThreadInit()
|
||||
{
|
||||
if (ipcThread)
|
||||
return PR_FAILURE;
|
||||
|
||||
ipcShutdown = PR_FALSE;
|
||||
|
||||
ipcMonitor = PR_NewMonitor();
|
||||
if (!ipcMonitor)
|
||||
return PR_FAILURE;
|
||||
|
||||
// spawn message thread
|
||||
ipcThread = PR_CreateThread(PR_USER_THREAD, ipcThreadFunc, NULL,
|
||||
PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
|
||||
PR_JOINABLE_THREAD, 0);
|
||||
if (!ipcThread) {
|
||||
NS_WARNING("thread creation failed");
|
||||
PR_DestroyMonitor(ipcMonitor);
|
||||
ipcMonitor = NULL;
|
||||
return PR_FAILURE;
|
||||
}
|
||||
|
||||
// wait for hidden window to be created
|
||||
{
|
||||
nsAutoMonitor mon(ipcMonitor);
|
||||
while (!ipcLocalHwnd && NS_SUCCEEDED(ipcThreadStatus))
|
||||
mon.Wait();
|
||||
}
|
||||
|
||||
if (NS_FAILED(ipcThreadStatus)) {
|
||||
NS_WARNING("message thread failed");
|
||||
return PR_FAILURE;
|
||||
}
|
||||
|
||||
return PR_SUCCESS;
|
||||
}
|
||||
|
||||
static PRStatus
|
||||
ipcThreadShutdown()
|
||||
{
|
||||
if (PR_AtomicSet(&ipcShutdown, PR_TRUE) == PR_FALSE) {
|
||||
LOG(("posting IPC_WM_SHUTDOWN message\n"));
|
||||
PostMessage(ipcLocalHwnd, IPC_WM_SHUTDOWN, 0, 0);
|
||||
}
|
||||
|
||||
LOG(("joining w/ message thread...\n"));
|
||||
PR_JoinThread(ipcThread);
|
||||
ipcThread = NULL;
|
||||
|
||||
//
|
||||
// ok, now the message thread is dead
|
||||
//
|
||||
|
||||
PR_DestroyMonitor(ipcMonitor);
|
||||
ipcMonitor = NULL;
|
||||
|
||||
return PR_SUCCESS;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// windows specific IPC connection impl
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
nsresult
|
||||
IPC_Disconnect()
|
||||
{
|
||||
LOG(("IPC_Disconnect\n"));
|
||||
|
||||
//XXX mHaveConnection = PR_FALSE;
|
||||
|
||||
if (!ipcDaemonHwnd)
|
||||
return NS_ERROR_NOT_INITIALIZED;
|
||||
|
||||
if (ipcThread)
|
||||
ipcThreadShutdown();
|
||||
|
||||
// clear our reference to the daemon's HWND.
|
||||
ipcDaemonHwnd = NULL;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
IPC_Connect(const char *daemonPath)
|
||||
{
|
||||
LOG(("IPC_Connect\n"));
|
||||
|
||||
NS_ENSURE_TRUE(ipcDaemonHwnd == NULL, NS_ERROR_ALREADY_INITIALIZED);
|
||||
nsresult rv;
|
||||
|
||||
ipcDaemonHwnd = FindWindow(IPC_WINDOW_CLASS, IPC_WINDOW_NAME);
|
||||
if (!ipcDaemonHwnd) {
|
||||
LOG((" daemon does not appear to be running\n"));
|
||||
//
|
||||
// daemon does not exist; spawn daemon...
|
||||
//
|
||||
rv = IPC_SpawnDaemon(daemonPath);
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
|
||||
ipcDaemonHwnd = FindWindow(IPC_WINDOW_CLASS, IPC_WINDOW_NAME);
|
||||
if (!ipcDaemonHwnd)
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
//
|
||||
// delay creation of the message thread until we know the daemon exists.
|
||||
//
|
||||
if (!ipcThread && ipcThreadInit() != PR_SUCCESS) {
|
||||
ipcDaemonHwnd = NULL;
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
IPC_SendMsg(ipcMessage *msg)
|
||||
{
|
||||
LOG(("IPC_SendMsg\n"));
|
||||
|
||||
if (ipcShutdown) {
|
||||
LOG(("unable to send message b/c message thread is shutdown\n"));
|
||||
goto loser;
|
||||
}
|
||||
if (!PostMessage(ipcLocalHwnd, IPC_WM_SENDMSG, 0, (LPARAM) msg)) {
|
||||
LOG((" PostMessage failed w/ error = %u\n", GetLastError()));
|
||||
goto loser;
|
||||
}
|
||||
return NS_OK;
|
||||
loser:
|
||||
delete msg;
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
|
@ -0,0 +1,127 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include "nsIServiceManager.h"
|
||||
#include "nsIGenericFactory.h"
|
||||
#include "nsICategoryManager.h"
|
||||
#include "ipcService.h"
|
||||
#include "ipcConfig.h"
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Define the contructor function for the objects
|
||||
//
|
||||
// NOTE: This creates an instance of objects by using the default constructor
|
||||
//-----------------------------------------------------------------------------
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(ipcService, Init)
|
||||
|
||||
// enable this code to make the IPC service auto-start.
|
||||
#if 0
|
||||
NS_METHOD
|
||||
ipcServiceRegisterProc(nsIComponentManager *aCompMgr,
|
||||
nsIFile *aPath,
|
||||
const char *registryLocation,
|
||||
const char *componentType,
|
||||
const nsModuleComponentInfo *info)
|
||||
{
|
||||
//
|
||||
// add ipcService to the XPCOM startup category
|
||||
//
|
||||
nsCOMPtr<nsICategoryManager> catman(do_GetService(NS_CATEGORYMANAGER_CONTRACTID));
|
||||
if (catman) {
|
||||
nsXPIDLCString prevEntry;
|
||||
catman->AddCategoryEntry(NS_XPCOM_STARTUP_OBSERVER_ID, "ipcService",
|
||||
IPC_SERVICE_CONTRACTID, PR_TRUE, PR_TRUE,
|
||||
getter_Copies(prevEntry));
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_METHOD
|
||||
ipcServiceUnregisterProc(nsIComponentManager *aCompMgr,
|
||||
nsIFile *aPath,
|
||||
const char *registryLocation,
|
||||
const nsModuleComponentInfo *info)
|
||||
{
|
||||
nsCOMPtr<nsICategoryManager> catman(do_GetService(NS_CATEGORYMANAGER_CONTRACTID));
|
||||
if (catman)
|
||||
catman->DeleteCategoryEntry(NS_XPCOM_STARTUP_OBSERVER_ID,
|
||||
IPC_SERVICE_CONTRACTID, PR_TRUE);
|
||||
return NS_OK;
|
||||
}
|
||||
#endif
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// extensions
|
||||
|
||||
#include "ipcLockService.h"
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(ipcLockService, Init)
|
||||
|
||||
#include "tmTransactionService.h"
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(tmTransactionService)
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Define a table of CIDs implemented by this module along with other
|
||||
// information like the function to create an instance, contractid, and
|
||||
// class name.
|
||||
//-----------------------------------------------------------------------------
|
||||
static const nsModuleComponentInfo components[] = {
|
||||
{ IPC_SERVICE_CLASSNAME,
|
||||
IPC_SERVICE_CID,
|
||||
IPC_SERVICE_CONTRACTID,
|
||||
ipcServiceConstructor },
|
||||
/*
|
||||
ipcServiceRegisterProc,
|
||||
ipcServiceUnregisterProc },
|
||||
*/
|
||||
//
|
||||
// extensions go here:
|
||||
//
|
||||
{ IPC_LOCKSERVICE_CLASSNAME,
|
||||
IPC_LOCKSERVICE_CID,
|
||||
IPC_LOCKSERVICE_CONTRACTID,
|
||||
ipcLockServiceConstructor },
|
||||
{ IPC_TRANSACTIONSERVICE_CLASSNAME,
|
||||
IPC_TRANSACTIONSERVICE_CID,
|
||||
IPC_TRANSACTIONSERVICE_CONTRACTID,
|
||||
tmTransactionServiceConstructor },
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Implement the NSGetModule() exported function for your module
|
||||
// and the entire implementation of the module object.
|
||||
//-----------------------------------------------------------------------------
|
||||
NS_IMPL_NSGETMODULE(ipcd, components)
|
|
@ -0,0 +1,558 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "plstr.h"
|
||||
|
||||
#include "nsIServiceManager.h"
|
||||
#include "nsIEventQueueService.h"
|
||||
#include "nsIEventQueue.h"
|
||||
#include "nsIObserverService.h"
|
||||
#include "nsICategoryManager.h"
|
||||
#include "nsCategoryManagerUtils.h"
|
||||
#include "nsEventQueueUtils.h"
|
||||
|
||||
#include "ipcConfig.h"
|
||||
#include "ipcLog.h"
|
||||
#include "ipcService.h"
|
||||
#include "ipcMessageUtils.h"
|
||||
#include "ipcm.h"
|
||||
|
||||
#include "ipcIMessageObserver.h"
|
||||
#include "ipcIClientObserver.h"
|
||||
#include "ipcIClientQueryHandler.h"
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// helpers
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
static PRBool PR_CALLBACK
|
||||
ipcReleaseMessageObserver(nsHashKey *aKey, void *aData, void* aClosure)
|
||||
{
|
||||
ipcIMessageObserver *obs = (ipcIMessageObserver *) aData;
|
||||
NS_RELEASE(obs);
|
||||
return PR_TRUE;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// ipcClientQuery
|
||||
//----------------------------------------------------------------------------
|
||||
|
||||
class ipcClientQuery
|
||||
{
|
||||
public:
|
||||
ipcClientQuery(PRUint32 cID, ipcIClientQueryHandler *handler)
|
||||
: mNext(nsnull)
|
||||
, mQueryID(++gLastQueryID)
|
||||
, mClientID(cID)
|
||||
, mHandler(handler)
|
||||
{ }
|
||||
|
||||
static PRUint32 gLastQueryID;
|
||||
|
||||
void SetClientID(PRUint32 cID) { mClientID = cID; }
|
||||
void OnQueryComplete(nsresult status, const ipcmMessageClientInfo *msg);
|
||||
|
||||
PRUint32 QueryID() { return mQueryID; }
|
||||
PRBool IsCanceled() { return mHandler.get() == NULL; }
|
||||
|
||||
ipcClientQuery *mNext;
|
||||
private:
|
||||
PRUint32 mQueryID;
|
||||
PRUint32 mClientID;
|
||||
nsCOMPtr<ipcIClientQueryHandler> mHandler;
|
||||
};
|
||||
|
||||
PRUint32 ipcClientQuery::gLastQueryID = 0;
|
||||
|
||||
void
|
||||
ipcClientQuery::OnQueryComplete(nsresult status, const ipcmMessageClientInfo *msg)
|
||||
{
|
||||
NS_ASSERTION(mHandler, "no handler");
|
||||
|
||||
PRUint32 nameCount = 0;
|
||||
PRUint32 targetCount = 0;
|
||||
const char **names = NULL;
|
||||
const nsID **targets = NULL;
|
||||
|
||||
if (NS_SUCCEEDED(status)) {
|
||||
nameCount = msg->NameCount();
|
||||
targetCount = msg->TargetCount();
|
||||
PRUint32 i;
|
||||
|
||||
names = (const char **) malloc(nameCount * sizeof(char *));
|
||||
const char *lastName = NULL;
|
||||
for (i = 0; i < nameCount; ++i) {
|
||||
lastName = msg->NextName(lastName);
|
||||
names[i] = lastName;
|
||||
}
|
||||
|
||||
targets = (const nsID **) malloc(targetCount * sizeof(nsID *));
|
||||
const nsID *lastTarget = NULL;
|
||||
for (i = 0; i < targetCount; ++i) {
|
||||
lastTarget = msg->NextTarget(lastTarget);
|
||||
targets[i] = lastTarget;
|
||||
}
|
||||
}
|
||||
|
||||
mHandler->OnQueryComplete(mQueryID,
|
||||
status,
|
||||
mClientID,
|
||||
names, nameCount,
|
||||
targets, targetCount);
|
||||
mHandler = NULL;
|
||||
|
||||
if (names)
|
||||
free(names);
|
||||
if (targets)
|
||||
free(targets);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// ipcService
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
ipcService::ipcService()
|
||||
: mTransport(nsnull)
|
||||
, mClientID(0)
|
||||
{
|
||||
NS_INIT_ISUPPORTS();
|
||||
|
||||
IPC_InitLog(">>>");
|
||||
}
|
||||
|
||||
ipcService::~ipcService()
|
||||
{
|
||||
NS_ASSERTION(mTransport == nsnull, "no xpcom-shutdown event??");
|
||||
}
|
||||
|
||||
nsresult
|
||||
ipcService::Init()
|
||||
{
|
||||
nsresult rv;
|
||||
|
||||
nsCOMPtr<nsIObserverService> observ(do_GetService("@mozilla.org/observer-service;1"));
|
||||
if (observ) {
|
||||
observ->AddObserver(this, "xpcom-shutdown", PR_FALSE);
|
||||
observ->AddObserver(this, "profile-change-net-teardown", PR_FALSE);
|
||||
observ->AddObserver(this, "profile-change-net-restore", PR_FALSE);
|
||||
}
|
||||
|
||||
mTransport = new ipcTransport();
|
||||
if (!mTransport)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
NS_ADDREF(mTransport);
|
||||
|
||||
rv = mTransport->Init(this);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
void
|
||||
ipcService::OnIPCMClientID(const ipcmMessageClientID *msg)
|
||||
{
|
||||
LOG(("ipcService::OnIPCMClientID\n"));
|
||||
|
||||
ipcClientQuery *query = mQueryQ.First();
|
||||
if (!query) {
|
||||
NS_WARNING("no pending query; ignoring message.");
|
||||
return;
|
||||
}
|
||||
|
||||
PRUint32 cID = msg->ClientID();
|
||||
PRBool sync = msg->TestFlag(IPC_MSG_FLAG_SYNC_REPLY);
|
||||
|
||||
//
|
||||
// (1) store client ID in query
|
||||
// (2) move query to end of queue
|
||||
// (3) issue CLIENT_INFO request
|
||||
//
|
||||
query->SetClientID(cID);
|
||||
|
||||
mQueryQ.RemoveFirst();
|
||||
mQueryQ.Append(query);
|
||||
|
||||
mTransport->SendMsg(new ipcmMessageQueryClientInfo(cID), sync);
|
||||
}
|
||||
|
||||
void
|
||||
ipcService::OnIPCMClientInfo(const ipcmMessageClientInfo *msg)
|
||||
{
|
||||
LOG(("ipcService::OnIPCMClientInfo\n"));
|
||||
|
||||
ipcClientQuery *query = mQueryQ.First();
|
||||
if (!query) {
|
||||
NS_WARNING("no pending query; ignoring message.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!query->IsCanceled())
|
||||
query->OnQueryComplete(NS_OK, msg);
|
||||
|
||||
mQueryQ.DeleteFirst();
|
||||
}
|
||||
|
||||
void
|
||||
ipcService::OnIPCMError(const ipcmMessageError *msg)
|
||||
{
|
||||
LOG(("ipcService::OnIPCMError [reason=0x%08x]\n", msg->Reason()));
|
||||
|
||||
ipcClientQuery *query = mQueryQ.First();
|
||||
if (!query) {
|
||||
NS_WARNING("no pending query; ignoring message.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!query->IsCanceled())
|
||||
query->OnQueryComplete(NS_ERROR_FAILURE, NULL);
|
||||
|
||||
mQueryQ.DeleteFirst();
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
ipcService::
|
||||
ProcessDelayedMsgQ_Event::ProcessDelayedMsgQ_Event(ipcService *serv,
|
||||
ipcMessageQ *msgQ)
|
||||
{
|
||||
NS_ADDREF(mServ = serv);
|
||||
mMsgQ = msgQ;
|
||||
}
|
||||
|
||||
ipcService::
|
||||
ProcessDelayedMsgQ_Event::~ProcessDelayedMsgQ_Event()
|
||||
{
|
||||
NS_RELEASE(mServ);
|
||||
}
|
||||
|
||||
void * PR_CALLBACK
|
||||
ipcService::ProcessDelayedMsgQ_EventHandler(PLEvent *plevent)
|
||||
{
|
||||
LOG(("ipcService::ProcessDelayedMsgQ_EventHandler\n"));
|
||||
|
||||
ProcessDelayedMsgQ_Event *ev = (ProcessDelayedMsgQ_Event *) plevent;
|
||||
|
||||
while (!ev->mMsgQ->IsEmpty()) {
|
||||
ipcMessage *msg = ev->mMsgQ->First();
|
||||
ev->mMsgQ->RemoveFirst();
|
||||
ev->mServ->OnMessageAvailable(msg);
|
||||
delete msg;
|
||||
}
|
||||
return nsnull;
|
||||
}
|
||||
|
||||
void PR_CALLBACK
|
||||
ipcService::ProcessDelayedMsgQ_EventCleanup(PLEvent *plevent)
|
||||
{
|
||||
delete (ProcessDelayedMsgQ_Event *) plevent;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// interface impl
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
NS_IMPL_ISUPPORTS2(ipcService, ipcIService, nsIObserver)
|
||||
|
||||
NS_IMETHODIMP
|
||||
ipcService::GetClientID(PRUint32 *clientID)
|
||||
{
|
||||
if (mClientID == 0)
|
||||
return NS_ERROR_NOT_AVAILABLE;
|
||||
|
||||
*clientID = mClientID;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
ipcService::AddClientName(const char *name)
|
||||
{
|
||||
NS_ENSURE_TRUE(mTransport, NS_ERROR_NOT_INITIALIZED);
|
||||
|
||||
ipcMessage *msg = new ipcmMessageClientAddName(name);
|
||||
if (!msg)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
|
||||
return mTransport->SendMsg(msg);
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
ipcService::RemoveClientName(const char *name)
|
||||
{
|
||||
NS_ENSURE_TRUE(mTransport, NS_ERROR_NOT_INITIALIZED);
|
||||
|
||||
ipcMessage *msg = new ipcmMessageClientDelName(name);
|
||||
if (!msg)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
|
||||
return mTransport->SendMsg(msg);
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
ipcService::QueryClientByName(const char *name,
|
||||
ipcIClientQueryHandler *handler,
|
||||
PRBool sync,
|
||||
PRUint32 *queryID)
|
||||
{
|
||||
if (!mTransport)
|
||||
return NS_ERROR_NOT_AVAILABLE;
|
||||
|
||||
ipcMessage *msg;
|
||||
|
||||
msg = new ipcmMessageQueryClientByName(name);
|
||||
if (!msg)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
|
||||
nsresult rv;
|
||||
|
||||
rv = mTransport->SendMsg(msg, sync);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
ipcClientQuery *query = new ipcClientQuery(0, handler);
|
||||
if (queryID)
|
||||
*queryID = query->QueryID();
|
||||
mQueryQ.Append(query);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
ipcService::QueryClientByID(PRUint32 clientID,
|
||||
ipcIClientQueryHandler *handler,
|
||||
PRBool sync,
|
||||
PRUint32 *queryID)
|
||||
{
|
||||
if (!mTransport)
|
||||
return NS_ERROR_NOT_AVAILABLE;
|
||||
|
||||
ipcMessage *msg;
|
||||
|
||||
msg = new ipcmMessageQueryClientInfo(clientID);
|
||||
if (!msg)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
|
||||
nsresult rv;
|
||||
|
||||
rv = mTransport->SendMsg(msg, sync);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
ipcClientQuery *query = new ipcClientQuery(clientID, handler);
|
||||
if (queryID)
|
||||
*queryID = query->QueryID();
|
||||
mQueryQ.Append(query);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
ipcService::CancelQuery(PRUint32 queryID)
|
||||
{
|
||||
ipcClientQuery *query = mQueryQ.First();
|
||||
while (query) {
|
||||
if (query->QueryID() == queryID) {
|
||||
query->OnQueryComplete(NS_ERROR_ABORT, NULL);
|
||||
break;
|
||||
}
|
||||
query = query->mNext;
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
ipcService::SetClientObserver(ipcIClientObserver *observer)
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
ipcService::SetMessageObserver(const nsID &target, ipcIMessageObserver *observer)
|
||||
{
|
||||
NS_ENSURE_TRUE(mTransport, NS_ERROR_NOT_INITIALIZED);
|
||||
|
||||
nsIDKey key(target);
|
||||
PRBool sendAdd = PR_TRUE;
|
||||
|
||||
ipcIMessageObserver *cobs = (ipcIMessageObserver *) mObserverDB.Get(&key);
|
||||
if (cobs) {
|
||||
NS_RELEASE(cobs);
|
||||
if (!observer) {
|
||||
mObserverDB.Remove(&key);
|
||||
//
|
||||
// send CLIENT_DEL_TARGET
|
||||
//
|
||||
mTransport->SendMsg(new ipcmMessageClientDelTarget(target));
|
||||
return NS_OK;
|
||||
}
|
||||
sendAdd = PR_FALSE;
|
||||
}
|
||||
if (observer) {
|
||||
NS_ADDREF(observer);
|
||||
mObserverDB.Put(&key, observer);
|
||||
if (sendAdd) {
|
||||
//
|
||||
// send CLIENT_ADD_TARGET
|
||||
//
|
||||
mTransport->SendMsg(new ipcmMessageClientAddTarget(target));
|
||||
}
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
ipcService::SendMessage(PRUint32 clientID,
|
||||
const nsID &target,
|
||||
const PRUint8 *data,
|
||||
PRUint32 dataLen,
|
||||
PRBool sync)
|
||||
{
|
||||
NS_ENSURE_TRUE(mTransport, NS_ERROR_NOT_INITIALIZED);
|
||||
|
||||
if (target.Equals(IPCM_TARGET)) {
|
||||
NS_ERROR("do not try to talk to the IPCM target directly");
|
||||
return NS_ERROR_INVALID_ARG;
|
||||
}
|
||||
|
||||
ipcMessage *msg;
|
||||
if (clientID)
|
||||
msg = new ipcmMessageForward(clientID, target, (const char *) data, dataLen);
|
||||
else
|
||||
msg = new ipcMessage(target, (const char *) data, dataLen);
|
||||
|
||||
if (!msg)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
|
||||
return mTransport->SendMsg(msg, sync);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// nsIObserver impl
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
NS_IMETHODIMP
|
||||
ipcService::Observe(nsISupports *subject, const char *topic, const PRUnichar *data)
|
||||
{
|
||||
if (strcmp(topic, "xpcom-shutdown") == 0 ||
|
||||
strcmp(topic, "profile-change-net-teardown") == 0) {
|
||||
// disconnect any message observers
|
||||
mObserverDB.Reset(ipcReleaseMessageObserver, nsnull);
|
||||
|
||||
// drop daemon connection
|
||||
if (mTransport) {
|
||||
mTransport->Shutdown();
|
||||
NS_RELEASE(mTransport);
|
||||
}
|
||||
}
|
||||
else if (strcmp(topic, "profile-change-net-restore") == 0) {
|
||||
if (mTransport)
|
||||
mTransport->Init(this);
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// ipcTransportObserver impl
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
void
|
||||
ipcService::OnConnectionEstablished(PRUint32 clientID)
|
||||
{
|
||||
LOG(("ipcService::OnConnectionEstablished [cid=%u]\n", clientID));
|
||||
|
||||
mClientID = clientID;
|
||||
|
||||
//
|
||||
// enumerate ipc startup category...
|
||||
//
|
||||
NS_CreateServicesFromCategory(IPC_SERVICE_STARTUP_CATEGORY,
|
||||
NS_STATIC_CAST(ipcIService *, this),
|
||||
IPC_SERVICE_STARTUP_TOPIC);
|
||||
}
|
||||
|
||||
void
|
||||
ipcService::OnConnectionLost()
|
||||
{
|
||||
mClientID = 0;
|
||||
|
||||
//
|
||||
// error out any pending queries
|
||||
//
|
||||
while (mQueryQ.First()) {
|
||||
ipcClientQuery *query = mQueryQ.First();
|
||||
query->OnQueryComplete(NS_ERROR_ABORT, NULL);
|
||||
mQueryQ.DeleteFirst();
|
||||
}
|
||||
|
||||
//
|
||||
// broadcast ipc shutdown...
|
||||
//
|
||||
nsCOMPtr<nsIObserverService> observ(
|
||||
do_GetService("@mozilla.org/observer-service;1"));
|
||||
if (observ)
|
||||
observ->NotifyObservers(NS_STATIC_CAST(ipcIService *, this),
|
||||
IPC_SERVICE_SHUTDOWN_TOPIC, nsnull);
|
||||
}
|
||||
|
||||
void
|
||||
ipcService::OnMessageAvailable(const ipcMessage *msg)
|
||||
{
|
||||
LOG(("ipcService::OnMessageAvailable [msg=%p]\n", msg));
|
||||
|
||||
if (msg->Target().Equals(IPCM_TARGET)) {
|
||||
//
|
||||
// all IPCM messages stop here.
|
||||
//
|
||||
PRUint32 type = IPCM_GetMsgType(msg);
|
||||
switch (type) {
|
||||
case IPCM_MSG_TYPE_CLIENT_ID:
|
||||
OnIPCMClientID((const ipcmMessageClientID *) msg);
|
||||
break;
|
||||
case IPCM_MSG_TYPE_CLIENT_INFO:
|
||||
OnIPCMClientInfo((const ipcmMessageClientInfo *) msg);
|
||||
break;
|
||||
case IPCM_MSG_TYPE_ERROR:
|
||||
OnIPCMError((const ipcmMessageError *) msg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
nsIDKey key(msg->Target());
|
||||
ipcIMessageObserver *observer = (ipcIMessageObserver *) mObserverDB.Get(&key);
|
||||
if (observer)
|
||||
observer->OnMessageAvailable(msg->Target(),
|
||||
(const PRUint8 *) msg->Data(),
|
||||
msg->DataLen());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,99 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#ifndef ipcService_h__
|
||||
#define ipcService_h__
|
||||
|
||||
#include "nsCOMPtr.h"
|
||||
#include "nsHashtable.h"
|
||||
#include "plevent.h"
|
||||
|
||||
#include "ipcIService.h"
|
||||
#include "ipcTransport.h"
|
||||
#include "ipcList.h"
|
||||
#include "ipcMessage.h"
|
||||
#include "ipcMessageQ.h"
|
||||
#include "ipcm.h"
|
||||
|
||||
typedef ipcList<class ipcClientQuery> ipcClientQueryQ;
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// ipcService
|
||||
//----------------------------------------------------------------------------
|
||||
|
||||
class ipcService : public ipcIService
|
||||
, public ipcTransportObserver
|
||||
, public nsIObserver
|
||||
{
|
||||
public:
|
||||
NS_DECL_ISUPPORTS
|
||||
NS_DECL_IPCISERVICE
|
||||
NS_DECL_NSIOBSERVER
|
||||
|
||||
ipcService();
|
||||
virtual ~ipcService();
|
||||
|
||||
nsresult Init();
|
||||
|
||||
// ipcTransportObserver:
|
||||
void OnConnectionEstablished(PRUint32 clientID);
|
||||
void OnConnectionLost();
|
||||
void OnMessageAvailable(const ipcMessage *);
|
||||
|
||||
private:
|
||||
nsresult ErrorAccordingToIPCM(PRUint32 err);
|
||||
void OnIPCMClientID(const ipcmMessageClientID *);
|
||||
void OnIPCMClientInfo(const ipcmMessageClientInfo *);
|
||||
void OnIPCMError(const ipcmMessageError *);
|
||||
|
||||
struct ProcessDelayedMsgQ_Event : PLEvent {
|
||||
ProcessDelayedMsgQ_Event(ipcService *, ipcMessageQ *);
|
||||
~ProcessDelayedMsgQ_Event();
|
||||
ipcService *mServ;
|
||||
ipcMessageQ *mMsgQ;
|
||||
};
|
||||
PR_STATIC_CALLBACK(void*) ProcessDelayedMsgQ_EventHandler(PLEvent *);
|
||||
PR_STATIC_CALLBACK(void) ProcessDelayedMsgQ_EventCleanup(PLEvent *);
|
||||
|
||||
nsHashtable mObserverDB;
|
||||
ipcTransport *mTransport;
|
||||
PRUint32 mClientID;
|
||||
|
||||
ipcClientQueryQ mQueryQ;
|
||||
};
|
||||
|
||||
#endif // !ipcService_h__
|
|
@ -0,0 +1,381 @@
|
|||
/* vim:set ts=4 sw=4 et cindent: */
|
||||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include "nsIServiceManager.h"
|
||||
#include "nsIObserverService.h"
|
||||
#include "nsIInputStream.h"
|
||||
#include "nsIOutputStream.h"
|
||||
#include "nsIFile.h"
|
||||
#include "nsIProcess.h"
|
||||
#include "nsDirectoryServiceDefs.h"
|
||||
#include "nsDirectoryServiceUtils.h"
|
||||
#include "nsEventQueueUtils.h"
|
||||
#include "nsAutoLock.h"
|
||||
#include "prproces.h"
|
||||
#include "prerror.h"
|
||||
#include "plstr.h"
|
||||
|
||||
#include "ipcConfig.h"
|
||||
#include "ipcLog.h"
|
||||
#include "ipcConnection.h"
|
||||
#include "ipcMessageUtils.h"
|
||||
#include "ipcTransport.h"
|
||||
#include "ipcm.h"
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
static ipcTransport *gTransport = nsnull;
|
||||
|
||||
void
|
||||
IPC_OnConnectionEnd(nsresult error)
|
||||
{
|
||||
LOG(("IPC_OnConnectionEnd [error=%x]\n", error));
|
||||
|
||||
// if we hit a connection error during a sync message, then we need to
|
||||
// unblock the waiting thread.
|
||||
{
|
||||
nsAutoMonitor mon(gTransport->mMonitor);
|
||||
gTransport->mHaveConnection = PR_FALSE;
|
||||
mon.Notify();
|
||||
}
|
||||
|
||||
gTransport->ProxyToMainThread(ipcTransport::ConnectionLost_EventHandler);
|
||||
NS_RELEASE(gTransport);
|
||||
}
|
||||
|
||||
void
|
||||
IPC_OnMessageAvailable(ipcMessage *msg)
|
||||
{
|
||||
gTransport->OnMessageAvailable(msg);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
nsresult
|
||||
IPC_SpawnDaemon(const char *path)
|
||||
{
|
||||
PRFileDesc *readable = NULL, *writable = NULL;
|
||||
PRProcessAttr *attr = NULL;
|
||||
nsresult rv = NS_ERROR_FAILURE;
|
||||
char *const argv[] = { (char *const) path, NULL };
|
||||
char c;
|
||||
|
||||
// setup an anonymous pipe that we can use to determine when the daemon
|
||||
// process has started up. the daemon will write a char to the pipe, and
|
||||
// when we read it, we'll know to proceed with trying to connect to the
|
||||
// daemon's socket port.
|
||||
|
||||
if (PR_CreatePipe(&readable, &writable) != PR_SUCCESS)
|
||||
goto end;
|
||||
PR_SetFDInheritable(writable, PR_TRUE);
|
||||
|
||||
attr = PR_NewProcessAttr();
|
||||
if (!attr)
|
||||
goto end;
|
||||
|
||||
if (PR_ProcessAttrSetInheritableFD(attr, writable, IPC_STARTUP_PIPE_NAME) != PR_SUCCESS)
|
||||
goto end;
|
||||
|
||||
if (PR_CreateProcessDetached(path, argv, NULL, attr) != PR_SUCCESS)
|
||||
goto end;
|
||||
|
||||
if ((PR_Read(readable, &c, 1) != 1) && (c != IPC_STARTUP_PIPE_MAGIC))
|
||||
goto end;
|
||||
|
||||
rv = NS_OK;
|
||||
end:
|
||||
if (readable)
|
||||
PR_Close(readable);
|
||||
if (writable)
|
||||
PR_Close(writable);
|
||||
if (attr)
|
||||
PR_DestroyProcessAttr(attr);
|
||||
return rv;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// ipcTransport
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
nsresult
|
||||
ipcTransport::Init(ipcTransportObserver *obs)
|
||||
{
|
||||
LOG(("ipcTransport::Init\n"));
|
||||
|
||||
nsresult rv;
|
||||
nsCOMPtr<nsIFile> file;
|
||||
nsCAutoString path;
|
||||
|
||||
rv = NS_GetSpecialDirectory(NS_XPCOM_CURRENT_PROCESS_DIR, getter_AddRefs(file));
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
rv = file->AppendNative(NS_LITERAL_CSTRING(IPC_DAEMON_APP_NAME));
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
rv = file->GetNativePath(path);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
// stash reference to self so we can handle the callbacks.
|
||||
NS_ADDREF(gTransport = this);
|
||||
rv = IPC_Connect(path.get());
|
||||
if (NS_SUCCEEDED(rv))
|
||||
rv = IPC_SendMsg(new ipcmMessageClientHello());
|
||||
// if we failed to connect or failed to send the client HELLO
|
||||
// message, then bail.
|
||||
if (NS_SUCCEEDED(rv))
|
||||
mObserver = obs;
|
||||
else
|
||||
NS_RELEASE(gTransport);
|
||||
return rv;
|
||||
}
|
||||
|
||||
nsresult
|
||||
ipcTransport::Shutdown()
|
||||
{
|
||||
LOG(("ipcTransport::Shutdown\n"));
|
||||
|
||||
mObserver = 0;
|
||||
return IPC_Disconnect();
|
||||
}
|
||||
|
||||
nsresult
|
||||
ipcTransport::SendMsg(ipcMessage *msg, PRBool sync)
|
||||
{
|
||||
NS_ENSURE_ARG_POINTER(msg);
|
||||
NS_ENSURE_TRUE(mObserver, NS_ERROR_NOT_INITIALIZED);
|
||||
|
||||
LOG(("ipcTransport::SendMsg [msg=%p dataLen=%u]\n", msg, msg->DataLen()));
|
||||
|
||||
ipcMessage *syncReply = nsnull;
|
||||
{
|
||||
nsAutoMonitor mon(mMonitor);
|
||||
nsresult rv;
|
||||
|
||||
if (sync) {
|
||||
msg->SetFlag(IPC_MSG_FLAG_SYNC_QUERY);
|
||||
// flag before sending to avoid race with background thread.
|
||||
mSyncWaiting = PR_TRUE;
|
||||
}
|
||||
|
||||
if (mHaveConnection) {
|
||||
rv = IPC_SendMsg(msg);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
}
|
||||
else {
|
||||
LOG((" delaying message until connected\n"));
|
||||
mDelayedQ.Append(msg);
|
||||
}
|
||||
|
||||
if (sync) {
|
||||
// XXX we need to break out of this loop if the connection is lost.
|
||||
while (!mSyncReplyMsg && mHaveConnection) {
|
||||
LOG((" waiting for response...\n"));
|
||||
mon.Wait();
|
||||
}
|
||||
|
||||
if (!mHaveConnection) {
|
||||
LOG(("connection lost while waiting for sync response\n"));
|
||||
return NS_ERROR_UNEXPECTED;
|
||||
}
|
||||
|
||||
syncReply = mSyncReplyMsg;
|
||||
mSyncReplyMsg = nsnull;
|
||||
}
|
||||
}
|
||||
if (syncReply) {
|
||||
// NOTE: may re-enter SendMsg
|
||||
mObserver->OnMessageAvailable(syncReply);
|
||||
delete syncReply;
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
void
|
||||
ipcTransport::ProcessIncomingMsgQ()
|
||||
{
|
||||
LOG(("ipcTransport::ProcessIncomingMsgQ\n"));
|
||||
|
||||
// we can't hold mMonitor while calling into the observer, so we grab
|
||||
// mIncomingMsgQ and NULL it out inside the monitor to prevent others
|
||||
// from modifying it while we iterate over it.
|
||||
ipcMessageQ *inQ;
|
||||
{
|
||||
nsAutoMonitor mon(mMonitor);
|
||||
inQ = mIncomingMsgQ;
|
||||
mIncomingMsgQ = nsnull;
|
||||
}
|
||||
if (inQ) {
|
||||
while (!inQ->IsEmpty()) {
|
||||
ipcMessage *msg = inQ->First();
|
||||
if (mObserver)
|
||||
mObserver->OnMessageAvailable(msg);
|
||||
inQ->DeleteFirst();
|
||||
}
|
||||
delete inQ;
|
||||
}
|
||||
}
|
||||
|
||||
void *
|
||||
ipcTransport::ProcessIncomingMsgQ_EventHandler(PLEvent *ev)
|
||||
{
|
||||
ipcTransport *self = (ipcTransport *) PL_GetEventOwner(ev);
|
||||
self->ProcessIncomingMsgQ();
|
||||
return nsnull;
|
||||
}
|
||||
|
||||
void *
|
||||
ipcTransport::ConnectionEstablished_EventHandler(PLEvent *ev)
|
||||
{
|
||||
ipcTransport *self = (ipcTransport *) PL_GetEventOwner(ev);
|
||||
if (self->mObserver)
|
||||
self->mObserver->OnConnectionEstablished(self->mClientID);
|
||||
return nsnull;
|
||||
}
|
||||
|
||||
void *
|
||||
ipcTransport::ConnectionLost_EventHandler(PLEvent *ev)
|
||||
{
|
||||
ipcTransport *self = (ipcTransport *) PL_GetEventOwner(ev);
|
||||
if (self->mObserver)
|
||||
self->mObserver->OnConnectionLost();
|
||||
return nsnull;
|
||||
}
|
||||
|
||||
void
|
||||
ipcTransport::Generic_EventCleanup(PLEvent *ev)
|
||||
{
|
||||
ipcTransport *self = (ipcTransport *) PL_GetEventOwner(ev);
|
||||
NS_RELEASE(self);
|
||||
delete ev;
|
||||
}
|
||||
|
||||
// called on a background thread
|
||||
void
|
||||
ipcTransport::OnMessageAvailable(ipcMessage *rawMsg)
|
||||
{
|
||||
LOG(("ipcTransport::OnMessageAvailable [msg=%p dataLen=%u]\n",
|
||||
rawMsg, rawMsg->DataLen()));
|
||||
|
||||
//
|
||||
// XXX FIX COMMENTS XXX
|
||||
//
|
||||
// 1- append to incoming message queue
|
||||
//
|
||||
// 2- post event to main thread to handle incoming message queue
|
||||
// or if sync waiting, unblock waiter so it can scan incoming
|
||||
// message queue.
|
||||
//
|
||||
|
||||
PRBool dispatchEvent = PR_FALSE;
|
||||
PRBool connectEvent = PR_FALSE;
|
||||
{
|
||||
nsAutoMonitor mon(mMonitor);
|
||||
|
||||
if (!mHaveConnection) {
|
||||
if (rawMsg->Target().Equals(IPCM_TARGET) &&
|
||||
IPCM_GetMsgType(rawMsg) == IPCM_MSG_TYPE_CLIENT_ID) {
|
||||
LOG((" connection established!\n"));
|
||||
mHaveConnection = PR_TRUE;
|
||||
|
||||
// remember our client ID
|
||||
ipcMessageCast<ipcmMessageClientID> msg(rawMsg);
|
||||
mClientID = msg->ClientID();
|
||||
connectEvent = PR_TRUE;
|
||||
|
||||
// move messages off the delayed message queue
|
||||
while (!mDelayedQ.IsEmpty()) {
|
||||
ipcMessage *msg = mDelayedQ.First();
|
||||
mDelayedQ.RemoveFirst();
|
||||
IPC_SendMsg(msg);
|
||||
}
|
||||
rawMsg = nsnull;
|
||||
}
|
||||
else
|
||||
LOG((" received unexpected first message!\n"));
|
||||
}
|
||||
|
||||
if (rawMsg) {
|
||||
LOG((" mSyncWaiting=%u MSG_FLAG_SYNC_REPLY=%u\n",
|
||||
mSyncWaiting, rawMsg->TestFlag(IPC_MSG_FLAG_SYNC_REPLY) != 0));
|
||||
|
||||
if (mSyncWaiting && rawMsg->TestFlag(IPC_MSG_FLAG_SYNC_REPLY)) {
|
||||
mSyncReplyMsg = rawMsg;
|
||||
mSyncWaiting = PR_FALSE;
|
||||
mon.Notify();
|
||||
}
|
||||
else {
|
||||
if (!mIncomingMsgQ) {
|
||||
mIncomingMsgQ = new ipcMessageQ();
|
||||
if (!mIncomingMsgQ)
|
||||
return;
|
||||
dispatchEvent = PR_TRUE;
|
||||
}
|
||||
mIncomingMsgQ->Append(rawMsg);
|
||||
}
|
||||
}
|
||||
|
||||
LOG((" connectEvent=%u dispatchEvent=%u mSyncReplyMsg=%p mIncomingMsgQ=%p\n",
|
||||
connectEvent, dispatchEvent, mSyncReplyMsg, mIncomingMsgQ));
|
||||
}
|
||||
|
||||
if (connectEvent)
|
||||
ProxyToMainThread(ConnectionEstablished_EventHandler);
|
||||
if (dispatchEvent)
|
||||
ProxyToMainThread(ProcessIncomingMsgQ_EventHandler);
|
||||
}
|
||||
|
||||
void
|
||||
ipcTransport::ProxyToMainThread(PLHandleEventProc proc)
|
||||
{
|
||||
LOG(("ipcTransport::ProxyToMainThread\n"));
|
||||
|
||||
nsCOMPtr<nsIEventQueue> eq;
|
||||
NS_GetMainEventQ(getter_AddRefs(eq));
|
||||
if (eq) {
|
||||
PLEvent *ev = new PLEvent();
|
||||
PL_InitEvent(ev, this, proc, Generic_EventCleanup);
|
||||
NS_ADDREF_THIS();
|
||||
if (NS_FAILED(eq->PostEvent(ev))) {
|
||||
LOG((" PostEvent failed"));
|
||||
NS_RELEASE_THIS();
|
||||
delete ev;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
NS_IMPL_THREADSAFE_ISUPPORTS0(ipcTransport)
|
|
@ -0,0 +1,132 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#ifndef ipcTransport_h__
|
||||
#define ipcTransport_h__
|
||||
|
||||
#include "nsIObserver.h"
|
||||
#include "nsITimer.h"
|
||||
#include "nsString.h"
|
||||
#include "nsCOMPtr.h"
|
||||
#include "prmon.h"
|
||||
|
||||
#include "ipcMessage.h"
|
||||
#include "ipcMessageQ.h"
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// ipcTransportObserver interface
|
||||
//----------------------------------------------------------------------------
|
||||
|
||||
class ipcTransportObserver
|
||||
{
|
||||
public:
|
||||
virtual void OnConnectionEstablished(PRUint32 clientID) = 0;
|
||||
virtual void OnConnectionLost() = 0;
|
||||
virtual void OnMessageAvailable(const ipcMessage *) = 0;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// ipcTransport
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
class ipcTransport : public nsISupports
|
||||
{
|
||||
public:
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
ipcTransport()
|
||||
: mMonitor(PR_NewMonitor())
|
||||
, mObserver(nsnull)
|
||||
, mIncomingMsgQ(nsnull)
|
||||
, mSyncReplyMsg(nsnull)
|
||||
, mSyncWaiting(nsnull)
|
||||
, mSentHello(PR_FALSE)
|
||||
, mHaveConnection(PR_FALSE)
|
||||
, mSpawnedDaemon(PR_FALSE)
|
||||
, mConnectionAttemptCount(0)
|
||||
, mClientID(0)
|
||||
{}
|
||||
|
||||
virtual ~ipcTransport()
|
||||
{
|
||||
PR_DestroyMonitor(mMonitor);
|
||||
}
|
||||
|
||||
nsresult Init(ipcTransportObserver *observer);
|
||||
nsresult Shutdown();
|
||||
|
||||
// takes ownership of |msg|
|
||||
nsresult SendMsg(ipcMessage *msg, PRBool sync = PR_FALSE);
|
||||
|
||||
public:
|
||||
//
|
||||
// internal to implementation
|
||||
//
|
||||
void OnMessageAvailable(ipcMessage *); // takes ownership
|
||||
|
||||
private:
|
||||
friend void IPC_OnMessageAvailable(ipcMessage *);
|
||||
friend void IPC_OnConnectionEnd(nsresult);
|
||||
|
||||
//
|
||||
// helpers
|
||||
//
|
||||
void ProxyToMainThread(PLHandleEventProc);
|
||||
void ProcessIncomingMsgQ();
|
||||
|
||||
PR_STATIC_CALLBACK(void *) ProcessIncomingMsgQ_EventHandler(PLEvent *);
|
||||
PR_STATIC_CALLBACK(void *) ConnectionEstablished_EventHandler(PLEvent *);
|
||||
PR_STATIC_CALLBACK(void *) ConnectionLost_EventHandler(PLEvent *);
|
||||
PR_STATIC_CALLBACK(void) Generic_EventCleanup(PLEvent *);
|
||||
|
||||
//
|
||||
// data
|
||||
//
|
||||
PRMonitor *mMonitor;
|
||||
ipcTransportObserver *mObserver; // weak reference
|
||||
ipcMessageQ mDelayedQ;
|
||||
ipcMessageQ *mIncomingMsgQ;
|
||||
ipcMessage *mSyncReplyMsg;
|
||||
PRPackedBool mSyncWaiting;
|
||||
PRPackedBool mSentHello;
|
||||
PRPackedBool mHaveConnection;
|
||||
PRPackedBool mSpawnedDaemon;
|
||||
PRUint32 mConnectionAttemptCount;
|
||||
PRUint32 mClientID;
|
||||
};
|
||||
|
||||
#endif // !ipcTransport_h__
|
|
@ -0,0 +1 @@
|
|||
Makefile
|
|
@ -0,0 +1,52 @@
|
|||
# vim: noexpandtab ts=4 sw=4
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
#
|
||||
# The contents of this file are subject to the Mozilla Public License Version
|
||||
# 1.1 (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
# http://www.mozilla.org/MPL/
|
||||
#
|
||||
# Software distributed under the License is distributed on an "AS IS" basis,
|
||||
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
# for the specific language governing rights and limitations under the
|
||||
# License.
|
||||
#
|
||||
# The Original Code is Mozilla IPC.
|
||||
#
|
||||
# The Initial Developer of the Original Code is
|
||||
# Netscape Communications Corporation.
|
||||
# Portions created by the Initial Developer are Copyright (C) 2002
|
||||
# the Initial Developer. All Rights Reserved.
|
||||
#
|
||||
# Contributor(s):
|
||||
# Darin Fisher <darin@netscape.com>
|
||||
#
|
||||
# Alternatively, the contents of this file may be used under the terms of
|
||||
# either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
# in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
# of those above. If you wish to allow use of your version of this file only
|
||||
# under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
# use your version of this file under the terms of the MPL, indicate your
|
||||
# decision by deleting the provisions above and replace them with the notice
|
||||
# and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
# the provisions above, a recipient may use your version of this file under
|
||||
# the terms of any one of the MPL, the GPL or the LGPL.
|
||||
#
|
||||
# ***** END LICENSE BLOCK *****
|
||||
|
||||
DEPTH = ../../../..
|
||||
topsrcdir = @top_srcdir@
|
||||
srcdir = @srcdir@
|
||||
VPATH = @srcdir@
|
||||
|
||||
include $(DEPTH)/config/autoconf.mk
|
||||
|
||||
MODULE = ipcd
|
||||
EXPORTS = \
|
||||
ipcModule.h \
|
||||
ipcModuleUtil.h \
|
||||
$(NULL)
|
||||
|
||||
include $(topsrcdir)/config/rules.mk
|
|
@ -0,0 +1,240 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#ifndef ipcModule_h__
|
||||
#define ipcModule_h__
|
||||
|
||||
#include "nsID.h"
|
||||
|
||||
//
|
||||
// a client handle is used to efficiently reference a client instance object
|
||||
// used by the daemon to represent a connection with a particular client app.
|
||||
//
|
||||
// modules should treat it as an opaque type.
|
||||
//
|
||||
typedef class ipcClient *ipcClientHandle;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// interface implemented by the module:
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
//
|
||||
// the version of ipcModuleMethods data structure.
|
||||
//
|
||||
#define IPC_MODULE_METHODS_VERSION (1<<16) // 1.0
|
||||
|
||||
//
|
||||
// each module defines the following structure:
|
||||
//
|
||||
struct ipcModuleMethods
|
||||
{
|
||||
//
|
||||
// this field holds the version of the data structure, which is always the
|
||||
// value of IPC_MODULE_METHODS_VERSION against which the module was built.
|
||||
//
|
||||
PRUint32 version;
|
||||
|
||||
//
|
||||
// called after this module is registered.
|
||||
//
|
||||
void (* init) (void);
|
||||
|
||||
//
|
||||
// called when this module will no longer be accessed.
|
||||
//
|
||||
void (* shutdown) (void);
|
||||
|
||||
//
|
||||
// called when a new message arrives for this module.
|
||||
//
|
||||
// params:
|
||||
// client - an opaque "handle" to an object representing the client that
|
||||
// sent the message. modules should not store the value of this
|
||||
// beyond the duration fo this function call. (e.g., the handle
|
||||
// may be invalid after this function call returns.) modules
|
||||
// wishing to hold onto a reference to a "client" should store
|
||||
// the client's ID (see IPC_GetClientID).
|
||||
// target - message target
|
||||
// data - message data
|
||||
// dataLen - message data length
|
||||
//
|
||||
void (* handleMsg) (ipcClientHandle client,
|
||||
const nsID &target,
|
||||
const void *data,
|
||||
PRUint32 dataLen);
|
||||
|
||||
//
|
||||
// called when a new client connects to the IPC daemon.
|
||||
//
|
||||
void (* clientUp) (ipcClientHandle client);
|
||||
|
||||
//
|
||||
// called when a client disconnects from the IPC daemon.
|
||||
//
|
||||
void (* clientDown) (ipcClientHandle client);
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// interface implemented by the daemon:
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
//
|
||||
// the version of ipcDaemonMethods data structure.
|
||||
//
|
||||
#define IPC_DAEMON_METHODS_VERSION (1<<16) // 1.0
|
||||
|
||||
//
|
||||
// enumeration functions may return FALSE to stop enumeration.
|
||||
//
|
||||
typedef PRBool (* ipcClientEnumFunc) (void *closure, ipcClientHandle client, PRUint32 clientID);
|
||||
typedef PRBool (* ipcClientNameEnumFunc) (void *closure, ipcClientHandle client, const char *name);
|
||||
typedef PRBool (* ipcClientTargetEnumFunc) (void *closure, ipcClientHandle client, const nsID &target);
|
||||
|
||||
//
|
||||
// the daemon provides the following structure:
|
||||
//
|
||||
struct ipcDaemonMethods
|
||||
{
|
||||
PRUint32 version;
|
||||
|
||||
//
|
||||
// called to send a message to another module.
|
||||
//
|
||||
// params:
|
||||
// client - identifies the client from which this message originated.
|
||||
// target - message target
|
||||
// data - message data
|
||||
// dataLen - message data length
|
||||
//
|
||||
// returns:
|
||||
// PR_SUCCESS if message was dispatched.
|
||||
// PR_FAILURE if message could not be dispatched (possibly because
|
||||
// no module is registered for the given message target).
|
||||
//
|
||||
PRStatus (* dispatchMsg) (ipcClientHandle client,
|
||||
const nsID &target,
|
||||
const void *data,
|
||||
PRUint32 dataLen);
|
||||
|
||||
//
|
||||
// called to send a message to a particular client or to broadcast a
|
||||
// message to all clients.
|
||||
//
|
||||
// params:
|
||||
// client - if null, then broadcast message to all clients. otherwise,
|
||||
// send message to the client specified.
|
||||
// target - message target
|
||||
// data - message data
|
||||
// dataLen - message data length
|
||||
//
|
||||
// returns:
|
||||
// PR_SUCCESS if message was sent (or queued up to be sent later).
|
||||
// PR_FAILURE if message could not be sent (possibly because the client
|
||||
// does not have a registered observer for the msg's target).
|
||||
//
|
||||
PRStatus (* sendMsg) (ipcClientHandle client,
|
||||
const nsID &target,
|
||||
const void *data,
|
||||
PRUint32 dataLen);
|
||||
|
||||
//
|
||||
// called to lookup a client handle given its client ID. each client has
|
||||
// a unique ID.
|
||||
//
|
||||
ipcClientHandle (* getClientByID) (PRUint32 clientID);
|
||||
|
||||
//
|
||||
// called to lookup a client by name or alias. names are not necessary
|
||||
// unique to individual clients. this function returns the client first
|
||||
// registered under the given name.
|
||||
//
|
||||
ipcClientHandle (* getClientByName) (const char *name);
|
||||
|
||||
//
|
||||
// called to enumerate all clients.
|
||||
//
|
||||
void (* enumClients) (ipcClientEnumFunc func, void *closure);
|
||||
|
||||
//
|
||||
// returns the client ID of the specified client.
|
||||
//
|
||||
PRUint32 (* getClientID) (ipcClientHandle client);
|
||||
|
||||
//
|
||||
// functions for inspecting the names and targets defined for a particular
|
||||
// client instance.
|
||||
//
|
||||
PRBool (* clientHasName) (ipcClientHandle client, const char *name);
|
||||
PRBool (* clientHasTarget) (ipcClientHandle client, const nsID &target);
|
||||
void (* enumClientNames) (ipcClientHandle client, ipcClientNameEnumFunc func, void *closure);
|
||||
void (* enumClientTargets) (ipcClientHandle client, ipcClientTargetEnumFunc func, void *closure);
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// interface exported by a DSO implementing one or more modules:
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
struct ipcModuleEntry
|
||||
{
|
||||
//
|
||||
// identifies the message target of this module.
|
||||
//
|
||||
nsID target;
|
||||
|
||||
//
|
||||
// module methods
|
||||
//
|
||||
ipcModuleMethods *methods;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#define IPC_EXPORT extern "C" NS_EXPORT
|
||||
|
||||
//
|
||||
// IPC_EXPORT int IPC_GetModules(const ipcDaemonMethods *, const ipcModuleEntry **);
|
||||
//
|
||||
// params:
|
||||
// methods - the daemon's methods
|
||||
// entries - the module entries defined by the DSO
|
||||
//
|
||||
// returns:
|
||||
// length of the |entries| array.
|
||||
//
|
||||
typedef int (* ipcGetModulesFunc) (const ipcDaemonMethods *methods, const ipcModuleEntry **entries);
|
||||
|
||||
#endif // !ipcModule_h__
|
|
@ -0,0 +1,151 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#ifndef ipcModuleUtil_h__
|
||||
#define ipcModuleUtil_h__
|
||||
|
||||
#include "prlog.h"
|
||||
#include "ipcModule.h"
|
||||
|
||||
extern const ipcDaemonMethods *gIPCDaemonMethods;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// inline wrapper functions
|
||||
//
|
||||
// these functions may only be called by a module that uses the
|
||||
// IPC_IMPL_GETMODULES macro.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
inline PRStatus
|
||||
IPC_DispatchMsg(ipcClientHandle client, const nsID &target, const void *data, PRUint32 dataLen)
|
||||
{
|
||||
PR_ASSERT(gIPCDaemonMethods);
|
||||
return gIPCDaemonMethods->dispatchMsg(client, target, data, dataLen);
|
||||
}
|
||||
|
||||
inline PRStatus
|
||||
IPC_SendMsg(ipcClientHandle client, const nsID &target, const void *data, PRUint32 dataLen)
|
||||
{
|
||||
PR_ASSERT(gIPCDaemonMethods);
|
||||
return gIPCDaemonMethods->sendMsg(client, target, data, dataLen);
|
||||
}
|
||||
|
||||
inline ipcClientHandle
|
||||
IPC_GetClientByID(PRUint32 id)
|
||||
{
|
||||
PR_ASSERT(gIPCDaemonMethods);
|
||||
return gIPCDaemonMethods->getClientByID(id);
|
||||
}
|
||||
|
||||
inline ipcClientHandle
|
||||
IPC_GetClientByName(const char *name)
|
||||
{
|
||||
PR_ASSERT(gIPCDaemonMethods);
|
||||
return gIPCDaemonMethods->getClientByName(name);
|
||||
}
|
||||
|
||||
inline void
|
||||
IPC_EnumClients(ipcClientEnumFunc func, void *closure)
|
||||
{
|
||||
PR_ASSERT(gIPCDaemonMethods);
|
||||
gIPCDaemonMethods->enumClients(func, closure);
|
||||
}
|
||||
|
||||
inline PRUint32
|
||||
IPC_GetClientID(ipcClientHandle client)
|
||||
{
|
||||
PR_ASSERT(gIPCDaemonMethods);
|
||||
return gIPCDaemonMethods->getClientID(client);
|
||||
}
|
||||
|
||||
inline PRBool
|
||||
IPC_ClientHasName(ipcClientHandle client, const char *name)
|
||||
{
|
||||
PR_ASSERT(gIPCDaemonMethods);
|
||||
return gIPCDaemonMethods->clientHasName(client, name);
|
||||
}
|
||||
|
||||
inline PRBool
|
||||
IPC_ClientHasTarget(ipcClientHandle client, const nsID &target)
|
||||
{
|
||||
PR_ASSERT(gIPCDaemonMethods);
|
||||
return gIPCDaemonMethods->clientHasTarget(client, target);
|
||||
}
|
||||
|
||||
inline void
|
||||
IPC_EnumClientNames(ipcClientHandle client, ipcClientNameEnumFunc func, void *closure)
|
||||
{
|
||||
PR_ASSERT(gIPCDaemonMethods);
|
||||
gIPCDaemonMethods->enumClientNames(client, func, closure);
|
||||
}
|
||||
|
||||
inline void
|
||||
IPC_EnumClientTargets(ipcClientHandle client, ipcClientTargetEnumFunc func, void *closure)
|
||||
{
|
||||
PR_ASSERT(gIPCDaemonMethods);
|
||||
gIPCDaemonMethods->enumClientTargets(client, func, closure);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// inline composite functions
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
inline PRStatus
|
||||
IPC_SendMsg(PRUint32 clientID, const nsID &target, const void *data, PRUint32 dataLen)
|
||||
{
|
||||
ipcClient *client = IPC_GetClientByID(clientID);
|
||||
if (!client)
|
||||
return PR_FAILURE;
|
||||
return IPC_SendMsg(client, target, data, dataLen);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// module factory macros
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#define IPC_IMPL_GETMODULES(_modName, _modEntries) \
|
||||
const ipcDaemonMethods *gIPCDaemonMethods; \
|
||||
IPC_EXPORT int \
|
||||
IPC_GetModules(const ipcDaemonMethods *dmeths, \
|
||||
const ipcModuleEntry **ents) { \
|
||||
/* XXX do version checking */ \
|
||||
gIPCDaemonMethods = dmeths; \
|
||||
*ents = _modEntries; \
|
||||
return sizeof(_modEntries) / sizeof(ipcModuleEntry); \
|
||||
}
|
||||
|
||||
#endif // !ipcModuleUtil_h__
|
|
@ -0,0 +1,2 @@
|
|||
Makefile
|
||||
mozipcd
|
|
@ -0,0 +1,88 @@
|
|||
# vim: noexpandtab ts=4 sw=4
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
#
|
||||
# The contents of this file are subject to the Mozilla Public License Version
|
||||
# 1.1 (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
# http://www.mozilla.org/MPL/
|
||||
#
|
||||
# Software distributed under the License is distributed on an "AS IS" basis,
|
||||
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
# for the specific language governing rights and limitations under the
|
||||
# License.
|
||||
#
|
||||
# The Original Code is Mozilla IPC.
|
||||
#
|
||||
# The Initial Developer of the Original Code is
|
||||
# Netscape Communications Corporation.
|
||||
# Portions created by the Initial Developer are Copyright (C) 2002
|
||||
# the Initial Developer. All Rights Reserved.
|
||||
#
|
||||
# Contributor(s):
|
||||
# Darin Fisher <darin@netscape.com>
|
||||
#
|
||||
# Alternatively, the contents of this file may be used under the terms of
|
||||
# either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
# in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
# of those above. If you wish to allow use of your version of this file only
|
||||
# under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
# use your version of this file under the terms of the MPL, indicate your
|
||||
# decision by deleting the provisions above and replace them with the notice
|
||||
# and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
# the provisions above, a recipient may use your version of this file under
|
||||
# the terms of any one of the MPL, the GPL or the LGPL.
|
||||
#
|
||||
# ***** END LICENSE BLOCK *****
|
||||
|
||||
DEPTH = ../../../..
|
||||
topsrcdir = @top_srcdir@
|
||||
srcdir = @srcdir@
|
||||
VPATH = @srcdir@
|
||||
|
||||
include $(DEPTH)/config/autoconf.mk
|
||||
|
||||
MODULE = ipcd
|
||||
|
||||
# required for #include "nsID.h"
|
||||
REQUIRES = \
|
||||
xpcom \
|
||||
$(NULL)
|
||||
|
||||
CPPSRCS = \
|
||||
ipcd.cpp \
|
||||
ipcClient.cpp \
|
||||
ipcModuleReg.cpp \
|
||||
ipcCommandModule.cpp
|
||||
|
||||
ifeq ($(OS_ARCH),WINNT)
|
||||
CPPSRCS += ipcdWin.cpp
|
||||
else
|
||||
ifeq ($(OS_ARCH),BeOS)
|
||||
CPPSRCS += ipcdStub.cpp
|
||||
else
|
||||
CPPSRCS += ipcdUnix.cpp
|
||||
endif
|
||||
endif
|
||||
|
||||
PROGRAM = mozipcd$(BIN_SUFFIX)
|
||||
|
||||
LOCAL_INCLUDES = \
|
||||
-I$(srcdir)/../../shared/src \
|
||||
$(NULL)
|
||||
|
||||
include $(topsrcdir)/config/config.mk
|
||||
|
||||
LIBS = \
|
||||
$(DIST)/lib/$(LIB_PREFIX)ipcdshared_s.$(LIB_SUFFIX) \
|
||||
$(EXTRA_DSO_LIBS) \
|
||||
$(NSPR_LIBS) \
|
||||
$(NULL)
|
||||
|
||||
include $(topsrcdir)/config/rules.mk
|
||||
|
||||
# For fruncate
|
||||
ifeq ($(OS_ARCH),Linux)
|
||||
DEFINES += -D_BSD_SOURCE
|
||||
endif
|
|
@ -0,0 +1,233 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include "ipcLog.h"
|
||||
#include "ipcClient.h"
|
||||
#include "ipcMessage.h"
|
||||
#include "ipcModuleReg.h"
|
||||
#include "ipcd.h"
|
||||
#include "ipcm.h"
|
||||
|
||||
#if defined(XP_UNIX) || defined(XP_OS2)
|
||||
#include "prio.h"
|
||||
#endif
|
||||
|
||||
PRUint32 ipcClient::gLastID = 0;
|
||||
|
||||
//
|
||||
// called to initialize this client context
|
||||
//
|
||||
// assumptions:
|
||||
// - object's memory has already been zero'd out.
|
||||
//
|
||||
void
|
||||
ipcClient::Init()
|
||||
{
|
||||
mID = ++gLastID;
|
||||
|
||||
// every client must be able to handle IPCM messages.
|
||||
mTargets.Append(IPCM_TARGET);
|
||||
|
||||
// although it is tempting to fire off the NotifyClientUp event at this
|
||||
// time, we must wait until the client sends us a CLIENT_HELLO event.
|
||||
// see ipcCommandModule::OnClientHello.
|
||||
}
|
||||
|
||||
//
|
||||
// called when this client context is going away
|
||||
//
|
||||
void
|
||||
ipcClient::Finalize()
|
||||
{
|
||||
IPC_NotifyClientDown(this);
|
||||
|
||||
mNames.DeleteAll();
|
||||
mTargets.DeleteAll();
|
||||
|
||||
#if defined(XP_UNIX) || defined(XP_OS2)
|
||||
mInMsg.Reset();
|
||||
mOutMsgQ.DeleteAll();
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
ipcClient::AddName(const char *name)
|
||||
{
|
||||
LOG(("adding client name: %s\n", name));
|
||||
|
||||
if (HasName(name))
|
||||
return;
|
||||
|
||||
mNames.Append(name);
|
||||
}
|
||||
|
||||
void
|
||||
ipcClient::DelName(const char *name)
|
||||
{
|
||||
LOG(("deleting client name: %s\n", name));
|
||||
|
||||
mNames.FindAndDelete(name);
|
||||
}
|
||||
|
||||
void
|
||||
ipcClient::AddTarget(const nsID &target)
|
||||
{
|
||||
LOG(("adding client target\n"));
|
||||
|
||||
if (HasTarget(target))
|
||||
return;
|
||||
|
||||
mTargets.Append(target);
|
||||
}
|
||||
|
||||
void
|
||||
ipcClient::DelTarget(const nsID &target)
|
||||
{
|
||||
LOG(("deleting client target\n"));
|
||||
|
||||
//
|
||||
// cannot remove the IPCM target
|
||||
//
|
||||
if (!target.Equals(IPCM_TARGET))
|
||||
mTargets.FindAndDelete(target);
|
||||
}
|
||||
|
||||
#if defined(XP_UNIX) || defined(XP_OS2)
|
||||
|
||||
//
|
||||
// called to process a client socket
|
||||
//
|
||||
// params:
|
||||
// fd - the client socket
|
||||
// poll_flags - the state of the client socket
|
||||
//
|
||||
// return:
|
||||
// 0 - to end session with this client
|
||||
// PR_POLL_READ - to wait for the client socket to become readable
|
||||
// PR_POLL_WRITE - to wait for the client socket to become writable
|
||||
//
|
||||
int
|
||||
ipcClient::Process(PRFileDesc *fd, int inFlags)
|
||||
{
|
||||
if (inFlags & (PR_POLL_ERR | PR_POLL_HUP |
|
||||
PR_POLL_EXCEPT | PR_POLL_NVAL)) {
|
||||
LOG(("client socket appears to have closed\n"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
// expect to wait for more data
|
||||
int outFlags = PR_POLL_READ;
|
||||
|
||||
if (inFlags & PR_POLL_READ) {
|
||||
LOG(("client socket is now readable\n"));
|
||||
|
||||
char buf[1024]; // XXX make this larger?
|
||||
PRInt32 n;
|
||||
|
||||
// find out how much data is available for reading...
|
||||
// n = PR_Available(fd);
|
||||
|
||||
n = PR_Read(fd, buf, sizeof(buf));
|
||||
if (n <= 0)
|
||||
return 0; // cancel connection
|
||||
|
||||
const char *ptr = buf;
|
||||
while (n) {
|
||||
PRUint32 nread;
|
||||
PRBool complete;
|
||||
|
||||
if (mInMsg.ReadFrom(ptr, PRUint32(n), &nread, &complete) == PR_FAILURE) {
|
||||
LOG(("message appears to be malformed; dropping client connection\n"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (complete) {
|
||||
IPC_DispatchMsg(this, &mInMsg);
|
||||
mInMsg.Reset();
|
||||
}
|
||||
|
||||
n -= nread;
|
||||
ptr += nread;
|
||||
}
|
||||
}
|
||||
|
||||
if (inFlags & PR_POLL_WRITE) {
|
||||
LOG(("client socket is now writable\n"));
|
||||
|
||||
if (mOutMsgQ.First())
|
||||
WriteMsgs(fd);
|
||||
}
|
||||
|
||||
if (mOutMsgQ.First())
|
||||
outFlags |= PR_POLL_WRITE;
|
||||
|
||||
return outFlags;
|
||||
}
|
||||
|
||||
//
|
||||
// called to write out any messages from the outgoing queue.
|
||||
//
|
||||
int
|
||||
ipcClient::WriteMsgs(PRFileDesc *fd)
|
||||
{
|
||||
while (mOutMsgQ.First()) {
|
||||
const char *buf = (const char *) mOutMsgQ.First()->MsgBuf();
|
||||
PRInt32 bufLen = (PRInt32) mOutMsgQ.First()->MsgLen();
|
||||
|
||||
if (mSendOffset) {
|
||||
buf += mSendOffset;
|
||||
bufLen -= mSendOffset;
|
||||
}
|
||||
|
||||
PRInt32 nw = PR_Write(fd, buf, bufLen);
|
||||
if (nw <= 0)
|
||||
break;
|
||||
|
||||
LOG(("wrote %d bytes\n", nw));
|
||||
|
||||
if (nw == bufLen) {
|
||||
mOutMsgQ.DeleteFirst();
|
||||
mSendOffset = 0;
|
||||
}
|
||||
else
|
||||
mSendOffset += nw;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,144 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#ifndef ipcClientUnix_h__
|
||||
#define ipcClientUnix_h__
|
||||
|
||||
#include "prio.h"
|
||||
#include "ipcMessageQ.h"
|
||||
#include "ipcStringList.h"
|
||||
#include "ipcIDList.h"
|
||||
|
||||
#ifdef XP_WIN
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// ipcClient
|
||||
//
|
||||
// NOTE: this class is an implementation detail of the IPC daemon. IPC daemon
|
||||
// modules (other than the built-in IPCM module) must not access methods on
|
||||
// this class directly. use the API provided via ipcd.h instead.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
class ipcClient
|
||||
{
|
||||
public:
|
||||
void Init();
|
||||
void Finalize();
|
||||
|
||||
PRUint32 ID() const { return mID; }
|
||||
|
||||
void AddName(const char *name);
|
||||
void DelName(const char *name);
|
||||
PRBool HasName(const char *name) const { return mNames.Find(name) != NULL; }
|
||||
|
||||
void AddTarget(const nsID &target);
|
||||
void DelTarget(const nsID &target);
|
||||
PRBool HasTarget(const nsID &target) const { return mTargets.Find(target) != NULL; }
|
||||
|
||||
// list iterators
|
||||
const ipcStringNode *Names() const { return mNames.First(); }
|
||||
const ipcIDNode *Targets() const { return mTargets.First(); }
|
||||
|
||||
// returns primary client name (the one specified in the "client hello" message)
|
||||
const char *PrimaryName() const { return mNames.First() ? mNames.First()->Value() : NULL; }
|
||||
|
||||
void SetExpectsSyncReply(PRBool val) { mExpectsSyncReply = val; }
|
||||
PRBool GetExpectsSyncReply() const { return mExpectsSyncReply; }
|
||||
|
||||
#ifdef XP_WIN
|
||||
PRUint32 PID() const { return mPID; }
|
||||
void SetPID(PRUint32 pid) { mPID = pid; }
|
||||
|
||||
HWND Hwnd() const { return mHwnd; }
|
||||
void SetHwnd(HWND hwnd) { mHwnd = hwnd; }
|
||||
#endif
|
||||
|
||||
#if defined(XP_UNIX) || defined(XP_OS2)
|
||||
//
|
||||
// called to process a client file descriptor. the value of pollFlags
|
||||
// indicates the state of the socket.
|
||||
//
|
||||
// returns:
|
||||
// 0 - to cancel client connection
|
||||
// PR_POLL_READ - to poll for a readable socket
|
||||
// PR_POLL_WRITE - to poll for a writable socket
|
||||
// (both flags) - to poll for either a readable or writable socket
|
||||
//
|
||||
// the socket is non-blocking.
|
||||
//
|
||||
int Process(PRFileDesc *sockFD, int pollFlags);
|
||||
|
||||
//
|
||||
// on success or failure, this function takes ownership of |msg| and will
|
||||
// delete it when appropriate.
|
||||
//
|
||||
void EnqueueOutboundMsg(ipcMessage *msg) { mOutMsgQ.Append(msg); }
|
||||
#endif
|
||||
|
||||
private:
|
||||
static PRUint32 gLastID;
|
||||
|
||||
PRUint32 mID;
|
||||
ipcStringList mNames;
|
||||
ipcIDList mTargets;
|
||||
PRBool mExpectsSyncReply;
|
||||
|
||||
#ifdef XP_WIN
|
||||
// on windows, we store the PID of the client process to help us determine
|
||||
// the client from which a message originated. each message has the PID
|
||||
// encoded in it.
|
||||
PRUint32 mPID;
|
||||
|
||||
// the hwnd of the client's message window.
|
||||
HWND mHwnd;
|
||||
#endif
|
||||
|
||||
#if defined(XP_UNIX) || defined(XP_OS2)
|
||||
ipcMessage mInMsg; // buffer for incoming message
|
||||
ipcMessageQ mOutMsgQ; // outgoing message queue
|
||||
|
||||
// keep track of the amount of the first message sent
|
||||
PRUint32 mSendOffset;
|
||||
|
||||
// utility function for writing out messages.
|
||||
int WriteMsgs(PRFileDesc *fd);
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif // !ipcClientUnix_h__
|
|
@ -0,0 +1,258 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "ipcLog.h"
|
||||
#include "ipcCommandModule.h"
|
||||
#include "ipcModule.h"
|
||||
#include "ipcClient.h"
|
||||
#include "ipcMessage.h"
|
||||
#include "ipcMessageUtils.h"
|
||||
#include "ipcModuleReg.h"
|
||||
#include "ipcd.h"
|
||||
#include "ipcm.h"
|
||||
|
||||
struct ipcCommandModule
|
||||
{
|
||||
typedef void (* MsgHandler)(ipcClient *, const ipcMessage *);
|
||||
|
||||
//
|
||||
// helpers
|
||||
//
|
||||
|
||||
static char **
|
||||
BuildStringArray(const ipcStringNode *nodes)
|
||||
{
|
||||
size_t count = 0;
|
||||
|
||||
const ipcStringNode *node;
|
||||
|
||||
for (node = nodes; node; node = node->mNext)
|
||||
count++;
|
||||
|
||||
char **strs = (char **) malloc((count + 1) * sizeof(char *));
|
||||
if (!strs)
|
||||
return NULL;
|
||||
|
||||
count = 0;
|
||||
for (node = nodes; node; node = node->mNext, ++count)
|
||||
strs[count] = (char *) node->Value();
|
||||
strs[count] = 0;
|
||||
|
||||
return strs;
|
||||
}
|
||||
|
||||
static nsID **
|
||||
BuildIDArray(const ipcIDNode *nodes)
|
||||
{
|
||||
size_t count = 0;
|
||||
|
||||
const ipcIDNode *node;
|
||||
|
||||
for (node = nodes; node; node = node->mNext)
|
||||
count++;
|
||||
|
||||
nsID **ids = (nsID **) calloc(count + 1, sizeof(nsID *));
|
||||
if (!ids)
|
||||
return NULL;
|
||||
|
||||
count = 0;
|
||||
for (node = nodes; node; node = node->mNext, ++count)
|
||||
ids[count] = (nsID *) &node->Value();
|
||||
|
||||
return ids;
|
||||
}
|
||||
|
||||
//
|
||||
// message handlers
|
||||
//
|
||||
|
||||
static void
|
||||
OnPing(ipcClient *client, const ipcMessage *rawMsg)
|
||||
{
|
||||
LOG(("got PING\n"));
|
||||
|
||||
IPC_SendMsg(client, new ipcmMessagePing());
|
||||
}
|
||||
|
||||
static void
|
||||
OnClientHello(ipcClient *client, const ipcMessage *rawMsg)
|
||||
{
|
||||
LOG(("got CLIENT_HELLO\n"));
|
||||
|
||||
IPC_SendMsg(client, new ipcmMessageClientID(client->ID()));
|
||||
|
||||
//
|
||||
// NOTE: it would almost make sense for this notification to live
|
||||
// in the transport layer code. however, clients expect to receive
|
||||
// a CLIENT_ID as the first message following a CLIENT_HELLO, so we
|
||||
// must not allow modules to see a client until after we have sent
|
||||
// the CLIENT_ID message.
|
||||
//
|
||||
IPC_NotifyClientUp(client);
|
||||
}
|
||||
|
||||
static void
|
||||
OnClientAddName(ipcClient *client, const ipcMessage *rawMsg)
|
||||
{
|
||||
LOG(("got CLIENT_ADD_NAME\n"));
|
||||
|
||||
ipcMessageCast<ipcmMessageClientAddName> msg(rawMsg);
|
||||
const char *name = msg->Name();
|
||||
if (name)
|
||||
client->AddName(name);
|
||||
}
|
||||
|
||||
static void
|
||||
OnClientDelName(ipcClient *client, const ipcMessage *rawMsg)
|
||||
{
|
||||
LOG(("got CLIENT_DEL_NAME\n"));
|
||||
|
||||
ipcMessageCast<ipcmMessageClientDelName> msg(rawMsg);
|
||||
const char *name = msg->Name();
|
||||
if (name)
|
||||
client->DelName(name);
|
||||
}
|
||||
|
||||
static void
|
||||
OnClientAddTarget(ipcClient *client, const ipcMessage *rawMsg)
|
||||
{
|
||||
LOG(("got CLIENT_ADD_TARGET\n"));
|
||||
|
||||
ipcMessageCast<ipcmMessageClientAddTarget> msg(rawMsg);
|
||||
client->AddTarget(msg->Target());
|
||||
}
|
||||
|
||||
static void
|
||||
OnClientDelTarget(ipcClient *client, const ipcMessage *rawMsg)
|
||||
{
|
||||
LOG(("got CLIENT_DEL_TARGET\n"));
|
||||
|
||||
ipcMessageCast<ipcmMessageClientDelTarget> msg(rawMsg);
|
||||
client->DelTarget(msg->Target());
|
||||
}
|
||||
|
||||
static void
|
||||
OnQueryClientByName(ipcClient *client, const ipcMessage *rawMsg)
|
||||
{
|
||||
LOG(("got QUERY_CLIENT_BY_NAME\n"));
|
||||
|
||||
ipcMessageCast<ipcmMessageQueryClientByName> msg(rawMsg);
|
||||
ipcClient *result = IPC_GetClientByName(msg->Name());
|
||||
if (result) {
|
||||
LOG((" client exists w/ ID = %u\n", result->ID()));
|
||||
IPC_SendMsg(client, new ipcmMessageClientID(result->ID()));
|
||||
}
|
||||
else {
|
||||
LOG((" client does not exist\n"));
|
||||
IPC_SendMsg(client, new ipcmMessageError(IPCM_ERROR_CLIENT_NOT_FOUND));
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
OnQueryClientInfo(ipcClient *client, const ipcMessage *rawMsg)
|
||||
{
|
||||
LOG(("got QUERY_CLIENT_INFO\n"));
|
||||
|
||||
ipcMessageCast<ipcmMessageQueryClientInfo> msg(rawMsg);
|
||||
ipcClient *result = IPC_GetClientByID(msg->ClientID());
|
||||
if (result) {
|
||||
char **names = BuildStringArray(result->Names());
|
||||
nsID **targets = BuildIDArray(result->Targets());
|
||||
|
||||
IPC_SendMsg(client, new ipcmMessageClientInfo(result->ID(),
|
||||
(const char **) names,
|
||||
(const nsID **) targets));
|
||||
|
||||
free(names);
|
||||
free(targets);
|
||||
}
|
||||
else {
|
||||
LOG((" client does not exist\n"));
|
||||
IPC_SendMsg(client, new ipcmMessageError(IPCM_ERROR_CLIENT_NOT_FOUND));
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
OnForward(ipcClient *client, const ipcMessage *rawMsg)
|
||||
{
|
||||
LOG(("got FORWARD\n"));
|
||||
|
||||
ipcMessageCast<ipcmMessageForward> msg(rawMsg);
|
||||
|
||||
ipcClient *dest = IPC_GetClientByID(msg->DestClientID());
|
||||
if (!dest) {
|
||||
LOG((" destination client not found!\n"));
|
||||
return;
|
||||
}
|
||||
ipcMessage *newMsg = new ipcMessage(msg->InnerTarget(),
|
||||
msg->InnerData(),
|
||||
msg->InnerDataLen());
|
||||
IPC_SendMsg(dest, newMsg);
|
||||
}
|
||||
};
|
||||
|
||||
void
|
||||
IPCM_HandleMsg(ipcClient *client, const ipcMessage *rawMsg)
|
||||
{
|
||||
static ipcCommandModule::MsgHandler handlers[] =
|
||||
{
|
||||
ipcCommandModule::OnPing,
|
||||
NULL, // ERROR
|
||||
ipcCommandModule::OnClientHello,
|
||||
NULL, // CLIENT_ID
|
||||
NULL, // CLIENT_INFO
|
||||
ipcCommandModule::OnClientAddName,
|
||||
ipcCommandModule::OnClientDelName,
|
||||
ipcCommandModule::OnClientAddTarget,
|
||||
ipcCommandModule::OnClientDelTarget,
|
||||
ipcCommandModule::OnQueryClientByName,
|
||||
ipcCommandModule::OnQueryClientInfo,
|
||||
ipcCommandModule::OnForward,
|
||||
};
|
||||
|
||||
int type = IPCM_GetMsgType(rawMsg);
|
||||
LOG(("IPCM_HandleMsg [type=%d]\n", type));
|
||||
|
||||
if (type < IPCM_MSG_TYPE_UNKNOWN) {
|
||||
if (handlers[type]) {
|
||||
ipcCommandModule::MsgHandler handler = handlers[type];
|
||||
handler(client, rawMsg);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#ifndef ipcCommandModule_h__
|
||||
#define ipcCommandModule_h__
|
||||
|
||||
#include "ipcm.h" // for IPCM_TARGET
|
||||
|
||||
class ipcClient;
|
||||
class ipcMessage;
|
||||
|
||||
void IPCM_HandleMsg(ipcClient *, const ipcMessage *);
|
||||
|
||||
#endif // !ipcCommandModule_h__
|
|
@ -0,0 +1,245 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "prlink.h"
|
||||
#include "prio.h"
|
||||
#include "prlog.h"
|
||||
#include "plstr.h"
|
||||
|
||||
#include "ipcConfig.h"
|
||||
#include "ipcLog.h"
|
||||
#include "ipcModuleReg.h"
|
||||
#include "ipcModule.h"
|
||||
#include "ipcCommandModule.h"
|
||||
#include "ipcd.h"
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
struct ipcModuleRegEntry
|
||||
{
|
||||
nsID target;
|
||||
ipcModuleMethods *methods;
|
||||
PRLibrary *lib;
|
||||
};
|
||||
|
||||
#define IPC_MAX_MODULE_COUNT 64
|
||||
|
||||
static ipcModuleRegEntry ipcModules[IPC_MAX_MODULE_COUNT];
|
||||
static int ipcModuleCount = 0;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
static PRStatus
|
||||
AddModule(const nsID &target, ipcModuleMethods *methods, const char *libPath)
|
||||
{
|
||||
if (ipcModuleCount == IPC_MAX_MODULE_COUNT) {
|
||||
LOG(("too many modules!\n"));
|
||||
return PR_FAILURE;
|
||||
}
|
||||
|
||||
if (!methods) {
|
||||
PR_NOT_REACHED("null module methods");
|
||||
return PR_FAILURE;
|
||||
}
|
||||
|
||||
//
|
||||
// each ipcModuleRegEntry holds a reference to a PRLibrary, and on
|
||||
// shutdown, each PRLibrary reference will be released. this ensures
|
||||
// that the library will not be unloaded until all of the modules in
|
||||
// that library are shutdown.
|
||||
//
|
||||
ipcModules[ipcModuleCount].target = target;
|
||||
ipcModules[ipcModuleCount].methods = methods;
|
||||
ipcModules[ipcModuleCount].lib = PR_LoadLibrary(libPath);
|
||||
|
||||
++ipcModuleCount;
|
||||
return PR_SUCCESS;
|
||||
}
|
||||
|
||||
static void
|
||||
InitModuleFromLib(const char *modulesDir, const char *fileName)
|
||||
{
|
||||
LOG(("InitModuleFromLib [%s]\n", fileName));
|
||||
|
||||
static const ipcDaemonMethods gDaemonMethods =
|
||||
{
|
||||
IPC_DAEMON_METHODS_VERSION,
|
||||
IPC_DispatchMsg,
|
||||
IPC_SendMsg,
|
||||
IPC_GetClientByID,
|
||||
IPC_GetClientByName,
|
||||
IPC_EnumClients,
|
||||
IPC_GetClientID,
|
||||
IPC_ClientHasName,
|
||||
IPC_ClientHasTarget,
|
||||
IPC_EnumClientNames,
|
||||
IPC_EnumClientTargets
|
||||
};
|
||||
|
||||
int dLen = strlen(modulesDir);
|
||||
int fLen = strlen(fileName);
|
||||
|
||||
char *buf = (char *) malloc(dLen + 1 + fLen + 1);
|
||||
memcpy(buf, modulesDir, dLen);
|
||||
buf[dLen] = IPC_PATH_SEP_CHAR;
|
||||
memcpy(buf + dLen + 1, fileName, fLen);
|
||||
buf[dLen + 1 + fLen] = '\0';
|
||||
|
||||
PRLibrary *lib = PR_LoadLibrary(buf);
|
||||
if (lib) {
|
||||
ipcGetModulesFunc func =
|
||||
(ipcGetModulesFunc) PR_FindFunctionSymbol(lib, "IPC_GetModules");
|
||||
|
||||
LOG((" func=%p\n", (void*) func));
|
||||
|
||||
if (func) {
|
||||
const ipcModuleEntry *entries = NULL;
|
||||
int count = func(&gDaemonMethods, &entries);
|
||||
for (int i=0; i<count; ++i) {
|
||||
if (AddModule(entries[i].target, entries[i].methods, buf) == PR_SUCCESS) {
|
||||
if (entries[i].methods->init)
|
||||
entries[i].methods->init();
|
||||
}
|
||||
}
|
||||
}
|
||||
PR_UnloadLibrary(lib);
|
||||
}
|
||||
|
||||
free(buf);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// ipcModuleReg API
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
void
|
||||
IPC_InitModuleReg(const char *exePath)
|
||||
{
|
||||
if (!(exePath && *exePath))
|
||||
return;
|
||||
|
||||
//
|
||||
// register plug-in modules
|
||||
//
|
||||
char *p = PL_strrchr(exePath, IPC_PATH_SEP_CHAR);
|
||||
if (p == NULL) {
|
||||
LOG(("unexpected exe path\n"));
|
||||
return;
|
||||
}
|
||||
|
||||
int baseLen = p - exePath;
|
||||
int finalLen = baseLen + 1 + sizeof(IPC_MODULES_DIR);
|
||||
|
||||
// build full path to ipc modules
|
||||
char *modulesDir = (char*) malloc(finalLen);
|
||||
memcpy(modulesDir, exePath, baseLen);
|
||||
modulesDir[baseLen] = IPC_PATH_SEP_CHAR;
|
||||
memcpy(modulesDir + baseLen + 1, IPC_MODULES_DIR, sizeof(IPC_MODULES_DIR));
|
||||
|
||||
LOG(("loading libraries in %s\n", modulesDir));
|
||||
//
|
||||
// scan directory for IPC modules
|
||||
//
|
||||
PRDir *dir = PR_OpenDir(modulesDir);
|
||||
if (dir) {
|
||||
PRDirEntry *ent;
|
||||
while ((ent = PR_ReadDir(dir, PR_SKIP_BOTH)) != NULL) {
|
||||
//
|
||||
// locate extension, and check if dynamic library
|
||||
//
|
||||
|
||||
const char *p = strrchr(ent->name, '.');
|
||||
if (p && PL_strcasecmp(p, MOZ_DLL_SUFFIX) == 0)
|
||||
InitModuleFromLib(modulesDir, ent->name);
|
||||
}
|
||||
PR_CloseDir(dir);
|
||||
}
|
||||
|
||||
free(modulesDir);
|
||||
}
|
||||
|
||||
void
|
||||
IPC_ShutdownModuleReg()
|
||||
{
|
||||
//
|
||||
// shutdown modules in reverse order
|
||||
//
|
||||
while (ipcModuleCount) {
|
||||
ipcModuleRegEntry &entry = ipcModules[--ipcModuleCount];
|
||||
if (entry.methods->shutdown)
|
||||
entry.methods->shutdown();
|
||||
if (entry.lib)
|
||||
PR_UnloadLibrary(entry.lib);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
IPC_NotifyClientUp(ipcClient *client)
|
||||
{
|
||||
for (int i = 0; i < ipcModuleCount; ++i) {
|
||||
ipcModuleRegEntry &entry = ipcModules[i];
|
||||
if (entry.methods->clientUp)
|
||||
entry.methods->clientUp(client);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
IPC_NotifyClientDown(ipcClient *client)
|
||||
{
|
||||
for (int i = 0; i < ipcModuleCount; ++i) {
|
||||
ipcModuleRegEntry &entry = ipcModules[i];
|
||||
if (entry.methods->clientDown)
|
||||
entry.methods->clientDown(client);
|
||||
}
|
||||
}
|
||||
|
||||
PRStatus
|
||||
IPC_DispatchMsg(ipcClient *client, const nsID &target, const void *data, PRUint32 dataLen)
|
||||
{
|
||||
// dispatch message to every module registered under the given target.
|
||||
for (int i=0; i<ipcModuleCount; ++i) {
|
||||
ipcModuleRegEntry &entry = ipcModules[i];
|
||||
if (entry.target.Equals(target)) {
|
||||
if (entry.methods->handleMsg)
|
||||
entry.methods->handleMsg(client, target, data, dataLen);
|
||||
}
|
||||
}
|
||||
return PR_SUCCESS;
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#ifndef ipcModuleReg_h__
|
||||
#define ipcModuleReg_h__
|
||||
|
||||
#include "ipcModule.h"
|
||||
|
||||
//
|
||||
// called to init the module registry. this may only be called once at
|
||||
// startup or once after calling IPC_ShutdownModuleReg.
|
||||
//
|
||||
// params:
|
||||
// exePath - path to the daemon executable. modules are loaded from a
|
||||
// directory relative to the daemon executable.
|
||||
//
|
||||
void IPC_InitModuleReg(const char *exePath);
|
||||
|
||||
//
|
||||
// called to shutdown the module registry. this may be called more than
|
||||
// once and need not follow a call to IPC_InitModuleReg.
|
||||
//
|
||||
void IPC_ShutdownModuleReg();
|
||||
|
||||
//
|
||||
// returns the ipcModuleMethods for the given target.
|
||||
//
|
||||
ipcModuleMethods *IPC_GetModuleByTarget(const nsID &target);
|
||||
|
||||
//
|
||||
// notifies all modules of client connect/disconnect
|
||||
//
|
||||
void IPC_NotifyClientUp(ipcClient *);
|
||||
void IPC_NotifyClientDown(ipcClient *);
|
||||
|
||||
#endif // !ipcModuleReg_h__
|
|
@ -0,0 +1,203 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include "prlog.h"
|
||||
#include "prio.h"
|
||||
|
||||
#include "ipcConfig.h"
|
||||
#include "ipcLog.h"
|
||||
#include "ipcMessage.h"
|
||||
#include "ipcClient.h"
|
||||
#include "ipcModuleReg.h"
|
||||
#include "ipcModule.h"
|
||||
#include "ipcCommandModule.h"
|
||||
#include "ipcdPrivate.h"
|
||||
#include "ipcd.h"
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
void
|
||||
IPC_NotifyParent()
|
||||
{
|
||||
PRFileDesc *fd = PR_GetInheritedFD(IPC_STARTUP_PIPE_NAME);
|
||||
if (fd) {
|
||||
char c = IPC_STARTUP_PIPE_MAGIC;
|
||||
PR_Write(fd, &c, 1);
|
||||
PR_Close(fd);
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
PRStatus
|
||||
IPC_DispatchMsg(ipcClient *client, const ipcMessage *msg)
|
||||
{
|
||||
PR_ASSERT(client);
|
||||
PR_ASSERT(msg);
|
||||
|
||||
// remember if client is expecting SYNC_REPLY. we'll add that flag to the
|
||||
// next message sent to the client.
|
||||
if (msg->TestFlag(IPC_MSG_FLAG_SYNC_QUERY)) {
|
||||
PR_ASSERT(client->GetExpectsSyncReply() == PR_FALSE);
|
||||
client->SetExpectsSyncReply(PR_TRUE);
|
||||
}
|
||||
|
||||
if (msg->Target().Equals(IPCM_TARGET)) {
|
||||
IPCM_HandleMsg(client, msg);
|
||||
return PR_SUCCESS;
|
||||
}
|
||||
|
||||
return IPC_DispatchMsg(client, msg->Target(), msg->Data(), msg->DataLen());
|
||||
}
|
||||
|
||||
PRStatus
|
||||
IPC_SendMsg(ipcClient *client, ipcMessage *msg)
|
||||
{
|
||||
PR_ASSERT(msg);
|
||||
|
||||
if (client == NULL) {
|
||||
//
|
||||
// broadcast
|
||||
//
|
||||
for (int i=0; i<ipcClientCount; ++i)
|
||||
IPC_SendMsg(&ipcClients[i], msg->Clone());
|
||||
delete msg;
|
||||
return PR_SUCCESS;
|
||||
}
|
||||
|
||||
// add SYNC_REPLY flag to message if client is expecting...
|
||||
if (client->GetExpectsSyncReply()) {
|
||||
msg->SetFlag(IPC_MSG_FLAG_SYNC_REPLY);
|
||||
client->SetExpectsSyncReply(PR_FALSE);
|
||||
}
|
||||
|
||||
if (client->HasTarget(msg->Target()))
|
||||
return IPC_PlatformSendMsg(client, msg);
|
||||
|
||||
LOG((" no registered message handler\n"));
|
||||
return PR_FAILURE;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// IPC daemon methods
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
PRStatus
|
||||
IPC_SendMsg(ipcClient *client, const nsID &target, const void *data, PRUint32 dataLen)
|
||||
{
|
||||
return IPC_SendMsg(client, new ipcMessage(target, (const char *) data, dataLen));
|
||||
}
|
||||
|
||||
ipcClient *
|
||||
IPC_GetClientByID(PRUint32 clientID)
|
||||
{
|
||||
// linear search OK since number of clients should be small
|
||||
for (int i = 0; i < ipcClientCount; ++i) {
|
||||
if (ipcClients[i].ID() == clientID)
|
||||
return &ipcClients[i];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ipcClient *
|
||||
IPC_GetClientByName(const char *name)
|
||||
{
|
||||
// linear search OK since number of clients should be small
|
||||
for (int i = 0; i < ipcClientCount; ++i) {
|
||||
if (ipcClients[i].HasName(name))
|
||||
return &ipcClients[i];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void
|
||||
IPC_EnumClients(ipcClientEnumFunc func, void *closure)
|
||||
{
|
||||
PR_ASSERT(func);
|
||||
for (int i = 0; i < ipcClientCount; ++i) {
|
||||
if (func(closure, &ipcClients[i], ipcClients[i].ID()) == PR_FALSE)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
PRUint32
|
||||
IPC_GetClientID(ipcClient *client)
|
||||
{
|
||||
PR_ASSERT(client);
|
||||
return client->ID();
|
||||
}
|
||||
|
||||
PRBool
|
||||
IPC_ClientHasName(ipcClient *client, const char *name)
|
||||
{
|
||||
PR_ASSERT(client);
|
||||
PR_ASSERT(name);
|
||||
return client->HasName(name);
|
||||
}
|
||||
|
||||
PRBool
|
||||
IPC_ClientHasTarget(ipcClient *client, const nsID &target)
|
||||
{
|
||||
PR_ASSERT(client);
|
||||
return client->HasTarget(target);
|
||||
}
|
||||
|
||||
void
|
||||
IPC_EnumClientNames(ipcClient *client, ipcClientNameEnumFunc func, void *closure)
|
||||
{
|
||||
PR_ASSERT(client);
|
||||
PR_ASSERT(func);
|
||||
const ipcStringNode *node = client->Names();
|
||||
while (node) {
|
||||
if (func(closure, client, node->Value()) == PR_FALSE)
|
||||
break;
|
||||
node = node->mNext;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
IPC_EnumClientTargets(ipcClient *client, ipcClientTargetEnumFunc func, void *closure)
|
||||
{
|
||||
PR_ASSERT(client);
|
||||
PR_ASSERT(func);
|
||||
const ipcIDNode *node = client->Targets();
|
||||
while (node) {
|
||||
if (func(closure, client, node->Value()) == PR_FALSE)
|
||||
break;
|
||||
node = node->mNext;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#ifndef IPCD_H__
|
||||
#define IPCD_H__
|
||||
|
||||
#include "ipcModule.h"
|
||||
#include "ipcMessage.h"
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// IPC daemon methods (see struct ipcDaemonMethods)
|
||||
//
|
||||
// these functions may only be called directly from within the daemon. plug-in
|
||||
// modules must access these through the ipcDaemonMethods structure.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
PRStatus IPC_DispatchMsg (ipcClientHandle client, const nsID &target, const void *data, PRUint32 dataLen);
|
||||
PRStatus IPC_SendMsg (ipcClientHandle client, const nsID &target, const void *data, PRUint32 dataLen);
|
||||
ipcClientHandle IPC_GetClientByID (PRUint32 id);
|
||||
ipcClientHandle IPC_GetClientByName (const char *name);
|
||||
void IPC_EnumClients (ipcClientEnumFunc func, void *closure);
|
||||
PRUint32 IPC_GetClientID (ipcClientHandle client);
|
||||
PRBool IPC_ClientHasName (ipcClientHandle client, const char *name);
|
||||
PRBool IPC_ClientHasTarget (ipcClientHandle client, const nsID &target);
|
||||
void IPC_EnumClientNames (ipcClientHandle client, ipcClientNameEnumFunc func, void *closure);
|
||||
void IPC_EnumClientTargets (ipcClientHandle client, ipcClientTargetEnumFunc func, void *closure);
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// other internal IPCD methods
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
//
|
||||
// dispatch message
|
||||
//
|
||||
PRStatus IPC_DispatchMsg(ipcClientHandle client, const ipcMessage *msg);
|
||||
|
||||
//
|
||||
// send message, takes ownership of |msg|.
|
||||
//
|
||||
PRStatus IPC_SendMsg(ipcClientHandle client, ipcMessage *msg);
|
||||
|
||||
#endif // !IPCD_H__
|
|
@ -0,0 +1,65 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#ifndef ipcdPrivate_h__
|
||||
#define ipcdPrivate_h__
|
||||
|
||||
class ipcClient;
|
||||
|
||||
//
|
||||
// upper limit on the number of active connections
|
||||
// XXX may want to make this more dynamic
|
||||
//
|
||||
#define IPC_MAX_CLIENTS 100
|
||||
|
||||
//
|
||||
// array of connected clients
|
||||
//
|
||||
extern ipcClient *ipcClients;
|
||||
extern int ipcClientCount;
|
||||
|
||||
//
|
||||
// platform specific send message function, takes ownership of |msg|.
|
||||
//
|
||||
PRStatus IPC_PlatformSendMsg(ipcClient *client, ipcMessage *msg);
|
||||
|
||||
//
|
||||
// notify parent that it can connect to the daemon.
|
||||
//
|
||||
void IPC_NotifyParent();
|
||||
|
||||
#endif // !ipcdPrivate_h__
|
|
@ -0,0 +1,77 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is IBM Corporation.
|
||||
* Portions created by IBM Corporation are Copyright (C) 2003
|
||||
* IBM Corporation. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@meer.net>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include "ipcd.h"
|
||||
#include "ipcdPrivate.h"
|
||||
#include "ipcLog.h"
|
||||
|
||||
#include "prerror.h"
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// use this file as a template to add server-side IPC connectivity.
|
||||
//
|
||||
// NOTE: if your platform supports local domain TCP sockets, then you should
|
||||
// be able to make use of ipcConnectionUnix.cpp.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
// these variables are declared in ipcdPrivate.h and must be initialized by
|
||||
// when the daemon starts up.
|
||||
ipcClient *ipcClients = NULL;
|
||||
int ipcClientCount = 0;
|
||||
|
||||
PRStatus
|
||||
IPC_PlatformSendMsg(ipcClient *client, ipcMessage *msg)
|
||||
{
|
||||
const char notimplemented[] = "IPC_PlatformSendMsg not implemented";
|
||||
PR_SetErrorText(sizeof(notimplemented), notimplemented);
|
||||
return PR_FAILURE;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
IPC_InitLog("###");
|
||||
|
||||
LOG(("daemon started...\n"));
|
||||
|
||||
/*
|
||||
IPC_InitModuleReg(argv[0]);
|
||||
IPC_ShutdownModuleReg();
|
||||
*/
|
||||
|
||||
// let the parent process know that we are up-and-running
|
||||
IPC_NotifyParent();
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,463 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "prio.h"
|
||||
#include "prerror.h"
|
||||
#include "prthread.h"
|
||||
#include "prinrval.h"
|
||||
#include "plstr.h"
|
||||
#include "prprf.h"
|
||||
|
||||
#include "ipcConfig.h"
|
||||
#include "ipcLog.h"
|
||||
#include "ipcMessage.h"
|
||||
#include "ipcClient.h"
|
||||
#include "ipcModuleReg.h"
|
||||
#include "ipcdPrivate.h"
|
||||
#include "ipcd.h"
|
||||
|
||||
#if 0
|
||||
static void
|
||||
IPC_Sleep(int seconds)
|
||||
{
|
||||
while (seconds > 0) {
|
||||
LOG(("\rsleeping for %d seconds...", seconds));
|
||||
PR_Sleep(PR_SecondsToInterval(1));
|
||||
--seconds;
|
||||
}
|
||||
LOG(("\ndone sleeping\n"));
|
||||
}
|
||||
#endif
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// ipc directory and locking...
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
//
|
||||
// advisory file locking is used to ensure that only one IPC daemon is active
|
||||
// and bound to the local domain socket at a time.
|
||||
//
|
||||
// XXX this code does not work on OS/2.
|
||||
//
|
||||
#if !defined(XP_OS2)
|
||||
#define IPC_USE_FILE_LOCK
|
||||
#endif
|
||||
|
||||
#ifdef IPC_USE_FILE_LOCK
|
||||
|
||||
static int ipcLockFD = 0;
|
||||
|
||||
static PRBool AcquireDaemonLock(const char *baseDir)
|
||||
{
|
||||
const char lockName[] = "lock";
|
||||
|
||||
int dirLen = strlen(baseDir);
|
||||
int len = dirLen // baseDir
|
||||
+ 1 // "/"
|
||||
+ sizeof(lockName); // "lock"
|
||||
|
||||
char *lockFile = (char *) malloc(len);
|
||||
memcpy(lockFile, baseDir, dirLen);
|
||||
lockFile[dirLen] = '/';
|
||||
memcpy(lockFile + dirLen + 1, lockName, sizeof(lockName));
|
||||
|
||||
//
|
||||
// open lock file. it remains open until we shutdown.
|
||||
//
|
||||
ipcLockFD = open(lockFile, O_WRONLY|O_CREAT, S_IWUSR|S_IRUSR);
|
||||
|
||||
free(lockFile);
|
||||
|
||||
if (ipcLockFD == -1)
|
||||
return PR_FALSE;
|
||||
|
||||
//
|
||||
// we use fcntl for locking. assumption: filesystem should be local.
|
||||
// this API is nice because the lock will be automatically released
|
||||
// when the process dies. it will also be released when the file
|
||||
// descriptor is closed.
|
||||
//
|
||||
struct flock lock;
|
||||
lock.l_type = F_WRLCK;
|
||||
lock.l_start = 0;
|
||||
lock.l_len = 0;
|
||||
lock.l_whence = SEEK_SET;
|
||||
if (fcntl(ipcLockFD, F_SETLK, &lock) == -1)
|
||||
return PR_FALSE;
|
||||
|
||||
//
|
||||
// truncate lock file once we have exclusive access to it.
|
||||
//
|
||||
ftruncate(ipcLockFD, 0);
|
||||
|
||||
//
|
||||
// write our PID into the lock file (this just seems like a good idea...
|
||||
// no real purpose otherwise).
|
||||
//
|
||||
char buf[32];
|
||||
int nb = PR_snprintf(buf, sizeof(buf), "%u\n", (unsigned long) getpid());
|
||||
write(ipcLockFD, buf, nb);
|
||||
|
||||
return PR_TRUE;
|
||||
}
|
||||
|
||||
static PRBool InitDaemonDir(const char *socketPath)
|
||||
{
|
||||
LOG(("InitDaemonDir [sock=%s]\n", socketPath));
|
||||
|
||||
char *baseDir = PL_strdup(socketPath);
|
||||
|
||||
//
|
||||
// make sure IPC directory exists (XXX this should be recursive)
|
||||
//
|
||||
char *p = strrchr(baseDir, '/');
|
||||
if (p)
|
||||
p[0] = '\0';
|
||||
mkdir(baseDir, 0700);
|
||||
|
||||
//
|
||||
// if we can't acquire the daemon lock, then another daemon
|
||||
// must be active, so bail.
|
||||
//
|
||||
PRBool haveLock = AcquireDaemonLock(baseDir);
|
||||
|
||||
PL_strfree(baseDir);
|
||||
|
||||
if (haveLock) {
|
||||
// delete an existing socket to prevent bind from failing.
|
||||
unlink(socketPath);
|
||||
}
|
||||
return haveLock;
|
||||
}
|
||||
|
||||
static void ShutdownDaemonDir()
|
||||
{
|
||||
LOG(("ShutdownDaemonDir\n"));
|
||||
|
||||
// deleting directory and files underneath it allows another process
|
||||
// to think it has exclusive access. better to just leave the hidden
|
||||
// directory in /tmp and let the OS clean it up via the usual tmpdir
|
||||
// cleanup cron job.
|
||||
|
||||
// this removes the advisory lock, allowing other processes to acquire it.
|
||||
if (ipcLockFD) {
|
||||
close(ipcLockFD);
|
||||
ipcLockFD = 0;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // IPC_USE_FILE_LOCK
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// poll list
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
//
|
||||
// declared in ipcdPrivate.h
|
||||
//
|
||||
ipcClient *ipcClients = NULL;
|
||||
int ipcClientCount = 0;
|
||||
|
||||
//
|
||||
// the first element of this array is always zero; this is done so that the
|
||||
// k'th element of ipcClientArray corresponds to the k'th element of
|
||||
// ipcPollList.
|
||||
//
|
||||
static ipcClient ipcClientArray[IPC_MAX_CLIENTS + 1];
|
||||
|
||||
//
|
||||
// element 0 contains the "server socket"
|
||||
//
|
||||
static PRPollDesc ipcPollList[IPC_MAX_CLIENTS + 1];
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
static int AddClient(PRFileDesc *fd)
|
||||
{
|
||||
if (ipcClientCount == IPC_MAX_CLIENTS) {
|
||||
LOG(("reached maximum client limit\n"));
|
||||
return -1;
|
||||
}
|
||||
|
||||
int pollCount = ipcClientCount + 1;
|
||||
|
||||
ipcClientArray[pollCount].Init();
|
||||
|
||||
ipcPollList[pollCount].fd = fd;
|
||||
ipcPollList[pollCount].in_flags = PR_POLL_READ;
|
||||
ipcPollList[pollCount].out_flags = 0;
|
||||
|
||||
++ipcClientCount;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int RemoveClient(int clientIndex)
|
||||
{
|
||||
PRPollDesc *pd = &ipcPollList[clientIndex];
|
||||
|
||||
PR_Close(pd->fd);
|
||||
|
||||
ipcClientArray[clientIndex].Finalize();
|
||||
|
||||
//
|
||||
// keep the clients and poll_fds contiguous; move the last one into
|
||||
// the spot held by the one that is going away.
|
||||
//
|
||||
int toIndex = clientIndex;
|
||||
int fromIndex = ipcClientCount;
|
||||
if (fromIndex != toIndex) {
|
||||
memcpy(&ipcClientArray[toIndex], &ipcClientArray[fromIndex], sizeof(ipcClient));
|
||||
memcpy(&ipcPollList[toIndex], &ipcPollList[fromIndex], sizeof(PRPollDesc));
|
||||
}
|
||||
|
||||
//
|
||||
// zero out the old entries.
|
||||
//
|
||||
memset(&ipcClientArray[fromIndex], 0, sizeof(ipcClient));
|
||||
memset(&ipcPollList[fromIndex], 0, sizeof(PRPollDesc));
|
||||
|
||||
--ipcClientCount;
|
||||
return 0;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
static void PollLoop(PRFileDesc *listenFD)
|
||||
{
|
||||
// the first element of ipcClientArray is unused.
|
||||
memset(ipcClientArray, 0, sizeof(ipcClientArray));
|
||||
ipcClients = ipcClientArray + 1;
|
||||
ipcClientCount = 0;
|
||||
|
||||
ipcPollList[0].fd = listenFD;
|
||||
ipcPollList[0].in_flags = PR_POLL_EXCEPT | PR_POLL_READ;
|
||||
|
||||
while (1) {
|
||||
PRInt32 rv;
|
||||
PRIntn i;
|
||||
|
||||
int pollCount = ipcClientCount + 1;
|
||||
|
||||
ipcPollList[0].out_flags = 0;
|
||||
|
||||
//
|
||||
// poll
|
||||
//
|
||||
// timeout after 5 minutes. if no connections after timeout, then
|
||||
// exit. this timeout ensures that we don't stay resident when no
|
||||
// clients are interested in connecting after spawning the daemon.
|
||||
//
|
||||
// XXX add #define for timeout value
|
||||
//
|
||||
LOG(("calling PR_Poll [pollCount=%d]\n", pollCount));
|
||||
rv = PR_Poll(ipcPollList, pollCount, PR_SecondsToInterval(60 * 5));
|
||||
if (rv == -1) {
|
||||
LOG(("PR_Poll failed [%d]\n", PR_GetError()));
|
||||
return;
|
||||
}
|
||||
|
||||
if (rv > 0) {
|
||||
//
|
||||
// process clients that are ready
|
||||
//
|
||||
for (i = 1; i < pollCount; ++i) {
|
||||
if (ipcPollList[i].out_flags != 0) {
|
||||
ipcPollList[i].in_flags =
|
||||
ipcClientArray[i].Process(ipcPollList[i].fd,
|
||||
ipcPollList[i].out_flags);
|
||||
ipcPollList[i].out_flags = 0;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// cleanup any dead clients (indicated by a zero in_flags)
|
||||
//
|
||||
for (i = pollCount - 1; i >= 1; --i) {
|
||||
if (ipcPollList[i].in_flags == 0)
|
||||
RemoveClient(i);
|
||||
}
|
||||
|
||||
//
|
||||
// check for new connection
|
||||
//
|
||||
if (ipcPollList[0].out_flags & PR_POLL_READ) {
|
||||
LOG(("got new connection\n"));
|
||||
|
||||
PRNetAddr clientAddr;
|
||||
memset(&clientAddr, 0, sizeof(clientAddr));
|
||||
PRFileDesc *clientFD;
|
||||
|
||||
clientFD = PR_Accept(listenFD, &clientAddr, PR_INTERVAL_NO_WAIT);
|
||||
if (clientFD == NULL) {
|
||||
// ignore this error... perhaps the client disconnected.
|
||||
LOG(("PR_Accept failed [%d]\n", PR_GetError()));
|
||||
}
|
||||
else {
|
||||
// make socket non-blocking
|
||||
PRSocketOptionData opt;
|
||||
opt.option = PR_SockOpt_Nonblocking;
|
||||
opt.value.non_blocking = PR_TRUE;
|
||||
PR_SetSocketOption(clientFD, &opt);
|
||||
|
||||
if (AddClient(clientFD) != 0)
|
||||
PR_Close(clientFD);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// shutdown if no clients
|
||||
//
|
||||
if (ipcClientCount == 0) {
|
||||
LOG(("shutting down\n"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
PRStatus
|
||||
IPC_PlatformSendMsg(ipcClient *client, ipcMessage *msg)
|
||||
{
|
||||
LOG(("IPC_PlatformSendMsg\n"));
|
||||
|
||||
//
|
||||
// must copy message onto send queue.
|
||||
//
|
||||
client->EnqueueOutboundMsg(msg);
|
||||
|
||||
//
|
||||
// since our Process method may have already been called, we must ensure
|
||||
// that the PR_POLL_WRITE flag is set.
|
||||
//
|
||||
int clientIndex = client - ipcClientArray;
|
||||
ipcPollList[clientIndex].in_flags |= PR_POLL_WRITE;
|
||||
|
||||
return PR_SUCCESS;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
PRFileDesc *listenFD = NULL;
|
||||
PRNetAddr addr;
|
||||
|
||||
//
|
||||
// ignore SIGINT so <ctrl-c> from terminal only kills the client
|
||||
// which spawned this daemon.
|
||||
//
|
||||
signal(SIGINT, SIG_IGN);
|
||||
// XXX block others? check cartman
|
||||
|
||||
// ensure strict file permissions
|
||||
umask(0077);
|
||||
|
||||
IPC_InitLog("###");
|
||||
|
||||
LOG(("daemon started...\n"));
|
||||
|
||||
//XXX uncomment these lines to test slow starting daemon
|
||||
//IPC_Sleep(2);
|
||||
|
||||
// set socket address
|
||||
addr.local.family = PR_AF_LOCAL;
|
||||
if (argc < 2)
|
||||
IPC_GetDefaultSocketPath(addr.local.path, sizeof(addr.local.path));
|
||||
else
|
||||
PL_strncpyz(addr.local.path, argv[1], sizeof(addr.local.path));
|
||||
|
||||
#ifdef IPC_USE_FILE_LOCK
|
||||
if (!InitDaemonDir(addr.local.path)) {
|
||||
LOG(("InitDaemonDir failed\n"));
|
||||
goto end;
|
||||
}
|
||||
#endif
|
||||
|
||||
listenFD = PR_OpenTCPSocket(PR_AF_LOCAL);
|
||||
if (!listenFD) {
|
||||
LOG(("PR_OpenTCPSocket failed [%d]\n", PR_GetError()));
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (PR_Bind(listenFD, &addr) != PR_SUCCESS) {
|
||||
LOG(("PR_Bind failed [%d]\n", PR_GetError()));
|
||||
goto end;
|
||||
}
|
||||
|
||||
IPC_InitModuleReg(argv[0]);
|
||||
|
||||
if (PR_Listen(listenFD, 5) != PR_SUCCESS) {
|
||||
LOG(("PR_Listen failed [%d]\n", PR_GetError()));
|
||||
goto end;
|
||||
}
|
||||
|
||||
IPC_NotifyParent();
|
||||
|
||||
PollLoop(listenFD);
|
||||
|
||||
end:
|
||||
IPC_ShutdownModuleReg();
|
||||
|
||||
IPC_NotifyParent();
|
||||
|
||||
//IPC_Sleep(5);
|
||||
|
||||
#ifdef IPC_USE_FILE_LOCK
|
||||
// it is critical that we release the lock before closing the socket,
|
||||
// otherwise, a client might launch another daemon that would be unable
|
||||
// to acquire the lock and would then leave the client without a daemon.
|
||||
|
||||
ShutdownDaemonDir();
|
||||
#endif
|
||||
|
||||
if (listenFD) {
|
||||
LOG(("closing socket\n"));
|
||||
PR_Close(listenFD);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,408 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#include "prthread.h"
|
||||
|
||||
#include "ipcConfig.h"
|
||||
#include "ipcLog.h"
|
||||
#include "ipcMessage.h"
|
||||
#include "ipcClient.h"
|
||||
#include "ipcModuleReg.h"
|
||||
#include "ipcdPrivate.h"
|
||||
#include "ipcd.h"
|
||||
#include "ipcm.h"
|
||||
|
||||
//
|
||||
// declared in ipcdPrivate.h
|
||||
//
|
||||
ipcClient *ipcClients = NULL;
|
||||
int ipcClientCount = 0;
|
||||
|
||||
static ipcClient ipcClientArray[IPC_MAX_CLIENTS];
|
||||
|
||||
static HWND ipcHwnd = NULL;
|
||||
static PRBool ipcShutdown = PR_FALSE;
|
||||
|
||||
#define IPC_PURGE_TIMER_ID 1
|
||||
#define IPC_WM_SENDMSG (WM_USER + 1)
|
||||
#define IPC_WM_SHUTDOWN (WM_USER + 2)
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// client array manipulation
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
static void
|
||||
RemoveClient(ipcClient *client)
|
||||
{
|
||||
LOG(("RemoveClient\n"));
|
||||
|
||||
int clientIndex = client - ipcClientArray;
|
||||
|
||||
client->Finalize();
|
||||
|
||||
//
|
||||
// move last ipcClient object down into the spot occupied by this client.
|
||||
//
|
||||
int fromIndex = ipcClientCount - 1;
|
||||
int toIndex = clientIndex;
|
||||
if (toIndex != fromIndex)
|
||||
memcpy(&ipcClientArray[toIndex], &ipcClientArray[fromIndex], sizeof(ipcClient));
|
||||
|
||||
memset(&ipcClientArray[fromIndex], 0, sizeof(ipcClient));
|
||||
|
||||
--ipcClientCount;
|
||||
LOG((" num clients = %u\n", ipcClientCount));
|
||||
|
||||
if (ipcClientCount == 0) {
|
||||
LOG((" shutting down...\n"));
|
||||
KillTimer(ipcHwnd, IPC_PURGE_TIMER_ID);
|
||||
PostQuitMessage(0);
|
||||
ipcShutdown = PR_TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
PurgeStaleClients()
|
||||
{
|
||||
if (ipcClientCount == 0)
|
||||
return;
|
||||
|
||||
LOG(("PurgeStaleClients [num-clients=%u]\n", ipcClientCount));
|
||||
//
|
||||
// walk the list of supposedly active clients, and verify the existance of
|
||||
// their respective message windows.
|
||||
//
|
||||
char wName[IPC_CLIENT_WINDOW_NAME_MAXLEN];
|
||||
for (int i=ipcClientCount-1; i>=0; --i) {
|
||||
ipcClient *client = &ipcClientArray[i];
|
||||
|
||||
LOG((" checking client at index %u [client-id=%u pid=%u]\n",
|
||||
i, client->ID(), client->PID()));
|
||||
|
||||
IPC_GetClientWindowName(client->PID(), wName);
|
||||
|
||||
// XXX dougt has ideas about how to make this better
|
||||
|
||||
HWND hwnd = FindWindow(IPC_CLIENT_WINDOW_CLASS, wName);
|
||||
if (!hwnd) {
|
||||
LOG((" client window not found; removing client!\n"));
|
||||
RemoveClient(client);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static ipcClient *
|
||||
AddClient(HWND hwnd, PRUint32 pid)
|
||||
{
|
||||
LOG(("AddClient\n"));
|
||||
|
||||
//
|
||||
// before adding a new client, verify that all existing clients are
|
||||
// still up and running. remove any stale clients.
|
||||
//
|
||||
PurgeStaleClients();
|
||||
|
||||
if (ipcClientCount == IPC_MAX_CLIENTS) {
|
||||
LOG((" reached maximum client count!\n"));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ipcClient *client = &ipcClientArray[ipcClientCount];
|
||||
client->Init();
|
||||
client->SetHwnd(hwnd);
|
||||
client->SetPID(pid); // XXX one function instead of 3
|
||||
|
||||
++ipcClientCount;
|
||||
LOG((" num clients = %u\n", ipcClientCount));
|
||||
|
||||
if (ipcClientCount == 1)
|
||||
SetTimer(ipcHwnd, IPC_PURGE_TIMER_ID, 1000, NULL);
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
static ipcClient *
|
||||
GetClientByPID(PRUint32 pid)
|
||||
{
|
||||
for (int i=0; i<ipcClientCount; ++i) {
|
||||
if (ipcClientArray[i].PID() == pid)
|
||||
return &ipcClientArray[i];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// message processing
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
static void
|
||||
ProcessMsg(HWND hwnd, PRUint32 pid, const ipcMessage *msg)
|
||||
{
|
||||
LOG(("ProcessMsg [pid=%u len=%u]\n", pid, msg->MsgLen()));
|
||||
|
||||
ipcClient *client = GetClientByPID(pid);
|
||||
|
||||
if (client) {
|
||||
//
|
||||
// if this is an IPCM "client hello" message, then reset the client
|
||||
// instance object.
|
||||
//
|
||||
if (msg->Target().Equals(IPCM_TARGET) &&
|
||||
IPCM_GetMsgType(msg) == IPCM_MSG_TYPE_CLIENT_HELLO) {
|
||||
RemoveClient(client);
|
||||
client = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (client == NULL) {
|
||||
client = AddClient(hwnd, pid);
|
||||
if (!client)
|
||||
return;
|
||||
}
|
||||
|
||||
IPC_DispatchMsg(client, msg);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
PRStatus
|
||||
IPC_PlatformSendMsg(ipcClient *client, ipcMessage *msg)
|
||||
{
|
||||
LOG(("IPC_PlatformSendMsg [clientID=%u clientPID=%u]\n",
|
||||
client->ID(), client->PID()));
|
||||
|
||||
// use PostMessage to make this asynchronous; otherwise we might get
|
||||
// some wierd SendMessage recursion between processes.
|
||||
|
||||
WPARAM wParam = (WPARAM) client->Hwnd();
|
||||
LPARAM lParam = (LPARAM) msg;
|
||||
if (!PostMessage(ipcHwnd, IPC_WM_SENDMSG, wParam, lParam)) {
|
||||
LOG(("PostMessage failed\n"));
|
||||
delete msg;
|
||||
return PR_FAILURE;
|
||||
}
|
||||
return PR_SUCCESS;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// windows message loop
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
static LRESULT CALLBACK
|
||||
WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
||||
{
|
||||
LOG(("got message [msg=%x wparam=%x lparam=%x]\n", uMsg, wParam, lParam));
|
||||
|
||||
if (uMsg == WM_COPYDATA) {
|
||||
if (ipcShutdown) {
|
||||
LOG(("ignoring message b/c daemon is shutting down\n"));
|
||||
return TRUE;
|
||||
}
|
||||
COPYDATASTRUCT *cd = (COPYDATASTRUCT *) lParam;
|
||||
if (cd && cd->lpData) {
|
||||
ipcMessage msg;
|
||||
PRUint32 bytesRead;
|
||||
PRBool complete;
|
||||
// XXX avoid extra malloc
|
||||
PRStatus rv = msg.ReadFrom((const char *) cd->lpData, cd->cbData,
|
||||
&bytesRead, &complete);
|
||||
if (rv == PR_SUCCESS && complete) {
|
||||
//
|
||||
// grab client PID and hwnd.
|
||||
//
|
||||
ProcessMsg((HWND) wParam, (PRUint32) cd->dwData, &msg);
|
||||
}
|
||||
else
|
||||
LOG(("ignoring malformed message\n"));
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
if (uMsg == IPC_WM_SENDMSG) {
|
||||
HWND hWndDest = (HWND) wParam;
|
||||
ipcMessage *msg = (ipcMessage *) lParam;
|
||||
|
||||
COPYDATASTRUCT cd;
|
||||
cd.dwData = GetCurrentProcessId();
|
||||
cd.cbData = (DWORD) msg->MsgLen();
|
||||
cd.lpData = (PVOID) msg->MsgBuf();
|
||||
|
||||
LOG(("calling SendMessage...\n"));
|
||||
SendMessage(hWndDest, WM_COPYDATA, (WPARAM) hWnd, (LPARAM) &cd);
|
||||
LOG((" done.\n"));
|
||||
|
||||
delete msg;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (uMsg == WM_TIMER) {
|
||||
PurgeStaleClients();
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if 0
|
||||
if (uMsg == IPC_WM_SHUTDOWN) {
|
||||
//
|
||||
// since this message is handled asynchronously, it is possible
|
||||
// that other clients may have come online since this was issued.
|
||||
// in which case, we need to ignore this message.
|
||||
//
|
||||
if (ipcClientCount == 0) {
|
||||
ipcShutdown = PR_TRUE;
|
||||
PostQuitMessage(0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
return DefWindowProc(hWnd, uMsg, wParam, lParam);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// daemon startup synchronization
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
static HANDLE ipcSyncEvent;
|
||||
|
||||
static PRBool
|
||||
AcquireLock()
|
||||
{
|
||||
ipcSyncEvent = CreateEvent(NULL, FALSE, FALSE,
|
||||
IPC_SYNC_EVENT_NAME);
|
||||
if (!ipcSyncEvent) {
|
||||
LOG(("CreateEvent failed [%u]\n", GetLastError()));
|
||||
return PR_FALSE;
|
||||
}
|
||||
|
||||
// check to see if event already existed prior to this call.
|
||||
if (GetLastError() == ERROR_ALREADY_EXISTS) {
|
||||
LOG((" lock already set; exiting...\n"));
|
||||
return PR_FALSE;
|
||||
}
|
||||
|
||||
LOG((" acquired lock\n"));
|
||||
return PR_TRUE;
|
||||
}
|
||||
|
||||
static void
|
||||
ReleaseLock()
|
||||
{
|
||||
if (ipcSyncEvent) {
|
||||
LOG(("releasing lock...\n"));
|
||||
CloseHandle(ipcSyncEvent);
|
||||
ipcSyncEvent = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// main
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifdef DEBUG
|
||||
int
|
||||
main()
|
||||
#else
|
||||
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
|
||||
#endif
|
||||
{
|
||||
IPC_InitLog("###");
|
||||
|
||||
LOG(("daemon started...\n"));
|
||||
|
||||
if (!AcquireLock()) {
|
||||
// unblock the parent; it should be able to find the IPC window of the
|
||||
// other daemon process.
|
||||
IPC_NotifyParent();
|
||||
return 0;
|
||||
}
|
||||
|
||||
// initialize global data
|
||||
memset(ipcClientArray, 0, sizeof(ipcClientArray));
|
||||
ipcClients = ipcClientArray;
|
||||
ipcClientCount = 0;
|
||||
|
||||
// create message window up front...
|
||||
WNDCLASS wc;
|
||||
memset(&wc, 0, sizeof(wc));
|
||||
wc.lpfnWndProc = WindowProc;
|
||||
wc.lpszClassName = IPC_WINDOW_CLASS;
|
||||
|
||||
RegisterClass(&wc);
|
||||
|
||||
ipcHwnd = CreateWindow(IPC_WINDOW_CLASS, IPC_WINDOW_NAME,
|
||||
0, 0, 0, 10, 10, NULL, NULL, NULL, NULL);
|
||||
|
||||
// unblock the parent process; it should now look for the IPC window.
|
||||
IPC_NotifyParent();
|
||||
|
||||
if (!ipcHwnd)
|
||||
return -1;
|
||||
|
||||
// load modules relative to the location of the executable...
|
||||
{
|
||||
char path[MAX_PATH];
|
||||
GetModuleFileName(NULL, path, sizeof(path));
|
||||
IPC_InitModuleReg(path);
|
||||
}
|
||||
|
||||
LOG(("entering message loop...\n"));
|
||||
MSG msg;
|
||||
while (GetMessage(&msg, ipcHwnd, 0, 0))
|
||||
DispatchMessage(&msg);
|
||||
|
||||
// unload modules
|
||||
IPC_ShutdownModuleReg();
|
||||
|
||||
//
|
||||
// we release the daemon lock before destroying the window because the
|
||||
// absence of our window is what will cause clients to try to spawn the
|
||||
// daemon.
|
||||
//
|
||||
ReleaseLock();
|
||||
|
||||
//LOG(("sleeping 5 seconds...\n"));
|
||||
//PR_Sleep(PR_SecondsToInterval(5));
|
||||
|
||||
LOG(("destroying message window...\n"));
|
||||
DestroyWindow(ipcHwnd);
|
||||
ipcHwnd = NULL;
|
||||
|
||||
LOG(("exiting\n"));
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
Makefile
|
|
@ -0,0 +1,49 @@
|
|||
# vim: noexpandtab ts=4 sw=4
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
#
|
||||
# The contents of this file are subject to the Mozilla Public License Version
|
||||
# 1.1 (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
# http://www.mozilla.org/MPL/
|
||||
#
|
||||
# Software distributed under the License is distributed on an "AS IS" basis,
|
||||
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
# for the specific language governing rights and limitations under the
|
||||
# License.
|
||||
#
|
||||
# The Original Code is Mozilla IPC.
|
||||
#
|
||||
# The Initial Developer of the Original Code is
|
||||
# Netscape Communications Corporation.
|
||||
# Portions created by the Initial Developer are Copyright (C) 2002
|
||||
# the Initial Developer. All Rights Reserved.
|
||||
#
|
||||
# Contributor(s):
|
||||
# Darin Fisher <darin@netscape.com>
|
||||
#
|
||||
# Alternatively, the contents of this file may be used under the terms of
|
||||
# either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
# in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
# of those above. If you wish to allow use of your version of this file only
|
||||
# under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
# use your version of this file under the terms of the MPL, indicate your
|
||||
# decision by deleting the provisions above and replace them with the notice
|
||||
# and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
# the provisions above, a recipient may use your version of this file under
|
||||
# the terms of any one of the MPL, the GPL or the LGPL.
|
||||
#
|
||||
# ***** END LICENSE BLOCK *****
|
||||
|
||||
DEPTH = ../../..
|
||||
topsrcdir = @top_srcdir@
|
||||
srcdir = @srcdir@
|
||||
VPATH = @srcdir@
|
||||
|
||||
include $(DEPTH)/config/autoconf.mk
|
||||
|
||||
MODULE = ipcd
|
||||
DIRS = lock transmngr
|
||||
|
||||
include $(topsrcdir)/config/rules.mk
|
|
@ -0,0 +1 @@
|
|||
Makefile
|
|
@ -0,0 +1,48 @@
|
|||
# vim: noexpandtab ts=4 sw=4
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
#
|
||||
# The contents of this file are subject to the Mozilla Public License Version
|
||||
# 1.1 (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
# http://www.mozilla.org/MPL/
|
||||
#
|
||||
# Software distributed under the License is distributed on an "AS IS" basis,
|
||||
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
# for the specific language governing rights and limitations under the
|
||||
# License.
|
||||
#
|
||||
# The Original Code is Mozilla IPC.
|
||||
#
|
||||
# The Initial Developer of the Original Code is
|
||||
# Netscape Communications Corporation.
|
||||
# Portions created by the Initial Developer are Copyright (C) 2002
|
||||
# the Initial Developer. All Rights Reserved.
|
||||
#
|
||||
# Contributor(s):
|
||||
# Darin Fisher <darin@netscape.com>
|
||||
#
|
||||
# Alternatively, the contents of this file may be used under the terms of
|
||||
# either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
# in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
# of those above. If you wish to allow use of your version of this file only
|
||||
# under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
# use your version of this file under the terms of the MPL, indicate your
|
||||
# decision by deleting the provisions above and replace them with the notice
|
||||
# and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
# the provisions above, a recipient may use your version of this file under
|
||||
# the terms of any one of the MPL, the GPL or the LGPL.
|
||||
#
|
||||
# ***** END LICENSE BLOCK *****
|
||||
|
||||
DEPTH = ../../../..
|
||||
topsrcdir = @top_srcdir@
|
||||
srcdir = @srcdir@
|
||||
VPATH = @srcdir@
|
||||
|
||||
include $(DEPTH)/config/autoconf.mk
|
||||
|
||||
DIRS = public src
|
||||
|
||||
include $(topsrcdir)/config/rules.mk
|
|
@ -0,0 +1 @@
|
|||
Makefile
|
|
@ -0,0 +1,54 @@
|
|||
# vim: noexpandtab ts=4 sw=4
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
#
|
||||
# The contents of this file are subject to the Mozilla Public License Version
|
||||
# 1.1 (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
# http://www.mozilla.org/MPL/
|
||||
#
|
||||
# Software distributed under the License is distributed on an "AS IS" basis,
|
||||
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
# for the specific language governing rights and limitations under the
|
||||
# License.
|
||||
#
|
||||
# The Original Code is Mozilla IPC.
|
||||
#
|
||||
# The Initial Developer of the Original Code is
|
||||
# Netscape Communications Corporation.
|
||||
# Portions created by the Initial Developer are Copyright (C) 2002
|
||||
# the Initial Developer. All Rights Reserved.
|
||||
#
|
||||
# Contributor(s):
|
||||
# Darin Fisher <darin@netscape.com>
|
||||
#
|
||||
# Alternatively, the contents of this file may be used under the terms of
|
||||
# either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
# in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
# of those above. If you wish to allow use of your version of this file only
|
||||
# under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
# use your version of this file under the terms of the MPL, indicate your
|
||||
# decision by deleting the provisions above and replace them with the notice
|
||||
# and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
# the provisions above, a recipient may use your version of this file under
|
||||
# the terms of any one of the MPL, the GPL or the LGPL.
|
||||
#
|
||||
# ***** END LICENSE BLOCK *****
|
||||
|
||||
DEPTH = ../../../../..
|
||||
topsrcdir = @top_srcdir@
|
||||
srcdir = @srcdir@
|
||||
VPATH = @srcdir@
|
||||
|
||||
include $(DEPTH)/config/autoconf.mk
|
||||
|
||||
MODULE = ipcd
|
||||
|
||||
XPIDLSRCS = \
|
||||
ipcILockService.idl \
|
||||
ipcILockNotify.idl \
|
||||
$(NULL)
|
||||
|
||||
include $(topsrcdir)/config/rules.mk
|
||||
|
|
@ -0,0 +1,91 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include "nsISupports.idl"
|
||||
|
||||
interface ipcILockNotify;
|
||||
|
||||
/**
|
||||
* This service provides named interprocess locking with either synchronous
|
||||
* or asynchronous waiting.
|
||||
*/
|
||||
[scriptable, uuid(9f6dbe15-d851-4b00-912a-5ac0be88a409)]
|
||||
interface ipcILockService : nsISupports
|
||||
{
|
||||
/**
|
||||
* Call this method to acquire a named lock. Pass a notification handler
|
||||
* to be notified asynchronously when the lock is acquired. Otherwise,
|
||||
* this function will block until the lock is acquired.
|
||||
*
|
||||
* @param aLockName
|
||||
* specifies the name of the lock
|
||||
* @param aNotify
|
||||
* notification callback (NULL to synchronously acquire lock)
|
||||
* @param aWaitIfBusy
|
||||
* wait for the lock to become available; otherwise, fail if lock
|
||||
* is already held by some other process.
|
||||
*/
|
||||
void acquireLock(in string aLockName,
|
||||
in ipcILockNotify aNotify,
|
||||
in boolean aWaitIfBusy);
|
||||
|
||||
/**
|
||||
* Call this method to release a named lock. This method can be called
|
||||
* before OnAcquireLockComplete has been called, which will effectively
|
||||
* cancel the request to acquire the named lock. OnAcquireLockComplete
|
||||
* will not be called after a call to ReleaseLock.
|
||||
*
|
||||
* @param aLockName
|
||||
* specifies the name of the lock
|
||||
*/
|
||||
void releaseLock(in string aLockName);
|
||||
};
|
||||
|
||||
%{C++
|
||||
// singleton implementing ipcILockService
|
||||
#define IPC_LOCKSERVICE_CLASSNAME \
|
||||
"ipcLockService"
|
||||
#define IPC_LOCKSERVICE_CONTRACTID \
|
||||
"@mozilla.org/ipc/lock-service;1"
|
||||
#define IPC_LOCKSERVICE_CID \
|
||||
{ /* d9e56bf8-e32e-4b6d-87f1-06d73b0ce7ca */ \
|
||||
0xd9e56bf8, \
|
||||
0xe32e, \
|
||||
0x4b6d, \
|
||||
{0x87, 0xf1, 0x06, 0xd7, 0x3b, 0x0c, 0xe7, 0xca} \
|
||||
}
|
||||
%}
|
|
@ -0,0 +1,53 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#ifndef ipcLockCID_h__
|
||||
#define ipcLockCID_h__
|
||||
|
||||
#define IPC_LOCKSERVICE_CLASSNAME \
|
||||
"ipcLockService"
|
||||
#define IPC_LOCKSERVICE_CONTRACTID \
|
||||
"@mozilla.org/ipc/lock-service;1"
|
||||
#define IPC_LOCKSERVICE_CID \
|
||||
{ /* d9e56bf8-e32e-4b6d-87f1-06d73b0ce7ca */ \
|
||||
0xd9e56bf8, \
|
||||
0xe32e, \
|
||||
0x4b6d, \
|
||||
{0x87, 0xf1, 0x06, 0xd7, 0x3b, 0x0c, 0xe7, 0xca} \
|
||||
}
|
||||
|
||||
#endif // !ipcLockCID_h__
|
|
@ -0,0 +1 @@
|
|||
Makefile
|
|
@ -0,0 +1,69 @@
|
|||
# vim: noexpandtab ts=4 sw=4
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
#
|
||||
# The contents of this file are subject to the Mozilla Public License Version
|
||||
# 1.1 (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
# http://www.mozilla.org/MPL/
|
||||
#
|
||||
# Software distributed under the License is distributed on an "AS IS" basis,
|
||||
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
# for the specific language governing rights and limitations under the
|
||||
# License.
|
||||
#
|
||||
# The Original Code is Mozilla IPC.
|
||||
#
|
||||
# The Initial Developer of the Original Code is
|
||||
# Netscape Communications Corporation.
|
||||
# Portions created by the Initial Developer are Copyright (C) 2002
|
||||
# the Initial Developer. All Rights Reserved.
|
||||
#
|
||||
# Contributor(s):
|
||||
# Darin Fisher <darin@netscape.com>
|
||||
#
|
||||
# Alternatively, the contents of this file may be used under the terms of
|
||||
# either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
# in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
# of those above. If you wish to allow use of your version of this file only
|
||||
# under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
# use your version of this file under the terms of the MPL, indicate your
|
||||
# decision by deleting the provisions above and replace them with the notice
|
||||
# and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
# the provisions above, a recipient may use your version of this file under
|
||||
# the terms of any one of the MPL, the GPL or the LGPL.
|
||||
#
|
||||
# ***** END LICENSE BLOCK *****
|
||||
|
||||
DEPTH = ../../../../..
|
||||
topsrcdir = @top_srcdir@
|
||||
srcdir = @srcdir@
|
||||
VPATH = @srcdir@
|
||||
|
||||
include $(DEPTH)/config/autoconf.mk
|
||||
|
||||
DIRS = module
|
||||
|
||||
MODULE = ipcd
|
||||
LIBRARY_NAME = ipcdlock_s
|
||||
FORCE_STATIC_LIB = 1
|
||||
MODULE_NAME = ipcd
|
||||
|
||||
FORCE_USE_PIC = 1
|
||||
|
||||
REQUIRES = \
|
||||
xpcom \
|
||||
string \
|
||||
$(NULL)
|
||||
|
||||
CPPSRCS = \
|
||||
ipcLockProtocol.cpp \
|
||||
ipcLockService.cpp \
|
||||
$(NULL)
|
||||
|
||||
LOCAL_INCLUDES = \
|
||||
-I$(srcdir)/../../../shared/src \
|
||||
$(NULL)
|
||||
|
||||
include $(topsrcdir)/config/rules.mk
|
|
@ -0,0 +1,87 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "prlog.h"
|
||||
#include "ipcLockProtocol.h"
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
static inline PRUint8 get_opcode(const PRUint8 *buf)
|
||||
{
|
||||
return (buf[0] & 0x0f);
|
||||
}
|
||||
|
||||
static inline PRUint8 get_flags(const PRUint8 *buf)
|
||||
{
|
||||
return (buf[0] & 0xf0) >> 4;
|
||||
}
|
||||
|
||||
static inline const char *get_key(const PRUint8 *buf)
|
||||
{
|
||||
return ((const char *) buf) + 1;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
PRUint8 *
|
||||
IPC_FlattenLockMsg(const ipcLockMsg *msg, PRUint32 *bufLen)
|
||||
{
|
||||
PRUint32 len = 1 // header byte
|
||||
+ strlen(msg->key) // key
|
||||
+ 1; // null terminator
|
||||
|
||||
PRUint8 *buf = (PRUint8 *) malloc(len);
|
||||
if (!buf)
|
||||
return NULL;
|
||||
|
||||
buf[0] = (msg->opcode | (msg->flags << 4));
|
||||
|
||||
memcpy(&buf[1], msg->key, len - 1);
|
||||
*bufLen = len;
|
||||
return buf;
|
||||
}
|
||||
|
||||
void
|
||||
IPC_UnflattenLockMsg(const PRUint8 *buf, PRUint32 bufLen, ipcLockMsg *msg)
|
||||
{
|
||||
PR_ASSERT(bufLen > 2); // malformed buffer otherwise
|
||||
msg->opcode = get_opcode(buf);
|
||||
msg->flags = get_flags(buf);
|
||||
msg->key = get_key(buf);
|
||||
}
|
|
@ -0,0 +1,97 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#ifndef ipcLockProtocol_h__
|
||||
#define ipcLockProtocol_h__
|
||||
|
||||
#include "prtypes.h"
|
||||
|
||||
//
|
||||
// ipc lock message format:
|
||||
//
|
||||
// +----------------------------------+
|
||||
// | opcode : 4 bits |
|
||||
// +----------------------------------+
|
||||
// | flags : 4 bits |
|
||||
// +----------------------------------+
|
||||
// | key : null terminated string |
|
||||
// +----------------------------------+
|
||||
//
|
||||
|
||||
// lock opcodes
|
||||
#define IPC_LOCK_OP_ACQUIRE 1
|
||||
#define IPC_LOCK_OP_RELEASE 2
|
||||
#define IPC_LOCK_OP_STATUS_ACQUIRED 3
|
||||
#define IPC_LOCK_OP_STATUS_FAILED 4
|
||||
#define IPC_LOCK_OP_STATUS_BUSY 5
|
||||
|
||||
// lock flags
|
||||
#define IPC_LOCK_FL_NONBLOCKING 1
|
||||
|
||||
// data structure for representing lock request message
|
||||
struct ipcLockMsg
|
||||
{
|
||||
PRUint8 opcode;
|
||||
PRUint8 flags;
|
||||
const char * key;
|
||||
};
|
||||
|
||||
//
|
||||
// flatten a lock message
|
||||
//
|
||||
// returns a malloc'd buffer containing the flattened message. on return,
|
||||
// bufLen contains the length of the flattened message.
|
||||
//
|
||||
PRUint8 *IPC_FlattenLockMsg(const ipcLockMsg *msg, PRUint32 *bufLen);
|
||||
|
||||
//
|
||||
// unflatten a lock message
|
||||
//
|
||||
void IPC_UnflattenLockMsg(const PRUint8 *buf, PRUint32 bufLen, ipcLockMsg *msg);
|
||||
|
||||
//
|
||||
// TargetID for message passing
|
||||
//
|
||||
#define IPC_LOCK_TARGETID \
|
||||
{ /* 703ada8a-2d38-4d5d-9d39-03d1ccceb567 */ \
|
||||
0x703ada8a, \
|
||||
0x2d38, \
|
||||
0x4d5d, \
|
||||
{0x9d, 0x39, 0x03, 0xd1, 0xcc, 0xce, 0xb5, 0x67} \
|
||||
}
|
||||
|
||||
#endif // !ipcLockProtocol_h__
|
|
@ -0,0 +1,154 @@
|
|||
#include <stdlib.h>
|
||||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include "nsIServiceManager.h"
|
||||
#include "ipcILockNotify.h"
|
||||
#include "ipcLockService.h"
|
||||
#include "ipcLockProtocol.h"
|
||||
#include "ipcLog.h"
|
||||
|
||||
static NS_DEFINE_IID(kIPCServiceCID, IPC_SERVICE_CID);
|
||||
static const nsID kLockTargetID = IPC_LOCK_TARGETID;
|
||||
|
||||
ipcLockService::ipcLockService()
|
||||
{
|
||||
}
|
||||
|
||||
ipcLockService::~ipcLockService()
|
||||
{
|
||||
}
|
||||
|
||||
nsresult
|
||||
ipcLockService::Init()
|
||||
{
|
||||
nsresult rv;
|
||||
|
||||
mIPCService = do_GetService(kIPCServiceCID, &rv);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
return mIPCService->SetMessageObserver(kLockTargetID, this);
|
||||
}
|
||||
|
||||
NS_IMPL_ISUPPORTS1(ipcLockService, ipcILockService)
|
||||
|
||||
NS_IMETHODIMP
|
||||
ipcLockService::AcquireLock(const char *lockName, ipcILockNotify *notify, PRBool waitIfBusy)
|
||||
{
|
||||
LOG(("ipcLockService::AcquireLock [lock=%s sync=%u wait=%u]\n",
|
||||
lockName, notify == nsnull, waitIfBusy));
|
||||
|
||||
ipcLockMsg msg;
|
||||
msg.opcode = IPC_LOCK_OP_ACQUIRE;
|
||||
msg.flags = (waitIfBusy ? 0 : IPC_LOCK_FL_NONBLOCKING);
|
||||
msg.key = lockName;
|
||||
|
||||
PRUint32 bufLen;
|
||||
PRUint8 *buf = IPC_FlattenLockMsg(&msg, &bufLen);
|
||||
if (!buf)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
|
||||
nsresult rv = mIPCService->SendMessage(0, kLockTargetID, buf, bufLen, (notify == nsnull));
|
||||
free(buf);
|
||||
if (NS_FAILED(rv)) {
|
||||
LOG((" SendMessage failed [rv=%x]\n", rv));
|
||||
return rv;
|
||||
}
|
||||
|
||||
if (notify) {
|
||||
nsCStringKey hashKey(lockName);
|
||||
mPendingTable.Put(&hashKey, notify);
|
||||
}
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
ipcLockService::ReleaseLock(const char *lockName)
|
||||
{
|
||||
LOG(("ipcLockService::ReleaseLock [lock=%s]\n", lockName));
|
||||
|
||||
ipcLockMsg msg;
|
||||
msg.opcode = IPC_LOCK_OP_RELEASE;
|
||||
msg.flags = 0;
|
||||
msg.key = lockName;
|
||||
|
||||
PRUint32 bufLen;
|
||||
PRUint8 *buf = IPC_FlattenLockMsg(&msg, &bufLen);
|
||||
if (!buf)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
|
||||
nsresult rv = mIPCService->SendMessage(0, kLockTargetID, buf, bufLen, PR_FALSE);
|
||||
free(buf);
|
||||
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
nsCStringKey hashKey(lockName);
|
||||
mPendingTable.Remove(&hashKey);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
ipcLockService::OnMessageAvailable(const nsID &target, const PRUint8 *data, PRUint32 dataLen)
|
||||
{
|
||||
ipcLockMsg msg;
|
||||
IPC_UnflattenLockMsg(data, dataLen, &msg);
|
||||
|
||||
LOG(("ipcLockService::OnMessageAvailable [lock=%s opcode=%u]\n", msg.key, msg.opcode));
|
||||
|
||||
nsresult status;
|
||||
if (msg.opcode == IPC_LOCK_OP_STATUS_ACQUIRED)
|
||||
status = NS_OK;
|
||||
else
|
||||
status = NS_ERROR_FAILURE;
|
||||
|
||||
NotifyComplete(msg.key, status);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
void
|
||||
ipcLockService::NotifyComplete(const char *lockName, nsresult status)
|
||||
{
|
||||
nsCStringKey hashKey(lockName);
|
||||
nsISupports *obj = mPendingTable.Get(&hashKey); // ADDREFS
|
||||
if (obj) {
|
||||
nsCOMPtr<ipcILockNotify> notify = do_QueryInterface(obj);
|
||||
NS_RELEASE(obj);
|
||||
if (notify)
|
||||
notify->OnAcquireLockComplete(lockName, status);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#ifndef ipcLockService_h__
|
||||
#define ipcLockService_h__
|
||||
|
||||
#include "ipcILockService.h"
|
||||
#include "ipcIService.h"
|
||||
#include "ipcIMessageObserver.h"
|
||||
#include "ipcList.h"
|
||||
#include "nsCOMPtr.h"
|
||||
#include "nsHashtable.h"
|
||||
|
||||
class ipcLockService : public ipcILockService
|
||||
, public ipcIMessageObserver
|
||||
{
|
||||
public:
|
||||
NS_DECL_ISUPPORTS
|
||||
NS_DECL_IPCILOCKSERVICE
|
||||
NS_DECL_IPCIMESSAGEOBSERVER
|
||||
|
||||
ipcLockService();
|
||||
virtual ~ipcLockService();
|
||||
|
||||
nsresult Init();
|
||||
|
||||
private:
|
||||
void NotifyComplete(const char *lockName, nsresult status);
|
||||
|
||||
nsCOMPtr<ipcIService> mIPCService;
|
||||
|
||||
// map from lockname to locknotify for pending notifications
|
||||
nsSupportsHashtable mPendingTable;
|
||||
};
|
||||
|
||||
#endif // !ipcLockService_h__
|
|
@ -0,0 +1 @@
|
|||
Makefile
|
|
@ -0,0 +1,91 @@
|
|||
# vim: noexpandtab ts=4 sw=4
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
#
|
||||
# The contents of this file are subject to the Mozilla Public License Version
|
||||
# 1.1 (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
# http://www.mozilla.org/MPL/
|
||||
#
|
||||
# Software distributed under the License is distributed on an "AS IS" basis,
|
||||
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
# for the specific language governing rights and limitations under the
|
||||
# License.
|
||||
#
|
||||
# The Original Code is Mozilla IPC.
|
||||
#
|
||||
# The Initial Developer of the Original Code is
|
||||
# Netscape Communications Corporation.
|
||||
# Portions created by the Initial Developer are Copyright (C) 2002
|
||||
# the Initial Developer. All Rights Reserved.
|
||||
#
|
||||
# Contributor(s):
|
||||
# Darin Fisher <darin@netscape.com>
|
||||
#
|
||||
# Alternatively, the contents of this file may be used under the terms of
|
||||
# either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
# in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
# of those above. If you wish to allow use of your version of this file only
|
||||
# under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
# use your version of this file under the terms of the MPL, indicate your
|
||||
# decision by deleting the provisions above and replace them with the notice
|
||||
# and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
# the provisions above, a recipient may use your version of this file under
|
||||
# the terms of any one of the MPL, the GPL or the LGPL.
|
||||
#
|
||||
# ***** END LICENSE BLOCK *****
|
||||
|
||||
DEPTH = ../../../../../..
|
||||
topsrcdir = @top_srcdir@
|
||||
srcdir = @srcdir@
|
||||
VPATH = @srcdir@
|
||||
|
||||
include $(DEPTH)/config/autoconf.mk
|
||||
|
||||
MODULE = ipcd
|
||||
LIBRARY_NAME = lockmodule
|
||||
ifeq ($(OS_ARCH), OS2)
|
||||
SHORT_LIBNAME = lockmod
|
||||
endif
|
||||
MODULE_NAME = ipcd
|
||||
|
||||
FORCE_SHARED_LIB = 1
|
||||
NO_DIST_INSTALL = 1
|
||||
|
||||
ifeq ($(OS_ARCH),Darwin)
|
||||
NO_COMPONENT_LINK_MAP = 1
|
||||
MOZ_COMPONENTS_VERSION_SCRIPT_LDFLAGS =
|
||||
endif
|
||||
|
||||
# required for #include "nsID.h"
|
||||
REQUIRES = \
|
||||
xpcom \
|
||||
$(NULL)
|
||||
|
||||
CPPSRCS = ipcLockModule.cpp
|
||||
|
||||
LOCAL_INCLUDES = \
|
||||
-I$(srcdir)/.. \
|
||||
$(NULL)
|
||||
|
||||
EXTRA_OBJS = ../ipcLockProtocol.$(OBJ_SUFFIX)
|
||||
|
||||
EXTRA_DSO_LDOPTS = \
|
||||
$(LIBS_DIR) \
|
||||
$(NSPR_LIBS) \
|
||||
$(EXTRA_DSO_LIBS) \
|
||||
$(EXTRA_OBJS) \
|
||||
$(NULL)
|
||||
|
||||
include $(topsrcdir)/config/rules.mk
|
||||
|
||||
_IPC_FILES = \
|
||||
$(DLL_PREFIX)$(LIBRARY_NAME)$(DLL_SUFFIX) \
|
||||
$(NULL)
|
||||
|
||||
libs:: $(_IPC_FILES)
|
||||
$(INSTALL) $^ $(DIST)/bin/ipc/modules
|
||||
|
||||
install:: $(_IPC_FILES)
|
||||
$(SYSINSTALL) $(IFLAGS1) $^ $(DESTDIR)$(mozappdir)/ipc/modules
|
|
@ -0,0 +1,275 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "ipcModuleUtil.h"
|
||||
#include "ipcLockProtocol.h"
|
||||
#include "plhash.h"
|
||||
|
||||
static const nsID kLockTargetID = IPC_LOCK_TARGETID;
|
||||
|
||||
static void
|
||||
ipcLockModule_Send(PRUint32 cid, const char *key, PRUint8 opcode)
|
||||
{
|
||||
ipcLockMsg msg = { opcode, 0, key };
|
||||
PRUint32 bufLen;
|
||||
PRUint8 *buf = IPC_FlattenLockMsg(&msg, &bufLen);
|
||||
if (!buf)
|
||||
return;
|
||||
IPC_SendMsg(cid, kLockTargetID, buf, bufLen);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
//
|
||||
// gLockTable stores mapping from lock name to ipcLockContext
|
||||
//
|
||||
static PLHashTable *gLockTable = NULL;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
struct ipcLockContext
|
||||
{
|
||||
PRUint32 mOwnerID; // client ID of this lock's owner
|
||||
struct ipcLockContext *mNextPending; // pointer to client next in line to
|
||||
// acquire this lock.
|
||||
|
||||
ipcLockContext(PRUint32 ownerID)
|
||||
: mOwnerID(ownerID)
|
||||
, mNextPending(NULL) {}
|
||||
};
|
||||
|
||||
static void
|
||||
ipcLockModule_AcquireLock(PRUint32 cid, PRUint8 flags, const char *key)
|
||||
{
|
||||
printf("$$$ acquiring lock [key=%s]\n", key);
|
||||
|
||||
if (!gLockTable)
|
||||
return;
|
||||
|
||||
ipcLockContext *ctx;
|
||||
|
||||
ctx = (ipcLockContext *) PL_HashTableLookup(gLockTable, key);
|
||||
if (ctx) {
|
||||
//
|
||||
// lock is already acquired, add this client to the queue. make
|
||||
// sure this client doesn't already own the lock or live on the queue.
|
||||
//
|
||||
while (ctx->mOwnerID != cid && ctx->mNextPending)
|
||||
ctx = ctx->mNextPending;
|
||||
if (ctx->mOwnerID != cid) {
|
||||
//
|
||||
// if nonblocking, then send busy status message. otherwise,
|
||||
// proceed to add this client to the pending queue.
|
||||
//
|
||||
if (flags & IPC_LOCK_FL_NONBLOCKING)
|
||||
ipcLockModule_Send(cid, key, IPC_LOCK_OP_STATUS_BUSY);
|
||||
else
|
||||
ctx->mNextPending = new ipcLockContext(cid);
|
||||
}
|
||||
}
|
||||
else {
|
||||
//
|
||||
// ok, add this lock to the table, and notify client that it now owns
|
||||
// the lock!
|
||||
//
|
||||
ctx = new ipcLockContext(cid);
|
||||
if (!ctx)
|
||||
return;
|
||||
|
||||
PL_HashTableAdd(gLockTable, key, ctx);
|
||||
|
||||
ipcLockModule_Send(cid, key, IPC_LOCK_OP_STATUS_ACQUIRED);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
ipcLockModule_ReleaseLock(PRUint32 cid, const char *key)
|
||||
{
|
||||
printf("$$$ releasing lock [key=%s]\n", key);
|
||||
|
||||
if (!gLockTable)
|
||||
return;
|
||||
|
||||
ipcLockContext *ctx;
|
||||
|
||||
ctx = (ipcLockContext *) PL_HashTableLookup(gLockTable, key);
|
||||
if (ctx) {
|
||||
//
|
||||
// lock is already acquired _or_ maybe client is on the pending list.
|
||||
//
|
||||
if (ctx->mOwnerID == cid) {
|
||||
if (ctx->mNextPending) {
|
||||
//
|
||||
// remove this element from the list. since this is the
|
||||
// first element in the list, instead of removing it we
|
||||
// shift the data from the next context into this one and
|
||||
// delete the next context.
|
||||
//
|
||||
ipcLockContext *next = ctx->mNextPending;
|
||||
ctx->mOwnerID = next->mOwnerID;
|
||||
ctx->mNextPending = next->mNextPending;
|
||||
delete next;
|
||||
//
|
||||
// notify client that it now owns the lock
|
||||
//
|
||||
ipcLockModule_Send(ctx->mOwnerID, key, IPC_LOCK_OP_STATUS_ACQUIRED);
|
||||
}
|
||||
else {
|
||||
delete ctx;
|
||||
PL_HashTableRemove(gLockTable, key);
|
||||
}
|
||||
}
|
||||
else {
|
||||
ipcLockContext *prev;
|
||||
for (;;) {
|
||||
prev = ctx;
|
||||
ctx = ctx->mNextPending;
|
||||
if (!ctx)
|
||||
break;
|
||||
if (ctx->mOwnerID == cid) {
|
||||
// remove ctx from list
|
||||
prev->mNextPending = ctx->mNextPending;
|
||||
delete ctx;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PR_STATIC_CALLBACK(PRIntn)
|
||||
ipcLockModule_ReleaseByCID(PLHashEntry *he, PRIntn i, void *arg)
|
||||
{
|
||||
PRUint32 cid = *(PRUint32 *) arg;
|
||||
ipcLockModule_ReleaseLock(cid, (const char *) he->key);
|
||||
return HT_ENUMERATE_NEXT;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
static void
|
||||
ipcLockModule_Init()
|
||||
{
|
||||
printf("$$$ ipcLockModule_Init\n");
|
||||
|
||||
gLockTable = PL_NewHashTable(32,
|
||||
PL_HashString,
|
||||
PL_CompareStrings,
|
||||
PL_CompareValues,
|
||||
NULL,
|
||||
NULL);
|
||||
}
|
||||
|
||||
static void
|
||||
ipcLockModule_Shutdown()
|
||||
{
|
||||
printf("$$$ ipcLockModule_Shutdown\n");
|
||||
|
||||
if (gLockTable) {
|
||||
// XXX walk table destroying all ipcLockContext objects
|
||||
|
||||
PL_HashTableDestroy(gLockTable);
|
||||
gLockTable = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
ipcLockModule_HandleMsg(ipcClientHandle client,
|
||||
const nsID &target,
|
||||
const void *data,
|
||||
PRUint32 dataLen)
|
||||
{
|
||||
PRUint32 cid = IPC_GetClientID(client);
|
||||
|
||||
printf("$$$ ipcLockModule_HandleMsg [cid=%u]\n", cid);
|
||||
|
||||
ipcLockMsg msg;
|
||||
IPC_UnflattenLockMsg((const PRUint8 *) data, dataLen, &msg);
|
||||
|
||||
switch (msg.opcode) {
|
||||
case IPC_LOCK_OP_ACQUIRE:
|
||||
ipcLockModule_AcquireLock(cid, msg.flags, msg.key);
|
||||
break;
|
||||
case IPC_LOCK_OP_RELEASE:
|
||||
ipcLockModule_ReleaseLock(cid, msg.key);
|
||||
break;
|
||||
default:
|
||||
PR_NOT_REACHED("invalid opcode");
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
ipcLockModule_ClientUp(ipcClientHandle client)
|
||||
{
|
||||
printf("$$$ ipcLockModule_ClientUp [%u]\n", IPC_GetClientID(client));
|
||||
}
|
||||
|
||||
static void
|
||||
ipcLockModule_ClientDown(ipcClientHandle client)
|
||||
{
|
||||
PRUint32 cid = IPC_GetClientID(client);
|
||||
|
||||
printf("$$$ ipcLockModule_ClientDown [%u]\n", cid);
|
||||
|
||||
//
|
||||
// enumerate lock table, release any locks held by this client.
|
||||
//
|
||||
|
||||
PL_HashTableEnumerateEntries(gLockTable, ipcLockModule_ReleaseByCID, &cid);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
static ipcModuleMethods gLockMethods =
|
||||
{
|
||||
IPC_MODULE_METHODS_VERSION,
|
||||
ipcLockModule_Init,
|
||||
ipcLockModule_Shutdown,
|
||||
ipcLockModule_HandleMsg,
|
||||
ipcLockModule_ClientUp,
|
||||
ipcLockModule_ClientDown
|
||||
};
|
||||
|
||||
static ipcModuleEntry gLockModuleEntry[] =
|
||||
{
|
||||
{ IPC_LOCK_TARGETID, &gLockMethods }
|
||||
};
|
||||
|
||||
IPC_IMPL_GETMODULES(ipcLockModule, gLockModuleEntry)
|
|
@ -0,0 +1 @@
|
|||
Makefile
|
|
@ -0,0 +1,52 @@
|
|||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
#
|
||||
# The contents of this file are subject to the Mozilla Public License Version
|
||||
# 1.1 (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
# http://www.mozilla.org/MPL/
|
||||
#
|
||||
# Software distributed under the License is distributed on an "AS IS" basis,
|
||||
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
# for the specific language governing rights and limitations under the
|
||||
# License.
|
||||
#
|
||||
# The Original Code is Mozilla Transaction Manager.
|
||||
#
|
||||
# The Initial Developer of the Original Code is
|
||||
# Netscape Communications Corp.
|
||||
# Portions created by the Initial Developer are Copyright (C) 2003
|
||||
# the Initial Developer. All Rights Reserved.
|
||||
#
|
||||
# Contributor(s):
|
||||
# John Gaunt <jgaunt@netscape.com>
|
||||
#
|
||||
# Alternatively, the contents of this file may be used under the terms of
|
||||
# either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
# in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
# of those above. If you wish to allow use of your version of this file only
|
||||
# under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
# use your version of this file under the terms of the MPL, indicate your
|
||||
# decision by deleting the provisions above and replace them with the notice
|
||||
# and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
# the provisions above, a recipient may use your version of this file under
|
||||
# the terms of any one of the MPL, the GPL or the LGPL.
|
||||
#
|
||||
# ***** END LICENSE BLOCK *****
|
||||
|
||||
DEPTH = ../../../..
|
||||
topsrcdir = @top_srcdir@
|
||||
srcdir = @srcdir@
|
||||
VPATH = @srcdir@
|
||||
|
||||
include $(DEPTH)/config/autoconf.mk
|
||||
|
||||
DIRS = common module public src
|
||||
|
||||
ifdef ENABLE_TESTS
|
||||
DIRS += test
|
||||
endif
|
||||
|
||||
include $(topsrcdir)/config/rules.mk
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
Makefile
|
||||
module.rc
|
||||
module.res
|
||||
tmModule.obj
|
||||
transmngr_client.dll
|
||||
transmngr_client.pdb
|
||||
transmngr_client.exp
|
||||
transmngr_client.lib
|
||||
transmngr_client.ilk
|
|
@ -0,0 +1,75 @@
|
|||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
#
|
||||
# The contents of this file are subject to the Mozilla Public License Version
|
||||
# 1.1 (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
# http://www.mozilla.org/MPL/
|
||||
#
|
||||
# Software distributed under the License is distributed on an "AS IS" basis,
|
||||
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
# for the specific language governing rights and limitations under the
|
||||
# License.
|
||||
#
|
||||
# The Original Code is Mozilla Transaction Manager.
|
||||
#
|
||||
# The Initial Developer of the Original Code is
|
||||
# Netscape Communications Corp.
|
||||
# Portions created by the Initial Developer are Copyright (C) 2003
|
||||
# the Initial Developer. All Rights Reserved.
|
||||
#
|
||||
# Contributor(s):
|
||||
# John Gaunt <jgaunt@netscape.com>
|
||||
#
|
||||
# Alternatively, the contents of this file may be used under the terms of
|
||||
# either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
# in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
# of those above. If you wish to allow use of your version of this file only
|
||||
# under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
# use your version of this file under the terms of the MPL, indicate your
|
||||
# decision by deleting the provisions above and replace them with the notice
|
||||
# and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
# the provisions above, a recipient may use your version of this file under
|
||||
# the terms of any one of the MPL, the GPL or the LGPL.
|
||||
#
|
||||
# ***** END LICENSE BLOCK *****
|
||||
|
||||
DEPTH = ../../../../..
|
||||
topsrcdir = @top_srcdir@
|
||||
srcdir = @srcdir@
|
||||
VPATH = @srcdir@
|
||||
|
||||
include $(DEPTH)/config/autoconf.mk
|
||||
|
||||
MODULE = transmngr
|
||||
LIBRARY_NAME = transmngr_client
|
||||
EXPORT_LIBRARY = 1
|
||||
IS_COMPONENT = 1
|
||||
MODULE_NAME = transmngr
|
||||
|
||||
REQUIRES = ipcd \
|
||||
string \
|
||||
xpcom \
|
||||
$(NULL)
|
||||
|
||||
CPPSRCS = tmModule.cpp
|
||||
EXPORTS = tmCID.h
|
||||
|
||||
SHARED_LIBRARY_LIBS = \
|
||||
$(DIST)/lib/$(LIB_PREFIX)transmngr_s.$(LIB_SUFFIX) \
|
||||
$(DIST)/lib/$(LIB_PREFIX)transmngrcom_s.$(LIB_SUFFIX) \
|
||||
$(NULL)
|
||||
|
||||
LOCAL_INCLUDES = \
|
||||
-I$(srcdir)/../src \
|
||||
-I$(srcdir)/../common \
|
||||
$(NULL)
|
||||
|
||||
EXTRA_DSO_LDOPTS = \
|
||||
$(LIBS_DIR) \
|
||||
$(EXTRA_DSO_LIBS) \
|
||||
$(MOZ_COMPONENT_LIBS) \
|
||||
$(NULL)
|
||||
|
||||
include $(topsrcdir)/config/rules.mk
|
|
@ -0,0 +1,53 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla Transaction Manager.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corp.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2003
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* John Gaunt <jgaunt@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#ifndef tmCID_h__
|
||||
#define tmCID_h__
|
||||
|
||||
#define TRANSACTION_SERVICE_CLASSNAME \
|
||||
"tmTransactionService"
|
||||
#define TRANSACTION_SERVICE_CONTRACTID \
|
||||
"@mozilla.org/transaction/service;1"
|
||||
#define TRANSACTION_SERVICE_CID \
|
||||
{ /* 1403adf4-94d1-4c67-a8ae-d9f86972d378 */ \
|
||||
0x1403adf4, \
|
||||
0x94d1, \
|
||||
0x4c67, \
|
||||
{0xa8, 0xae, 0xd9, 0xf8, 0x69, 0x72, 0xd3, 0x78} \
|
||||
}
|
||||
|
||||
#endif // !tmCID_h__
|
|
@ -0,0 +1,71 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla Transaction Manager.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corp.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2003
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* John Gaunt <jgaunt@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include "nsICategoryManager.h"
|
||||
#include "nsIGenericFactory.h"
|
||||
#include "nsIServiceManager.h"
|
||||
#include "tmCID.h"
|
||||
#include "tmTransactionService.h"
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Define the contructor function for the objects
|
||||
//
|
||||
// NOTE: This creates an instance of objects by using the default constructor
|
||||
//-----------------------------------------------------------------------------
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(tmTransactionService)
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Define a table of CIDs implemented by this module along with other
|
||||
// information like the function to create an instance, contractid, and
|
||||
// class name.
|
||||
//-----------------------------------------------------------------------------
|
||||
static const nsModuleComponentInfo components[] = {
|
||||
{ TRANSACTION_SERVICE_CLASSNAME,
|
||||
TRANSACTION_SERVICE_CID,
|
||||
TRANSACTION_SERVICE_CONTRACTID,
|
||||
tmTransactionServiceConstructor },
|
||||
/*
|
||||
tmTransactionServiceRegisterProc,
|
||||
tmTransactionServiceUnregisterProc },
|
||||
*/
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Implement the NSGetModule() exported function for your module
|
||||
// and the entire implementation of the module object.
|
||||
//-----------------------------------------------------------------------------
|
||||
NS_IMPL_NSGETMODULE(transmngr, components)
|
|
@ -0,0 +1,5 @@
|
|||
Makefile
|
||||
tmTransaction.obj
|
||||
tmVector.obj
|
||||
transmngrcom_s.pdb
|
||||
transmngrcom_s.lib
|
|
@ -0,0 +1,61 @@
|
|||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
#
|
||||
# The contents of this file are subject to the Mozilla Public License Version
|
||||
# 1.1 (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
# http://www.mozilla.org/MPL/
|
||||
#
|
||||
# Software distributed under the License is distributed on an "AS IS" basis,
|
||||
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
# for the specific language governing rights and limitations under the
|
||||
# License.
|
||||
#
|
||||
# The Original Code is Mozilla Transaction Manager.
|
||||
#
|
||||
# The Initial Developer of the Original Code is
|
||||
# Netscape Communications Corp.
|
||||
# Portions created by the Initial Developer are Copyright (C) 2003
|
||||
# the Initial Developer. All Rights Reserved.
|
||||
#
|
||||
# Contributor(s):
|
||||
# John Gaunt <jgaunt@netscape.com>
|
||||
#
|
||||
# Alternatively, the contents of this file may be used under the terms of
|
||||
# either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
# in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
# of those above. If you wish to allow use of your version of this file only
|
||||
# under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
# use your version of this file under the terms of the MPL, indicate your
|
||||
# decision by deleting the provisions above and replace them with the notice
|
||||
# and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
# the provisions above, a recipient may use your version of this file under
|
||||
# the terms of any one of the MPL, the GPL or the LGPL.
|
||||
#
|
||||
# ***** END LICENSE BLOCK *****
|
||||
|
||||
DEPTH = ../../../../..
|
||||
topsrcdir = @top_srcdir@
|
||||
srcdir = @srcdir@
|
||||
VPATH = @srcdir@
|
||||
|
||||
include $(DEPTH)/config/autoconf.mk
|
||||
|
||||
MODULE = ipcd
|
||||
LIBRARY_NAME = transmngrcom_s
|
||||
FORCE_STATIC_LIB = 1
|
||||
MODULE_NAME = ipcd
|
||||
|
||||
FORCE_USE_PIC = 1
|
||||
|
||||
REQUIRES = \
|
||||
xpcom \
|
||||
$(NULL)
|
||||
|
||||
CPPSRCS = \
|
||||
tmTransaction.cpp \
|
||||
tmVector.cpp \
|
||||
$(NULL)
|
||||
|
||||
include $(topsrcdir)/config/rules.mk
|
|
@ -0,0 +1,92 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla Transaction Manager.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corp.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2003
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* John Gaunt <jgaunt@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "tmTransaction.h"
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Constructor(s) & Destructor
|
||||
|
||||
tmTransaction::~tmTransaction() {
|
||||
if (mHeader)
|
||||
free(mHeader);
|
||||
}
|
||||
|
||||
// call only once per lifetime of object. does not reclaim the
|
||||
// raw message, only sets it.
|
||||
nsresult
|
||||
tmTransaction::Init(PRUint32 aOwnerID,
|
||||
PRInt32 aQueueID,
|
||||
PRUint32 aAction,
|
||||
PRInt32 aStatus,
|
||||
const PRUint8 *aMessage,
|
||||
PRUint32 aLength) {
|
||||
nsresult rv = NS_OK;
|
||||
tmHeader *header = nsnull;
|
||||
|
||||
// indicates the message is the entire raw message
|
||||
if (aQueueID == TM_INVALID_ID) {
|
||||
header = (tmHeader*) malloc(aLength);
|
||||
if (header) {
|
||||
mRawMessageLength = aLength;
|
||||
memcpy(header, aMessage, aLength);
|
||||
}
|
||||
else
|
||||
rv = NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
else { // need to create the tmHeader and concat the message
|
||||
header = (tmHeader*) malloc (sizeof(tmHeader) + aLength);
|
||||
if (header) {
|
||||
mRawMessageLength = sizeof(tmHeader) + aLength;
|
||||
header->action = aAction;
|
||||
header->queueID = aQueueID;
|
||||
header->status = aStatus;
|
||||
header->reserved = 0x00000000;
|
||||
if (aLength > 0) // add the message if it exists
|
||||
memcpy((header + 1), aMessage, aLength);
|
||||
}
|
||||
else
|
||||
rv = NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
mOwnerID = aOwnerID;
|
||||
mHeader = header;
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
|
@ -0,0 +1,234 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla Transaction Manager.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corp.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2003
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* John Gaunt <jgaunt@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#ifndef _tmTransaction_H_
|
||||
#define _tmTransaction_H_
|
||||
|
||||
#include "tmUtils.h"
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Message format
|
||||
//
|
||||
// |------------------------------------|--
|
||||
// |QueueID | |
|
||||
// |------------------------------------| |
|
||||
// | Action - Post/Flush/Attach etc | |- this is the tmHeader struct
|
||||
// |------------------------------------| |
|
||||
// |Status | |
|
||||
// |------------------------------------| |
|
||||
// |Padding | |
|
||||
// |------------------------------------|--
|
||||
// |Message Data (payload) |
|
||||
// |------------------------------------|
|
||||
//
|
||||
// The Attach call is a special case in that it doesn't have a QueueID yet. A
|
||||
// QueueID will be 0's. The message Data will be the Queue Name String which
|
||||
// will be the profile name with a domain attached, a domain being
|
||||
// [prefs|cookies|etc]
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* tmHeader contains various flags identifying
|
||||
*/
|
||||
struct tmHeader {
|
||||
PRInt32 queueID; // will be the index of the queue in the TM, can be < 0
|
||||
PRUint32 action; // defined by tmUtils.h will be > 0
|
||||
PRInt32 status; // return values from methods, could be < 0
|
||||
PRUint32 reserved; // not currently used, maintaining word alignment
|
||||
};
|
||||
|
||||
/**
|
||||
* Using tmTransaction:
|
||||
*
|
||||
* After creating a tmTransaction either through new or as a member
|
||||
* or local variable a process must call Init() with the proper set of
|
||||
* arguements to initialize the state of the transaction. tmTransaction is
|
||||
* set up to accept 3 types of initialization.
|
||||
*
|
||||
* 1) Raw message - All data is carried in the byte pointer aMessage,
|
||||
* args 2,3,4 should be set to TM_NO_ID and aLength
|
||||
* must be set to the full length of aMessage, including null
|
||||
* termination if the payload is a null-term string and the size of the
|
||||
* tmHeader struct preceeding the message. Currently this
|
||||
* format is used at the IPC boundary, where we receive a byte pointer
|
||||
* from the IPC Daemon.
|
||||
*
|
||||
* 2) Flags only - aQueueID, aAction and aStatus are all set. aMessage
|
||||
* should be set to nsnull and aLength to 0. This format is used when
|
||||
* sending reply messages (except for ATTACH_REPLY) and when the TS
|
||||
* Transaction Service is sending "control" messages to the Manager -
|
||||
* flush, detach, etc...
|
||||
*
|
||||
* 3) Flags and message - All arguements are set. The aMessage is only
|
||||
* the message for the client app. aLength should be set to the length
|
||||
* of aMessage and not include the length of the tmHeader struct.
|
||||
*
|
||||
* The only data member you can set post initialization is the QueueID.
|
||||
* You should only call Init() once in the lifetime of a tmTransaction
|
||||
* as it doesn't clean up the exisiting data before assigning the new
|
||||
* data. Therefore it would leak most heinously if Init() were to be
|
||||
* called twice.
|
||||
*
|
||||
* mOwnerID only has relevance on the IPC daemon side of things. The
|
||||
* Transaction Service has no knowledge of this ID and makes no use
|
||||
* of it.
|
||||
*/
|
||||
class tmTransaction
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Constructor(s) & Destructor
|
||||
|
||||
tmTransaction(): mHeader(nsnull), mRawMessageLength(0), mOwnerID(0) { }
|
||||
|
||||
virtual ~tmTransaction();
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Public Member Functions
|
||||
|
||||
// Initializer ////////////
|
||||
|
||||
/**
|
||||
* Sets up the internal data of the transaction. Allows for 3 basic ways
|
||||
* to call this function: No flags and just one big raw message, Just
|
||||
* flags and no message, and finally flags and message. If the message
|
||||
* exists it is copied into the transaction.
|
||||
*
|
||||
* @param aOwnerID is given to us by the IPC Daemon and is specific
|
||||
* to that transport layer. It is only set when transactions
|
||||
* are sent from the TM to the TS.
|
||||
*
|
||||
* @param aQueueID is the either the destination queue, or the queue from
|
||||
* where this transaction is eminating
|
||||
*
|
||||
* @param aAction is the action that occured to generate this transaction
|
||||
*
|
||||
* @param aStatus is the success state of the action.
|
||||
*
|
||||
* @param aMessage can be a raw message including the 3 flags above or it
|
||||
* can be just the "payload" of the transaction that the destination
|
||||
* process is going deal with.
|
||||
*
|
||||
* @param aLength is the length of the message. If there is a null
|
||||
* terminated string in the message make sure the length includes
|
||||
* the null termination.
|
||||
*
|
||||
* @returns NS_OK if everything was successful
|
||||
* @returns NS_ERROR_OUT_OF_MEMORY if allocation of space for the
|
||||
* copy of the message fails.
|
||||
*/
|
||||
nsresult Init(PRUint32 aOwnerID,
|
||||
PRInt32 aQueueID,
|
||||
PRUint32 aAction,
|
||||
PRInt32 aStatus,
|
||||
const PRUint8 *aMessage,
|
||||
PRUint32 aLength);
|
||||
|
||||
// Data Accessors /////////
|
||||
|
||||
/**
|
||||
* @returns a const byte pointer to the message
|
||||
*/
|
||||
const PRUint8* GetMessage() const { return (PRUint8*)(mHeader + 1); }
|
||||
|
||||
/**
|
||||
* @returns the length of the message
|
||||
*/
|
||||
PRUint32 GetMessageLength() const {
|
||||
return (mRawMessageLength > sizeof(tmHeader)) ?
|
||||
(mRawMessageLength - sizeof(tmHeader)) : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns a const pointer to the memory containing the
|
||||
* flag information followed immediately by the message
|
||||
* data.
|
||||
*/
|
||||
const PRUint8* GetRawMessage() const { return (PRUint8*) mHeader; }
|
||||
|
||||
/**
|
||||
* @returns the length of the flags and message combined
|
||||
*/
|
||||
PRUint32 GetRawMessageLength() const { return mRawMessageLength; }
|
||||
|
||||
/**
|
||||
* @returns the id of the destination or sending queue, depending on the
|
||||
* direction of the transaction.
|
||||
*/
|
||||
PRInt32 GetQueueID() const { return mHeader->queueID; }
|
||||
|
||||
/**
|
||||
* @returns the action represented by this transaction
|
||||
*/
|
||||
PRUint32 GetAction() const { return mHeader->action; }
|
||||
|
||||
/**
|
||||
* @returns the success state, if applicable of the action leading
|
||||
* up to this message
|
||||
*/
|
||||
PRInt32 GetStatus() const { return mHeader->status; }
|
||||
|
||||
/**
|
||||
* @returns the client ID (in IPC daemon terms) of the client who initiated
|
||||
* the exchange that generated this transaction.
|
||||
*/
|
||||
PRUint32 GetOwnerID() const { return mOwnerID; }
|
||||
|
||||
// Data Mutator ///////////
|
||||
|
||||
/**
|
||||
* Sets the ID of the destination or source queue. Init should have been
|
||||
* called before the call to this function.
|
||||
*/
|
||||
void SetQueueID(PRInt32 aID) { mHeader->queueID = aID; }
|
||||
|
||||
protected:
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Protected Member Variables
|
||||
|
||||
tmHeader* mHeader; // points to beginning of entire message
|
||||
PRUint32 mRawMessageLength; // length of entire message, incl tmHeader
|
||||
PRUint32 mOwnerID; // client who sent this trans. - a IPC ClientID
|
||||
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,93 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla Transaction Manager.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corp.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2003
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* John Gaunt <jgaunt@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#ifndef _tmUtils_H_
|
||||
#define _tmUtils_H_
|
||||
|
||||
#include "nscore.h"
|
||||
#include "nsError.h"
|
||||
#include "nsID.h"
|
||||
#include "prlog.h"
|
||||
#include <stdio.h>
|
||||
|
||||
// UUID used to identify the Transaction Module in both daemon and client
|
||||
// not part of the XPCOM hooks, but rather a means of identifying
|
||||
// modules withing the IPC daemon.
|
||||
#define TRANSACTION_MODULE_ID \
|
||||
{ /* c3dfbcd5-f51d-420b-abf4-3bae445b96a9 */ \
|
||||
0xc3dfbcd5, \
|
||||
0xf51d, \
|
||||
0x420b, \
|
||||
{0xab, 0xf4, 0x3b, 0xae, 0x44, 0x5b, 0x96, 0xa9} \
|
||||
}
|
||||
|
||||
//static const nsID kTransModuleID = TRANSACTION_MODULE_ID;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// match NS_ERROR_FOO error code formats
|
||||
//
|
||||
// only create new errors for those errors that are specific to TM
|
||||
|
||||
#define NS_ERROR_MODULE_TM 27 /* XXX goes in nserror.h -- integrating with ns error codes */
|
||||
|
||||
#define TM_ERROR NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_TM, 1)
|
||||
#define TM_ERROR_WRONG_QUEUE NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_TM, 2)
|
||||
#define TM_ERROR_NOT_POSTED NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_TM, 3)
|
||||
#define TM_ERROR_QUEUE_EXISTS NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_TM, 4)
|
||||
#define TM_SUCCESS_DELETE_QUEUE NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_TM, 6)
|
||||
|
||||
|
||||
// XXX clean up:
|
||||
#define TM_INVALID_ID 0xFFFFFFFF
|
||||
#define TM_INVALID 0xFFFFFFFF
|
||||
#define TM_NO_ID 0xFFFFFFFE
|
||||
|
||||
// Transaction Actions
|
||||
enum {
|
||||
TM_ATTACH = 0,
|
||||
TM_ATTACH_REPLY,
|
||||
TM_POST,
|
||||
TM_POST_REPLY,
|
||||
TM_NOTIFY,
|
||||
TM_FLUSH,
|
||||
TM_FLUSH_REPLY,
|
||||
TM_DETACH,
|
||||
TM_DETACH_REPLY
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,160 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla Transaction Manager.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corp.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2003
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* John Gaunt <jgaunt@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "tmVector.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Constructor(s) & Destructor
|
||||
|
||||
// can not be responsible for reclaiming memory pointed to by the void*s in
|
||||
// the collection - how would we reclaim, don't know how they were allocated
|
||||
tmVector::~tmVector() {
|
||||
if (mElements)
|
||||
free((void*)mElements);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Public Member Functions
|
||||
|
||||
nsresult
|
||||
tmVector::Init() {
|
||||
|
||||
mElements = (void**) calloc (mCapacity, sizeof(void*));
|
||||
if (!mElements)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// mutators
|
||||
|
||||
PRInt32
|
||||
tmVector::Append(void *aElement){
|
||||
PR_ASSERT(aElement);
|
||||
|
||||
// make sure there is room
|
||||
if (mNext == mCapacity)
|
||||
if (NS_FAILED(Grow()))
|
||||
return -1;
|
||||
|
||||
// put the element in the array
|
||||
mElements[mNext] = aElement;
|
||||
mCount++;
|
||||
|
||||
// encapsulates the index into a success value
|
||||
return mNext++; // post increment.
|
||||
}
|
||||
|
||||
void
|
||||
tmVector::Remove(void *aElement) {
|
||||
PR_ASSERT(aElement);
|
||||
|
||||
for (PRUint32 index = 0; index < mNext; index++) {
|
||||
if (mElements[index] == aElement) {
|
||||
mElements[index] = nsnull;
|
||||
mCount--;
|
||||
if (index == mNext-1) { // if we removed the last element
|
||||
mNext--;
|
||||
// don't test for success of the shrink
|
||||
Shrink();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
tmVector::RemoveAt(PRUint32 aIndex) {
|
||||
PR_ASSERT(aIndex < mNext);
|
||||
|
||||
// remove the element if it isn't already nsnull
|
||||
if (mElements[aIndex] != nsnull) {
|
||||
mElements[aIndex] = nsnull;
|
||||
mCount--;
|
||||
if (aIndex == mNext-1) { // if we removed the last element
|
||||
mNext--;
|
||||
// don't test for success of the shrink
|
||||
Shrink();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//void*
|
||||
//tmVector::operator[](int index) {
|
||||
// if (index < mNext && index >= 0)
|
||||
// return mElements[index];
|
||||
// return nsnull;
|
||||
//}
|
||||
|
||||
// Does not delete any of the data, merely removes references to them
|
||||
void
|
||||
tmVector::Clear(){
|
||||
memset(mElements, 0, mCapacity);
|
||||
mCount = 0;
|
||||
mNext = 0;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Protected Member Functions
|
||||
|
||||
// increases the capacity by the growth increment
|
||||
nsresult
|
||||
tmVector::Grow() {
|
||||
|
||||
PRUint32 newcap = mCapacity + GROWTH_INC;
|
||||
mElements = (void**) realloc(mElements, (newcap * sizeof(void*)));
|
||||
if (mElements) {
|
||||
mCapacity = newcap;
|
||||
return NS_OK;
|
||||
}
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
// reduces the capacity by the growth increment. leaves room
|
||||
// for one more add before needing to Grow().
|
||||
nsresult
|
||||
tmVector::Shrink() {
|
||||
|
||||
PRUint32 newcap = mCapacity - GROWTH_INC;
|
||||
if (mNext < newcap) {
|
||||
mElements = (void**) realloc(mElements, newcap * sizeof(void*));
|
||||
if (!mElements)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
mCapacity = newcap;
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
|
@ -0,0 +1,160 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla Transaction Manager.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corp.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2003
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* John Gaunt <jgaunt@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#ifndef _tmVector_H_
|
||||
#define _tmVector_H_
|
||||
|
||||
#include "tmUtils.h"
|
||||
|
||||
#define GROWTH_INC 5
|
||||
|
||||
/**
|
||||
* A simple, clear, self-growing, collection of objects. typed independant
|
||||
* basically a growing array. Useful in situations where you need an
|
||||
* indexed collection but do not know the size in advance and need the
|
||||
* ability for increase and decrease in size. Not optimized for anything
|
||||
* in particular, or any size in particular.
|
||||
*
|
||||
* Is able to guarantee the index of an item will
|
||||
* not change due to removals of a lower indexed item. The growing,
|
||||
* and shrinking all happens to the end of the collection
|
||||
*
|
||||
* Does not backfill, adds to the end. At some point this should be
|
||||
* changed to make best use of space.
|
||||
*/
|
||||
class tmVector
|
||||
{
|
||||
public:
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Constructor(s) & Destructor
|
||||
|
||||
/**
|
||||
* Set some sane default values to set up the internal storage. Init()
|
||||
* must be called after construction of the object to allcate the
|
||||
* backing store.
|
||||
*/
|
||||
tmVector() : mNext(0), mCount(0), mCapacity(10), mElements(nsnull) {;}
|
||||
|
||||
/**
|
||||
* Reclaim the memory allocated in the Init() method.
|
||||
*/
|
||||
virtual ~tmVector();
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Public Member Functions
|
||||
|
||||
/**
|
||||
* Allocates the storage back-end
|
||||
*
|
||||
* @returns NS_OK if allocation succeeded
|
||||
* @returns NS_ERROR_OUT_OF_MEMORY if the allocation failed
|
||||
*/
|
||||
nsresult Init();
|
||||
|
||||
// mutators
|
||||
|
||||
/**
|
||||
* @returns the index of the element added, if successful
|
||||
* @returns -1 if an error occured during allocation of space
|
||||
*/
|
||||
PRInt32 Append(void *aElement);
|
||||
|
||||
/**
|
||||
* This does not collapse the collection, it leaves holes. Note, it also
|
||||
* doesn't delete the element, it merely removes it from the collection
|
||||
*/
|
||||
void Remove(void *aElement);
|
||||
|
||||
/**
|
||||
* This does not collapse the collection, it leaves holes. Note, it also
|
||||
* doesn't delete the element, it merely removes it from the collection
|
||||
*/
|
||||
void RemoveAt(PRUint32 aIndex);
|
||||
|
||||
/**
|
||||
* Does not call delete on the elements since we have no idea how to
|
||||
* reclaim the memory. Sets all array slots to 0.
|
||||
*/
|
||||
void Clear();
|
||||
|
||||
/**
|
||||
* @returns the element at the index indicated, including nsnull if the
|
||||
* slot is empty.
|
||||
*/
|
||||
void* operator[](PRUint32 index) {
|
||||
PR_ASSERT(index < mNext);
|
||||
return mElements[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns the number of elements stored
|
||||
*/
|
||||
PRUint32 Count() { return mCount; }
|
||||
|
||||
/**
|
||||
* Meant to be used as the conditional in a loop. |index < size| should
|
||||
* reach all elements of the collection and not run out of bounds. If
|
||||
* slots 0,1,4,5,6 contain elements Size() will return 7, Count() will
|
||||
* return 5.
|
||||
*
|
||||
* @returns the number of slots in the array taken, irrespective of
|
||||
* holes in the collection.
|
||||
*/
|
||||
PRUint32 Size() { return mNext; }
|
||||
|
||||
protected:
|
||||
|
||||
nsresult Grow(); // mCapacity += GROWTH_INC - realloc()s
|
||||
nsresult Shrink(); // mCapacity -= GROWTH_INC - dumb, free, malloc
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Protected Member Variables
|
||||
|
||||
// bookkeeping variables
|
||||
PRUint32 mNext; // next element insertion slot (0 based)
|
||||
PRUint32 mCount; // how many elements in the Vector (1 based)
|
||||
PRUint32 mCapacity; // current capacity of the Vector (1 based)
|
||||
|
||||
// the actual array of objects being stored
|
||||
void **mElements;
|
||||
|
||||
private:
|
||||
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,11 @@
|
|||
Makefile
|
||||
module.rc
|
||||
module.res
|
||||
tmIPCModule.obj
|
||||
tmQueue.obj
|
||||
tmTransactionManager.obj
|
||||
transmngr.dll
|
||||
transmngr.exp
|
||||
transmngr.ilk
|
||||
transmngr.lib
|
||||
transmngr.pdb
|
|
@ -0,0 +1,99 @@
|
|||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
#
|
||||
# The contents of this file are subject to the Mozilla Public License Version
|
||||
# 1.1 (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
# http://www.mozilla.org/MPL/
|
||||
#
|
||||
# Software distributed under the License is distributed on an "AS IS" basis,
|
||||
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
# for the specific language governing rights and limitations under the
|
||||
# License.
|
||||
#
|
||||
# The Original Code is Mozilla Transaction Manager.
|
||||
#
|
||||
# The Initial Developer of the Original Code is
|
||||
# Netscape Communications Corp.
|
||||
# Portions created by the Initial Developer are Copyright (C) 2003
|
||||
# the Initial Developer. All Rights Reserved.
|
||||
#
|
||||
# Contributor(s):
|
||||
# John Gaunt <jgaunt@netscape.com>
|
||||
#
|
||||
# Alternatively, the contents of this file may be used under the terms of
|
||||
# either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
# in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
# of those above. If you wish to allow use of your version of this file only
|
||||
# under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
# use your version of this file under the terms of the MPL, indicate your
|
||||
# decision by deleting the provisions above and replace them with the notice
|
||||
# and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
# the provisions above, a recipient may use your version of this file under
|
||||
# the terms of any one of the MPL, the GPL or the LGPL.
|
||||
#
|
||||
# ***** END LICENSE BLOCK *****
|
||||
|
||||
DEPTH = ../../../../..
|
||||
topsrcdir = @top_srcdir@
|
||||
srcdir = @srcdir@
|
||||
VPATH = @srcdir@
|
||||
|
||||
include $(DEPTH)/config/autoconf.mk
|
||||
|
||||
MODULE = ipcd
|
||||
LIBRARY_NAME = transmngr
|
||||
MODULE_NAME = ipcd
|
||||
|
||||
FORCE_SHARED_LIB = 1
|
||||
NO_DIST_INSTALL = 1
|
||||
|
||||
ifeq ($(OS_ARCH),Darwin)
|
||||
NO_COMPONENT_LINK_MAP = 1
|
||||
MOZ_COMPONENTS_VERSION_SCRIPT_LDFLAGS =
|
||||
endif
|
||||
|
||||
REQUIRES = nspr \
|
||||
xpcom \
|
||||
$(NULL)
|
||||
|
||||
CPPSRCS = \
|
||||
tmIPCModule.cpp \
|
||||
tmQueue.cpp \
|
||||
tmTransactionManager.cpp \
|
||||
$(NULL)
|
||||
|
||||
EXPORTS = \
|
||||
tmIPCModule.h \
|
||||
$(NULL)
|
||||
|
||||
LOCAL_INCLUDES = \
|
||||
-I$(srcdir)/../common \
|
||||
$(NULL)
|
||||
|
||||
include $(topsrcdir)/config/config.mk
|
||||
|
||||
LIBS = \
|
||||
$(EXTRA_DSO_LIBS) \
|
||||
$(NSPR_LIBS) \
|
||||
$(NULL)
|
||||
|
||||
EXTRA_DSO_LDOPTS = \
|
||||
$(LIBS_DIR) \
|
||||
$(NSPR_LIBS) \
|
||||
$(DIST)/lib/$(LIB_PREFIX)transmngrcom_s.$(LIB_SUFFIX) \
|
||||
$(EXTRA_DSO_LIBS) \
|
||||
$(NULL)
|
||||
|
||||
include $(topsrcdir)/config/rules.mk
|
||||
|
||||
_IPC_FILES = \
|
||||
$(DLL_PREFIX)$(LIBRARY_NAME)$(DLL_SUFFIX) \
|
||||
$(NULL)
|
||||
|
||||
libs:: $(_IPC_FILES)
|
||||
$(INSTALL) $^ $(DIST)/bin/ipc/modules
|
||||
|
||||
install:: $(_IPC_FILES)
|
||||
$(SYSINSTALL) $(IFLAGS1) $^ $(DESTDIR)$(mozappdir)/ipc/modules
|
|
@ -0,0 +1,137 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla Transaction Manager.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corp.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2003
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* John Gaunt <jgaunt@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include "tmIPCModule.h"
|
||||
#include "tmTransaction.h"
|
||||
#include "tmTransactionManager.h"
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// IPC Daemon hookup stuff
|
||||
|
||||
// functionpointer array giving access to this module from the IPC daemon
|
||||
static ipcModuleMethods gTransMethods =
|
||||
{
|
||||
IPC_MODULE_METHODS_VERSION,
|
||||
tmIPCModule::Init,
|
||||
tmIPCModule::Shutdown,
|
||||
tmIPCModule::HandleMsg
|
||||
};
|
||||
|
||||
static ipcModuleEntry gTransModuleEntry[] =
|
||||
{
|
||||
{ TRANSACTION_MODULE_ID, &gTransMethods }
|
||||
};
|
||||
|
||||
IPC_IMPL_GETMODULES(TransactionModule, gTransModuleEntry)
|
||||
|
||||
static const nsID kTransModuleID = TRANSACTION_MODULE_ID;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Define global variable
|
||||
|
||||
tmTransactionManager *tmIPCModule::tm;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// IPC Module API
|
||||
|
||||
void
|
||||
tmIPCModule::Init() {
|
||||
if (!tm)
|
||||
InitInternal();
|
||||
}
|
||||
|
||||
void
|
||||
tmIPCModule::Shutdown() {
|
||||
if (tm) {
|
||||
delete tm;
|
||||
tm = nsnull;
|
||||
}
|
||||
}
|
||||
|
||||
// straight pass-through, don't check args, let the TM do it.
|
||||
void
|
||||
tmIPCModule::HandleMsg(ipcClientHandle client, const nsID &target,
|
||||
const void *data, PRUint32 dataLen) {
|
||||
|
||||
// make sure the trans mngr is there
|
||||
if (!tm && (InitInternal() < 0))
|
||||
return;
|
||||
|
||||
// create the transaction
|
||||
tmTransaction *trans = new tmTransaction();
|
||||
|
||||
// initialize it
|
||||
if (trans) {
|
||||
if(NS_SUCCEEDED(trans->Init(IPC_GetClientID(client), // who owns it
|
||||
TM_INVALID_ID, // in data
|
||||
TM_INVALID, // in data
|
||||
TM_INVALID, // in data
|
||||
(PRUint8 *)data, // raw message
|
||||
dataLen))) { // length of message
|
||||
// pass it on to the trans mngr
|
||||
tm->HandleTransaction(trans);
|
||||
}
|
||||
else
|
||||
delete trans;
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// tmIPCModule API
|
||||
|
||||
// straight pass-through, don't check args, let the TS & TM do it.
|
||||
void
|
||||
tmIPCModule::SendMsg(PRUint32 aDestClientIPCID, tmTransaction *aTransaction) {
|
||||
|
||||
IPC_SendMsg(aDestClientIPCID,
|
||||
kTransModuleID,
|
||||
(void *)aTransaction->GetRawMessage(),
|
||||
aTransaction->GetRawMessageLength());
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Protected Methods
|
||||
|
||||
PRInt32
|
||||
tmIPCModule::InitInternal() {
|
||||
|
||||
tm = new tmTransactionManager();
|
||||
if (tm)
|
||||
return tm->Init();
|
||||
return -1;
|
||||
}
|
||||
|
|
@ -0,0 +1,109 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla Transaction Manager.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corp.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2003
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* John Gaunt <jgaunt@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#ifndef _tmIPCModule_H_
|
||||
#define _tmIPCModule_H_
|
||||
|
||||
#include "ipcModuleUtil.h"
|
||||
#include "tmUtils.h"
|
||||
|
||||
// forward declarations
|
||||
class tmTransaction;
|
||||
class tmTransactionManager;
|
||||
|
||||
/**
|
||||
* Basically an interface between the tmTransactionManager and the IPC
|
||||
* daemon. Does little else than format the data from one party into
|
||||
* a format understandable to the other.
|
||||
*
|
||||
* The reason for this class is to try and abstract the transportation
|
||||
* layer the transaction service uses. By using this class the Transaction
|
||||
* Manager itself only needs to know that clients are identified by
|
||||
* PRUint32 IDs.
|
||||
*/
|
||||
class tmIPCModule
|
||||
{
|
||||
public:
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// ipcModule API - called from IPC daemon
|
||||
|
||||
/**
|
||||
* Clean up the TM
|
||||
*/
|
||||
static void Shutdown();
|
||||
|
||||
/**
|
||||
* Check the TM, create it if neccessary.
|
||||
*/
|
||||
static void Init();
|
||||
|
||||
/**
|
||||
* Receives a message from the IPC daemon, creates a transaction and sends
|
||||
* it to the TM to deal with.
|
||||
*/
|
||||
static void HandleMsg(ipcClientHandle client,
|
||||
const nsID &target,
|
||||
const void *data,
|
||||
PRUint32 dataLen);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// tmIPCModule API - called from tmTransactionManager
|
||||
|
||||
/**
|
||||
* Sends the message to the IPC daemon to be deliverd to the client arg.
|
||||
*/
|
||||
static void SendMsg(PRUint32 aDestClientIPCID, tmTransaction *aTransaction);
|
||||
|
||||
protected:
|
||||
|
||||
/**
|
||||
* tm should be null coming into this method. This does NOT delete tm
|
||||
* first.
|
||||
*
|
||||
* @returns NS_OK if everything succeeds
|
||||
* @returns -1 if initialization fails
|
||||
*/
|
||||
static PRInt32 InitInternal();
|
||||
|
||||
static tmTransactionManager *tm;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
|
@ -0,0 +1,223 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla Transaction Manager.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corp.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2003
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* John Gaunt <jgaunt@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include "plstr.h"
|
||||
#include "tmQueue.h"
|
||||
#include "tmTransaction.h"
|
||||
#include "tmTransactionManager.h"
|
||||
#include "tmUtils.h"
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Constructors & Destructor
|
||||
|
||||
tmQueue::~tmQueue() {
|
||||
|
||||
// empty the vectors
|
||||
PRUint32 index = 0;
|
||||
PRUint32 size = mTransactions.Size();
|
||||
for ( ; index < size ; index++) {
|
||||
if (mTransactions[index])
|
||||
delete (tmTransaction *)mTransactions[index];
|
||||
}
|
||||
|
||||
// don't need to delete the mListeners because
|
||||
// we just insert PRUint32s, no allocation
|
||||
|
||||
mTM = nsnull;
|
||||
mID = 0;
|
||||
if (mName)
|
||||
PL_strfree(mName);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Public Methods
|
||||
|
||||
PRInt32
|
||||
tmQueue::Init(const char* aName, PRUint32 aID, tmTransactionManager *aTM) {
|
||||
PR_ASSERT(mTM == nsnull);
|
||||
|
||||
if (NS_SUCCEEDED(mTransactions.Init()) &&
|
||||
NS_SUCCEEDED(mListeners.Init()) &&
|
||||
((mName = PL_strdup(aName)) != nsnull) ) {
|
||||
mTM = aTM;
|
||||
mID = aID;
|
||||
return NS_OK;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
PRInt32
|
||||
tmQueue::AttachClient(PRUint32 aClientID) {
|
||||
|
||||
PRInt32 status = NS_OK; // success of adding client
|
||||
|
||||
if (!IsAttached(aClientID)) {
|
||||
// add the client to the listener list -- null safe call
|
||||
status = mListeners.Append((void*) aClientID);
|
||||
}
|
||||
else
|
||||
status = -2;
|
||||
|
||||
// create & init a reply transaction
|
||||
tmTransaction trans;
|
||||
if (NS_SUCCEEDED(trans.Init(aClientID, // owner's ipc ID
|
||||
mID, // client gets our ID
|
||||
TM_ATTACH_REPLY, // action
|
||||
status, // success of the add
|
||||
(PRUint8*)mName, // client matches name to ID
|
||||
PL_strlen(mName)+1))) {
|
||||
// send the reply
|
||||
mTM->SendTransaction(aClientID, &trans);
|
||||
}
|
||||
|
||||
// if we successfully added the client - send all current transactions
|
||||
if (status >= 0) { // append returns the index of the added element
|
||||
|
||||
PRUint32 size = mTransactions.Size();
|
||||
for (PRUint32 index = 0; index < size; index++) {
|
||||
if (mTransactions[index])
|
||||
mTM->SendTransaction(aClientID, (tmTransaction*) mTransactions[index]);
|
||||
}
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
PRInt32
|
||||
tmQueue::DetachClient(PRUint32 aClientID) {
|
||||
|
||||
PRUint32 size = mListeners.Size();
|
||||
PRUint32 id = 0;
|
||||
PRInt32 status = -1;
|
||||
|
||||
for (PRUint32 index = 0; index < size; index++) {
|
||||
id = (PRUint32)NS_PTR_TO_INT32(mListeners[index]);
|
||||
if(id == aClientID) {
|
||||
mListeners.RemoveAt(index);
|
||||
status = NS_OK;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
tmTransaction trans;
|
||||
if (NS_SUCCEEDED(trans.Init(aClientID,
|
||||
mID,
|
||||
TM_DETACH_REPLY,
|
||||
status,
|
||||
nsnull,
|
||||
0))) {
|
||||
// send the reply
|
||||
mTM->SendTransaction(aClientID, &trans);
|
||||
}
|
||||
|
||||
// if we've removed all the listeners, remove the queue.
|
||||
if (mListeners.Size() == 0)
|
||||
return TM_SUCCESS_DELETE_QUEUE;
|
||||
return status;
|
||||
}
|
||||
|
||||
void
|
||||
tmQueue::FlushQueue(PRUint32 aClientID) {
|
||||
|
||||
if(!IsAttached(aClientID))
|
||||
return;
|
||||
|
||||
PRUint32 size = mTransactions.Size();
|
||||
for (PRUint32 index = 0; index < size; index++)
|
||||
if (mTransactions[index])
|
||||
delete (tmTransaction*)mTransactions[index];
|
||||
|
||||
mTransactions.Clear();
|
||||
|
||||
tmTransaction trans;
|
||||
if (NS_SUCCEEDED(trans.Init(aClientID,
|
||||
mID,
|
||||
TM_FLUSH_REPLY,
|
||||
NS_OK,
|
||||
nsnull,
|
||||
0))) {
|
||||
mTM->SendTransaction(aClientID, &trans);
|
||||
}
|
||||
}
|
||||
|
||||
PRInt32
|
||||
tmQueue::PostTransaction(tmTransaction *aTrans) {
|
||||
|
||||
PRInt32 status = -1;
|
||||
PRUint32 ownerID = aTrans->GetOwnerID();
|
||||
|
||||
// if we are attached, have the right queue and have successfully
|
||||
// appended the transaction to the queue, send the transaction
|
||||
// to all the listeners.
|
||||
|
||||
if (IsAttached(ownerID) && aTrans->GetQueueID() == mID)
|
||||
status = mTransactions.Append(aTrans);
|
||||
|
||||
if (status >= 0) {
|
||||
// send the transaction to all members of mListeners except the owner
|
||||
PRUint32 size = mListeners.Size();
|
||||
PRUint32 id = 0;
|
||||
for (PRUint32 index = 0; index < size; index++) {
|
||||
id = (PRUint32)NS_PTR_TO_INT32(mListeners[index]);
|
||||
if (ownerID != id)
|
||||
mTM->SendTransaction(id, aTrans);
|
||||
}
|
||||
}
|
||||
|
||||
tmTransaction trans;
|
||||
if (NS_SUCCEEDED(trans.Init(ownerID,
|
||||
mID,
|
||||
TM_POST_REPLY,
|
||||
status,
|
||||
nsnull,
|
||||
0))) {
|
||||
// send the reply
|
||||
mTM->SendTransaction(ownerID, &trans);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
PRBool
|
||||
tmQueue::IsAttached(PRUint32 aClientID) {
|
||||
// XXX could be an issue if the aClientID is 0 and there
|
||||
// is a "hole" in the mListeners vector. - may NEED to store PRUint32*s
|
||||
PRUint32 size = mListeners.Size();
|
||||
for (PRUint32 index = 0; index < size; index++) {
|
||||
if (aClientID == (PRUint32)NS_PTR_TO_INT32(mListeners[index]))
|
||||
return PR_TRUE;
|
||||
}
|
||||
return PR_FALSE;
|
||||
}
|
|
@ -0,0 +1,186 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla Transaction Manager.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corp.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2003
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* John Gaunt <jgaunt@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#ifndef _tmQueue_H_
|
||||
#define _tmQueue_H_
|
||||
|
||||
#include "tmUtils.h"
|
||||
#include "tmVector.h"
|
||||
|
||||
class tmClient;
|
||||
class tmTransaction;
|
||||
class tmTransactionManager;
|
||||
|
||||
/**
|
||||
* This class isn't so much a queue as it is storage for transactions. It
|
||||
* is set up to recieve and store transactions in a growing collection
|
||||
* (using tmVectors). Different messages can be recieved from the
|
||||
* Transaction Manager(TM) the queue belongs to which can add and remove
|
||||
* listeners, empty the queue (flush), and add messages to the queue.
|
||||
*
|
||||
* See the documentation in tmTransactionService.h for details on the
|
||||
* messages you can send to and recieve from the queues in the TM
|
||||
*/
|
||||
class tmQueue
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Constructor & Destructor
|
||||
|
||||
/**
|
||||
* Set the internal state to default values. Init() must be called
|
||||
* after construction to allocate the storage and set the name and ID.
|
||||
*/
|
||||
tmQueue(): mID(0), mName(nsnull), mTM(nsnull) { }
|
||||
|
||||
/**
|
||||
* Reclaim the memory allocated in Init(). Destroys the transactions in
|
||||
* the transaction storage and the ids in the listener storage
|
||||
*/
|
||||
virtual ~tmQueue();
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Public Member Functions
|
||||
|
||||
/**
|
||||
* Initialize internal storage vectors and set the name of the queue
|
||||
* and the pointer to the TM container.
|
||||
*
|
||||
* @returns NS_OK if everything succeeds
|
||||
* @returns -1 if initialization fails
|
||||
*/
|
||||
PRInt32 Init(const char* aName, PRUint32 aID, tmTransactionManager *aTM);
|
||||
|
||||
// Queue Operations
|
||||
|
||||
/**
|
||||
* Adds the clientID to the list of queue listeners. A reply is created
|
||||
* and sent to the client. The reply contains both the name of the
|
||||
* queue and the id, so the client can match the id to the name and
|
||||
* then use the id in all further communications to the queue. All
|
||||
* current transactions in the queue are then sent to the client.
|
||||
*
|
||||
* If the client was already attached the reply is sent, but not the
|
||||
* outstanding transactions, the assumption being made that all
|
||||
* transactions have already been sent to the client.
|
||||
*
|
||||
* The reply is sent for all cases, with the return value in the status
|
||||
* field.
|
||||
*
|
||||
* @returns >= 0 if the client was attached successfully
|
||||
* @returns -1 if the client was not attached
|
||||
* @returns -2 if the client was already attached
|
||||
*/
|
||||
PRInt32 AttachClient(PRUint32 aClientID);
|
||||
|
||||
/**
|
||||
* Removes the client from the list of queue listeners. A reply is created
|
||||
* and sent to the client to indicate the success of the removal.
|
||||
*
|
||||
* The reply is sent for all cases, with the status field set to either
|
||||
* -1 or NS_OK.
|
||||
*
|
||||
* @returns NS_OK on success
|
||||
* @returns -1 if client is not attached to this queue
|
||||
* @returns TM_SUCCESS_DELETE_QUEUE if there are no more listeners,
|
||||
* instructing the Transaction Mangaer to delete the queue.
|
||||
*/
|
||||
PRInt32 DetachClient(PRUint32 aClientID);
|
||||
|
||||
/**
|
||||
* Removes all the transactions being held in the queue.
|
||||
* A reply is created and sent to the client to indicate the
|
||||
* completion of the operation.
|
||||
*/
|
||||
void FlushQueue(PRUint32 aClientID);
|
||||
|
||||
/**
|
||||
* Places the transaction passed in on the queue. Takes ownership of the
|
||||
* transaction, deletes it in the destructor. A reply is created and
|
||||
* sent to the client to indicate the success of the posting of the
|
||||
* transaction.
|
||||
*
|
||||
* The reply is sent for all cases, with the status field containing the
|
||||
* return value.
|
||||
*
|
||||
* @returns >= 0 if the message was posted properly.
|
||||
* @returns -1 if the client posting is not attached to this queue,
|
||||
* if the transaction has been posted to the wrong queue or
|
||||
* if an error occured when trying to add the post to the
|
||||
* internal storage.
|
||||
*/
|
||||
PRInt32 PostTransaction(tmTransaction *aTrans);
|
||||
|
||||
// Accessors
|
||||
|
||||
/**
|
||||
* @returns the ID of the queue
|
||||
*/
|
||||
PRUint32 GetID() const { return mID; }
|
||||
|
||||
/**
|
||||
* @returns the name of the queue
|
||||
*/
|
||||
const char* GetName() const { return mName; }
|
||||
|
||||
protected:
|
||||
|
||||
/**
|
||||
* Helper method to determine if the client has already attached.
|
||||
*
|
||||
* @returns PR_TRUE if the client is attached to the queue.
|
||||
* @returns PR_FALSE if the client is not attached to the queue.
|
||||
*/
|
||||
PRBool IsAttached(PRUint32 aClientID);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Protected Member Variables
|
||||
|
||||
// storage
|
||||
tmVector mTransactions; // transactions that have been posted
|
||||
tmVector mListeners; // programs listening to this queue
|
||||
|
||||
// bookkeeping
|
||||
PRUint32 mID; // a number linked to the name in the mTM
|
||||
char *mName; // format: [namespace][domainname(ie prefs)]
|
||||
tmTransactionManager *mTM; // the container that holds the queue
|
||||
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,163 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla Transaction Manager.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corp.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2003
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* John Gaunt <jgaunt@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include "plstr.h"
|
||||
#include <stdlib.h>
|
||||
#include "tmQueue.h"
|
||||
#include "tmTransactionManager.h"
|
||||
#include "tmTransaction.h"
|
||||
#include "tmUtils.h"
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Constructors & Destructor & Initializer
|
||||
|
||||
tmTransactionManager::~tmTransactionManager() {
|
||||
|
||||
PRUint32 size = mQueues.Size();
|
||||
tmQueue *queue = nsnull;
|
||||
for (PRUint32 index = 0; index < size; index++) {
|
||||
queue = (tmQueue *)mQueues[index];
|
||||
if (queue) {
|
||||
delete queue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PRInt32
|
||||
tmTransactionManager::Init() {
|
||||
return mQueues.Init();
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// public transaction module methods
|
||||
|
||||
void
|
||||
tmTransactionManager::HandleTransaction(tmTransaction *aTrans) {
|
||||
|
||||
nsresult rv = NS_OK;
|
||||
PRUint32 action = aTrans->GetAction();
|
||||
PRUint32 ownerID = aTrans->GetOwnerID();
|
||||
tmQueue *queue = nsnull;
|
||||
|
||||
// get the right queue -- attaches do it differently
|
||||
if (action == TM_ATTACH) {
|
||||
const char *name = (char*) aTrans->GetMessage(); // is qName for Attaches
|
||||
queue = GetQueue(name);
|
||||
if (!queue) {
|
||||
PRInt32 index = AddQueue(name);
|
||||
if (index >= 0)
|
||||
queue = GetQueue(index); // GetQueue may return nsnull
|
||||
}
|
||||
}
|
||||
else // all other trans should have a valid queue ID already
|
||||
queue = GetQueue(aTrans->GetQueueID());
|
||||
|
||||
if (queue) {
|
||||
// All possible actions should have a case, default is not valid
|
||||
// delete trans when done with them, let the queue own the trans
|
||||
// that are posted to them.
|
||||
PRInt32 result = 0;
|
||||
switch (action) {
|
||||
case TM_ATTACH:
|
||||
queue->AttachClient(ownerID);
|
||||
break;
|
||||
case TM_POST:
|
||||
result = queue->PostTransaction(aTrans);
|
||||
if (result >= 0) // post failed, aTrans cached in a tmQueue
|
||||
return;
|
||||
break;
|
||||
case TM_FLUSH:
|
||||
queue->FlushQueue(ownerID);
|
||||
break;
|
||||
case TM_DETACH:
|
||||
if (queue->DetachClient(ownerID) == TM_SUCCESS_DELETE_QUEUE) {
|
||||
// the last client has been removed, remove the queue
|
||||
RemoveQueue(aTrans->GetQueueID()); // this _could_ be out of bounds
|
||||
}
|
||||
break;
|
||||
default:
|
||||
PR_NOT_REACHED("bad action in the transaction");
|
||||
}
|
||||
}
|
||||
delete aTrans;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Protected member functions
|
||||
|
||||
//
|
||||
// Queue Handling
|
||||
//
|
||||
|
||||
tmQueue*
|
||||
tmTransactionManager::GetQueue(const char *aQueueName) {
|
||||
|
||||
PRUint32 size = mQueues.Size();
|
||||
tmQueue *queue = nsnull;
|
||||
for (PRUint32 index = 0; index < size; index++) {
|
||||
queue = (tmQueue*) mQueues[index];
|
||||
if (queue && strcmp(queue->GetName(), aQueueName) == 0)
|
||||
return queue;
|
||||
}
|
||||
return nsnull;
|
||||
}
|
||||
|
||||
// if successful the nsresult contains the index of the added queue
|
||||
PRInt32
|
||||
tmTransactionManager::AddQueue(const char *aQueueName) {
|
||||
|
||||
tmQueue* queue = new tmQueue();
|
||||
if (!queue)
|
||||
return -1;
|
||||
PRInt32 index = mQueues.Append(queue);
|
||||
if (index < 0)
|
||||
delete queue;
|
||||
else
|
||||
queue->Init(aQueueName, index, this);
|
||||
return index;
|
||||
}
|
||||
|
||||
void
|
||||
tmTransactionManager::RemoveQueue(PRUint32 aQueueID) {
|
||||
PR_ASSERT(aQueueID <= mQueues.Size());
|
||||
|
||||
tmQueue *queue = (tmQueue*)mQueues[aQueueID];
|
||||
if (queue) {
|
||||
mQueues.RemoveAt(aQueueID);
|
||||
delete queue;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,147 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla Transaction Manager.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corp.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2003
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* John Gaunt <jgaunt@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#ifndef _tmTransactionManager_H_
|
||||
#define _tmTransactionManager_H_
|
||||
|
||||
#include "plhash.h"
|
||||
#include "tmUtils.h"
|
||||
#include "tmVector.h"
|
||||
#include "tmIPCModule.h"
|
||||
|
||||
// forward declarations
|
||||
class tmQueue;
|
||||
class tmClient;
|
||||
class tmTransaction;
|
||||
|
||||
/**
|
||||
* This class manages the flow of messages from the IPC daemon (coming to
|
||||
* it through the tmIPCModule) that ultimately come from a Transaction
|
||||
* Service (TS) in a mozilla based client somewhere. The message is
|
||||
* delivered to the proper queue, where it is dealt with.
|
||||
*
|
||||
* New queues get created here as clients request them.
|
||||
*/
|
||||
class tmTransactionManager
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Constructor(s) & Destructor & Initializer
|
||||
|
||||
/**
|
||||
* reclaim the memory allcoated during initialization
|
||||
*/
|
||||
virtual ~tmTransactionManager();
|
||||
|
||||
/**
|
||||
* Set up the storage of the queues - initialize the vector
|
||||
*
|
||||
* @returns NS_OK if successful
|
||||
* @returns -1 if initialization fails
|
||||
*/
|
||||
PRInt32 Init();
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Public Member Functions
|
||||
|
||||
/**
|
||||
* Called from the tmIPCModule. Decide where to send the message and
|
||||
* dispatch it.
|
||||
*/
|
||||
void HandleTransaction(tmTransaction *aTrans);
|
||||
|
||||
/**
|
||||
* Called by the queues when they need to get a message back out to a
|
||||
* client.
|
||||
*/
|
||||
void SendTransaction(PRUint32 aDestClientIPCID, tmTransaction *aTrans) {
|
||||
PR_ASSERT(aTrans);
|
||||
tmIPCModule::SendMsg(aDestClientIPCID, aTrans);
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Protected Member Functions
|
||||
|
||||
// Queue management
|
||||
|
||||
/**
|
||||
* @returns the queue indexed by the ID passed in, which could be nsnull
|
||||
*/
|
||||
tmQueue* GetQueue(PRUint32 aQueueID) {
|
||||
return (tmQueue*) mQueues[aQueueID];
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns the queue with the name passed in
|
||||
* @returns nsnull if there is no queue with that name
|
||||
*/
|
||||
tmQueue* GetQueue(const char *aQueueName);
|
||||
|
||||
/**
|
||||
* If all is successful a new queue with the name provided will be created,
|
||||
* and added to the collection of queues. It will be initialized and ready
|
||||
* to have transactions added.
|
||||
*
|
||||
* This doesn't check for the existance of a queue with this name. IF
|
||||
* there is already a queue with this name then you will
|
||||
* get that when using GetQueue(qName) and never get the new queue
|
||||
* created here. A call to GetQueue(qID) will be able to get at the new
|
||||
* queue, however you had better cache the ID.
|
||||
*
|
||||
* @returns -1 if the queue can't be created, or is not added
|
||||
* @returns >= 0 if the queue was added successfully
|
||||
*/
|
||||
PRInt32 AddQueue(const char *aQueueType);
|
||||
|
||||
/**
|
||||
*/
|
||||
void RemoveQueue(PRUint32 aQueueID);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Protected Member Variables
|
||||
|
||||
tmVector mQueues;
|
||||
|
||||
private:
|
||||
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1 @@
|
|||
Makefile
|
|
@ -0,0 +1,53 @@
|
|||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
#
|
||||
# The contents of this file are subject to the Mozilla Public License Version
|
||||
# 1.1 (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
# http://www.mozilla.org/MPL/
|
||||
#
|
||||
# Software distributed under the License is distributed on an "AS IS" basis,
|
||||
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
# for the specific language governing rights and limitations under the
|
||||
# License.
|
||||
#
|
||||
# The Original Code is Mozilla Transaction Manager.
|
||||
#
|
||||
# The Initial Developer of the Original Code is
|
||||
# Netscape Communications Corp.
|
||||
# Portions created by the Initial Developer are Copyright (C) 2003
|
||||
# the Initial Developer. All Rights Reserved.
|
||||
#
|
||||
# Contributor(s):
|
||||
# John Gaunt <jgaunt@netscape.com>
|
||||
#
|
||||
# Alternatively, the contents of this file may be used under the terms of
|
||||
# either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
# in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
# of those above. If you wish to allow use of your version of this file only
|
||||
# under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
# use your version of this file under the terms of the MPL, indicate your
|
||||
# decision by deleting the provisions above and replace them with the notice
|
||||
# and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
# the provisions above, a recipient may use your version of this file under
|
||||
# the terms of any one of the MPL, the GPL or the LGPL.
|
||||
#
|
||||
# ***** END LICENSE BLOCK *****
|
||||
|
||||
DEPTH = ../../../../..
|
||||
topsrcdir = @top_srcdir@
|
||||
srcdir = @srcdir@
|
||||
VPATH = @srcdir@
|
||||
|
||||
include $(DEPTH)/config/autoconf.mk
|
||||
|
||||
MODULE = ipcd
|
||||
|
||||
XPIDLSRCS = \
|
||||
ipcITransactionService.idl \
|
||||
ipcITransactionObserver.idl \
|
||||
$(NULL)
|
||||
|
||||
include $(topsrcdir)/config/rules.mk
|
||||
|
|
@ -0,0 +1,100 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla Transaction Manager.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corp.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2003
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* John Gaunt <jgaunt@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include "nsISupports.idl"
|
||||
|
||||
[scriptable, uuid(656c0a6a-5cb3-45ec-8cb6-e7678897f937)]
|
||||
interface ipcITransactionObserver : nsISupports
|
||||
{
|
||||
/**
|
||||
* This gets called when a Transaction has been sent from the
|
||||
* TransactionManager. If the data passed in needs to be stored
|
||||
* for longer than the life of the method the observer needs
|
||||
* to make a copy.
|
||||
*
|
||||
* @param aQueueID
|
||||
* The queue from which the transaction originated
|
||||
*
|
||||
* @param aData
|
||||
* The data to be sent.
|
||||
*
|
||||
* @param aDataLen
|
||||
* The length of the data argument
|
||||
*/
|
||||
void onTransactionAvailable(in unsigned long aQueueID,
|
||||
[array, const, size_is(aDataLen)]
|
||||
in octet aData,
|
||||
in unsigned long aDataLen);
|
||||
|
||||
/**
|
||||
* Called after an application sends an Attach message to the
|
||||
* Transaction Manager.
|
||||
*
|
||||
* @param aQueueID
|
||||
* The client has been attached to the queue with this ID
|
||||
*
|
||||
* @param aStatus
|
||||
* The status of the operation, as defined in tmUtils.h
|
||||
*/
|
||||
void onAttachReply(in unsigned long aQueueID, in unsigned long aStatus);
|
||||
|
||||
/**
|
||||
* Called after an application sends a Detach message. Indicates
|
||||
* to the client that no more messages will be coming from the
|
||||
* the TM to this client. Also, no messages posted from this
|
||||
* client to the indicated queue will be accepted.
|
||||
*
|
||||
* @param aQueueID
|
||||
* The client has been detached from the queue with this ID
|
||||
*
|
||||
* @param aStatus
|
||||
* The status of the operation, as defined in tmUtils.h
|
||||
*/
|
||||
void onDetachReply(in unsigned long aQueueID, in unsigned long aStatus);
|
||||
|
||||
/**
|
||||
* The reply from the TM indicating all messages have been removed
|
||||
* from the queue indicated.
|
||||
*
|
||||
* @param aQueueID
|
||||
* The queue that has been flushed.
|
||||
*
|
||||
* @param aStatus
|
||||
* The status of the operation, as defined in tmUtils.h
|
||||
*/
|
||||
void onFlushReply(in unsigned long aQueueID, in unsigned long aStatus);
|
||||
};
|
|
@ -0,0 +1,239 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla Transaction Manager.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corp.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2003
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* John Gaunt <jgaunt@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
// from tmTransactionManager.h
|
||||
//
|
||||
// XXX documentation needs work
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Overview of TransactionManager IPC Module
|
||||
//
|
||||
// Classes:
|
||||
// tmIPCModule - From the tmTransactionManager's point of view, this
|
||||
// is a proxy for the IPC daemon itself. The reverse is true
|
||||
// from the daemon's point of view. This is an interface for the
|
||||
// Transaction system to work with the IPC daemon as its transport
|
||||
// layer.
|
||||
// tmTransactionManager (TM) - Manages the different queues. Maintains
|
||||
// the queues neccessary for different clients. Receives messages
|
||||
// from the tmIPCModule and passes message to the IPC daemon through
|
||||
// the tmIPCModule.
|
||||
// tmQueue - this class manages the transactions for the different areas
|
||||
// of the profiles being shared. Broken down by functional area there
|
||||
// will be a queue for prefs, cookies etc, but not for profileA and
|
||||
// profileB, and not for pref_delete, pref_create, pref_change etc...
|
||||
// tmTransaction - the actual transaction being shared with the different
|
||||
// tmClients. It contains the type of transaction, which will equate with
|
||||
// a type of queue in existance, the owner of the transaction (an IPC daemon ID)
|
||||
// and the actual text message to be shared.
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// XXX some docs that need to be put somewhere:
|
||||
//
|
||||
// from tmqueue.cpp
|
||||
// Docs - note that the status of the TM_ATTACH_REPLY is only for checking
|
||||
// for TM_ERROR_FAILURE. Other numbers have no importance
|
||||
// success of the status means the NS_ERROR_GET_CODE(status) will
|
||||
// yield the index of the listener.
|
||||
//
|
||||
// move to documentation page - from tmqueue.h
|
||||
//
|
||||
// a queue is specific to profile
|
||||
//
|
||||
// UUID going out from the module is a handler in the client
|
||||
// (will go to the XPCOM service impling that UUID)
|
||||
// -- does it make sense to have different UUIDs for cookies/prefs/etc
|
||||
//
|
||||
|
||||
#include "nsISupports.idl"
|
||||
|
||||
interface ipcITransactionObserver;
|
||||
|
||||
[scriptable, uuid(15561efb-8c58-4a47-813a-fa91cf730895)]
|
||||
interface ipcITransactionService : nsISupports
|
||||
{
|
||||
/**
|
||||
* Connects the application to the transaction manager, defines the
|
||||
* namespace and initializes internal storage
|
||||
*
|
||||
* @param aNamespace
|
||||
* A string defining the scope of the transaction domains. It is
|
||||
* used internally to seperate process listening to the same domain
|
||||
* (ie. preferences) but for two different namespaces (ie. profile1 vs
|
||||
* profile2).
|
||||
*
|
||||
* @returns NS_OK if all memory allocated properly and the IPC service was
|
||||
* reached and attached to successfully.
|
||||
*
|
||||
* @returns an NS_ERROR_<foo> code specific to the failure otherwise
|
||||
*/
|
||||
void init(in ACString aNamespace);
|
||||
|
||||
/**
|
||||
* Links the observer passed in with the domain specified. This will allow
|
||||
* the observer to post transactions dealing with this domain as well as
|
||||
* receive transactions posted by other applications observing this
|
||||
* domain.
|
||||
*
|
||||
* Return codes for this method confer information about the success of
|
||||
* this call, not of the actual attaching of the observer to the domain.
|
||||
* (except the TM_ERROR code - which means the observer can not attach)
|
||||
* If the attach is successful the observer will have its OnAttachReply
|
||||
* method called before this method returns.
|
||||
*
|
||||
* Note: This call is synchronous and will not return until the call to
|
||||
* OnAttachReply is made.
|
||||
*
|
||||
* @param aDomainName
|
||||
* the name of the domain, in the current namespace, to listen for
|
||||
* transactions from. i.e. cookies
|
||||
*
|
||||
* @param aObserver
|
||||
* this will be used to notify the application when transactions
|
||||
* and messages come in.
|
||||
*
|
||||
* @param aLockingCall
|
||||
* Have the Transaction Sevice acquire a lock based on the domain
|
||||
* before attaching. This should be used when persistant storage
|
||||
* is being used to prevent data corruption.
|
||||
*
|
||||
* @returns NS_OK if the attach message was sent to the Transaction Manager.
|
||||
*
|
||||
* @returns an NS_ERROR_<foo> code specific to the failure otherwise
|
||||
*
|
||||
* @returns TM_ERROR_QUEUE_EXISTS if the queue already exists which means
|
||||
* someone has already attached to it.
|
||||
*/
|
||||
void attach(in ACString aDomainName,
|
||||
in ipcITransactionObserver aObserver,
|
||||
in PRBool aLockingCall);
|
||||
|
||||
/**
|
||||
* Sends a detach message to the Transaction Manager to unlink the observer
|
||||
* associated with the domain passed in.
|
||||
*
|
||||
* As in attach, return codes do not indicate success of detachment. The
|
||||
* observer will have it's OnDetach method called if it is successfully
|
||||
* detached.
|
||||
*
|
||||
* Note: This call is an asynchronous call.
|
||||
*
|
||||
* @param aDomainName
|
||||
* the domain, in the current namespace, from which the client
|
||||
* should be removed.
|
||||
*
|
||||
* @returns NS_OK if the detach message is sent to the Transaction Manager
|
||||
*
|
||||
* @returns NS_ERROR_FAILURE is something goes wrong
|
||||
*
|
||||
* @returns NS_ERRROR_UNEXPECTD if the domain does not have an observer
|
||||
* attached
|
||||
*/
|
||||
void detach(in ACString aDomainName);
|
||||
|
||||
/**
|
||||
* Sends a flush message to the Transaction Manager to remove all
|
||||
* transactions for the domain. After this call there will be no
|
||||
* transactions in the Transaction Manager for the namespace/domain
|
||||
* pairing. It is up to the application to coordinate the flushing
|
||||
* of the Transaction Manager with the writing of data to files,
|
||||
* if needed.
|
||||
*
|
||||
* Note: This call is synchronous and will not return until the call to
|
||||
* OnFlushReply is made.
|
||||
*
|
||||
* @param aDomainName
|
||||
* The domain, in the current namespace, to flush.
|
||||
*
|
||||
* @param aLockingCall
|
||||
* Have the Transaction Sevice acquire a lock based on the domain
|
||||
* before flushing. This should be used when persistant storage
|
||||
* is being used to prevent data corruption.
|
||||
*
|
||||
* @returns NS_OK if the flush message is sent to the Transaction Manager
|
||||
*
|
||||
* @returns NS_ERROR_FAILURE is something goes wrong
|
||||
*
|
||||
* @returns NS_ERRROR_UNEXPECTD if the domain does not have an observer
|
||||
* attached
|
||||
*/
|
||||
void flush(in ACString aDomainName, in PRBool aLockingCall);
|
||||
|
||||
/**
|
||||
* Send the data to the Transaction Manager to be broadcast to any
|
||||
* applications that have registered as observers of this particular
|
||||
* namespace/domain pairing.
|
||||
*
|
||||
* If this domain is not being observed (attach has not been called for
|
||||
* this domain) the message is queued until the attach is made and then
|
||||
* the message is sent to the Transaction Manager with the proper domain
|
||||
* information.
|
||||
*
|
||||
* XXXjg - this may not be neccessary with the synch attach call.
|
||||
*
|
||||
* Note: This call is an asynchronous call.
|
||||
*
|
||||
* @param aDomainName
|
||||
* the domain, in the current namespace, to which the data will be
|
||||
* sent.
|
||||
*
|
||||
* @param aData
|
||||
* The actual data to be sent.
|
||||
*
|
||||
* @param aDataLen
|
||||
* The length of the data argument
|
||||
*/
|
||||
void postTransaction(in ACString aDomainName,
|
||||
[array, const, size_is(aDataLen)]
|
||||
in octet aData,
|
||||
in unsigned long aDataLen);
|
||||
};
|
||||
|
||||
%{C++
|
||||
// singleton implementing ipcITransactionService
|
||||
#define IPC_TRANSACTIONSERVICE_CLASSNAME \
|
||||
"tmTransactionService"
|
||||
#define IPC_TRANSACTIONSERVICE_CONTRACTID \
|
||||
"@mozilla.org/ipc/transaction-service;1"
|
||||
#define IPC_TRANSACTIONSERVICE_CID \
|
||||
{ /* 1403adf4-94d1-4c67-a8ae-d9f86972d378 */ \
|
||||
0x1403adf4, \
|
||||
0x94d1, \
|
||||
0x4c67, \
|
||||
{0xa8, 0xae, 0xd9, 0xf8, 0x69, 0x72, 0xd3, 0x78} \
|
||||
}
|
||||
%}
|
|
@ -0,0 +1,4 @@
|
|||
Makefile
|
||||
tmTransactionService.obj
|
||||
transmngr_s.lib
|
||||
transmngr_s.pdb
|
|
@ -0,0 +1,64 @@
|
|||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
#
|
||||
# The contents of this file are subject to the Mozilla Public License Version
|
||||
# 1.1 (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
# http://www.mozilla.org/MPL/
|
||||
#
|
||||
# Software distributed under the License is distributed on an "AS IS" basis,
|
||||
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
# for the specific language governing rights and limitations under the
|
||||
# License.
|
||||
#
|
||||
# The Original Code is Mozilla Transaction Manager.
|
||||
#
|
||||
# The Initial Developer of the Original Code is
|
||||
# Netscape Communications Corp.
|
||||
# Portions created by the Initial Developer are Copyright (C) 2003
|
||||
# the Initial Developer. All Rights Reserved.
|
||||
#
|
||||
# Contributor(s):
|
||||
# John Gaunt <jgaunt@netscape.com>
|
||||
#
|
||||
# Alternatively, the contents of this file may be used under the terms of
|
||||
# either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
# in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
# of those above. If you wish to allow use of your version of this file only
|
||||
# under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
# use your version of this file under the terms of the MPL, indicate your
|
||||
# decision by deleting the provisions above and replace them with the notice
|
||||
# and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
# the provisions above, a recipient may use your version of this file under
|
||||
# the terms of any one of the MPL, the GPL or the LGPL.
|
||||
#
|
||||
# ***** END LICENSE BLOCK *****
|
||||
|
||||
DEPTH = ../../../../..
|
||||
topsrcdir = @top_srcdir@
|
||||
srcdir = @srcdir@
|
||||
VPATH = @srcdir@
|
||||
|
||||
include $(DEPTH)/config/autoconf.mk
|
||||
|
||||
MODULE = ipcd
|
||||
LIBRARY_NAME = transmngr_s
|
||||
FORCE_STATIC_LIB = 1
|
||||
MODULE_NAME = ipcd
|
||||
|
||||
REQUIRES = string \
|
||||
xpcom \
|
||||
$(NULL)
|
||||
|
||||
CPPSRCS = \
|
||||
tmTransactionService.cpp \
|
||||
$(NULL)
|
||||
|
||||
include $(topsrcdir)/config/config.mk
|
||||
|
||||
LOCAL_INCLUDES = \
|
||||
-I$(srcdir)/../common \
|
||||
$(NULL)
|
||||
|
||||
include $(topsrcdir)/config/rules.mk
|
|
@ -0,0 +1,504 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla Transaction Manager.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corp.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2003
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* John Gaunt <jgaunt@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include "nsCOMPtr.h"
|
||||
#include "nsIServiceManager.h"
|
||||
#include "nsReadableUtils.h"
|
||||
#include "plstr.h"
|
||||
#include "ipcITransactionObserver.h"
|
||||
#include "tmTransaction.h"
|
||||
#include "tmTransactionService.h"
|
||||
#include "tmUtils.h"
|
||||
|
||||
static const nsID kTransModuleID = TRANSACTION_MODULE_ID;
|
||||
|
||||
struct tm_waiting_msg {
|
||||
tmTransaction trans; // a transaction waiting to be sent to a queue
|
||||
char* domainName; // the short queue name
|
||||
|
||||
~tm_waiting_msg();
|
||||
};
|
||||
|
||||
tm_waiting_msg::~tm_waiting_msg() {
|
||||
if (domainName)
|
||||
PL_strfree(domainName);
|
||||
}
|
||||
|
||||
struct tm_queue_mapping {
|
||||
PRInt32 queueID; // the ID in the TM
|
||||
char* domainName; // used by the consumers of this service
|
||||
char* joinedQueueName; // used by the service -- namespace + domain name
|
||||
|
||||
~tm_queue_mapping();
|
||||
};
|
||||
|
||||
tm_queue_mapping::~tm_queue_mapping() {
|
||||
if (domainName)
|
||||
PL_strfree(domainName);
|
||||
if (joinedQueueName)
|
||||
PL_strfree(joinedQueueName);
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Constructor and Destructor
|
||||
|
||||
tmTransactionService::~tmTransactionService() {
|
||||
|
||||
// just destroy this, it contains 2 pointers it doesn't own.
|
||||
if (mObservers)
|
||||
PL_HashTableDestroy(mObservers);
|
||||
|
||||
PRUint32 index = 0;
|
||||
PRUint32 size = mWaitingMessages.Size();
|
||||
tm_waiting_msg *msg = nsnull;
|
||||
for ( ; index < size; index ++) {
|
||||
msg = (tm_waiting_msg*) mWaitingMessages[index];
|
||||
delete msg;
|
||||
}
|
||||
|
||||
size = mQueueMaps.Size();
|
||||
tm_queue_mapping *qmap = nsnull;
|
||||
for (index = 0; index < size; index++) {
|
||||
qmap = (tm_queue_mapping*) mQueueMaps[index];
|
||||
if (qmap)
|
||||
delete qmap;
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// ISupports
|
||||
|
||||
NS_IMPL_ISUPPORTS2(tmTransactionService,
|
||||
ipcITransactionService,
|
||||
ipcIMessageObserver)
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// ipcITransactionService
|
||||
|
||||
NS_IMETHODIMP
|
||||
tmTransactionService::Init(const nsACString & aNamespace) {
|
||||
|
||||
// register with the IPC service
|
||||
ipcService = do_GetService("@mozilla.org/ipc/service;1");
|
||||
if (!ipcService)
|
||||
return NS_ERROR_FAILURE;
|
||||
if(NS_FAILED(ipcService->SetMessageObserver(kTransModuleID, this)))
|
||||
return NS_ERROR_FAILURE;
|
||||
|
||||
// get the lock service
|
||||
lockService = do_GetService("@mozilla.org/ipc/lock-service;1");
|
||||
if (!lockService)
|
||||
return NS_ERROR_FAILURE;
|
||||
|
||||
// create some internal storage
|
||||
mObservers = PL_NewHashTable(20,
|
||||
PL_HashString,
|
||||
PL_CompareStrings,
|
||||
PL_CompareValues, 0, 0);
|
||||
if (!mObservers)
|
||||
return NS_ERROR_FAILURE;
|
||||
|
||||
// init some internal storage
|
||||
mQueueMaps.Init();
|
||||
mWaitingMessages.Init();
|
||||
|
||||
// store the namespace
|
||||
mNamespace.Assign(aNamespace);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
tmTransactionService::Attach(const nsACString & aDomainName,
|
||||
ipcITransactionObserver *aObserver,
|
||||
PRBool aLockingCall) {
|
||||
|
||||
// if the queue already exists, then someone else is attached to it. must
|
||||
// return an error here. Only one module attached to a queue per app.
|
||||
if (GetQueueID(aDomainName) != TM_NO_ID)
|
||||
return TM_ERROR_QUEUE_EXISTS;
|
||||
|
||||
// create the full queue name: namespace + queue
|
||||
nsCString jQName;
|
||||
jQName.Assign(mNamespace);
|
||||
jQName.Append(aDomainName);
|
||||
|
||||
// this char* has two homes, make sure it gets PL_free()ed properly
|
||||
char* joinedQueueName = ToNewCString(jQName);
|
||||
if (!joinedQueueName)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
|
||||
// link the observer to the joinedqueuename. home #1 for joinedQueueName
|
||||
// these currently don't get removed until the destructor on this is called.
|
||||
PL_HashTableAdd(mObservers, joinedQueueName, aObserver);
|
||||
|
||||
// store the domainName and JoinedQueueName, create a place to store the ID
|
||||
tm_queue_mapping *qm = new tm_queue_mapping();
|
||||
if (!qm)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
qm->queueID = TM_NO_ID; // initially no ID for the queue
|
||||
qm->joinedQueueName = joinedQueueName; // home #2, owner of joinedQueueName
|
||||
qm->domainName = ToNewCString(aDomainName);
|
||||
if (!qm->domainName) {
|
||||
PL_HashTableRemove(mObservers, joinedQueueName);
|
||||
delete qm;
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
mQueueMaps.Append(qm);
|
||||
|
||||
nsresult rv = NS_ERROR_FAILURE;
|
||||
tmTransaction trans;
|
||||
|
||||
// acquire a lock if neccessary
|
||||
if (aLockingCall)
|
||||
lockService->AcquireLock(joinedQueueName, nsnull, PR_TRUE);
|
||||
|
||||
if (NS_SUCCEEDED(trans.Init(0, // no IPC client
|
||||
TM_NO_ID, // qID gets returned to us
|
||||
TM_ATTACH, // action
|
||||
NS_OK, // default status
|
||||
(PRUint8 *)joinedQueueName, // qName gets copied
|
||||
PL_strlen(joinedQueueName)+1))) { // message length
|
||||
// send the attach msg
|
||||
SendMessage(&trans, PR_TRUE); // synchronous
|
||||
rv = NS_OK;
|
||||
}
|
||||
|
||||
// drop the lock if neccessary
|
||||
if (aLockingCall)
|
||||
lockService->ReleaseLock(joinedQueueName);
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
// actual removal of the observer takes place when we get the detach reply
|
||||
NS_IMETHODIMP
|
||||
tmTransactionService::Detach(const nsACString & aDomainName) {
|
||||
|
||||
// asynchronous detach
|
||||
return SendDetachOrFlush(GetQueueID(aDomainName), TM_DETACH, PR_FALSE);
|
||||
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
tmTransactionService::Flush(const nsACString & aDomainName,
|
||||
PRBool aLockingCall) {
|
||||
// acquire a lock if neccessary
|
||||
if (aLockingCall)
|
||||
lockService->AcquireLock(GetJoinedQueueName(aDomainName), nsnull, PR_TRUE);
|
||||
|
||||
// synchronous flush
|
||||
nsresult rv = SendDetachOrFlush(GetQueueID(aDomainName), TM_FLUSH, PR_TRUE);
|
||||
|
||||
// drop the lock if neccessary
|
||||
if (aLockingCall)
|
||||
lockService->ReleaseLock(GetJoinedQueueName(aDomainName));
|
||||
|
||||
return rv;
|
||||
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
tmTransactionService::PostTransaction(const nsACString & aDomainName,
|
||||
const PRUint8 *aData,
|
||||
PRUint32 aDataLen) {
|
||||
|
||||
tmTransaction trans;
|
||||
if (NS_SUCCEEDED(trans.Init(0, // no IPC client
|
||||
GetQueueID(aDomainName), // qID returned to us
|
||||
TM_POST, // action
|
||||
NS_OK, // default status
|
||||
aData, // message data
|
||||
aDataLen))) { // message length
|
||||
if (trans.GetQueueID() == TM_NO_ID) {
|
||||
// stack it and pack it
|
||||
tm_waiting_msg *msg = new tm_waiting_msg();
|
||||
if (!msg)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
msg->trans = trans;
|
||||
msg->domainName = ToNewCString(aDomainName);
|
||||
if (!msg->domainName) {
|
||||
delete msg;
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
mWaitingMessages.Append(msg);
|
||||
}
|
||||
else {
|
||||
// send it
|
||||
SendMessage(&trans, PR_FALSE);
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// ipcIMessageObserver
|
||||
|
||||
NS_IMETHODIMP
|
||||
tmTransactionService::OnMessageAvailable(const nsID & aTarget,
|
||||
const PRUint8 *aData,
|
||||
PRUint32 aDataLength) {
|
||||
|
||||
nsresult rv = NS_ERROR_OUT_OF_MEMORY; // prime the return value
|
||||
|
||||
tmTransaction *trans = new tmTransaction();
|
||||
if (trans) {
|
||||
rv = trans->Init(0, // no IPC client ID
|
||||
TM_INVALID_ID, // in aData
|
||||
TM_INVALID_ID, // in aData
|
||||
TM_INVALID_ID, // in aData
|
||||
aData, // message data
|
||||
aDataLength); // message length
|
||||
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
switch(trans->GetAction()) {
|
||||
case TM_ATTACH_REPLY:
|
||||
OnAttachReply(trans);
|
||||
break;
|
||||
case TM_POST_REPLY:
|
||||
// OnPostReply() would be called here
|
||||
// isn't neccessary at the current time 2/19/03
|
||||
break;
|
||||
case TM_DETACH_REPLY:
|
||||
OnDetachReply(trans);
|
||||
break;
|
||||
case TM_FLUSH_REPLY:
|
||||
OnFlushReply(trans);
|
||||
break;
|
||||
case TM_POST:
|
||||
OnPost(trans);
|
||||
break;
|
||||
default: // error, should not happen
|
||||
NS_NOTREACHED("Recieved a TM reply outside of mapped messages");
|
||||
break;
|
||||
}
|
||||
}
|
||||
delete trans;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Protected Member Functions
|
||||
|
||||
void
|
||||
tmTransactionService::SendMessage(tmTransaction *aTrans, PRBool aSync) {
|
||||
|
||||
NS_ASSERTION(aTrans, "tmTransactionService::SendMessage called with null transaction");
|
||||
NS_ASSERTION(ipcService, "Failed to get the ipcService");
|
||||
|
||||
ipcService->SendMessage(0,
|
||||
kTransModuleID,
|
||||
aTrans->GetRawMessage(),
|
||||
aTrans->GetRawMessageLength(),
|
||||
aSync);
|
||||
}
|
||||
|
||||
void
|
||||
tmTransactionService::OnAttachReply(tmTransaction *aTrans) {
|
||||
|
||||
// if we attached, store the queue's ID
|
||||
if (aTrans->GetStatus() >= 0) {
|
||||
|
||||
PRUint32 size = mQueueMaps.Size();
|
||||
tm_queue_mapping *qmap = nsnull;
|
||||
for (PRUint32 index = 0; index < size; index++) {
|
||||
qmap = (tm_queue_mapping*) mQueueMaps[index];
|
||||
if (qmap &&
|
||||
PL_strcmp(qmap->joinedQueueName, (char*) aTrans->GetMessage()) == 0) {
|
||||
|
||||
// set the ID in the mapping
|
||||
qmap->queueID = aTrans->GetQueueID();
|
||||
// send any stored messges to the queue
|
||||
DispatchStoredMessages(qmap);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// notify the observer we have attached (or didn't)
|
||||
ipcITransactionObserver *observer =
|
||||
(ipcITransactionObserver *)PL_HashTableLookup(mObservers,
|
||||
(char*)aTrans->GetMessage());
|
||||
if (observer)
|
||||
observer->OnAttachReply(aTrans->GetQueueID(), aTrans->GetStatus());
|
||||
}
|
||||
|
||||
void
|
||||
tmTransactionService::OnDetachReply(tmTransaction *aTrans) {
|
||||
|
||||
tm_queue_mapping *qmap = GetQueueMap(aTrans->GetQueueID());
|
||||
|
||||
// get the observer before we release the hashtable entry
|
||||
ipcITransactionObserver *observer =
|
||||
(ipcITransactionObserver *)PL_HashTableLookup(mObservers,
|
||||
qmap->joinedQueueName);
|
||||
|
||||
// if it was removed, clean up
|
||||
if (aTrans->GetStatus() >= 0) {
|
||||
|
||||
// remove the link between observer and queue
|
||||
PL_HashTableRemove(mObservers, qmap->joinedQueueName);
|
||||
|
||||
// remove the mapping of queue names and id
|
||||
mQueueMaps.Remove(qmap);
|
||||
delete qmap;
|
||||
}
|
||||
|
||||
|
||||
// notify the observer -- could be didn't detach
|
||||
if (observer)
|
||||
observer->OnDetachReply(aTrans->GetQueueID(), aTrans->GetStatus());
|
||||
}
|
||||
|
||||
void
|
||||
tmTransactionService::OnFlushReply(tmTransaction *aTrans) {
|
||||
|
||||
ipcITransactionObserver *observer =
|
||||
(ipcITransactionObserver *)PL_HashTableLookup(mObservers,
|
||||
GetJoinedQueueName(aTrans->GetQueueID()));
|
||||
if (observer)
|
||||
observer->OnFlushReply(aTrans->GetQueueID(), aTrans->GetStatus());
|
||||
}
|
||||
|
||||
void
|
||||
tmTransactionService::OnPost(tmTransaction *aTrans) {
|
||||
|
||||
ipcITransactionObserver *observer =
|
||||
(ipcITransactionObserver*) PL_HashTableLookup(mObservers,
|
||||
GetJoinedQueueName(aTrans->GetQueueID()));
|
||||
if (observer)
|
||||
observer->OnTransactionAvailable(aTrans->GetQueueID(),
|
||||
aTrans->GetMessage(),
|
||||
aTrans->GetMessageLength());
|
||||
}
|
||||
|
||||
void
|
||||
tmTransactionService::DispatchStoredMessages(tm_queue_mapping *aQMapping) {
|
||||
|
||||
PRUint32 size = mWaitingMessages.Size();
|
||||
tm_waiting_msg *msg = nsnull;
|
||||
for (PRUint32 index = 0; index < size; index ++) {
|
||||
msg = (tm_waiting_msg*) mWaitingMessages[index];
|
||||
// if the message is waiting on the queue passed in
|
||||
if (msg && strcmp(aQMapping->domainName, msg->domainName) == 0) {
|
||||
|
||||
// found a match, send it and remove
|
||||
msg->trans.SetQueueID(aQMapping->queueID);
|
||||
SendMessage(&(msg->trans), PR_FALSE);
|
||||
|
||||
// clean up
|
||||
mWaitingMessages.Remove(msg);
|
||||
delete msg;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// searches against the short queue name
|
||||
PRInt32
|
||||
tmTransactionService::GetQueueID(const nsACString & aDomainName) {
|
||||
|
||||
PRUint32 size = mQueueMaps.Size();
|
||||
tm_queue_mapping *qmap = nsnull;
|
||||
for (PRUint32 index = 0; index < size; index++) {
|
||||
qmap = (tm_queue_mapping*) mQueueMaps[index];
|
||||
if (qmap && aDomainName.Equals(qmap->domainName))
|
||||
return qmap->queueID;
|
||||
}
|
||||
return TM_NO_ID;
|
||||
}
|
||||
|
||||
char*
|
||||
tmTransactionService::GetJoinedQueueName(PRUint32 aQueueID) {
|
||||
|
||||
PRUint32 size = mQueueMaps.Size();
|
||||
tm_queue_mapping *qmap = nsnull;
|
||||
for (PRUint32 index = 0; index < size; index++) {
|
||||
qmap = (tm_queue_mapping*) mQueueMaps[index];
|
||||
if (qmap && qmap->queueID == aQueueID)
|
||||
return qmap->joinedQueueName;
|
||||
}
|
||||
return nsnull;
|
||||
}
|
||||
|
||||
char*
|
||||
tmTransactionService::GetJoinedQueueName(const nsACString & aDomainName) {
|
||||
|
||||
PRUint32 size = mQueueMaps.Size();
|
||||
tm_queue_mapping *qmap = nsnull;
|
||||
for (PRUint32 index = 0; index < size; index++) {
|
||||
qmap = (tm_queue_mapping*) mQueueMaps[index];
|
||||
if (qmap && aDomainName.Equals(qmap->domainName))
|
||||
return qmap->joinedQueueName;
|
||||
}
|
||||
return nsnull;
|
||||
}
|
||||
|
||||
tm_queue_mapping*
|
||||
tmTransactionService::GetQueueMap(PRUint32 aQueueID) {
|
||||
|
||||
PRUint32 size = mQueueMaps.Size();
|
||||
tm_queue_mapping *qmap = nsnull;
|
||||
for (PRUint32 index = 0; index < size; index++) {
|
||||
qmap = (tm_queue_mapping*) mQueueMaps[index];
|
||||
if (qmap && qmap->queueID == aQueueID)
|
||||
return qmap;
|
||||
}
|
||||
return nsnull;
|
||||
}
|
||||
|
||||
nsresult
|
||||
tmTransactionService::SendDetachOrFlush(PRUint32 aQueueID,
|
||||
PRUint32 aAction,
|
||||
PRBool aSync) {
|
||||
|
||||
// if the queue isn't attached to, just return
|
||||
if (aQueueID == TM_NO_ID)
|
||||
return NS_ERROR_UNEXPECTED;
|
||||
|
||||
tmTransaction trans;
|
||||
if (NS_SUCCEEDED(trans.Init(0, // no IPC client
|
||||
aQueueID, // qID to detach from
|
||||
aAction, // action
|
||||
NS_OK, // default status
|
||||
nsnull, // no message
|
||||
0))) { // no message
|
||||
// send it
|
||||
SendMessage(&trans, aSync);
|
||||
return NS_OK;
|
||||
}
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
|
@ -0,0 +1,197 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla Transaction Manager.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corp.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2003
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* John Gaunt <jgaunt@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#ifndef _tmTransactionService_H_
|
||||
#define _tmTransactionService_H_
|
||||
|
||||
#include "ipcIService.h"
|
||||
#include "ipcILockService.h"
|
||||
#include "ipcIMessageObserver.h"
|
||||
#include "ipcITransactionService.h"
|
||||
#include "nsString.h"
|
||||
#include "nsVoidArray.h"
|
||||
#include "plhash.h"
|
||||
#include "tmTransaction.h"
|
||||
#include "tmVector.h"
|
||||
|
||||
struct tm_queue_mapping;
|
||||
|
||||
/**
|
||||
* The tmTransactionService shares packets of information
|
||||
* (transactions) with other Gecko based applications interested in the same
|
||||
* namespace and domain. An application registers with the Transaction Service
|
||||
* for a particular namespace and domain and then can post transactions to the
|
||||
* service and receive transactions from the service.
|
||||
*
|
||||
* For applications using the Transaction Service to share changes in state that
|
||||
* get reflected in files on disk there are certain pattersn to follow to ensure
|
||||
* data loss does not occur.
|
||||
*
|
||||
* Startup: XXX docs needed
|
||||
*
|
||||
* Shutdown/writing to disk: XXX docs needed
|
||||
*
|
||||
*
|
||||
*/
|
||||
class tmTransactionService : public ipcITransactionService,
|
||||
public ipcIMessageObserver
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Constructor & Destructor
|
||||
tmTransactionService() : mObservers(0) {};
|
||||
|
||||
/**
|
||||
* Reclaim all the memory allocated: PL_hashtable, tmVectors
|
||||
*/
|
||||
virtual ~tmTransactionService();
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Interface Declarations
|
||||
|
||||
// for API docs, see the respective *.idl files
|
||||
NS_DECL_ISUPPORTS
|
||||
NS_DECL_IPCITRANSACTIONSERVICE
|
||||
NS_DECL_IPCIMESSAGEOBSERVER
|
||||
|
||||
protected:
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Protected Member Functions
|
||||
|
||||
/**
|
||||
* Pulls the raw message out of the transaction and sends it to the IPC
|
||||
* service to be delivered to the TM.
|
||||
*
|
||||
* @param aTrans
|
||||
* The transaction to send to the TM
|
||||
*
|
||||
* @param aSync
|
||||
* If TRUE, calling thread will be blocked until a reply is
|
||||
* received.
|
||||
*/
|
||||
void SendMessage(tmTransaction *aTrans, PRBool aSync);
|
||||
|
||||
// handlers for reply messages from TransactionManager
|
||||
|
||||
/**
|
||||
* Pulls the queueID out of the ATTACH_REPLY message and stores it in the
|
||||
* proper tm_queue_mapping object. Calls DispatchStoredMessages() to make
|
||||
* sure we send any messages that have been waiting on the ATTACH_REPLY.
|
||||
* Also calls the OnAttachReply() method for the observer of the queue.
|
||||
*/
|
||||
void OnAttachReply(tmTransaction *aTrans);
|
||||
|
||||
/**
|
||||
* Removes the tm_queue_mapping object and calls the OnDetachReply() method
|
||||
* on the observer of the queue detached.
|
||||
*/
|
||||
void OnDetachReply(tmTransaction *aTrans);
|
||||
|
||||
/**
|
||||
* Calls the OnFlushReply method of the observer of the queue.
|
||||
*/
|
||||
void OnFlushReply(tmTransaction *aTrans);
|
||||
|
||||
/**
|
||||
* Calls the OnPost method of the observer of the queue.
|
||||
*/
|
||||
void OnPost(tmTransaction *aTrans);
|
||||
|
||||
// other helper functions
|
||||
|
||||
/**
|
||||
* Cycle through the collection of transactions waiting to go out and
|
||||
* send any that are waiting on an ATTACH_REPLY from the queue
|
||||
* specified by the tm_queue_mapping passed in.
|
||||
*/
|
||||
void DispatchStoredMessages(tm_queue_mapping *aQMapping);
|
||||
|
||||
// helper methods for accessing the void arrays
|
||||
|
||||
/**
|
||||
* @returns the ID corresponding to the domain name passed in
|
||||
* @returns TM_NO_ID if the name is not found.
|
||||
*/
|
||||
PRInt32 GetQueueID(const nsACString & aDomainName);
|
||||
|
||||
/**
|
||||
* @returns the joined queue name - namespace + domain
|
||||
* (prefs, cookies etc) corresponding to the ID passed in.
|
||||
* @returns nsnull if the ID is not found.
|
||||
*/
|
||||
char* GetJoinedQueueName(PRUint32 aQueueID);
|
||||
|
||||
/**
|
||||
* @returns the joined queue name - namespace + domain
|
||||
* (prefs, cookies etc) corresponding to the ID passed in.
|
||||
* @returns nsnull if the ID is not found.
|
||||
*/
|
||||
char* GetJoinedQueueName(const nsACString & aDomainName);
|
||||
|
||||
/**
|
||||
* @returns the tm_queue_mapping object that contains the ID passed in.
|
||||
* @returns nsnull if the ID is not found.
|
||||
*/
|
||||
tm_queue_mapping* GetQueueMap(PRUint32 aQueueID);
|
||||
|
||||
/**
|
||||
* Helper method for Detach and Flush requests.
|
||||
*/
|
||||
nsresult SendDetachOrFlush(PRUint32 aQueueID,
|
||||
PRUint32 aAction,
|
||||
PRBool aSync);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Protected Member Variables
|
||||
|
||||
nsCString mNamespace; // limit domains to the namespace
|
||||
PLHashTable *mObservers; // maps qName -> ipcITransactionObserver
|
||||
|
||||
tmVector mQueueMaps; // queue - name - domain mappings
|
||||
tmVector mWaitingMessages; // messages sent before ATTACH_REPLY
|
||||
|
||||
nsCOMPtr<ipcIService> ipcService; // cache the ipc service
|
||||
nsCOMPtr<ipcILockService> lockService; // cache the lock service
|
||||
|
||||
private:
|
||||
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,6 @@
|
|||
Makefile
|
||||
tmModuleTest.exe
|
||||
tmModuleTest.ilk
|
||||
tmModuleTest.pdb
|
||||
tmModuleTest.obj
|
||||
tmModuleTest
|
|
@ -0,0 +1,69 @@
|
|||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
#
|
||||
# The contents of this file are subject to the Mozilla Public License Version
|
||||
# 1.1 (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
# http://www.mozilla.org/MPL/
|
||||
#
|
||||
# Software distributed under the License is distributed on an "AS IS" basis,
|
||||
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
# for the specific language governing rights and limitations under the
|
||||
# License.
|
||||
#
|
||||
# The Original Code is Mozilla Transaction Manager.
|
||||
#
|
||||
# The Initial Developer of the Original Code is
|
||||
# Netscape Communications Corp.
|
||||
# Portions created by the Initial Developer are Copyright (C) 2003
|
||||
# the Initial Developer. All Rights Reserved.
|
||||
#
|
||||
# Contributor(s):
|
||||
# John Gaunt <jgaunt@netscape.com>
|
||||
#
|
||||
# Alternatively, the contents of this file may be used under the terms of
|
||||
# either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
# in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
# of those above. If you wish to allow use of your version of this file only
|
||||
# under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
# use your version of this file under the terms of the MPL, indicate your
|
||||
# decision by deleting the provisions above and replace them with the notice
|
||||
# and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
# the provisions above, a recipient may use your version of this file under
|
||||
# the terms of any one of the MPL, the GPL or the LGPL.
|
||||
#
|
||||
# ***** END LICENSE BLOCK *****
|
||||
|
||||
DEPTH = ../../../../..
|
||||
topsrcdir = @top_srcdir@
|
||||
srcdir = @srcdir@
|
||||
VPATH = @srcdir@
|
||||
|
||||
include $(DEPTH)/config/autoconf.mk
|
||||
|
||||
MODULE = tm_test
|
||||
|
||||
REQUIRES = ipcd \
|
||||
nspr \
|
||||
string \
|
||||
xpcom \
|
||||
$(NULL)
|
||||
|
||||
CPPSRCS = \
|
||||
tmModuleTest.cpp \
|
||||
$(NULL)
|
||||
|
||||
SIMPLE_PROGRAMS = $(CPPSRCS:.cpp=$(BIN_SUFFIX))
|
||||
|
||||
include $(topsrcdir)/config/config.mk
|
||||
|
||||
LIBS = \
|
||||
$(EXTRA_DSO_LIBS) \
|
||||
$(MOZ_JS_LIBS) \
|
||||
$(XPCOM_LIBS) \
|
||||
$(NSPR_LIBS) \
|
||||
$(NULL)
|
||||
|
||||
include $(topsrcdir)/config/rules.mk
|
||||
|
|
@ -0,0 +1,323 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla Transaction Manager.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corp.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2003
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* John Gaunt <jgaunt@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
// transaction manager includes
|
||||
#include "ipcITransactionService.h"
|
||||
#include "ipcITransactionObserver.h"
|
||||
|
||||
// ipc daemon includes
|
||||
#include "ipcIService.h"
|
||||
|
||||
// core & xpcom ns includes
|
||||
#include "nsDebug.h"
|
||||
#include "nsIEventQueueService.h"
|
||||
#include "nsIServiceManager.h"
|
||||
#include "nsIComponentRegistrar.h"
|
||||
#include "nsString.h"
|
||||
|
||||
// nspr includes
|
||||
#include "prmem.h"
|
||||
#include "plgetopt.h"
|
||||
#include "nspr.h"
|
||||
#include "prlog.h"
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Testing/Debug/Logging BEGIN
|
||||
|
||||
const int NameSize = 1024;
|
||||
|
||||
/* command line options */
|
||||
PRIntn optDebug = 0;
|
||||
char optMode = 's';
|
||||
char *profileName = new char[NameSize];
|
||||
char *queueName = new char[NameSize];
|
||||
|
||||
char *data = new char[NameSize];
|
||||
PRUint32 dataLen = 10; // includes the null terminator for "test data"
|
||||
|
||||
// Testing/Debug/Logging END
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#define RETURN_IF_FAILED(rv, step) \
|
||||
PR_BEGIN_MACRO \
|
||||
if (NS_FAILED(rv)) { \
|
||||
printf("*** %s failed: rv=%x\n", step, rv); \
|
||||
return rv;\
|
||||
} \
|
||||
PR_END_MACRO
|
||||
|
||||
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
|
||||
static nsIEventQueue* gEventQ = nsnull;
|
||||
static PRBool gKeepRunning = PR_TRUE;
|
||||
//static PRInt32 gMsgCount = 0;
|
||||
static ipcIService *gIpcServ = nsnull;
|
||||
static ipcITransactionService *gTransServ = nsnull;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
class myTransactionObserver : public ipcITransactionObserver
|
||||
{
|
||||
public:
|
||||
NS_DECL_ISUPPORTS
|
||||
NS_DECL_IPCITRANSACTIONOBSERVER
|
||||
|
||||
myTransactionObserver() { }
|
||||
};
|
||||
|
||||
NS_IMPL_ISUPPORTS1(myTransactionObserver, ipcITransactionObserver)
|
||||
|
||||
NS_IMETHODIMP myTransactionObserver::OnTransactionAvailable(PRUint32 aQueueID, const PRUint8 *aData, PRUint32 aDataLen)
|
||||
{
|
||||
printf("tmModuleTest: myTransactionObserver::OnTransactionAvailable [%s]\n", aData);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP myTransactionObserver::OnAttachReply(PRUint32 aQueueID, PRUint32 aStatus)
|
||||
{
|
||||
printf("tmModuleTest: myTransactionObserver::OnAttachReply [%d]\n", aStatus);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP myTransactionObserver::OnDetachReply(PRUint32 aQueueID, PRUint32 aStatus)
|
||||
{
|
||||
printf("tmModuleTest: myTransactionObserver::OnDetachReply [%d]\n", aStatus);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP myTransactionObserver::OnFlushReply(PRUint32 aQueueID, PRUint32 aStatus)
|
||||
{
|
||||
printf("tmModuleTest: myTransactionObserver::OnFlushReply [%d]\n", aStatus);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
int main(PRInt32 argc, char *argv[])
|
||||
{
|
||||
nsresult rv;
|
||||
|
||||
// default string values
|
||||
strcpy(profileName, "defaultProfile");
|
||||
strcpy(queueName, "defaultQueue");
|
||||
strcpy(data, "test data");
|
||||
|
||||
{ // scope the command line option gathering (needed for some reason)
|
||||
|
||||
// Get command line options
|
||||
PLOptStatus os;
|
||||
PLOptState *opt = PL_CreateOptState(argc, argv, "bdfhlp:q:");
|
||||
|
||||
while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
|
||||
{
|
||||
if (PL_OPT_BAD == os) continue;
|
||||
switch (opt->option)
|
||||
{
|
||||
case 'b': /* broadcast a bunch of messages */
|
||||
printf("tmModuleTest: broadcaster\n");
|
||||
optMode = 'b';
|
||||
break;
|
||||
case 'd': /* debug mode */
|
||||
printf("tmModuleTest: debugging baby\n");
|
||||
optDebug = 1;
|
||||
break;
|
||||
case 'f': /* broadcast and flush */
|
||||
printf("tmModuleTest: flusher\n");
|
||||
optMode = 'f';
|
||||
break;
|
||||
case 'h': /* broadcast and detach */
|
||||
printf("tmModuleTest: hit-n-run\n");
|
||||
optMode = 'h';
|
||||
break;
|
||||
case 'l': /* don't broadcast, just listen */
|
||||
printf("tmModuleTest: listener\n");
|
||||
optMode = 'l';
|
||||
break;
|
||||
case 'p': /* set the profile name */
|
||||
strcpy(profileName, opt->value);
|
||||
printf("tmModuleTest: profilename:%s\n",profileName);
|
||||
break;
|
||||
case 'q': /* set the queue name */
|
||||
strcpy(queueName, opt->value);
|
||||
printf("tmModuleTest: queuename:%s\n",queueName);
|
||||
break;
|
||||
default:
|
||||
printf("tmModuleTest: default\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
PL_DestroyOptState(opt);
|
||||
} // scope the command line option gathering (needed for some reason)
|
||||
|
||||
{ // scope the nsCOMPtrs
|
||||
|
||||
printf("tmModuleTest: Starting xpcom\n");
|
||||
|
||||
// xpcom startup stuff
|
||||
nsCOMPtr<nsIServiceManager> servMan;
|
||||
NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
|
||||
nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
|
||||
NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
|
||||
if (registrar)
|
||||
registrar->AutoRegister(nsnull);
|
||||
|
||||
// Create the Event Queue for this thread...
|
||||
nsCOMPtr<nsIEventQueueService> eqs = do_GetService(kEventQueueServiceCID, &rv);
|
||||
RETURN_IF_FAILED(rv, "do_GetService(EventQueueService)");
|
||||
|
||||
rv = eqs->CreateMonitoredThreadEventQueue();
|
||||
RETURN_IF_FAILED(rv, "CreateMonitoredThreadEventQueue");
|
||||
|
||||
rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
|
||||
RETURN_IF_FAILED(rv, "GetThreadEventQueue");
|
||||
|
||||
// Need to make sure the ipc system has been started
|
||||
printf("tmModuleTest: getting ipc service\n");
|
||||
nsCOMPtr<ipcIService> ipcServ(do_GetService("@mozilla.org/ipc/service;1", &rv));
|
||||
RETURN_IF_FAILED(rv, "do_GetService(ipcServ)");
|
||||
NS_ADDREF(gIpcServ = ipcServ);
|
||||
|
||||
// Get the transaction service
|
||||
printf("tmModuleTest: getting transaction service\n");
|
||||
nsCOMPtr<ipcITransactionService> transServ(do_GetService("@mozilla.org/ipc/transaction-service;1", &rv));
|
||||
RETURN_IF_FAILED(rv, "do_GetService(transServ)");
|
||||
NS_ADDREF(gTransServ = transServ);
|
||||
|
||||
// transaction specifc startup stuff, done for all cases
|
||||
|
||||
nsCOMPtr<ipcITransactionObserver> observ = new myTransactionObserver();
|
||||
|
||||
// initialize the transaction service with a specific profile
|
||||
gTransServ->Init(nsDependentCString(profileName));
|
||||
printf("tmModuleTest: using profileName [%s]\n", profileName);
|
||||
|
||||
// attach to the queue in the transaction manager
|
||||
gTransServ->Attach(nsDependentCString(queueName), observ, PR_TRUE);
|
||||
printf("tmModuleTest: observing queue [%s]\n", queueName);
|
||||
|
||||
|
||||
// run specific patterns based on the mode
|
||||
int i = 0; // wasn't working inside the cases
|
||||
switch (optMode)
|
||||
{
|
||||
case 's':
|
||||
printf("tmModuleTest: start standard\n");
|
||||
// post a couple events
|
||||
for (; i < 5 ; i++) {
|
||||
gTransServ->PostTransaction(nsDependentCString(queueName), (PRUint8 *)data, dataLen);
|
||||
}
|
||||
// listen for events
|
||||
while (gKeepRunning)
|
||||
gEventQ->ProcessPendingEvents();
|
||||
printf("tmModuleTest: end standard\n");
|
||||
break;
|
||||
case 'b':
|
||||
printf("tmModuleTest: start broadcast\n");
|
||||
// post a BUNCH of messages
|
||||
for (; i < 50; i++) {
|
||||
gTransServ->PostTransaction(nsDependentCString(queueName), (PRUint8 *)data, dataLen);
|
||||
}
|
||||
// listen for events
|
||||
while (gKeepRunning)
|
||||
gEventQ->ProcessPendingEvents();
|
||||
printf("tmModuleTest: end broadcast\n");
|
||||
break;
|
||||
case 'f':
|
||||
printf("tmModuleTest: start flush\n");
|
||||
// post a couple events
|
||||
for (; i < 5; i++) {
|
||||
gTransServ->PostTransaction(nsDependentCString(queueName), (PRUint8 *)data, dataLen);
|
||||
}
|
||||
// flush the queue
|
||||
gTransServ->Flush(nsDependentCString(queueName), PR_TRUE);
|
||||
// post a couple events
|
||||
for (i=0; i < 8; i++) {
|
||||
gTransServ->PostTransaction(nsDependentCString(queueName), (PRUint8 *)data, dataLen);
|
||||
}
|
||||
// listen for events
|
||||
while (gKeepRunning)
|
||||
gEventQ->ProcessPendingEvents();
|
||||
// detach
|
||||
gTransServ->Detach(nsDependentCString(queueName));
|
||||
printf("tmModuleTest: end flush\n");
|
||||
break;
|
||||
case 'h':
|
||||
printf("tmModuleTest: start hit-n-run\n");
|
||||
// post a couple events
|
||||
for (; i < 5; i++) {
|
||||
gTransServ->PostTransaction(nsDependentCString(queueName), (PRUint8 *)data, dataLen);
|
||||
}
|
||||
// detach
|
||||
gTransServ->Detach(nsDependentCString(queueName));
|
||||
printf("tmModuleTest: end hit-n-run\n");
|
||||
break;
|
||||
case 'l':
|
||||
printf("tmModuleTest: start listener\n");
|
||||
// listen for events
|
||||
while (gKeepRunning)
|
||||
gEventQ->ProcessPendingEvents();
|
||||
printf("tmModuleTest: end listener\n");
|
||||
break;
|
||||
default :
|
||||
printf("tmModuleTest: start & end default\n");
|
||||
break;
|
||||
}
|
||||
|
||||
// shutdown process
|
||||
|
||||
NS_RELEASE(gTransServ);
|
||||
NS_RELEASE(gIpcServ);
|
||||
|
||||
printf("tmModuleTest: processing remaining events\n");
|
||||
|
||||
// process any remaining events
|
||||
PLEvent *ev;
|
||||
while (NS_SUCCEEDED(gEventQ->GetEvent(&ev)) && ev)
|
||||
gEventQ->HandleEvent(ev);
|
||||
|
||||
printf("tmModuleTest: done\n");
|
||||
} // this scopes the nsCOMPtrs
|
||||
|
||||
// helps with shutdown on some cases
|
||||
PR_Sleep(PR_SecondsToInterval(4));
|
||||
|
||||
// no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
|
||||
rv = NS_ShutdownXPCOM(nsnull);
|
||||
NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
Makefile
|
|
@ -0,0 +1,65 @@
|
|||
# vim: noexpandtab ts=4 sw=4
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
#
|
||||
# The contents of this file are subject to the Mozilla Public License Version
|
||||
# 1.1 (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
# http://www.mozilla.org/MPL/
|
||||
#
|
||||
# Software distributed under the License is distributed on an "AS IS" basis,
|
||||
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
# for the specific language governing rights and limitations under the
|
||||
# License.
|
||||
#
|
||||
# The Original Code is Mozilla IPC.
|
||||
#
|
||||
# The Initial Developer of the Original Code is
|
||||
# Netscape Communications Corporation.
|
||||
# Portions created by the Initial Developer are Copyright (C) 2002
|
||||
# the Initial Developer. All Rights Reserved.
|
||||
#
|
||||
# Contributor(s):
|
||||
# Darin Fisher <darin@netscape.com>
|
||||
#
|
||||
# Alternatively, the contents of this file may be used under the terms of
|
||||
# either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
# in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
# of those above. If you wish to allow use of your version of this file only
|
||||
# under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
# use your version of this file under the terms of the MPL, indicate your
|
||||
# decision by deleting the provisions above and replace them with the notice
|
||||
# and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
# the provisions above, a recipient may use your version of this file under
|
||||
# the terms of any one of the MPL, the GPL or the LGPL.
|
||||
#
|
||||
# ***** END LICENSE BLOCK *****
|
||||
|
||||
DEPTH = ../../../..
|
||||
topsrcdir = @top_srcdir@
|
||||
srcdir = @srcdir@
|
||||
VPATH = @srcdir@
|
||||
|
||||
include $(DEPTH)/config/autoconf.mk
|
||||
|
||||
MODULE = ipcd
|
||||
LIBRARY_NAME = ipcdshared_s
|
||||
FORCE_STATIC_LIB = 1
|
||||
MODULE_NAME = ipcd
|
||||
|
||||
# required for #include "nsID.h"
|
||||
REQUIRES = \
|
||||
xpcom \
|
||||
$(NULL)
|
||||
|
||||
CPPSRCS = \
|
||||
ipcLog.cpp \
|
||||
ipcConfig.cpp \
|
||||
ipcMessage.cpp \
|
||||
ipcMessagePrimitives.cpp \
|
||||
ipcStringList.cpp \
|
||||
ipcIDList.cpp \
|
||||
ipcm.cpp
|
||||
|
||||
include $(topsrcdir)/config/rules.mk
|
|
@ -0,0 +1,80 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2003
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#ifdef XP_WIN
|
||||
#else
|
||||
#include <string.h>
|
||||
#include "ipcConfig.h"
|
||||
#include "ipcLog.h"
|
||||
#include "prenv.h"
|
||||
#include "plstr.h"
|
||||
|
||||
#ifdef XP_OS2
|
||||
static const char kDefaultSocketPrefix[] = "\\socket\\mozilla-";
|
||||
static const char kDefaultSocketSuffix[] = "-ipc\\ipcd";
|
||||
#else
|
||||
static const char kDefaultSocketPrefix[] = "/tmp/.mozilla-";
|
||||
static const char kDefaultSocketSuffix[] = "-ipc/ipcd";
|
||||
#endif
|
||||
|
||||
void IPC_GetDefaultSocketPath(char *buf, PRUint32 bufLen)
|
||||
{
|
||||
const char *logName;
|
||||
int len;
|
||||
|
||||
PL_strncpyz(buf, kDefaultSocketPrefix, bufLen);
|
||||
buf += (sizeof(kDefaultSocketPrefix) - 1);
|
||||
bufLen -= (sizeof(kDefaultSocketPrefix) - 1);
|
||||
|
||||
logName = PR_GetEnv("LOGNAME");
|
||||
if (!logName || !logName[0]) {
|
||||
logName = PR_GetEnv("USER");
|
||||
if (!logName || !logName[0]) {
|
||||
LOG(("could not determine username from environment\n"));
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
PL_strncpyz(buf, logName, bufLen);
|
||||
len = strlen(logName);
|
||||
buf += len;
|
||||
bufLen -= len;
|
||||
|
||||
end:
|
||||
PL_strncpyz(buf, kDefaultSocketSuffix, bufLen);
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,91 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#ifndef ipcProto_h__
|
||||
#define ipcProto_h__
|
||||
|
||||
#if defined(XP_WIN)
|
||||
//
|
||||
// use WM_COPYDATA messages
|
||||
//
|
||||
#include "prprf.h"
|
||||
|
||||
#define IPC_WINDOW_CLASS "Mozilla:IPCWindowClass"
|
||||
#define IPC_WINDOW_NAME "Mozilla:IPCWindow"
|
||||
#define IPC_CLIENT_WINDOW_CLASS "Mozilla:IPCAppWindowClass"
|
||||
#define IPC_CLIENT_WINDOW_NAME_PREFIX "Mozilla:IPCAppWindow:"
|
||||
#define IPC_SYNC_EVENT_NAME "Local\\MozillaIPCSyncEvent"
|
||||
#define IPC_DAEMON_APP_NAME "mozipcd.exe"
|
||||
#define IPC_PATH_SEP_CHAR '\\'
|
||||
#define IPC_MODULES_DIR "ipc\\modules"
|
||||
|
||||
#define IPC_CLIENT_WINDOW_NAME_MAXLEN (sizeof(IPC_CLIENT_WINDOW_NAME_PREFIX) + 20)
|
||||
|
||||
// writes client name into buf. buf must be at least
|
||||
// IPC_CLIENT_WINDOW_NAME_MAXLEN bytes in length.
|
||||
inline void IPC_GetClientWindowName(PRUint32 pid, char *buf)
|
||||
{
|
||||
PR_snprintf(buf, IPC_CLIENT_WINDOW_NAME_MAXLEN, "%s%u",
|
||||
IPC_CLIENT_WINDOW_NAME_PREFIX, pid);
|
||||
}
|
||||
|
||||
#else
|
||||
#include "prtypes.h"
|
||||
//
|
||||
// use UNIX domain socket
|
||||
//
|
||||
#define IPC_PORT 0
|
||||
#define IPC_SOCKET_TYPE "ipc"
|
||||
#define IPC_DAEMON_APP_NAME "mozipcd"
|
||||
#ifdef XP_OS2
|
||||
#define IPC_PATH_SEP_CHAR '\\'
|
||||
#define IPC_MODULES_DIR "ipc\\modules"
|
||||
#else
|
||||
#define IPC_PATH_SEP_CHAR '/'
|
||||
#define IPC_MODULES_DIR "ipc/modules"
|
||||
#endif
|
||||
|
||||
void IPC_GetDefaultSocketPath(char *buf, PRUint32 bufLen);
|
||||
|
||||
#endif
|
||||
|
||||
// common shared configuration values
|
||||
|
||||
#define IPC_STARTUP_PIPE_NAME "ipc:startup-pipe"
|
||||
#define IPC_STARTUP_PIPE_MAGIC 0x1C
|
||||
|
||||
#endif // !ipcProto_h__
|
|
@ -0,0 +1,62 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include "ipcIDList.h"
|
||||
|
||||
ipcIDNode *
|
||||
ipcIDList::FindNode(ipcIDNode *node, const nsID &id)
|
||||
{
|
||||
while (node) {
|
||||
if (node->Equals(id))
|
||||
return node;
|
||||
node = node->mNext;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ipcIDNode *
|
||||
ipcIDList::FindNodeBefore(ipcIDNode *node, const nsID &id)
|
||||
{
|
||||
ipcIDNode *prev = NULL;
|
||||
while (node) {
|
||||
if (node->Equals(id))
|
||||
return prev;
|
||||
prev = node;
|
||||
node = node->mNext;
|
||||
}
|
||||
return NULL;
|
||||
}
|
|
@ -0,0 +1,102 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#ifndef ipcIDList_h__
|
||||
#define ipcIDList_h__
|
||||
|
||||
#include "nsID.h"
|
||||
#include "ipcList.h"
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// nsID node
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
class ipcIDNode
|
||||
{
|
||||
public:
|
||||
ipcIDNode(const nsID &id)
|
||||
: mID(id)
|
||||
{ }
|
||||
|
||||
const nsID &Value() const { return mID; }
|
||||
|
||||
PRBool Equals(const nsID &id) const { return mID.Equals(id); }
|
||||
|
||||
class ipcIDNode *mNext;
|
||||
private:
|
||||
nsID mID;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// singly-linked list of nsIDs
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
class ipcIDList : public ipcList<ipcIDNode>
|
||||
{
|
||||
public:
|
||||
typedef ipcList<ipcIDNode> Super;
|
||||
|
||||
void Prepend(const nsID &id)
|
||||
{
|
||||
Super::Prepend(new ipcIDNode(id));
|
||||
}
|
||||
|
||||
void Append(const nsID &id)
|
||||
{
|
||||
Super::Append(new ipcIDNode(id));
|
||||
}
|
||||
|
||||
const ipcIDNode *Find(const nsID &id) const
|
||||
{
|
||||
return FindNode(mHead, id);
|
||||
}
|
||||
|
||||
void FindAndDelete(const nsID &id)
|
||||
{
|
||||
ipcIDNode *node = FindNodeBefore(mHead, id);
|
||||
if (node)
|
||||
DeleteAfter(node);
|
||||
else
|
||||
DeleteFirst();
|
||||
}
|
||||
|
||||
private:
|
||||
static ipcIDNode *FindNode (ipcIDNode *head, const nsID &id);
|
||||
static ipcIDNode *FindNodeBefore(ipcIDNode *head, const nsID &id);
|
||||
};
|
||||
|
||||
#endif // !ipcIDList_h__
|
|
@ -0,0 +1,176 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#ifndef ipcList_h__
|
||||
#define ipcList_h__
|
||||
|
||||
#include "prtypes.h"
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// simple list of singly-linked objects. class T must have the following
|
||||
// structure:
|
||||
//
|
||||
// class T {
|
||||
// ...
|
||||
// public:
|
||||
// T *mNext;
|
||||
// };
|
||||
//
|
||||
// objects added to the list must be allocated with operator new.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class T>
|
||||
class ipcList
|
||||
{
|
||||
public:
|
||||
ipcList()
|
||||
: mHead(NULL)
|
||||
, mTail(NULL)
|
||||
{ }
|
||||
~ipcList() { DeleteAll(); }
|
||||
|
||||
//
|
||||
// prepends obj at the beginning of the list.
|
||||
//
|
||||
void Prepend(T *obj)
|
||||
{
|
||||
obj->mNext = mHead;
|
||||
mHead = obj;
|
||||
if (!mTail)
|
||||
mTail = mHead;
|
||||
}
|
||||
|
||||
//
|
||||
// appends obj to the end of the list.
|
||||
//
|
||||
void Append(T *obj)
|
||||
{
|
||||
obj->mNext = NULL;
|
||||
if (mTail) {
|
||||
mTail->mNext = obj;
|
||||
mTail = obj;
|
||||
}
|
||||
else
|
||||
mTail = mHead = obj;
|
||||
}
|
||||
|
||||
//
|
||||
// inserts b into the list after a.
|
||||
//
|
||||
void InsertAfter(T *a, T *b)
|
||||
{
|
||||
b->mNext = a->mNext;
|
||||
a->mNext = b;
|
||||
if (mTail == a)
|
||||
mTail = b;
|
||||
}
|
||||
|
||||
//
|
||||
// removes first element w/o deleting it
|
||||
//
|
||||
void RemoveFirst()
|
||||
{
|
||||
if (mHead)
|
||||
AdvanceHead();
|
||||
}
|
||||
|
||||
//
|
||||
// removes element after the given element w/o deleting it
|
||||
//
|
||||
void RemoveAfter(T *obj)
|
||||
{
|
||||
T *rej = obj->mNext;
|
||||
if (rej) {
|
||||
obj->mNext = rej->mNext;
|
||||
if (rej == mTail)
|
||||
mTail = obj;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// deletes first element
|
||||
//
|
||||
void DeleteFirst()
|
||||
{
|
||||
T *first = mHead;
|
||||
if (first) {
|
||||
AdvanceHead();
|
||||
delete first;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// deletes element after the given element
|
||||
//
|
||||
void DeleteAfter(T *obj)
|
||||
{
|
||||
T *rej = obj->mNext;
|
||||
if (rej) {
|
||||
RemoveAfter(obj);
|
||||
delete rej;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// deletes all elements
|
||||
//
|
||||
void DeleteAll()
|
||||
{
|
||||
while (mHead)
|
||||
DeleteFirst();
|
||||
}
|
||||
|
||||
const T *First() const { return mHead; }
|
||||
T *First() { return mHead; }
|
||||
const T *Last() const { return mTail; }
|
||||
T *Last() { return mTail; }
|
||||
|
||||
PRBool IsEmpty() const { return mHead == NULL; }
|
||||
|
||||
protected:
|
||||
void AdvanceHead()
|
||||
{
|
||||
mHead = mHead->mNext;
|
||||
if (!mHead)
|
||||
mTail = NULL;
|
||||
}
|
||||
|
||||
T *mHead;
|
||||
T *mTail;
|
||||
};
|
||||
|
||||
#endif // !ipcList_h__
|
|
@ -0,0 +1,115 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include "ipcLog.h"
|
||||
|
||||
#ifdef IPC_LOGGING
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "prenv.h"
|
||||
#include "prprf.h"
|
||||
#include "plstr.h"
|
||||
|
||||
PRBool ipcLogEnabled = PR_FALSE;
|
||||
char ipcLogPrefix[10] = {0};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// UNIX
|
||||
//-----------------------------------------------------------------------------
|
||||
#if defined(XP_UNIX) || defined(XP_OS2)
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static inline PRUint32
|
||||
WritePrefix(char *buf, PRUint32 bufLen)
|
||||
{
|
||||
return PR_snprintf(buf, bufLen, "[%u] %s ",
|
||||
(unsigned) getpid(),
|
||||
ipcLogPrefix);
|
||||
}
|
||||
#endif
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// WIN32
|
||||
//-----------------------------------------------------------------------------
|
||||
#ifdef XP_WIN
|
||||
#include <windows.h>
|
||||
|
||||
static inline PRUint32
|
||||
WritePrefix(char *buf, PRUint32 bufLen)
|
||||
{
|
||||
return PR_snprintf(buf, bufLen, "[%u:%u] %s ",
|
||||
GetCurrentProcessId(),
|
||||
GetCurrentThreadId(),
|
||||
ipcLogPrefix);
|
||||
}
|
||||
#endif
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// logging API impl
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
void
|
||||
IPC_InitLog(const char *prefix)
|
||||
{
|
||||
if (PR_GetEnv("IPC_LOG_ENABLE")) {
|
||||
ipcLogEnabled = PR_TRUE;
|
||||
PL_strncpyz(ipcLogPrefix, prefix, sizeof(ipcLogPrefix));
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
IPC_Log(const char *fmt, ... )
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
PRUint32 nb = 0;
|
||||
char buf[512];
|
||||
|
||||
if (ipcLogPrefix[0])
|
||||
nb = WritePrefix(buf, sizeof(buf));
|
||||
|
||||
PR_vsnprintf(buf + nb, sizeof(buf) - nb, fmt, ap);
|
||||
buf[sizeof(buf) - 1] = '\0';
|
||||
|
||||
fwrite(buf, strlen(buf), 1, stdout);
|
||||
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,68 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#ifndef ipcLog_h__
|
||||
#define ipcLog_h__
|
||||
|
||||
#include "prtypes.h"
|
||||
|
||||
#ifdef DEBUG
|
||||
#define IPC_LOGGING
|
||||
#endif
|
||||
|
||||
#ifdef IPC_LOGGING
|
||||
|
||||
extern PRBool ipcLogEnabled;
|
||||
extern void IPC_InitLog(const char *prefix);
|
||||
extern void IPC_Log(const char *fmt, ...);
|
||||
|
||||
#define IPC_LOG(_args) \
|
||||
PR_BEGIN_MACRO \
|
||||
if (ipcLogEnabled) \
|
||||
IPC_Log _args; \
|
||||
PR_END_MACRO
|
||||
|
||||
#define LOG(args) IPC_LOG(args)
|
||||
#define LOG_ENABLED() ipcLogEnabled
|
||||
|
||||
#else
|
||||
#define IPC_InitLog(prefix)
|
||||
#define LOG(args)
|
||||
#define LOG_ENABLED() (0)
|
||||
#endif
|
||||
|
||||
#endif // !ipcLog_h__
|
|
@ -0,0 +1,245 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "prlog.h"
|
||||
#include "ipcMessage.h"
|
||||
|
||||
ipcMessage::~ipcMessage()
|
||||
{
|
||||
if (mMsgHdr)
|
||||
free(mMsgHdr);
|
||||
}
|
||||
|
||||
void
|
||||
ipcMessage::Reset()
|
||||
{
|
||||
if (mMsgHdr) {
|
||||
free(mMsgHdr);
|
||||
mMsgHdr = NULL;
|
||||
}
|
||||
|
||||
mMsgOffset = 0;
|
||||
mMsgComplete = PR_FALSE;
|
||||
}
|
||||
|
||||
ipcMessage *
|
||||
ipcMessage::Clone() const
|
||||
{
|
||||
ipcMessage *clone = new ipcMessage();
|
||||
if (!clone)
|
||||
return NULL;
|
||||
|
||||
// copy buf if non-null
|
||||
if (mMsgHdr) {
|
||||
clone->mMsgHdr = (ipcMessageHeader *) malloc(mMsgHdr->mLen);
|
||||
memcpy(clone->mMsgHdr, mMsgHdr, mMsgHdr->mLen);
|
||||
}
|
||||
else
|
||||
clone->mMsgHdr = NULL;
|
||||
|
||||
clone->mMsgOffset = mMsgOffset;
|
||||
clone->mMsgComplete = mMsgComplete;
|
||||
|
||||
return clone;
|
||||
}
|
||||
|
||||
PRStatus
|
||||
ipcMessage::Init(const nsID &target, const char *data, PRUint32 dataLen)
|
||||
{
|
||||
if (mMsgHdr)
|
||||
free(mMsgHdr);
|
||||
mMsgComplete = PR_FALSE;
|
||||
|
||||
// allocate message data
|
||||
PRUint32 msgLen = IPC_MSG_HEADER_SIZE + dataLen;
|
||||
mMsgHdr = (ipcMessageHeader *) malloc(msgLen);
|
||||
if (!mMsgHdr) {
|
||||
mMsgHdr = NULL;
|
||||
return PR_FAILURE;
|
||||
}
|
||||
|
||||
// fill in message data
|
||||
mMsgHdr->mLen = msgLen;
|
||||
mMsgHdr->mVersion = IPC_MSG_VERSION;
|
||||
mMsgHdr->mFlags = 0;
|
||||
mMsgHdr->mTarget = target;
|
||||
|
||||
if (data)
|
||||
SetData(0, data, dataLen);
|
||||
|
||||
mMsgComplete = PR_TRUE;
|
||||
return PR_SUCCESS;
|
||||
}
|
||||
|
||||
PRStatus
|
||||
ipcMessage::SetData(PRUint32 offset, const char *data, PRUint32 dataLen)
|
||||
{
|
||||
PR_ASSERT(mMsgHdr != NULL);
|
||||
|
||||
if (offset + dataLen > DataLen())
|
||||
return PR_FAILURE;
|
||||
|
||||
memcpy((char *) Data() + offset, data, dataLen);
|
||||
return PR_SUCCESS;
|
||||
}
|
||||
|
||||
PRBool
|
||||
ipcMessage::Equals(const nsID &target, const char *data, PRUint32 dataLen) const
|
||||
{
|
||||
return mMsgComplete &&
|
||||
mMsgHdr->mTarget.Equals(target) &&
|
||||
DataLen() == dataLen &&
|
||||
memcmp(Data(), data, dataLen) == 0;
|
||||
}
|
||||
|
||||
PRBool
|
||||
ipcMessage::Equals(const ipcMessage *msg) const
|
||||
{
|
||||
PRUint32 msgLen = MsgLen();
|
||||
return mMsgComplete && msg->mMsgComplete &&
|
||||
msgLen == msg->MsgLen() &&
|
||||
memcmp(MsgBuf(), msg->MsgBuf(), msgLen) == 0;
|
||||
}
|
||||
|
||||
PRStatus
|
||||
ipcMessage::WriteTo(char *buf,
|
||||
PRUint32 bufLen,
|
||||
PRUint32 *bytesWritten,
|
||||
PRBool *complete)
|
||||
{
|
||||
if (!mMsgComplete)
|
||||
return PR_FAILURE;
|
||||
|
||||
if (mMsgOffset == MsgLen()) {
|
||||
*bytesWritten = 0;
|
||||
*complete = PR_TRUE;
|
||||
return PR_SUCCESS;
|
||||
}
|
||||
|
||||
PRUint32 count = MsgLen() - mMsgOffset;
|
||||
if (count > bufLen)
|
||||
count = bufLen;
|
||||
|
||||
memcpy(buf, MsgBuf() + mMsgOffset, count);
|
||||
mMsgOffset += count;
|
||||
|
||||
*bytesWritten = count;
|
||||
*complete = (mMsgOffset == MsgLen());
|
||||
|
||||
return PR_SUCCESS;
|
||||
}
|
||||
|
||||
PRStatus
|
||||
ipcMessage::ReadFrom(const char *buf,
|
||||
PRUint32 bufLen,
|
||||
PRUint32 *bytesRead,
|
||||
PRBool *complete)
|
||||
{
|
||||
*bytesRead = 0;
|
||||
|
||||
if (mMsgComplete) {
|
||||
*complete = PR_TRUE;
|
||||
return PR_SUCCESS;
|
||||
}
|
||||
|
||||
if (mMsgHdr) {
|
||||
// appending data to buffer
|
||||
if (mMsgOffset < sizeof(PRUint32)) {
|
||||
// we haven't learned the message length yet
|
||||
if (mMsgOffset + bufLen < sizeof(PRUint32)) {
|
||||
// we still don't know the length of the message!
|
||||
memcpy((char *) mMsgHdr + mMsgOffset, buf, bufLen);
|
||||
mMsgOffset += bufLen;
|
||||
*bytesRead = bufLen;
|
||||
*complete = PR_FALSE;
|
||||
return PR_SUCCESS;
|
||||
}
|
||||
else {
|
||||
// we now have enough data to determine the message length
|
||||
PRUint32 count = sizeof(PRUint32) - mMsgOffset;
|
||||
memcpy((char *) MsgBuf() + mMsgOffset, buf, count);
|
||||
mMsgOffset += count;
|
||||
buf += count;
|
||||
bufLen -= count;
|
||||
*bytesRead = count;
|
||||
|
||||
if (MsgLen() > IPC_MSG_GUESSED_SIZE) {
|
||||
// realloc message buffer to the correct size
|
||||
mMsgHdr = (ipcMessageHeader *) realloc(mMsgHdr, MsgLen());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (bufLen < sizeof(PRUint32)) {
|
||||
// not enough data available in buffer to determine allocation size
|
||||
// allocate a partial buffer
|
||||
PRUint32 msgLen = IPC_MSG_GUESSED_SIZE;
|
||||
mMsgHdr = (ipcMessageHeader *) malloc(msgLen);
|
||||
if (!mMsgHdr)
|
||||
return PR_FAILURE;
|
||||
memcpy(mMsgHdr, buf, bufLen);
|
||||
mMsgOffset = bufLen;
|
||||
*bytesRead = bufLen;
|
||||
*complete = PR_FALSE;
|
||||
return PR_SUCCESS;
|
||||
}
|
||||
else {
|
||||
PRUint32 msgLen = *(PRUint32 *) buf;
|
||||
mMsgHdr = (ipcMessageHeader *) malloc(msgLen);
|
||||
if (!mMsgHdr)
|
||||
return PR_FAILURE;
|
||||
mMsgHdr->mLen = msgLen;
|
||||
mMsgOffset = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// have mMsgHdr at this point
|
||||
|
||||
PRUint32 count = MsgLen() - mMsgOffset;
|
||||
if (count > bufLen)
|
||||
count = bufLen;
|
||||
|
||||
memcpy((char *) mMsgHdr + mMsgOffset, buf, count);
|
||||
mMsgOffset += count;
|
||||
*bytesRead += count;
|
||||
|
||||
*complete = mMsgComplete = (mMsgOffset == MsgLen());
|
||||
return PR_SUCCESS;
|
||||
}
|
|
@ -0,0 +1,198 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla IPC.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#ifndef ipcMessage_h__
|
||||
#define ipcMessage_h__
|
||||
|
||||
#include "nsID.h"
|
||||
|
||||
//
|
||||
// ipc message format:
|
||||
//
|
||||
// +------------------------------------+
|
||||
// | DWORD : length |
|
||||
// +------------------+-----------------+
|
||||
// | WORD : version | WORD : flags |
|
||||
// +------------------+-----------------+
|
||||
// | nsID : target |
|
||||
// +------------------------------------+
|
||||
// | data |
|
||||
// +------------------------------------+
|
||||
//
|
||||
// header is 24 bytes. flags are defined below. default value of flags is
|
||||
// zero. protocol implementations should ignore unrecognized flags. target
|
||||
// is a 16 byte UUID indicating the intended receiver of this message.
|
||||
//
|
||||
|
||||
struct ipcMessageHeader
|
||||
{
|
||||
PRUint32 mLen;
|
||||
PRUint16 mVersion;
|
||||
PRUint16 mFlags;
|
||||
nsID mTarget;
|
||||
};
|
||||
|
||||
#define IPC_MSG_VERSION (0x1)
|
||||
#define IPC_MSG_HEADER_SIZE (sizeof(ipcMessageHeader))
|
||||
#define IPC_MSG_GUESSED_SIZE (IPC_MSG_HEADER_SIZE + 64)
|
||||
|
||||
//
|
||||
// the IPC message protocol supports synchronous messages. these messages can
|
||||
// only be sent from a client to the daemon. a daemon module cannot send a
|
||||
// synchronous message. the client sets the SYNC_QUERY flag to indicate that
|
||||
// it is expecting a response with the SYNC_REPLY flag set.
|
||||
//
|
||||
#define IPC_MSG_FLAG_SYNC_QUERY (0x1)
|
||||
#define IPC_MSG_FLAG_SYNC_REPLY (0x2)
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// ipcMessage
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
class ipcMessage
|
||||
{
|
||||
public:
|
||||
ipcMessage()
|
||||
: mNext(NULL)
|
||||
, mMsgHdr(NULL)
|
||||
, mMsgOffset(0)
|
||||
, mMsgComplete(PR_FALSE)
|
||||
{ }
|
||||
ipcMessage(const nsID &target, const char *data, PRUint32 dataLen)
|
||||
: mNext(NULL)
|
||||
, mMsgHdr(NULL)
|
||||
, mMsgOffset(0)
|
||||
{ Init(target, data, dataLen); }
|
||||
~ipcMessage();
|
||||
|
||||
//
|
||||
// reset message to uninitialized state
|
||||
//
|
||||
void Reset();
|
||||
|
||||
//
|
||||
// create a copy of this message
|
||||
//
|
||||
ipcMessage *Clone() const;
|
||||
|
||||
//
|
||||
// initialize message
|
||||
//
|
||||
// param:
|
||||
// topic - message topic string
|
||||
// data - message data (may be null to leave data uninitialized)
|
||||
// dataLen - message data len
|
||||
//
|
||||
PRStatus Init(const nsID &target, const char *data, PRUint32 dataLen);
|
||||
|
||||
//
|
||||
// copy data into the message's data section, starting from offset. this
|
||||
// function can be used to write any portion of the message's data.
|
||||
//
|
||||
// param:
|
||||
// offset - destination offset
|
||||
// data - data to write
|
||||
// dataLen - number of bytes to write
|
||||
//
|
||||
PRStatus SetData(PRUint32 offset, const char *data, PRUint32 dataLen);
|
||||
|
||||
//
|
||||
// access message flags
|
||||
//
|
||||
void SetFlag(PRUint16 flag) { mMsgHdr->mFlags |= flag; }
|
||||
void ClearFlag(PRUint16 flag) { mMsgHdr->mFlags &= ~flag; }
|
||||
PRBool TestFlag(PRUint16 flag) const { return mMsgHdr->mFlags & flag; }
|
||||
|
||||
//
|
||||
// if true, the message is complete and the members of the message
|
||||
// can be accessed.
|
||||
//
|
||||
PRBool IsComplete() const { return mMsgComplete; }
|
||||
|
||||
//
|
||||
// readonly accessors
|
||||
//
|
||||
const ipcMessageHeader *Header() const { return mMsgHdr; }
|
||||
const nsID &Target() const { return mMsgHdr->mTarget; }
|
||||
const char *Data() const { return (char *) mMsgHdr + IPC_MSG_HEADER_SIZE; }
|
||||
PRUint32 DataLen() const { return mMsgHdr->mLen - IPC_MSG_HEADER_SIZE; }
|
||||
const char *MsgBuf() const { return (char *) mMsgHdr; }
|
||||
PRUint32 MsgLen() const { return mMsgHdr->mLen; }
|
||||
|
||||
//
|
||||
// message comparison functions
|
||||
//
|
||||
// param:
|
||||
// topic - message topic (may be null)
|
||||
// data - message data (must not be null)
|
||||
// dataLen - message data length
|
||||
//
|
||||
PRBool Equals(const nsID &target, const char *data, PRUint32 dataLen) const;
|
||||
PRBool Equals(const ipcMessage *msg) const;
|
||||
|
||||
//
|
||||
// write the message to a buffer segment; segment need not be large
|
||||
// enough to hold entire message. called repeatedly.
|
||||
//
|
||||
PRStatus WriteTo(char *buf,
|
||||
PRUint32 bufLen,
|
||||
PRUint32 *bytesWritten,
|
||||
PRBool *complete);
|
||||
|
||||
//
|
||||
// read the message from a buffer segment; segment need not contain
|
||||
// the entire messgae. called repeatedly.
|
||||
//
|
||||
PRStatus ReadFrom(const char *buf,
|
||||
PRUint32 bufLen,
|
||||
PRUint32 *bytesRead,
|
||||
PRBool *complete);
|
||||
|
||||
//
|
||||
// a message can be added to a singly-linked list.
|
||||
//
|
||||
class ipcMessage *mNext;
|
||||
|
||||
private:
|
||||
ipcMessageHeader *mMsgHdr;
|
||||
|
||||
// XXX document me
|
||||
PRUint32 mMsgOffset;
|
||||
PRPackedBool mMsgComplete;
|
||||
};
|
||||
|
||||
#endif // !ipcMessage_h__
|
Некоторые файлы не были показаны из-за слишком большого количества измененных файлов Показать больше
Загрузка…
Ссылка в новой задаче