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 <tqcstring.h> 00028 #include <tqhostaddress.h> 00029 #include <tqobject.h> 00030 #include <tqstring.h> 00031 #include <tqptrqueue.h> 00032 #include <tqfile.h> 00033 #include <kdebug.h> 00034 00035 class TQSocket; 00036 class KProcess; 00037 //class TQFile; 00038 00039 00056 class KMessageIO : public TQObject 00057 { 00058 Q_OBJECT 00059 TQ_OBJECT 00060 00061 public: 00065 KMessageIO (TQObject *parent = 0, const char *name = 0); 00066 00070 ~KMessageIO (); 00071 00075 virtual int rtti() const {return 0;} 00076 00080 //virtual bool isNetwork () const = 0; 00081 virtual bool isNetwork () const 00082 { 00083 kdError(11001) << "Calling PURE virtual isNetwork...BAD" << endl; 00084 return false; 00085 } 00086 00094 //virtual bool isConnected () const = 0; 00095 virtual bool isConnected () const 00096 { 00097 kdError(11001) << "Calling PURE virtual isConencted...BAD" << endl; 00098 return false; 00099 } 00100 00109 void setId (TQ_UINT32 id); 00110 00114 TQ_UINT32 id (); 00115 00120 virtual TQ_UINT16 peerPort () const { return 0; } 00121 00126 virtual TQString peerName () const { return TQString::fromLatin1("localhost"); } 00127 00128 00129 signals: 00135 void received (const TQByteArray &msg); 00136 00145 void connectionBroken (); 00146 00147 public slots: 00148 00158 virtual void send (const TQByteArray &msg) = 0; 00159 00160 protected: 00161 TQ_UINT32 m_id; 00162 }; 00163 00164 00170 class KMessageSocket : public KMessageIO 00171 { 00172 Q_OBJECT 00173 TQ_OBJECT 00174 00175 public: 00186 KMessageSocket (TQString host, TQ_UINT16 port, TQObject *parent = 0, 00187 const char *name = 0); 00188 00197 KMessageSocket (TQHostAddress host, TQ_UINT16 port, TQObject *parent = 0, 00198 const char *name = 0); 00199 00211 KMessageSocket (TQSocket *socket, TQObject *parent = 0, const char *name = 0); 00212 00224 KMessageSocket (int socketFD, TQObject *parent = 0, const char *name = 0); 00225 00229 ~KMessageSocket (); 00230 00234 virtual int rtti() const {return 1;} 00235 00240 virtual TQ_UINT16 peerPort () const; 00241 00246 virtual TQString peerName () const; 00247 00251 bool isNetwork() const { return true; } 00252 00256 bool isConnected () const; 00257 00264 void send (const TQByteArray &msg); 00265 00266 protected slots: 00267 virtual void processNewData (); 00268 00269 protected: 00270 void initSocket (); 00271 TQSocket *mSocket; 00272 bool mAwaitingHeader; 00273 TQ_UINT32 mNextBlockLength; 00274 00275 bool isRecursive; // workaround for "bug" in TQSocket, TQt 2.2.3 or older 00276 }; 00277 00278 00297 class KMessageDirect : public KMessageIO 00298 { 00299 Q_OBJECT 00300 TQ_OBJECT 00301 00302 public: 00310 KMessageDirect (KMessageDirect *partner = 0, TQObject *parent = 0, const char 00311 *name = 0); 00312 00316 ~KMessageDirect (); 00317 00321 virtual int rtti() const {return 2;} 00322 00323 00327 bool isNetwork() const { return false; } 00328 00337 bool isConnected () const; 00338 00345 void send (const TQByteArray &msg); 00346 00347 protected: 00348 KMessageDirect *mPartner; 00349 }; 00350 00351 class KMessageProcess : public KMessageIO 00352 { 00353 Q_OBJECT 00354 TQ_OBJECT 00355 00356 public: 00357 KMessageProcess(TQObject *parent, TQString file); 00358 ~KMessageProcess(); 00359 bool isConnected() const; 00360 void send (const TQByteArray &msg); 00361 void writeToProcess(); 00362 00366 bool isNetwork() const { return false; } 00367 00371 virtual int rtti() const {return 3;} 00372 00373 00374 00375 public slots: 00376 void slotReceivedStdout(KProcess *proc, char *buffer, int buflen); 00377 void slotReceivedStderr(KProcess *proc, char *buffer, int buflen); 00378 void slotProcessExited(KProcess *p); 00379 void slotWroteStdin(KProcess *p); 00380 00381 private: 00382 TQString mProcessName; 00383 KProcess *mProcess; 00384 TQPtrQueue <TQByteArray> mQueue; 00385 TQByteArray *mSendBuffer; 00386 TQByteArray mReceiveBuffer; 00387 unsigned int mReceiveCount; 00388 }; 00389 00390 class KMessageFilePipe : public KMessageIO 00391 { 00392 Q_OBJECT 00393 TQ_OBJECT 00394 00395 public: 00396 KMessageFilePipe(TQObject *parent,TQFile *readFile,TQFile *writeFile); 00397 ~KMessageFilePipe(); 00398 bool isConnected() const; 00399 void send (const TQByteArray &msg); 00400 void exec(); 00401 00405 bool isNetwork() const { return false; } 00406 00410 virtual int rtti() const {return 4;} 00411 00412 00413 00414 private: 00415 TQFile *mReadFile; 00416 TQFile *mWriteFile; 00417 TQByteArray mReceiveBuffer; 00418 unsigned int mReceiveCount; 00419 }; 00420 00421 #endif