summaryrefslogtreecommitdiffstats
path: root/libkdegames/kgame/kmessageio.h
blob: 5a102e11c87f132c9b94ae7b6055aea504611d8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
    This file is part of the KDE games library
    Copyright (C) 2001 Burkhard Lehner (Burkhard.Lehner@gmx.de)

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License version 2 as published by the Free Software Foundation.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

/*
     KMessageIO class and subclasses KMessageSocket and KMessageDirect
*/

#ifndef _KMESSAGEIO_H_
#define _KMESSAGEIO_H_

#include <tqcstring.h>
#include <tqhostaddress.h>
#include <tqobject.h>
#include <tqstring.h>
#include <tqptrqueue.h>
#include <tqfile.h>
#include <kdebug.h>

class TQSocket;
class KProcess;
//class TQFile;


/**
  This abstract base class represents one end of a message connections
  between two clients. Each client has one object of a subclass of
  KMessageIO. Calling /e send() on one object will emit the signal
  /e received() on the other object, and vice versa.

  For each type of connection (TCP/IP socket, COM port, direct connection
  within the same class) a subclass of KMessageIO must be defined that
  implements the pure virtual methods /e send() and /e isConnected(),
  and sends the signals. (See /e KMessageSocket for an example implementation.)

  Two subclasses are already included: /e KMessageSocket (connection using
  TCP/IP sockets) and /e KMessageDirect (connection using method calls, both
  sides must be within the same process).
*/

class KMessageIO : public QObject
{
  Q_OBJECT

public:
  /**
   * The usual TQObject constructor, does nothing else.
   **/
  KMessageIO (TQObject *parent = 0, const char *name = 0);

  /**
   * The usual destructor, does nothing special.
   **/
  ~KMessageIO ();

  /**
  * The runtime idendifcation
  */
  virtual int rtti() const {return 0;}

  /**
   * @return Whether this KMessageIO is a network IO or not.
   **/
  //virtual bool isNetwork () const = 0;
  virtual bool isNetwork () const
  {
   kdError(11001) << "Calling PURE virtual isNetwork...BAD" << endl;
   return false;
  }

  /**
    This method returns the status of the object, whether it is already
    (or still) connected to another KMessageIO object or not.

    This is a pure virtual method, so it has to be implemented in a subclass
    of KMessageIO.
  */
  //virtual bool isConnected () const = 0;
  virtual bool isConnected () const
  {
   kdError(11001) << "Calling PURE virtual isConencted...BAD" << endl;
   return false;
  }

  /**
    Sets the ID number of this object. This number can for example be used to
    distinguish several objects in a server.

    NOTE: Sometimes it is useful to let two connected KMessageIO objects
    have the same ID number. You have to do so yourself, KMessageIO doesn't
    change this value on its own!
  */
  void setId (Q_UINT32 id);

  /**
    Queries the ID of this object.
  */
  Q_UINT32 id ();

  /**
    @since 3.2
    @return 0 in the default implementation. Reimplemented in @ref KMessageSocket.
  */
  virtual Q_UINT16 peerPort () const { return 0; }

  /**
    @since 3.2
    @return "localhost" in the default implementation. Reimplemented in @ref KMessageSocket
  */
  virtual TQString peerName () const { return TQString::fromLatin1("localhost"); }


signals:
  /**
    This signal is emitted when /e send() on the connected KMessageIO
    object is called. The parameter contains the same data array in /e msg
    as was used in /e send().
  */
  void received (const TQByteArray &msg);

  /**
    This signal is emitted when the connection is closed. This can be caused
    by a hardware error (e.g. broken internet connection) or because the other
    object was killed.

    Note: Sometimes a broken connection can be undetected for a long time,
    or may never be detected at all. So don't rely on this signal!
  */
  void connectionBroken ();

public slots:

  /**
    This slot sends the data block in /e msg to the connected object, that will
    emit /e received().

    For a concrete class, you have to subclass /e KMessageIO and overwrite this
    method. In the subclass, implement this method as an ordinary method, not
    as a slot! (Otherwise another slot would be defined. It would work, but uses
    more memory and time.) See /e KMessageSocket for an example implementation.
  */
  virtual void send (const TQByteArray &msg) = 0;

protected:
  Q_UINT32 m_id;
};


/**
  This class implements the message communication using a TCP/IP socket. The
  object can connect to a server socket, or can use an already connected socket.
*/

class KMessageSocket : public KMessageIO
{
  Q_OBJECT

public:
  /**
    Connects to a server socket on /e host with /e port. host can be an
    numerical (e.g. "192.168.0.212") or symbolic (e.g. "wave.peter.org")
    IP address. You can immediately use the /e sendSystem() and
    /e sendBroadcast() methods. The messages are stored and sent to the
    receiver after the connection is established.

    If the connection could not be established (e.g. unknown host or no server
    socket at this port), the signal /e connectionBroken is emitted.
  */
  KMessageSocket (TQString host, Q_UINT16 port, TQObject *parent = 0,
                  const char *name = 0);

  /**
    Connects to a server socket on /e host with /e port. You can immediately
    use the /e sendSystem() and /e sendBroadcast() methods. The messages are
    stored and sent to the receiver after the connection is established.

    If the connection could not be established (e.g. unknown host or no server
    socket at this port), the signal /e connectionBroken is emitted.
  */
  KMessageSocket (TQHostAddress host, Q_UINT16 port, TQObject *parent = 0,
                  const char *name = 0);

