1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is Mozilla IPC.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 2002
19 * the Initial Developer. All Rights Reserved.
22 * Darin Fisher <darin@netscape.com>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
41 #include "ipcMessage.h"
43 ipcMessage::~ipcMessage()
58 mMsgComplete
= PR_FALSE
;
62 ipcMessage::Clone() const
64 ipcMessage
*clone
= new ipcMessage();
68 // copy buf if non-null
70 clone
->mMsgHdr
= (ipcMessageHeader
*) malloc(mMsgHdr
->mLen
);
71 memcpy(clone
->mMsgHdr
, mMsgHdr
, mMsgHdr
->mLen
);
74 clone
->mMsgHdr
= NULL
;
76 clone
->mMsgOffset
= mMsgOffset
;
77 clone
->mMsgComplete
= mMsgComplete
;
83 ipcMessage::Init(const nsID
&target
, const char *data
, PRUint32 dataLen
)
87 mMsgComplete
= PR_FALSE
;
89 // allocate message data
90 PRUint32 msgLen
= IPC_MSG_HEADER_SIZE
+ dataLen
;
91 mMsgHdr
= (ipcMessageHeader
*) malloc(msgLen
);
97 // fill in message data
98 mMsgHdr
->mLen
= msgLen
;
99 mMsgHdr
->mVersion
= IPC_MSG_VERSION
;
101 mMsgHdr
->mTarget
= target
;
104 SetData(0, data
, dataLen
);
106 mMsgComplete
= PR_TRUE
;
111 ipcMessage::SetData(PRUint32 offset
, const char *data
, PRUint32 dataLen
)
113 PR_ASSERT(mMsgHdr
!= NULL
);
115 if (offset
+ dataLen
> DataLen())
118 memcpy((char *) Data() + offset
, data
, dataLen
);
123 ipcMessage::Equals(const nsID
&target
, const char *data
, PRUint32 dataLen
) const
125 return mMsgComplete
&&
126 mMsgHdr
->mTarget
.Equals(target
) &&
127 DataLen() == dataLen
&&
128 memcmp(Data(), data
, dataLen
) == 0;
132 ipcMessage::Equals(const ipcMessage
*msg
) const
134 PRUint32 msgLen
= MsgLen();
135 return mMsgComplete
&& msg
->mMsgComplete
&&
136 msgLen
== msg
->MsgLen() &&
137 memcmp(MsgBuf(), msg
->MsgBuf(), msgLen
) == 0;
141 ipcMessage::WriteTo(char *buf
,
143 PRUint32
*bytesWritten
,
149 if (mMsgOffset
== MsgLen()) {
155 PRUint32 count
= MsgLen() - mMsgOffset
;
159 memcpy(buf
, MsgBuf() + mMsgOffset
, count
);
162 *bytesWritten
= count
;
163 *complete
= (mMsgOffset
== MsgLen());
169 ipcMessage::ReadFrom(const char *buf
,
182 // appending data to buffer
183 if (mMsgOffset
< sizeof(PRUint32
)) {
184 // we haven't learned the message length yet
185 if (mMsgOffset
+ bufLen
< sizeof(PRUint32
)) {
186 // we still don't know the length of the message!
187 memcpy((char *) mMsgHdr
+ mMsgOffset
, buf
, bufLen
);
188 mMsgOffset
+= bufLen
;
190 *complete
= PR_FALSE
;
194 // we now have enough data to determine the message length
195 PRUint32 count
= sizeof(PRUint32
) - mMsgOffset
;
196 memcpy((char *) MsgBuf() + mMsgOffset
, buf
, count
);
202 if (MsgLen() > IPC_MSG_GUESSED_SIZE
) {
203 // realloc message buffer to the correct size
204 mMsgHdr
= (ipcMessageHeader
*) realloc(mMsgHdr
, MsgLen());
210 if (bufLen
< sizeof(PRUint32
)) {
211 // not enough data available in buffer to determine allocation size
212 // allocate a partial buffer
213 PRUint32 msgLen
= IPC_MSG_GUESSED_SIZE
;
214 mMsgHdr
= (ipcMessageHeader
*) malloc(msgLen
);
217 memcpy(mMsgHdr
, buf
, bufLen
);
220 *complete
= PR_FALSE
;
224 PRUint32 msgLen
= *(PRUint32
*) buf
;
225 mMsgHdr
= (ipcMessageHeader
*) malloc(msgLen
);
228 mMsgHdr
->mLen
= msgLen
;
233 // have mMsgHdr at this point
235 PRUint32 count
= MsgLen() - mMsgOffset
;
239 memcpy((char *) mMsgHdr
+ mMsgOffset
, buf
, count
);
243 *complete
= mMsgComplete
= (mMsgOffset
== MsgLen());