VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/ipc/ipcd/shared/src/ipcMessage.cpp@ 101996

Last change on this file since 101996 was 101996, checked in by vboxsync, 7 months ago

libs/xpcom/ipc: More conversion to IPRT infrastructure, bugref:10545

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.1 KB
Line 
1/* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 *
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/
8 *
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
12 * License.
13 *
14 * The Original Code is Mozilla IPC.
15 *
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.
20 *
21 * Contributor(s):
22 * Darin Fisher <darin@netscape.com>
23 *
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.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38#include <stdlib.h>
39#include <string.h>
40#include "ipcMessage.h"
41
42#include <iprt/assert.h>
43#include <iprt/mem.h>
44
45
46ipcMessage::~ipcMessage()
47{
48 if (mMsgHdr)
49 RTMemFree(mMsgHdr);
50}
51
52void
53ipcMessage::Reset()
54{
55 if (mMsgHdr) {
56 RTMemFree(mMsgHdr);
57 mMsgHdr = NULL;
58 }
59
60 mMsgOffset = 0;
61 mMsgComplete = PR_FALSE;
62}
63
64ipcMessage *
65ipcMessage::Clone() const
66{
67 ipcMessage *clone = new ipcMessage();
68 if (!clone)
69 return NULL;
70
71 // copy buf if non-null
72 if (mMsgHdr) {
73 clone->mMsgHdr = (ipcMessageHeader *) RTMemDup(mMsgHdr, mMsgHdr->mLen);
74 }
75 else
76 clone->mMsgHdr = NULL;
77
78 clone->mMsgOffset = mMsgOffset;
79 clone->mMsgComplete = mMsgComplete;
80
81 return clone;
82}
83
84PRStatus
85ipcMessage::Init(const nsID &target, const char *data, PRUint32 dataLen)
86{
87 if (mMsgHdr)
88 RTMemFree(mMsgHdr);
89
90 mMsgComplete = PR_FALSE;
91
92 // allocate message data
93 PRUint32 msgLen = IPC_MSG_HEADER_SIZE + dataLen;
94 mMsgHdr = (ipcMessageHeader *) RTMemAlloc(msgLen);
95 if (!mMsgHdr) {
96 mMsgHdr = NULL;
97 return PR_FAILURE;
98 }
99
100 // fill in message data
101 mMsgHdr->mLen = msgLen;
102 mMsgHdr->mVersion = IPC_MSG_VERSION;
103 mMsgHdr->mFlags = 0;
104 mMsgHdr->mTarget = target;
105
106 if (data)
107 SetData(0, data, dataLen);
108
109 mMsgComplete = PR_TRUE;
110 return PR_SUCCESS;
111}
112
113PRStatus
114ipcMessage::SetData(PRUint32 offset, const char *data, PRUint32 dataLen)
115{
116 Assert(mMsgHdr != NULL);
117
118 if (offset + dataLen > DataLen())
119 return PR_FAILURE;
120
121 memcpy((char *) Data() + offset, data, dataLen);
122 return PR_SUCCESS;
123}
124
125PRBool
126ipcMessage::Equals(const nsID &target, const char *data, PRUint32 dataLen) const
127{
128 return mMsgComplete &&
129 mMsgHdr->mTarget.Equals(target) &&
130 DataLen() == dataLen &&
131 memcmp(Data(), data, dataLen) == 0;
132}
133
134PRBool
135ipcMessage::Equals(const ipcMessage *msg) const
136{
137 PRUint32 msgLen = MsgLen();
138 return mMsgComplete && msg->mMsgComplete &&
139 msgLen == msg->MsgLen() &&
140 memcmp(MsgBuf(), msg->MsgBuf(), msgLen) == 0;
141}
142
143PRStatus
144ipcMessage::WriteTo(char *buf,
145 PRUint32 bufLen,
146 PRUint32 *bytesWritten,
147 PRBool *complete)
148{
149 if (!mMsgComplete)
150 return PR_FAILURE;
151
152 if (mMsgOffset == MsgLen()) {
153 *bytesWritten = 0;
154 *complete = PR_TRUE;
155 return PR_SUCCESS;
156 }
157
158 PRUint32 count = MsgLen() - mMsgOffset;
159 if (count > bufLen)
160 count = bufLen;
161
162 memcpy(buf, MsgBuf() + mMsgOffset, count);
163 mMsgOffset += count;
164
165 *bytesWritten = count;
166 *complete = (mMsgOffset == MsgLen());
167
168 return PR_SUCCESS;
169}
170
171PRStatus
172ipcMessage::ReadFrom(const char *buf,
173 PRUint32 bufLen,
174 PRUint32 *bytesRead,
175 PRBool *complete)
176{
177 *bytesRead = 0;
178
179 if (mMsgComplete) {
180 *complete = PR_TRUE;
181 return PR_SUCCESS;
182 }
183
184 if (mMsgHdr) {
185 // appending data to buffer
186 if (mMsgOffset < sizeof(PRUint32)) {
187 // we haven't learned the message length yet
188 if (mMsgOffset + bufLen < sizeof(PRUint32)) {
189 // we still don't know the length of the message!
190 memcpy((char *) mMsgHdr + mMsgOffset, buf, bufLen);
191 mMsgOffset += bufLen;
192 *bytesRead = bufLen;
193 *complete = PR_FALSE;
194 return PR_SUCCESS;
195 }
196 else {
197 // we now have enough data to determine the message length
198 PRUint32 count = sizeof(PRUint32) - mMsgOffset;
199 memcpy((char *) MsgBuf() + mMsgOffset, buf, count);
200 mMsgOffset += count;
201 buf += count;
202 bufLen -= count;
203 *bytesRead = count;
204
205 if (MsgLen() > IPC_MSG_GUESSED_SIZE) {
206 // realloc message buffer to the correct size
207#ifdef VBOX_USE_IPRT_IN_XPCOM
208 mMsgHdr = (ipcMessageHeader *) RTMemRealloc(mMsgHdr, MsgLen());
209#else
210 mMsgHdr = (ipcMessageHeader *) realloc(mMsgHdr, MsgLen());
211#endif
212 }
213 }
214 }
215 }
216 else {
217 if (bufLen < sizeof(PRUint32)) {
218 // not enough data available in buffer to determine allocation size
219 // allocate a partial buffer
220 PRUint32 msgLen = IPC_MSG_GUESSED_SIZE;
221#ifdef VBOX_USE_IPRT_IN_XPCOM
222 mMsgHdr = (ipcMessageHeader *) RTMemAlloc(msgLen);
223#else
224 mMsgHdr = (ipcMessageHeader *) malloc(msgLen);
225#endif
226 if (!mMsgHdr)
227 return PR_FAILURE;
228 memcpy(mMsgHdr, buf, bufLen);
229 mMsgOffset = bufLen;
230 *bytesRead = bufLen;
231 *complete = PR_FALSE;
232 return PR_SUCCESS;
233 }
234 else {
235 PRUint32 msgLen = *(PRUint32 *) buf;
236#ifdef VBOX_USE_IPRT_IN_XPCOM
237 mMsgHdr = (ipcMessageHeader *) RTMemAlloc(msgLen);
238#else
239 mMsgHdr = (ipcMessageHeader *) malloc(msgLen);
240#endif
241 if (!mMsgHdr)
242 return PR_FAILURE;
243 mMsgHdr->mLen = msgLen;
244 mMsgOffset = 0;
245 }
246 }
247
248 // have mMsgHdr at this point
249
250 PRUint32 count = MsgLen() - mMsgOffset;
251 if (count > bufLen)
252 count = bufLen;
253
254 memcpy((char *) mMsgHdr + mMsgOffset, buf, count);
255 mMsgOffset += count;
256 *bytesRead += count;
257
258 *complete = mMsgComplete = (mMsgOffset == MsgLen());
259 return PR_SUCCESS;
260}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use