• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

libkdegames/kgame

kmessageio.h

Go to the documentation of this file.
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 <QtCore/QObject>
00028 #include <QtCore/QProcess>
00029 #include <QtCore/QString>
00030 #include <QtNetwork/QHostAddress>
00031 #include <kdebug.h>
00032 
00033 class QTcpSocket;
00034 class KProcess;
00035 class QFile;
00036 
00037 
00056 class KMessageIO : public QObject
00057 {
00058   Q_OBJECT
00059 
00060 public:
00064   KMessageIO (QObject *parent = 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    kError(11001) << "Calling PURE virtual isNetwork...BAD";
00083    return false;
00084   }
00085 
00093   //virtual bool isConnected () const = 0;
00094   virtual bool isConnected () const
00095   {
00096    kError(11001) << "Calling PURE virtual isConencted...BAD";
00097    return false;
00098   }
00099 
00108   void setId (quint32 id);
00109 
00113   quint32 id ();
00114 
00118   virtual quint16 peerPort () const { return 0; }
00119 
00123   virtual QString peerName () const { return QString::fromLatin1("localhost"); }
00124 
00125 
00126 Q_SIGNALS:
00132   void received (const QByteArray &msg);
00133 
00142   void connectionBroken ();
00143 
00144 public Q_SLOTS:
00145 
00155   virtual void send (const QByteArray &msg) = 0;
00156 
00157 protected:
00158   quint32 m_id;
00159 };
00160 
00161 
00169 class KMessageSocket : public KMessageIO
00170 {
00171   Q_OBJECT
00172 
00173 public:
00184   KMessageSocket (const QString& host, quint16 port, QObject *parent = 0 );
00185 
00194   KMessageSocket (QHostAddress host, quint16 port, QObject *parent = 0);
00195 
00207   explicit KMessageSocket (QTcpSocket *socket, QObject *parent = 0);
00208 
00220   explicit KMessageSocket (int socketFD, QObject *parent = 0);
00221 
00225   ~KMessageSocket ();
00226 
00230   virtual int rtti() const {return 1;}
00231 
00235   virtual quint16 peerPort () const;
00236 
00240   virtual QString peerName () const;
00241 
00245   bool isNetwork() const { return true; }
00246 
00250   bool isConnected () const;
00251 
00258   void send (const QByteArray &msg);
00259 
00260 protected Q_SLOTS:
00261   virtual void processNewData ();
00262 
00263 protected:
00264   void initSocket ();
00265   QTcpSocket *mSocket;
00266   bool mAwaitingHeader;
00267   quint32 mNextBlockLength;
00268 
00269   bool isRecursive;  // workaround for "bug" in QSocket, Qt 2.2.3 or older
00270 };
00271 
00272 
00293 class KMessageDirect : public KMessageIO
00294 {
00295   Q_OBJECT
00296 
00297 public:
00305   explicit KMessageDirect (KMessageDirect *partner = 0, QObject *parent = 0);
00306 
00310   ~KMessageDirect ();
00311 
00315   virtual int rtti() const {return 2;}
00316 
00317 
00321   bool isNetwork() const { return false; }
00322 
00331   bool isConnected () const;
00332 
00339   void send (const QByteArray &msg);
00340 
00341 protected:
00342   KMessageDirect *mPartner;
00343 };
00344 
00348 class KMessageProcess : public KMessageIO
00349 {
00350   Q_OBJECT 
00351 
00352   public:
00353     KMessageProcess(QObject *parent, const QString& file);
00354     ~KMessageProcess();
00355     bool isConnected() const;
00356     void send (const QByteArray &msg);
00357 
00361     bool isNetwork() const { return false; }
00362 
00366   virtual int rtti() const {return 3;}
00367 
00368 
00369 
00370   public Q_SLOTS:
00371   void  slotReceivedStdout();
00372   void  slotReceivedStderr();
00373   void  slotProcessExited(int, QProcess::ExitStatus);
00374 
00375   Q_SIGNALS:
00376     void signalReceivedStderr(QString msg);
00377 
00378   private:
00379     QString mProcessName;
00380     KProcess *mProcess;
00381     QByteArray* mSendBuffer;
00382     QByteArray mReceiveBuffer;
00383     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     int mReceiveCount;
00414 };
00415 
00416 #endif

libkdegames/kgame

Skip menu "libkdegames/kgame"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • kblackbox
  • kgoldrunner
  • kmahjongg
  • ksquares
  • libkdegames
  •   highscore
  •   kgame
  •   kggzgames
  •   kggzmod
  •   kggznet
  • libkmahjongg
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal