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 | #ifndef ipcMessage_h__
|
---|
39 | #define ipcMessage_h__
|
---|
40 |
|
---|
41 | #include "nsID.h"
|
---|
42 |
|
---|
43 | //
|
---|
44 | // ipc message format:
|
---|
45 | //
|
---|
46 | // +------------------------------------+
|
---|
47 | // | DWORD : length |
|
---|
48 | // +------------------+-----------------+
|
---|
49 | // | WORD : version | WORD : flags |
|
---|
50 | // +------------------+-----------------+
|
---|
51 | // | nsID : target |
|
---|
52 | // +------------------------------------+
|
---|
53 | // | data |
|
---|
54 | // +------------------------------------+
|
---|
55 | //
|
---|
56 | // header is 24 bytes. flags are defined below. default value of flags is
|
---|
57 | // zero. protocol implementations should ignore unrecognized flags. target
|
---|
58 | // is a 16 byte UUID indicating the intended receiver of this message.
|
---|
59 | //
|
---|
60 |
|
---|
61 | struct ipcMessageHeader
|
---|
62 | {
|
---|
63 | PRUint32 mLen;
|
---|
64 | PRUint16 mVersion;
|
---|
65 | PRUint16 mFlags;
|
---|
66 | nsID mTarget;
|
---|
67 | };
|
---|
68 |
|
---|
69 | #define IPC_MSG_VERSION (0x1)
|
---|
70 | #define IPC_MSG_HEADER_SIZE (sizeof(ipcMessageHeader))
|
---|
71 | #define IPC_MSG_GUESSED_SIZE (IPC_MSG_HEADER_SIZE + 64)
|
---|
72 |
|
---|
73 | //
|
---|
74 | // the IPC message protocol supports synchronous messages. these messages can
|
---|
75 | // only be sent from a client to the daemon. a daemon module cannot send a
|
---|
76 | // synchronous message. the client sets the SYNC_QUERY flag to indicate that
|
---|
77 | // it is expecting a response with the SYNC_REPLY flag set.
|
---|
78 | //
|
---|
79 | #define IPC_MSG_FLAG_SYNC_QUERY (0x1)
|
---|
80 | #define IPC_MSG_FLAG_SYNC_REPLY (0x2)
|
---|
81 |
|
---|
82 | //
|
---|
83 | // a special flag to prevent repeated processing of the same message by two
|
---|
84 | // or more selectors when walking through the queue of pending messages
|
---|
85 | // in WaitTarget().
|
---|
86 | //
|
---|
87 | #define IPC_MSG_FLAG_IN_PROCESS (0x4)
|
---|
88 |
|
---|
89 | //-----------------------------------------------------------------------------
|
---|
90 | // ipcMessage
|
---|
91 | //-----------------------------------------------------------------------------
|
---|
92 |
|
---|
93 | class ipcMessage
|
---|
94 | {
|
---|
95 | public:
|
---|
96 | ipcMessage()
|
---|
97 | : mNext(NULL)
|
---|
98 | , mMetaData(0)
|
---|
99 | , mMsgHdr(NULL)
|
---|
100 | , mMsgOffset(0)
|
---|
101 | , mMsgComplete(PR_FALSE)
|
---|
102 | { }
|
---|
103 | ipcMessage(const nsID &target, const char *data, PRUint32 dataLen)
|
---|
104 | : mNext(NULL)
|
---|
105 | , mMetaData(0)
|
---|
106 | , mMsgHdr(NULL)
|
---|
107 | , mMsgOffset(0)
|
---|
108 | { Init(target, data, dataLen); }
|
---|
109 | ~ipcMessage() NS_HIDDEN;
|
---|
110 |
|
---|
111 | //
|
---|
112 | // reset message to uninitialized state
|
---|
113 | //
|
---|
114 | NS_HIDDEN_(void) Reset();
|
---|
115 |
|
---|
116 | //
|
---|
117 | // create a copy of this message
|
---|
118 | //
|
---|
119 | NS_HIDDEN_(ipcMessage *) Clone() const;
|
---|
120 |
|
---|
121 | //
|
---|
122 | // initialize message
|
---|
123 | //
|
---|
124 | // param:
|
---|
125 | // topic - message topic string
|
---|
126 | // data - message data (may be null to leave data uninitialized)
|
---|
127 | // dataLen - message data len
|
---|
128 | //
|
---|
129 | NS_HIDDEN_(PRStatus) Init(const nsID &target, const char *data, PRUint32 dataLen);
|
---|
130 |
|
---|
131 | //
|
---|
132 | // copy data into the message's data section, starting from offset. this
|
---|
133 | // function can be used to write any portion of the message's data.
|
---|
134 | //
|
---|
135 | // param:
|
---|
136 | // offset - destination offset
|
---|
137 | // data - data to write
|
---|
138 | // dataLen - number of bytes to write
|
---|
139 | //
|
---|
140 | NS_HIDDEN_(PRStatus) SetData(PRUint32 offset, const char *data, PRUint32 dataLen);
|
---|
141 |
|
---|
142 | //
|
---|
143 | // access message flags
|
---|
144 | //
|
---|
145 | void SetFlag(PRUint16 flag) { mMsgHdr->mFlags |= flag; }
|
---|
146 | void ClearFlag(PRUint16 flag) { mMsgHdr->mFlags &= ~flag; }
|
---|
147 | PRBool TestFlag(PRUint16 flag) const { return mMsgHdr->mFlags & flag; }
|
---|
148 |
|
---|
149 | //
|
---|
150 | // if true, the message is complete and the members of the message
|
---|
151 | // can be accessed.
|
---|
152 | //
|
---|
153 | PRBool IsComplete() const { return mMsgComplete; }
|
---|
154 |
|
---|
155 | //
|
---|
156 | // readonly accessors
|
---|
157 | //
|
---|
158 | const ipcMessageHeader *Header() const { return mMsgHdr; }
|
---|
159 | const nsID &Target() const { return mMsgHdr->mTarget; }
|
---|
160 | const char *Data() const { return (char *) mMsgHdr + IPC_MSG_HEADER_SIZE; }
|
---|
161 | PRUint32 DataLen() const { return mMsgHdr->mLen - IPC_MSG_HEADER_SIZE; }
|
---|
162 | const char *MsgBuf() const { return (char *) mMsgHdr; }
|
---|
163 | PRUint32 MsgLen() const { return mMsgHdr->mLen; }
|
---|
164 |
|
---|
165 | //
|
---|
166 | // message comparison functions
|
---|
167 | //
|
---|
168 | // param:
|
---|
169 | // topic - message topic (may be null)
|
---|
170 | // data - message data (must not be null)
|
---|
171 | // dataLen - message data length
|
---|
172 | //
|
---|
173 | NS_HIDDEN_(PRBool) Equals(const nsID &target, const char *data, PRUint32 dataLen) const;
|
---|
174 | NS_HIDDEN_(PRBool) Equals(const ipcMessage *msg) const;
|
---|
175 |
|
---|
176 | //
|
---|
177 | // write the message to a buffer segment; segment need not be large
|
---|
178 | // enough to hold entire message. called repeatedly.
|
---|
179 | //
|
---|
180 | NS_HIDDEN_(PRStatus) WriteTo(char *buf,
|
---|
181 | PRUint32 bufLen,
|
---|
182 | PRUint32 *bytesWritten,
|
---|
183 | PRBool *complete);
|
---|
184 |
|
---|
185 | //
|
---|
186 | // read the message from a buffer segment; segment need not contain
|
---|
187 | // the entire messgae. called repeatedly.
|
---|
188 | //
|
---|
189 | NS_HIDDEN_(PRStatus) ReadFrom(const char *buf,
|
---|
190 | PRUint32 bufLen,
|
---|
191 | PRUint32 *bytesRead,
|
---|
192 | PRBool *complete);
|
---|
193 |
|
---|
194 | //
|
---|
195 | // a message can be added to a singly-linked list.
|
---|
196 | //
|
---|
197 | class ipcMessage *mNext;
|
---|
198 |
|
---|
199 | //
|
---|
200 | // meta data associated with this message object. the owner of the
|
---|
201 | // ipcMessage object is free to use this field for any purpose. by
|
---|
202 | // default, it is initialized to 0.
|
---|
203 | //
|
---|
204 | PRUint32 mMetaData;
|
---|
205 |
|
---|
206 | private:
|
---|
207 | ipcMessageHeader *mMsgHdr;
|
---|
208 |
|
---|
209 | // XXX document me
|
---|
210 | PRUint32 mMsgOffset;
|
---|
211 | PRPackedBool mMsgComplete;
|
---|
212 | };
|
---|
213 |
|
---|
214 | #endif // !ipcMessage_h__
|
---|