KDEGames

kmessageio.h
1 /*
2  This file is part of the KDE games library
3  Copyright (C) 2001 Burkhard Lehner ([email protected])
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License version 2 as published by the Free Software Foundation.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 /*
21  KMessageIO class and subclasses KMessageSocket and KMessageDirect
22 */
23 
24 #ifndef _KMESSAGEIO_H_
25 #define _KMESSAGEIO_H_
26 
27 #include <QObject>
28 #include <QProcess>
29 #include <QString>
30 #include <QHostAddress>
31 #include <QLoggingCategory>
32 
33 #include "../libkdegamesprivate_export.h"
34 
35 /*
36  This macro shouldn't be here ideally. Already declared in kgame.h, but throws error if not placed here.
37 */
38 Q_DECLARE_LOGGING_CATEGORY(GAMES_PRIVATE_KGAME)
39 
40 class QTcpSocket;
41 class KProcess;
42 
43 
62 class KDEGAMESPRIVATE_EXPORT KMessageIO : public QObject
63 {
64  Q_OBJECT
65 
66 public:
70  explicit KMessageIO (QObject *parent = nullptr);
71 
75  ~KMessageIO ();
76 
80  virtual int rtti() const {return 0;}
81 
85  //virtual bool isNetwork () const = 0;
86  virtual bool isNetwork () const
87  {
88  qCCritical(GAMES_PRIVATE_KGAME) << "Calling PURE virtual isNetwork...BAD";
89  return false;
90  }
91 
99  //virtual bool isConnected () const = 0;
100  virtual bool isConnected () const
101  {
102  qCCritical(GAMES_PRIVATE_KGAME) << "Calling PURE virtual isConnected...BAD";
103  return false;
104  }
105 
114  void setId (quint32 id);
115 
119  quint32 id ();
120 
124  virtual quint16 peerPort () const { return 0; }
125 
129  virtual QString peerName () const { return QStringLiteral("localhost"); }
130 
131 
132 Q_SIGNALS:
138  void received (const QByteArray &msg);
139 
148  void connectionBroken ();
149 
150 public Q_SLOTS:
151 
161  virtual void send (const QByteArray &msg) = 0;
162 
163 protected:
164  quint32 m_id;
165 };
166 
167 
176 {
177  Q_OBJECT
178 
179 public:
190  KMessageSocket (const QString& host, quint16 port, QObject *parent = nullptr );
191 
200  KMessageSocket (const QHostAddress &host, quint16 port, QObject *parent = nullptr);
201 
213  explicit KMessageSocket (QTcpSocket *socket, QObject *parent = nullptr);
214 
226  explicit KMessageSocket (int socketFD, QObject *parent = nullptr);
227 
231  ~KMessageSocket ();
232 
236  int rtti() const override {return 1;}
237 
241  quint16 peerPort () const override;
242 
246  QString peerName () const override;
247 
251  bool isNetwork() const override { return true; }
252 
256  bool isConnected () const override;
257 
264  void send (const QByteArray &msg) override;
265 
266 protected Q_SLOTS:
267  virtual void processNewData ();
268 
269 protected:
270  void initSocket ();
271  QTcpSocket *mSocket;
272  bool mAwaitingHeader;
273  quint32 mNextBlockLength;
274 
275  bool isRecursive; // workaround for "bug" in QSocket, Qt 2.2.3 or older
276 };
277 
278 
300 {
301  Q_OBJECT
302 
303 public:
311  explicit KMessageDirect (KMessageDirect *partner = nullptr, QObject *parent = nullptr);
312 
316  ~KMessageDirect ();
317 
321  int rtti() const override {return 2;}
322 
323 
327  bool isNetwork() const override { return false; }
328 
337  bool isConnected () const override;
338 
345  void send (const QByteArray &msg) override;
346 
347 protected:
348  KMessageDirect *mPartner;
349 };
350 
354 class KMessageProcess : public KMessageIO
355 {
356  Q_OBJECT
357 
358  public:
359  KMessageProcess(QObject *parent, const QString& file);
360  ~KMessageProcess();
361  bool isConnected() const override;
362  void send (const QByteArray &msg) override;
363 
367  bool isNetwork() const override { return false; }
368 
372  int rtti() const override {return 3;}
373 
374 
375 
376  public Q_SLOTS:
377  void slotReceivedStdout();
378  void slotReceivedStderr();
379  void slotProcessExited(int, QProcess::ExitStatus);
380 
381  Q_SIGNALS:
382  void signalReceivedStderr(const QString &msg);
383 
384  private:
385  QString mProcessName;
386  KProcess *mProcess;
387  QByteArray* mSendBuffer;
388  QByteArray mReceiveBuffer;
389  int mReceiveCount;
390 };
391 
392 #endif
393 
This class implements the message communication using a TCP/IP socket.
Definition: kmessageio.h:175
QString peerName() const override
Definition: kmessageio.cpp:174
virtual bool isConnected() const
This method returns the status of the object, whether it is already (or still) connected to another K...
Definition: kmessageio.h:100
virtual int rtti() const
The runtime identification.
Definition: kmessageio.h:80
Q_SIGNALSQ_SIGNALS
This class implements the message communication using function calls directly.
Definition: kmessageio.h:299
int rtti() const override
The runtime identification.
Definition: kmessageio.h:321
void send(const QByteArray &msg) override
Overwritten slot method from KMessageIO.
Definition: kmessageio.cpp:92
Q_OBJECTQ_OBJECT
quint16 peerPort() const override
Definition: kmessageio.cpp:169
This abstract base class represents one end of a message connections between two clients.
Definition: kmessageio.h:62
bool isConnected() const override
Returns true if the socket is in state /e connected.
Definition: kmessageio.cpp:87
bool isNetwork() const override
Definition: kmessageio.h:251
~KMessageSocket()
Destructor, closes the socket.
Definition: kmessageio.cpp:82
bool isNetwork() const override
Definition: kmessageio.h:327
virtual quint16 peerPort() const
Definition: kmessageio.h:124
virtual bool isNetwork() const
Definition: kmessageio.h:86
virtual QString peerName() const
Definition: kmessageio.h:129
Q_SLOTSQ_SLOTS
QObject * parent() const const
KMessageSocket(const QString &host, quint16 port, QObject *parent=nullptr)
Connects to a server socket on /e host with /e port.
Definition: kmessageio.cpp:51
int rtti() const override
The runtime identification.
Definition: kmessageio.h:236
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Nov 30 2020 22:37:54 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.