home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / kgame / kmessageclient.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-10  |  15.6 KB  |  423 lines

  1. /*
  2.     This file is part of the KDE games library
  3.     Copyright (C) 2001 Burkhard Lehner (Burkhard.Lehner@gmx.de)
  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. #ifndef __KMESSAGECLIENT_H__
  21. #define __KMESSAGECLIENT_H__
  22.  
  23. #include <qobject.h>
  24. #include <qstring.h>
  25. #include <qvaluelist.h>
  26.  
  27. class KMessageIO;
  28. class KMessageServer;
  29. class KMessageClientPrivate;
  30.  
  31. /**
  32.   @short A client to connect to a KMessageServer
  33.  
  34.   This class implements a client that can connect to a KMessageServer object.
  35.   It can be used to exchange messages between clients.
  36.  
  37.   Usually you will connect the signals broadcastReceived and forwardReceived to
  38.   some specific slots. In these slot methods you can analyse the messages that are
  39.   sent to you from other clients.
  40.  
  41.   To send messages to other clients, use the methods sendBroadcast() (to send to all
  42.   clients) or sendForward() (to send to a list of selected clients).
  43.  
  44.   If you want to communicate with the KMessageServer object directly (on a more low
  45.   level base), use the method sendServerMessage to send a command to the server and
  46.   connect to the signal serverMessageReceived to see all the incoming messages.
  47.   In that case the messages must be of the format specified in KMessageServer.
  48.   @author Burkhard Lehner <Burkhard.Lehner@gmx.de>
  49. */
  50. class KMessageClient : public QObject
  51. {
  52.   Q_OBJECT
  53.  
  54. public:
  55.  
  56.   /**
  57.     Constructor.
  58.     Creates an unconnected KMessageClient object. Use setServer() later to connect to a
  59.     KMessageServer object.
  60.   */
  61.   KMessageClient (QObject *parent = 0, const char *name = 0);
  62.  
  63.   /**
  64.     Destructor.
  65.     Disconnects from the server, if any connection was established.
  66.   */
  67.   ~KMessageClient ();
  68.  
  69.   /**
  70.     @return The client ID of this client. Every client that is connected to a KMessageServer
  71.     has a unique ID number.
  72.  
  73.     NOTE: As long as the object is not yet connected to the server, and as long as the server
  74.     hasn't sent the client ID, this method returns 0.
  75.   */
  76.   Q_UINT32 id () const;
  77.  
  78.   /**
  79.     @return Whether or not this client is the server admin.
  80.     One of the clients connected to the server is the admin and can administrate the server
  81.     (set maximum number of clients, remove clients, ...).
  82.  
  83.     If you use admin commands without being the admin, these commands are simply ignored by
  84.     the server.
  85.  
  86.     NOTE: As long as you are not connected to a server, this method returns false.
  87.   */
  88.   bool isAdmin () const;
  89.  
  90.   /**
  91.     @return The ID of the admin client on the message server.
  92.   */
  93.   Q_UINT32 adminId() const;
  94.  
  95.   /**
  96.     @return The list of the IDs of all the message clients connected to the message server.
  97.   */
  98.   const QValueList <Q_UINT32> &clientList() const;
  99.  
  100.   /**
  101.     Connects the client to (another) server.
  102.  
  103.     Tries to connect via a TCP/IP socket to a KMessageServer object
  104.     on the given host, listening on the specified port.
  105.  
  106.     If we were already connected, the old connection is closed.
  107.     @param host The name of the host to connect to. Must be either a hostname which can 
  108.     be resolved to an IP or just an IP
  109.     @param port The port to connect to
  110.   */
  111.   void setServer (const QString &host, Q_UINT16 port);
  112.  
  113.   /**
  114.     Connects the client to (another) server.
  115.  
  116.     Connects to the given server, using KMessageDirect.
  117.     (The server object has to be in the same process.)
  118.  
  119.     If we were already connected, the old connection is closed.
  120.     @param server The KMessageServer to connect to
  121.   */
  122.   void setServer (KMessageServer *server);
  123.  
  124.   /**
  125.    * Corresponds to setServer(0); but also emits the connectionBroken signal
  126.    **/
  127.   void disconnect();
  128.  
  129.   /**
  130.     Connects the client to (another) server.
  131.  
  132.     To use this method, you have to create a KMessageIO object with new (indeed you must
  133.     create an instance of a subclass of KMessageIO, e.g. KMessageSocket or KMessageDirect).
  134.     This object must already be connected to the new server.
  135.  
  136.     Calling this method disconnects any earlier connection, and uses the new KMessageIO
  137.     object instead. This object gets owned by the KMessageClient object, so don't delete
  138.     or manipulate it afterwards.
  139.  
  140.     With this method it is possible to change the server on the fly. But be careful that
  141.     there are no important messages from the old server not yet delivered.
  142.  
  143.     NOTE: It is very likely that we will have another client ID on the new server. The
  144.     value returned by clientID may be a little outdated until the new server tells us
  145.     our new ID.
  146.  
  147.     NOTE: The two other setServer methods are for convenience. If you use them, you don't
  148.     have to create a KMessageIO object yourself.
  149.   */
  150.   virtual void setServer (KMessageIO *connection);
  151.  
  152.   /**
  153.     @return True, if a connection to a KMessageServer has been started, and if the
  154.     connection is ready for transferring data. (It will return false e.g. as long as
  155.     a socket connection hasn't been established, and it will also return false after
  156.     a socket connection is broken.)
  157.   */
  158.   bool isConnected () const;
  159.  
  160.   /**
  161.     @return TRUE if isConnected() is true AND this is not a local (like
  162.     KMessageDirect) connection.
  163.   */
  164.   bool isNetwork () const;
  165.  
  166.   /**
  167.     @return 0 if isConnected() is FALSE, otherwise the port number this client is
  168.     connected to. See also KMessageIO::peerPort and QSocket::peerPort.
  169.     @since 3.2
  170.   */
  171.   Q_UINT16 peerPort () const;
  172.  
  173.   /**
  174.     @since 3.2
  175.     @return "localhost" if isConnected() is FALSE, otherwise the hostname this client is
  176.     connected to. See also KMessageIO::peerName() and QSocket::peerName().
  177.   */
  178.   QString peerName() const;
  179.  
  180.   /**
  181.     Sends a message to the KMessageServer. If we are not yet connected to one, nothing
  182.     happens.
  183.  
  184.     Use this method to send a low level command to the server. It has to be in the
  185.     format specified in KMessageServer.
  186.  
  187.     If you want to send messages to other clients, you should use sendBroadcast()
  188.     and sendForward().
  189.     @param msg The message to be sent to the server. Must be in the format specified in KMessageServer.
  190.   */
  191.   void sendServerMessage (const QByteArray &msg);
  192.  
  193.   /**
  194.     Sends a message to all the clients connected to the server, including ourself.
  195.     The message consists of an arbitrary block of data with arbitrary length.
  196.  
  197.     All the clients will receive an exact copy of this block of data, which will be
  198.     processed in their processBroadcast() method.
  199.     @param msg The message to be sent to the clients
  200.   */
  201.   //AB: processBroadcast doesn't exist!! is processIncomingMessage meant?
  202.   void sendBroadcast (const QByteArray &msg);
  203.  
  204.   /**
  205.     Sends a message to all the clients in a list.
  206.     The message consists of an arbitrary block of data with arbitrary length.
  207.  
  208.     All clients will receive an exact copy of this block of data, which will be
  209.     processed in their processForward() method.
  210.  
  211.     If the list contains client IDs that are not defined, they are ignored. If
  212.     it contains an ID several times, that client will receive the message several
  213.     times.
  214.  
  215.     To send a message to the admin of the KMessageServer, you can use 0 as clientID,
  216.     instead of using the real client ID.
  217.     @param msg The message to be sent to the clients
  218.     @param clients A list of clients the message should be sent to
  219.   */
  220.   //AB: processForward doesn't exist!! is processIncomingMessage meant?
  221.   void sendForward (const QByteArray &msg, const QValueList <Q_UINT32> &clients);
  222.  
  223.   /**
  224.     Sends a message to a single client. This is a convenieance method. It calls
  225.     sendForward (const QByteArray &msg, const QValueList <Q_UINT32> &clients)
  226.     with a list containing only one client ID.
  227.  
  228.     To send a message to the admin of the  KMessageServer, you can use 0 as clientID,
  229.     instead of using the real client ID.
  230.     @param msg The message to be sent to the client
  231.     @param client The id of the client the message shall be sent to
  232.   */
  233.   void sendForward (const QByteArray &msg, Q_UINT32 client);
  234.  
  235.   /**
  236.     Once this function is called no message will be received anymore.
  237.     processIncomingMessage() gets delayed until unlock() is called.
  238.  
  239.     Note that all messages are still received, but their delivery (like
  240.     broadcastReceived()) get delayed only.
  241.    */
  242.   void lock();
  243.  
  244.   /**
  245.     Deliver every message that was delayed by lock() and actually deliver 
  246.     all messages that get received from now on.
  247.    */
  248.   void unlock();
  249.  
  250.   /**
  251.     @return The number of messages that got delayed since lock() was called
  252.    */
  253.   unsigned int delayedMessageCount() const;
  254.  
  255. signals:
  256.   /**
  257.     This signal is emitted when the client receives a broadcast message from the
  258.     KMessageServer, sent by another client. Connect to this signal to analyse the
  259.     received message and do the right reaction.
  260.  
  261.     senderID contains the ID of the client that sent the broadcast message. You can
  262.     use this e.g. to send a reply message to only that client. Or you can use it
  263.     to ignore broadcast messages that were sent by yourself:
  264.  
  265.     \code
  266.       void myObject::myBroadcastSlot (const QByteArray &msg, Q_UINT32 senderID)
  267.       {
  268.         if (senderID == ((KMessageClient *)sender())->id())
  269.           return;
  270.         ...
  271.       }
  272.     \endcode
  273.     @param msg The message that has been sent to us
  274.     @param senderID The ID of the client which sent the message
  275.   */
  276.   void broadcastReceived (const QByteArray &msg, Q_UINT32 senderID);
  277.  
  278.   /**
  279.     This signal is emitted when the client receives a forward message from the
  280.     KMessageServer, sent by another client. Connect to this signal to analyse the
  281.     received message and do the right reaction.
  282.  
  283.     senderID contains the ID of the client that sent the broadcast message. You can
  284.     use this e.g. to send a reply message to only that client.
  285.  
  286.     receivers contains the list of the clients that got the message. (If this list
  287.     only contains one number, this will be your client ID, and it was exclusivly
  288.     sent to you.)
  289.  
  290.     If you don't want to distinguish between broadcast and forward messages and
  291.     treat them the same, you can connect forwardReceived signal to the
  292.     broadcastReceived signal. (Yes, that's possible! You can connect a Qt signal to
  293.     a Qt signal, and the second one can have less parameters.)
  294.  
  295.     \code
  296.       KMessageClient *client = new KMessageClient ();
  297.       connect (client, SIGNAL (forwardReceived (const QByteArray &, Q_UINT32, const QValueList <Q_UINT32>&)),
  298.                client, SIGNAL (broadcastReceived (const QByteArray &, Q_UINT32)));
  299.     \endcode
  300.  
  301.     Then connect the broadcast signal to your slot that analyzes the message.
  302.     @param msg The message that has been sent to us
  303.     @param senderID The ID of the client which sent the message
  304.     @param receivers All clients which receive this message
  305.   */
  306.   void forwardReceived (const QByteArray &msg, Q_UINT32 senderID, const QValueList <Q_UINT32> &receivers);
  307.  
  308.   /**
  309.     This signal is emitted when the connection to the KMessageServer is broken.
  310.     Reasons for this can be: a network error, a server breakdown, or you were just kicked
  311.     from the server.
  312.  
  313.     When this signal is sent, the connection is already lost and the client is unconnected.
  314.     You can connect to another server by calling setServer() afterwards. But keep in mind that
  315.     some important messages might have vanished.
  316.   */
  317.   void connectionBroken ();
  318.  
  319.   /**
  320.     This signal is emitted right before the client disconnects. It can be used
  321.     to this store the id of the client which is about to be lost.
  322.   */
  323.   void aboutToDisconnect(Q_UINT32 id);
  324.  
  325.   /**
  326.     This signal is emitted when this client becomes the admin client or when it loses
  327.     the admin client status. Connect to this signal if you have to do any initialization
  328.     or cleanup.
  329.     @param isAdmin Whether we are now admin or not
  330.   */
  331.   void adminStatusChanged (bool isAdmin);
  332.  
  333.   /**
  334.     This signal is emitted when another client has connected
  335.     to the server. Connect to this method if that clients needs initialization.
  336.     This should usually only be done in one client, e.g. the admin client.
  337.     @param clientID The ID of the client that has newly connectd.
  338.   */
  339.   void eventClientConnected (Q_UINT32 clientID);
  340.  
  341.   /**
  342.     This signal is emitted when the server has lost the
  343.     connection to one of the clients (This could be because of a bad internet connection
  344.     or because the client disconnected on purpose).
  345.     @param clientID The ID of the client that has disconnected
  346.     @param broken true if it was disconnected because of a network error
  347.   */
  348.   void eventClientDisconnected (Q_UINT32 clientID, bool broken);
  349.  
  350.   /**
  351.     This signal is emitted on every message that came from the server. You can connect to this
  352.     signal to see the messages directly. They are in the format specified in KMessageServer.
  353.  
  354.     @param msg The message that has been sent to us
  355.     @param unknown True when KMessageClient didn't recognize the message, i.e. it contained an unknown
  356.     message ID. If you want to add additional message types to the client, connect to this signal,
  357.     and if unknown is true, analyse the message by yourself. If you recognized the message,
  358.     set unknown to false (Otherwise a debug message will be printed).
  359.   */
  360.   //AB: maybe add a setNoEmit() so that the other signals can be deactivated?
  361.   //Could be a performance benefit (note: KMessageClient is a time critical
  362.   //class!!!)
  363.   void serverMessageReceived (const QByteArray &msg, bool &unknown);
  364.  
  365. protected:
  366.   /**
  367.     This slot is called from processIncomingMessage or 
  368.     processFirstMessage, depending on whether the client is locked or a delayed
  369.     message is still here (see lock)
  370.  
  371.     It processes the message and analyses it. If it is a broadcast or a forward message from
  372.     another client, it emits the signal processBroadcast or processForward accordingly.
  373.  
  374.     If you want to treat additional server messages, you can overwrite this method. Don't
  375.     forget to call processIncomingMessage of your superclass!
  376.  
  377.     At the moment, the following server messages are interpreted:
  378.  
  379.     MSG_BROADCAST, MSG_FORWARD, ANS_CLIENT_ID, ANS_ADMIN_ID, ANS_CLIENT_LIST
  380.     @param msg The incoming message
  381.   */
  382.  
  383.   virtual void processMessage (const QByteArray& msg);
  384.  
  385. protected slots:
  386.   /**
  387.     This slot is called from the signal KMessageIO::received whenever a message from the
  388.     KMessageServer arrives.
  389.  
  390.     It processes the message and analyses it. If it is a broadcast or a forward message from
  391.     another client, it emits the signal processBroadcast or processForward accordingly.
  392.  
  393.     If you want to treat additional server messages, you can overwrite this method. Don't
  394.     forget to call processIncomingMessage() of your superclass!
  395.  
  396.     At the moment, the following server messages are interpreted:
  397.  
  398.     MSG_BROADCAST, MSG_FORWARD, ANS_CLIENT_ID, ANS_ADMIN_ID, ANS_CLIENT_LIST
  399.     @param msg The incoming message
  400.   */
  401.   virtual void processIncomingMessage (const QByteArray &msg);
  402.  
  403.   /**
  404.     Called from unlock() (using QTimer::singleShot) until all delayed 
  405.     messages are delivered.
  406.   */
  407.   void processFirstMessage();
  408.  
  409.   /**
  410.     This slot is called from the signal KMessageIO::connectionBroken.
  411.  
  412.     It deletes the internal KMessageIO object, and resets the client to default
  413.     values. To connect again to another server, use setServer.
  414.   */
  415.   virtual void removeBrokenConnection ();
  416.   void removeBrokenConnection2 ();
  417.  
  418. private:
  419.   KMessageClientPrivate *d;
  420. };
  421.  
  422. #endif
  423.