Back to index

lightning-sunbird  0.9+nobinonly
ipcClient.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla IPC.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 2002
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Darin Fisher <darin@netscape.com>
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "ipcLog.h"
00039 #include "ipcClient.h"
00040 #include "ipcMessage.h"
00041 #include "ipcModuleReg.h"
00042 #include "ipcd.h"
00043 #include "ipcm.h"
00044 
00045 #if defined(XP_UNIX) || defined(XP_OS2)
00046 #include "prio.h"
00047 #endif
00048 
00049 PRUint32 ipcClient::gLastID = 0;
00050 
00051 //
00052 // called to initialize this client context
00053 //
00054 // assumptions:
00055 //  - object's memory has already been zero'd out.
00056 //
00057 void
00058 ipcClient::Init()
00059 {
00060     mID = ++gLastID;
00061 
00062     // every client must be able to handle IPCM messages.
00063     mTargets.Append(IPCM_TARGET);
00064 
00065     // although it is tempting to fire off the NotifyClientUp event at this
00066     // time, we must wait until the client sends us a CLIENT_HELLO event.
00067     // see ipcCommandModule::OnClientHello.
00068 }
00069 
00070 //
00071 // called when this client context is going away
00072 //
00073 void
00074 ipcClient::Finalize()
00075 {
00076     IPC_NotifyClientDown(this);
00077 
00078     mNames.DeleteAll();
00079     mTargets.DeleteAll();
00080 
00081 #if defined(XP_UNIX) || defined(XP_OS2)
00082     mInMsg.Reset();
00083     mOutMsgQ.DeleteAll();
00084 #endif
00085 }
00086 
00087 void
00088 ipcClient::AddName(const char *name)
00089 {
00090     LOG(("adding client name: %s\n", name));
00091 
00092     if (HasName(name))
00093         return;
00094 
00095     mNames.Append(name);
00096 }
00097 
00098 void
00099 ipcClient::DelName(const char *name)
00100 {
00101     LOG(("deleting client name: %s\n", name));
00102 
00103     mNames.FindAndDelete(name);
00104 }
00105 
00106 void
00107 ipcClient::AddTarget(const nsID &target)
00108 {
00109     LOG(("adding client target\n"));
00110 
00111     if (HasTarget(target))
00112         return;
00113 
00114     mTargets.Append(target);
00115 }
00116 
00117 void
00118 ipcClient::DelTarget(const nsID &target)
00119 {
00120     LOG(("deleting client target\n"));
00121 
00122     //
00123     // cannot remove the IPCM target
00124     //
00125     if (!target.Equals(IPCM_TARGET))
00126         mTargets.FindAndDelete(target);
00127 }
00128 
00129 #if defined(XP_UNIX) || defined(XP_OS2)
00130 
00131 //
00132 // called to process a client socket
00133 //
00134 // params:
00135 //   fd         - the client socket
00136 //   poll_flags - the state of the client socket
00137 //
00138 // return:
00139 //   0             - to end session with this client
00140 //   PR_POLL_READ  - to wait for the client socket to become readable
00141 //   PR_POLL_WRITE - to wait for the client socket to become writable
00142 //
00143 int
00144 ipcClient::Process(PRFileDesc *fd, int inFlags)
00145 {
00146     if (inFlags & (PR_POLL_ERR    | PR_POLL_HUP |
00147                    PR_POLL_EXCEPT | PR_POLL_NVAL)) {
00148         LOG(("client socket appears to have closed\n"));
00149         return 0;
00150     }
00151 
00152     // expect to wait for more data
00153     int outFlags = PR_POLL_READ;
00154 
00155     if (inFlags & PR_POLL_READ) {
00156         LOG(("client socket is now readable\n"));
00157 
00158         char buf[1024]; // XXX make this larger?
00159         PRInt32 n;
00160 
00161         // find out how much data is available for reading...
00162         // n = PR_Available(fd);
00163 
00164         n = PR_Read(fd, buf, sizeof(buf));
00165         if (n <= 0)
00166             return 0; // cancel connection
00167 
00168         const char *ptr = buf;
00169         while (n) {
00170             PRUint32 nread;
00171             PRBool complete;
00172 
00173             if (mInMsg.ReadFrom(ptr, PRUint32(n), &nread, &complete) == PR_FAILURE) {
00174                 LOG(("message appears to be malformed; dropping client connection\n"));
00175                 return 0;
00176             }
00177 
00178             if (complete) {
00179                 IPC_DispatchMsg(this, &mInMsg);
00180                 mInMsg.Reset();
00181             }
00182 
00183             n -= nread;
00184             ptr += nread;
00185         }
00186     }
00187   
00188     if (inFlags & PR_POLL_WRITE) {
00189         LOG(("client socket is now writable\n"));
00190 
00191         if (mOutMsgQ.First())
00192             WriteMsgs(fd);
00193     }
00194 
00195     if (mOutMsgQ.First())
00196         outFlags |= PR_POLL_WRITE;
00197 
00198     return outFlags;
00199 }
00200 
00201 //
00202 // called to write out any messages from the outgoing queue.
00203 //
00204 int
00205 ipcClient::WriteMsgs(PRFileDesc *fd)
00206 {
00207     while (mOutMsgQ.First()) {
00208         const char *buf = (const char *) mOutMsgQ.First()->MsgBuf();
00209         PRInt32 bufLen = (PRInt32) mOutMsgQ.First()->MsgLen();
00210 
00211         if (mSendOffset) {
00212             buf += mSendOffset;
00213             bufLen -= mSendOffset;
00214         }
00215 
00216         PRInt32 nw = PR_Write(fd, buf, bufLen);
00217         if (nw <= 0)
00218             break;
00219 
00220         LOG(("wrote %d bytes\n", nw));
00221 
00222         if (nw == bufLen) {
00223             mOutMsgQ.DeleteFirst();
00224             mSendOffset = 0;
00225         }
00226         else
00227             mSendOffset += nw;
00228     }
00229 
00230     return 0;
00231 }
00232 
00233 #endif