2002-11-06 01:27:04 +00:00
|
|
|
/* ***** 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>
|
|
|
|
|
2002-11-06 08:47:31 +00:00
|
|
|
#include "prprf.h"
|
|
|
|
|
2002-11-06 01:27:04 +00:00
|
|
|
#include "ipcConfig.h"
|
2002-11-06 07:55:05 +00:00
|
|
|
#include "ipcLog.h"
|
|
|
|
#include "ipcMessage.h"
|
|
|
|
#include "ipcClient.h"
|
|
|
|
#include "ipcModuleReg.h"
|
2002-11-06 01:27:04 +00:00
|
|
|
#include "ipcdPrivate.h"
|
2002-11-06 07:55:05 +00:00
|
|
|
#include "ipcd.h"
|
|
|
|
#include "ipcm.h"
|
2002-11-06 01:27:04 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// declared in ipcdPrivate.h
|
|
|
|
//
|
|
|
|
ipcClient *ipcClients;
|
|
|
|
int ipcClientCount;
|
|
|
|
|
2002-11-06 07:55:05 +00:00
|
|
|
static ipcClient ipcClientArray[IPC_MAX_CLIENTS];
|
|
|
|
|
2002-11-06 08:47:31 +00:00
|
|
|
static HWND ipcHwnd;
|
|
|
|
|
2002-11-06 07:55:05 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// client array manipulation
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2002-11-06 08:47:31 +00:00
|
|
|
static void
|
|
|
|
RemoveClient(ipcClient *client)
|
|
|
|
{
|
|
|
|
LOG(("RemoveClient [num-clients=%u]\n", ipcClientCount - 1));
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
PurgeDeadClients()
|
|
|
|
{
|
|
|
|
LOG(("PurgeDeadClients\n"));
|
|
|
|
//
|
|
|
|
// walk the list of supposedly active clients, and verify the existance of
|
|
|
|
// their respective message windows.
|
|
|
|
//
|
|
|
|
char wName[sizeof(IPC_CLIENT_WINDOW_NAME_PREFIX) + 20];
|
|
|
|
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()));
|
|
|
|
|
|
|
|
// construct window name
|
|
|
|
PR_snprintf(wName, sizeof(wName), "%s%u",
|
|
|
|
IPC_CLIENT_WINDOW_NAME_PREFIX, client->PID());
|
|
|
|
|
|
|
|
HWND hwnd = FindWindow(IPC_CLIENT_WINDOW_CLASS, wName);
|
|
|
|
if (!hwnd) {
|
|
|
|
LOG((" window not found; removing client!\n"));
|
|
|
|
RemoveClient(client);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-06 07:55:05 +00:00
|
|
|
static ipcClient *
|
|
|
|
AddClient(HWND hwnd, PRUint32 pid)
|
|
|
|
{
|
2002-11-06 08:47:31 +00:00
|
|
|
LOG(("AddClient [num-clients=%u]\n", ipcClientCount + 1));
|
|
|
|
|
|
|
|
/*
|
|
|
|
if (ipcClientCount > 0)
|
|
|
|
PurgeDeadClients();
|
|
|
|
*/
|
|
|
|
|
2002-11-06 07:55:05 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
++ipcClientCount;
|
|
|
|
return client;
|
|
|
|
}
|
|
|
|
|
2002-11-06 08:47:31 +00:00
|
|
|
static ipcClient *
|
2002-11-06 07:55:05 +00:00
|
|
|
GetClientByPID(PRUint32 pid)
|
|
|
|
{
|
|
|
|
for (int i=0; i<ipcClientCount; ++i) {
|
|
|
|
if (ipcClientArray[i].PID() == pid)
|
|
|
|
return &ipcClientArray[i];
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// message processing
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
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) {
|
2002-11-06 08:47:31 +00:00
|
|
|
RemoveClient(client);
|
|
|
|
client = AddClient(hwnd, pid);
|
2002-11-06 07:55:05 +00:00
|
|
|
}
|
|
|
|
}
|
2002-11-06 08:47:31 +00:00
|
|
|
else
|
2002-11-06 07:55:05 +00:00
|
|
|
client = AddClient(hwnd, pid);
|
2002-11-06 08:47:31 +00:00
|
|
|
|
|
|
|
if (client == NULL)
|
|
|
|
return;
|
2002-11-06 07:55:05 +00:00
|
|
|
|
|
|
|
IPC_DispatchMsg(client, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void
|
|
|
|
IPC_SendMessageNow(ipcClient *client, const ipcMessage *msg)
|
|
|
|
{
|
2002-11-06 08:47:31 +00:00
|
|
|
LOG(("IPC_SendMessageNow [clientID=%u clientPID=%u]\n",
|
|
|
|
client->ID(), client->PID()));
|
|
|
|
|
2002-11-06 07:55:05 +00:00
|
|
|
COPYDATASTRUCT cd;
|
|
|
|
cd.dwData = GetCurrentProcessId();
|
|
|
|
cd.cbData = (DWORD) msg->MsgLen();
|
|
|
|
cd.lpData = (PVOID) msg->MsgBuf();
|
2002-11-06 08:47:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
LOG(("calling SendMessage...\n"));
|
2002-11-06 07:55:05 +00:00
|
|
|
SendMessageA(client->Hwnd(), WM_COPYDATA, 0, (LPARAM) &cd);
|
2002-11-06 08:47:31 +00:00
|
|
|
// SendMessageA(hwnd, WM_COPYDATA, (WPARAM) ipcHwnd, (LPARAM) &cd);
|
|
|
|
LOG((" done.\n"));
|
2002-11-06 07:55:05 +00:00
|
|
|
}
|
|
|
|
|
2002-11-06 01:27:04 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// windows message loop
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static LRESULT CALLBACK
|
|
|
|
ipcWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
2002-11-06 07:55:05 +00:00
|
|
|
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;
|
|
|
|
PRUint32 bytesRead;
|
|
|
|
PRBool complete;
|
|
|
|
PRStatus rv = msg.ReadFrom((const char *) cd->lpData, cd->cbData,
|
|
|
|
&bytesRead, &complete);
|
|
|
|
if (rv == PR_SUCCESS && complete) {
|
|
|
|
//
|
|
|
|
// grab client PID and hwnd.
|
|
|
|
//
|
|
|
|
DWORD pid = cd->dwData;
|
|
|
|
HWND hwnd = (HWND) wParam;
|
|
|
|
|
|
|
|
ProcessMsg(hwnd, pid, &msg);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LOG(("ignoring malformed message\n"));
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2002-11-06 01:27:04 +00:00
|
|
|
return DefWindowProc(hWnd, uMsg, wParam, lParam);
|
|
|
|
}
|
|
|
|
|
2002-11-06 07:55:05 +00:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
2002-11-06 01:27:04 +00:00
|
|
|
{
|
2002-11-06 07:55:05 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
IPC_InitLog("###");
|
|
|
|
#endif
|
|
|
|
LOG(("daemon started...\n"));
|
2002-11-06 01:27:04 +00:00
|
|
|
|
2002-11-06 07:55:05 +00:00
|
|
|
ipcClients = ipcClientArray;
|
|
|
|
ipcClientCount = 0;
|
|
|
|
|
|
|
|
//
|
|
|
|
// create message window up front...
|
|
|
|
//
|
|
|
|
WNDCLASS wc;
|
2002-11-06 01:27:04 +00:00
|
|
|
memset(&wc, 0, sizeof(wc));
|
|
|
|
wc.lpfnWndProc = ipcWindowProc;
|
|
|
|
wc.lpszClassName = IPC_WINDOW_CLASS;
|
|
|
|
|
|
|
|
RegisterClass(&wc);
|
|
|
|
|
|
|
|
ipcHwnd = CreateWindow(IPC_WINDOW_CLASS, IPC_WINDOW_NAME,
|
|
|
|
0, 0, 0, 10, 10, NULL, NULL, NULL, NULL);
|
|
|
|
if (!ipcHwnd)
|
|
|
|
return -1;
|
|
|
|
|
2002-11-06 07:55:05 +00:00
|
|
|
//
|
|
|
|
// load modules
|
|
|
|
//
|
|
|
|
IPC_InitModuleReg(argv[0]);
|
|
|
|
|
|
|
|
//
|
|
|
|
// process messages
|
|
|
|
//
|
|
|
|
LOG(("entering message loop...\n"));
|
|
|
|
|
2002-11-06 01:27:04 +00:00
|
|
|
MSG msg;
|
2002-11-06 07:55:05 +00:00
|
|
|
while (GetMessage(&msg, ipcHwnd, 0, 0))
|
2002-11-06 01:27:04 +00:00
|
|
|
DispatchMessage(&msg);
|
|
|
|
|
2002-11-06 07:55:05 +00:00
|
|
|
LOG(("exiting\n"));
|
2002-11-06 01:27:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|