kmessageserver.h
00001 /* 00002 This file is part of the TDE 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 #ifndef __KMESSAGESERVER_H__ 00021 #define __KMESSAGESERVER_H__ 00022 00023 #include <tqobject.h> 00024 #include <tqserversocket.h> 00025 #include <tqstring.h> 00026 #include <tqvaluelist.h> 00027 00028 class KMessageIO; 00029 class KMessageServerPrivate; 00030 00175 class KMessageServer : public TQObject 00176 { 00177 Q_OBJECT 00178 00179 00180 public: 00184 enum { 00185 REQ_BROADCAST = 1, 00186 REQ_FORWARD, 00187 REQ_CLIENT_ID, 00188 REQ_ADMIN_ID, 00189 REQ_ADMIN_CHANGE, 00190 REQ_REMOVE_CLIENT, 00191 REQ_MAX_NUM_CLIENTS, 00192 REQ_CLIENT_LIST, 00193 REQ_MAX_REQ = 0xffff }; 00194 00198 enum { 00199 MSG_BROADCAST = 101, 00200 MSG_FORWARD, 00201 ANS_CLIENT_ID, 00202 ANS_ADMIN_ID, 00203 ANS_CLIENT_LIST, 00204 EVNT_CLIENT_CONNECTED, 00205 EVNT_CLIENT_DISCONNECTED, 00206 EVNT_MAX_EVNT = 0xffff 00207 }; 00208 00212 KMessageServer(TQ_UINT16 cookie = 42, TQObject* parent = 0); 00213 00214 ~KMessageServer(); 00215 00219 virtual void Debug(); 00220 00221 //---------------------------------- TCP/IP server stuff 00222 00230 bool initNetwork (TQ_UINT16 port = 0); 00231 00238 TQ_UINT16 serverPort () const; 00239 00245 void stopNetwork(); 00246 00251 bool isOfferingConnections() const; 00252 00253 //---------------------------------- adding / removing clients 00254 00255 public slots: 00269 void addClient (KMessageIO *); 00270 00282 void removeClient (KMessageIO *io, bool broken); 00283 00287 void deleteClients(); 00288 00289 private slots: 00296 void removeBrokenClient (); 00297 00298 public: 00310 void setMaxClients(int maxnumber); 00311 00317 int maxClients() const; 00318 00324 int clientCount() const; 00325 00329 TQValueList <TQ_UINT32> clientIDs() const; 00330 00336 KMessageIO *findClient (TQ_UINT32 no) const; 00337 00344 TQ_UINT32 adminID() const; 00345 00351 void setAdmin (TQ_UINT32 adminID); 00352 00353 00354 //------------------------------ ID stuff 00355 00356 /* 00357 * The unique ID of this game 00358 * 00359 * @return int id 00360 **/ 00361 // int gameId() const; 00362 00363 /* 00364 * Application cookie. this idendifies the game application. It 00365 * help to distinguish between e.g. KPoker and KWin4 00366 * 00367 * @return the application cookie 00368 **/ 00369 // int cookie() const; 00370 00371 //------------------------------ Message stuff 00372 00373 public: 00379 virtual void broadcastMessage (const TQByteArray &msg); 00380 00389 virtual void sendMessage (TQ_UINT32 id, const TQByteArray &msg); 00390 00398 virtual void sendMessage (const TQValueList <TQ_UINT32> &ids, const TQByteArray &msg); 00399 00400 protected slots: 00410 virtual void getReceivedMessage (const TQByteArray &msg); 00411 00420 virtual void processOneMessage (); 00421 00422 //---------------------------- Signals 00423 00424 signals: 00429 void clientConnected (KMessageIO *client); 00430 00438 void connectionLost (KMessageIO *client); 00439 00452 void messageReceived (const TQByteArray &data, TQ_UINT32 clientID, bool &unknown); 00453 00454 protected: 00460 TQ_UINT32 uniqueClientNumber() const; 00461 00462 private: 00463 KMessageServerPrivate* d; 00464 }; 00465 00466 00477 class KMessageServerSocket : public TQServerSocket 00478 { 00479 Q_OBJECT 00480 00481 00482 public: 00483 KMessageServerSocket (TQ_UINT16 port, TQObject *parent = 0); 00484 ~KMessageServerSocket (); 00485 00486 void newConnection (int socket); 00487 00488 signals: 00489 void newClientConnected (KMessageIO *client); 00490 }; 00491 00492 00493 00494 #endif