  /**
    Uses /e socket to do the communication.

    The socket should already be connected, or at least be in /e connecting
    state.

    Note: The /e socket object is then owned by the /e KMessageSocket object.
    So don't use it otherwise any more and don't delete it. It is deleted
    together with this KMessageSocket object. (Use 0 as parent for the QSocket
    object t ensure it is not deleted.)
  */
  KMessageSocket (TQSocket *socket, TQObject *parent = 0, const char *name = 0);

  /**
    Uses the socket specified by the socket descriptor socketFD to do the
    communication. The socket must already be connected.

    This constructor can be used with a TQServerSocket within the (pure
    virtual) method /e newConnection.

    Note: The socket is then owned by the /e KMessageSocket object. So don't
    manipulate the socket afterwards, especially don't close it. The socket is
    automatically closed when KMessageSocket is deleted.
  */
  KMessageSocket (int socketFD, TQObject *parent = 0, const char *name = 0);

  /**
    Destructor, closes the socket.
  */
  ~KMessageSocket ();

  /**
  * The runtime idendifcation
  */
  virtual int rtti() const {return 1;}

  /**
    @since 3.2
    @return The port that this object is connected to. See TQSocket::peerPort
  */
  virtual Q_UINT16 peerPort () const;

  /**
    @since 3.2
    @return The hostname this object is connected to. See TQSocket::peerName.
  */
  virtual TQString peerName () const;

  /**
    @return TRUE as this is a network IO.
  */
  bool isNetwork() const { return true; }

  /**
    Returns true if the socket is in state /e connected.
  */
  bool isConnected () const;

  /**
    Overwritten slot method from KMessageIO.

    Note: It is not declared as a slot method, since the slot is already
    defined in KMessageIO as a virtual method.
  */
  void send (const TQByteArray &msg);

protected slots:
  virtual void processNewData ();

protected:
  void initSocket ();
  TQSocket *mSocket;
  bool mAwaitingHeader;
  Q_UINT32 mNextBlockLength;

  bool isRecursive;  // workaround for "bug" in TQSocket, Qt 2.2.3 or older
};


/**
  This class implements the message communication using function calls
  directly. It can only be used when both sides of the message pipe are
  within the same process. The communication is very fast.

  To establish a communication, you have to create two instances of
  KMessageDirect, the first one with no parameters in the constructor,
  the second one with the first as parameter:

  /code
    KMessageDirect *peer1, *peer2;
    peer1 = new KMessageDirect ();       // unconnected
    peer2 = new KMessageDirect (peer1);  // connect with peer1
  /endcode

  The connection is only closed when one of the instances is deleted.
*/

class KMessageDirect : public KMessageIO
{
  Q_OBJECT

public:
  /**
    Creates an object and connects it to the object given in the first
    parameter. Use 0 as first parameter to create an unconnected object,
    that is later connected.

    If that object is already connected, the object remains unconnected.
  */
  KMessageDirect (KMessageDirect *partner = 0, TQObject *parent = 0, const char
*name = 0);

  /**
    Destructor, closes the connection.
  */
  ~KMessageDirect ();

  /**
  * The runtime idendifcation
  */
  virtual int rtti() const {return 2;}


  /**
    @return FALSE as this is no network IO.
  */
  bool isNetwork() const { return false; }

  /**
    Returns true, if the object is connected to another instance.

    If you use the first constructor, the object is unconnected unless another
    object is created with this one as parameter.

    The connection can only be closed by deleting one of the objects.
  */
  bool isConnected () const;

  /**
    Overwritten slot method from KMessageIO.

    Note: It is not declared as a slot method, since the slot is already
    defined in KMessageIO as a virtual method.
  */
  void send (const TQByteArray &msg);

protected:
  KMessageDirect *mPartner;
};

class KMessageProcess : public KMessageIO
{
  Q_OBJECT 

  public:
    KMessageProcess(TQObject *parent, TQString file);
    ~KMessageProcess();
    bool isConnected() const;
    void send (const TQByteArray &msg);
    void writeToProcess();

    /**
      @return FALSE as this is no network IO.
    */
    bool isNetwork() const { return false; }

  /**
  * The runtime idendifcation
  */
  virtual int rtti() const {return 3;}



  public slots:
  void  slotReceivedStdout(KProcess *proc, char *buffer, int buflen);
  void  slotReceivedStderr(KProcess *proc, char *buffer, int buflen);
  void  slotProcessExited(KProcess *p);
  void  slotWroteStdin(KProcess *p);

  private:
    TQString mProcessName;
    KProcess *mProcess;
    TQPtrQueue <TQByteArray> mQueue;
    TQByteArray *mSendBuffer;
    TQByteArray mReceiveBuffer;
    unsigned int mReceiveCount;
};

class KMessageFilePipe : public KMessageIO
{
  Q_OBJECT 

  public:
    KMessageFilePipe(TQObject *parent,TQFile *readFile,TQFile *writeFile);
    ~KMessageFilePipe();
    bool isConnected() const;
    void send (const TQByteArray &msg);
    void exec();

    /**
      @return FALSE as this is no network IO.
    */
    bool isNetwork() const { return false; }

  /**
  * The runtime idendifcation
  */
  virtual int rtti() const {return 4;}



  private:
    TQFile *mReadFile;
    TQFile *mWriteFile;
    TQByteArray mReceiveBuffer;
    unsigned int mReceiveCount;
};

#endif