kmessageio.h

00001 /*
00002     This file is part of the KDE games library
00003     Copyright (C) 2001 Burkhard Lehner (Burkhard.Lehner@gmx.de)
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License version 2 as published by the Free Software Foundation.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to
00016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017     Boston, MA 02110-1301, USA.
00018 */
00019 
00020 /*
00021      KMessageIO class and subclasses KMessageSocket and KMessageDirect
00022 */
00023 
00024 #ifndef _KMESSAGEIO_H_
00025 #define _KMESSAGEIO_H_
00026 
00027 #include <qcstring.h>
00028 #include <qhostaddress.h>
00029 #include <qobject.h>
00030 #include <qstring.h>
00031 #include <qptrqueue.h>
00032 #include <qfile.h>
00033 #include <kdebug.h>
00034 
00035 class QSocket;
00036 class KProcess;
00037 //class QFile;
00038 
00039 
00056 class KMessageIO : public QObject
00057 {
00058   Q_OBJECT
00059 
00060 public:
00064   KMessageIO (QObject *parent = 0, const char *name = 0);
00065 
00069   ~KMessageIO ();
00070 
00074   virtual int rtti() const {return 0;}
00075 
00079   //virtual bool isNetwork () const = 0;
00080   virtual bool isNetwork () const
00081   {
00082    kdError(11001) << "Calling PURE virtual isNetwork...BAD" << endl;
00083    return false;
00084   }
00085 
00093   //virtual bool isConnected () const = 0;
00094   virtual bool isConnected () const
00095   {
00096    kdError(11001) << "Calling PURE virtual isConencted...BAD" << endl;
00097    return false;
00098   }
00099 
00108   void setId (Q_UINT32 id);
00109 
00113   Q_UINT32 id ();
00114 
00119   virtual Q_UINT16 peerPort () const { return 0; }
00120 
00125   virtual QString peerName () const { return QString::fromLatin1("localhost"); }
00126 
00127 
00128 signals:
00134   void received (const QByteArray &msg);
00135 
00144   void connectionBroken ();
00145 
00146 public slots:
00147 
00157   virtual void send (const QByteArray &msg) = 0;
00158 
00159 protected:
00160   Q_UINT32 m_id;
00161 };
00162 
00163 
00169 class KMessageSocket : public KMessageIO
00170 {
00171   Q_OBJECT
00172 
00173 public:
00184   KMessageSocket (QString host, Q_UINT16 port, QObject *parent = 0,
00185                   const char *name = 0);
00186 
00195   KMessageSocket (QHostAddress host, Q_UINT16 port, QObject *parent = 0,
00196                   const char *name = 0);
00197 
00209   KMessageSocket (QSocket *socket, QObject *parent = 0, const char *name = 0);
00210 
00222   KMessageSocket (int socketFD, QObject *parent = 0, const char *name = 0);
00223 
00227   ~KMessageSocket ();
00228 
00232   virtual int rtti() const {return 1;}
00233 
00238   virtual Q_UINT16 peerPort () const;
00239 
00244   virtual QString peerName () const;
00245 
00249   bool isNetwork() const { return true; }
00250 
00254   bool isConnected () const;
00255 
00262   void send (const QByteArray &msg);
00263 
00264 protected slots:
00265   virtual void processNewData ();
00266 
00267 protected:
00268   void initSocket ();
00269   QSocket *mSocket;
00270   bool mAwaitingHeader;
00271   Q_UINT32 mNextBlockLength;
00272 
00273   bool isRecursive;  // workaround for "bug" in QSocket, Qt 2.2.3 or older
00274 };
00275 
00276 
00295 class KMessageDirect : public KMessageIO
00296 {
00297   Q_OBJECT
00298 
00299 public:
00307   KMessageDirect (KMessageDirect *partner = 0, QObject *parent = 0, const char
00308 *name = 0);
00309 
00313   ~KMessageDirect ();
00314 
00318   virtual int rtti() const {return 2;}
00319 
00320 
00324   bool isNetwork() const { return false; }
00325 
00334   bool isConnected () const;
00335 
00342   void send (const QByteArray &msg);
00343 
00344 protected:
00345   KMessageDirect *mPartner;
00346 };
00347 
00348 class KMessageProcess : public KMessageIO
00349 {
00350   Q_OBJECT 
00351 
00352   public:
00353     KMessageProcess(QObject *parent, QString file);
00354     ~KMessageProcess();
00355     bool isConnected() const;
00356     void send (const QByteArray &msg);
00357     void writeToProcess();
00358 
00362     bool isNetwork() const { return false; }
00363 
00367   virtual int rtti() const {return 3;}
00368 
00369 
00370 
00371   public slots:
00372   void  slotReceivedStdout(KProcess *proc, char *buffer, int buflen);
00373   void  slotReceivedStderr(KProcess *proc, char *buffer, int buflen);
00374   void  slotProcessExited(KProcess *p);
00375   void  slotWroteStdin(KProcess *p);
00376 
00377   private:
00378     QString mProcessName;
00379     KProcess *mProcess;
00380     QPtrQueue <QByteArray> mQueue;
00381     QByteArray *mSendBuffer;
00382     QByteArray mReceiveBuffer;
00383     unsigned int mReceiveCount;
00384 };
00385 
00386 class KMessageFilePipe : public KMessageIO
00387 {
00388   Q_OBJECT 
00389 
00390   public:
00391     KMessageFilePipe(QObject *parent,QFile *readFile,QFile *writeFile);
00392     ~KMessageFilePipe();
00393     bool isConnected() const;
00394     void send (const QByteArray &msg);
00395     void exec();
00396 
00400     bool isNetwork() const { return false; }
00401 
00405   virtual int rtti() const {return 4;}
00406 
00407 
00408 
00409   private:
00410     QFile *mReadFile;
00411     QFile *mWriteFile;
00412     QByteArray mReceiveBuffer;
00413     unsigned int mReceiveCount;
00414 };
00415 
00416 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys