KMessageClient Class Reference
A client to connect to a KMessageServer. More...
#include <kmessageclient.h>
Inheritance diagram for KMessageClient:

Signals | |
void | broadcastReceived (const QByteArray &msg, Q_UINT32 senderID) |
void | forwardReceived (const QByteArray &msg, Q_UINT32 senderID, const QValueList< Q_UINT32 > &receivers) |
void | connectionBroken () |
void | aboutToDisconnect (Q_UINT32 id) |
void | adminStatusChanged (bool isAdmin) |
void | eventClientConnected (Q_UINT32 clientID) |
void | eventClientDisconnected (Q_UINT32 clientID, bool broken) |
void | serverMessageReceived (const QByteArray &msg, bool &unknown) |
Public Member Functions | |
KMessageClient (QObject *parent=0, const char *name=0) | |
~KMessageClient () | |
Q_UINT32 | id () const |
bool | isAdmin () const |
Q_UINT32 | adminId () const |
const QValueList< Q_UINT32 > & | clientList () const |
void | setServer (const QString &host, Q_UINT16 port) |
void | setServer (KMessageServer *server) |
void | disconnect () |
virtual void | setServer (KMessageIO *connection) |
bool | isConnected () const |
bool | isNetwork () const |
Q_UINT16 | peerPort () const |
QString | peerName () const |
void | sendServerMessage (const QByteArray &msg) |
void | sendBroadcast (const QByteArray &msg) |
void | sendForward (const QByteArray &msg, const QValueList< Q_UINT32 > &clients) |
void | sendForward (const QByteArray &msg, Q_UINT32 client) |
void | lock () |
void | unlock () |
unsigned int | delayedMessageCount () const |
Protected Slots | |
virtual void | processIncomingMessage (const QByteArray &msg) |
void | processFirstMessage () |
virtual void | removeBrokenConnection () |
void | removeBrokenConnection2 () |
Protected Member Functions | |
virtual void | processMessage (const QByteArray &msg) |
Detailed Description
A client to connect to a KMessageServer.This class implements a client that can connect to a KMessageServer object. It can be used to exchange messages between clients.
Usually you will connect the signals broadcastReceived and forwardReceived to some specific slots. In these slot methods you can analyse the messages that are sent to you from other clients.
To send messages to other clients, use the methods sendBroadcast() (to send to all clients) or sendForward() (to send to a list of selected clients).
If you want to communicate with the KMessageServer object directly (on a more low level base), use the method sendServerMessage to send a command to the server and connect to the signal serverMessageReceived to see all the incoming messages. In that case the messages must be of the format specified in KMessageServer.
- Author:
- Burkhard Lehner <Burkhard.Lehner@gmx.de>
Definition at line 50 of file kmessageclient.h.
Constructor & Destructor Documentation
|
Constructor. Creates an unconnected KMessageClient object. Use setServer() later to connect to a KMessageServer object. Definition at line 51 of file kmessageclient.cpp. References KMessageClient(). Referenced by KMessageClient(). |
|
Destructor. Disconnects from the server, if any connection was established. Definition at line 58 of file kmessageclient.cpp. |
Member Function Documentation
|
Definition at line 98 of file kmessageclient.cpp. Referenced by isAdmin(). |
|
NOTE: As long as you are not connected to a server, this method returns false. Definition at line 103 of file kmessageclient.cpp. References adminId(), and id(). Referenced by processMessage(). |
|
Definition at line 108 of file kmessageclient.cpp. Referenced by isAdmin(). |
|
Definition at line 113 of file kmessageclient.cpp. |
|
Connects the client to (another) server. Tries to connect via a TCP/IP socket to a KMessageServer object on the given host, listening on the specified port. If we were already connected, the old connection is closed.
Definition at line 66 of file kmessageclient.cpp. References setServer(). Referenced by setServer(). |
|
Connects the client to (another) server. Connects to the given server, using KMessageDirect. (The server object has to be in the same process.) If we were already connected, the old connection is closed.
Definition at line 71 of file kmessageclient.cpp. References KMessageServer::addClient(), and setServer(). |
|
Corresponds to setServer(0); but also emits the connectionBroken signal.
Definition at line 342 of file kmessageclient.cpp. References connectionBroken(). |
|
Connects the client to (another) server. To use this method, you have to create a KMessageIO object with new (indeed you must create an instance of a subclass of KMessageIO, e.g. KMessageSocket or KMessageDirect). This object must already be connected to the new server. Calling this method disconnects any earlier connection, and uses the new KMessageIO object instead. This object gets owned by the KMessageClient object, so don't delete or manipulate it afterwards. With this method it is possible to change the server on the fly. But be careful that there are no important messages from the old server not yet delivered. NOTE: It is very likely that we will have another client ID on the new server. The value returned by clientID may be a little outdated until the new server tells us our new ID. NOTE: The two other setServer methods are for convenience. If you use them, you don't have to create a KMessageIO object yourself. Definition at line 78 of file kmessageclient.cpp. References connectionBroken(), processIncomingMessage(), removeBrokenConnection(), and setServer(). |
|
Definition at line 118 of file kmessageclient.cpp. Referenced by isNetwork(). |
|
Definition at line 123 of file kmessageclient.cpp. References isConnected(). |
|
Definition at line 128 of file kmessageclient.cpp. |
|
Definition at line 133 of file kmessageclient.cpp. |
|
Sends a message to the KMessageServer. If we are not yet connected to one, nothing happens. Use this method to send a low level command to the server. It has to be in the format specified in KMessageServer. If you want to send messages to other clients, you should use sendBroadcast() and sendForward().
Definition at line 140 of file kmessageclient.cpp. References sendServerMessage(). Referenced by sendBroadcast(), sendForward(), and sendServerMessage(). |
|
Sends a message to all the clients connected to the server, including ourself. The message consists of an arbitrary block of data with arbitrary length. All the clients will receive an exact copy of this block of data, which will be processed in their processBroadcast() method.
Definition at line 150 of file kmessageclient.cpp. References sendBroadcast(), and sendServerMessage(). Referenced by sendBroadcast(). |
|
Sends a message to all the clients in a list. The message consists of an arbitrary block of data with arbitrary length. All clients will receive an exact copy of this block of data, which will be processed in their processForward() method. If the list contains client IDs that are not defined, they are ignored. If it contains an ID several times, that client will receive the message several times. To send a message to the admin of the KMessageServer, you can use 0 as clientID, instead of using the real client ID.
Definition at line 162 of file kmessageclient.cpp. References sendForward(), and sendServerMessage(). Referenced by sendForward(). |
|
Sends a message to a single client. This is a convenieance method. It calls sendForward (const QByteArray &msg, const QValueList <Q_UINT32> &clients) with a list containing only one client ID. To send a message to the admin of the KMessageServer, you can use 0 as clientID, instead of using the real client ID.
Definition at line 174 of file kmessageclient.cpp. References sendForward(). |
|
Once this function is called no message will be received anymore. processIncomingMessage() gets delayed until unlock() is called. Note that all messages are still received, but their delivery (like broadcastReceived()) get delayed only. Definition at line 354 of file kmessageclient.cpp. Referenced by KGameNetwork::lock(). |
|
Deliver every message that was delayed by lock() and actually deliver all messages that get received from now on.
Definition at line 359 of file kmessageclient.cpp. References processFirstMessage(). Referenced by KGameNetwork::unlock(). |
|
Definition at line 368 of file kmessageclient.cpp. |
|
This signal is emitted when the client receives a broadcast message from the KMessageServer, sent by another client. Connect to this signal to analyse the received message and do the right reaction. senderID contains the ID of the client that sent the broadcast message. You can use this e.g. to send a reply message to only that client. Or you can use it to ignore broadcast messages that were sent by yourself:
Referenced by processMessage(). |
|
This signal is emitted when the client receives a forward message from the KMessageServer, sent by another client. Connect to this signal to analyse the received message and do the right reaction. senderID contains the ID of the client that sent the broadcast message. You can use this e.g. to send a reply message to only that client. receivers contains the list of the clients that got the message. (If this list only contains one number, this will be your client ID, and it was exclusivly sent to you.) If you don't want to distinguish between broadcast and forward messages and treat them the same, you can connect forwardReceived signal to the broadcastReceived signal. (Yes, that's possible! You can connect a Qt signal to a Qt signal, and the second one can have less parameters.)
Then connect the broadcast signal to your slot that analyzes the message.
Referenced by processMessage(). |
|
This signal is emitted when the connection to the KMessageServer is broken. Reasons for this can be: a network error, a server breakdown, or you were just kicked from the server. When this signal is sent, the connection is already lost and the client is unconnected. You can connect to another server by calling setServer() afterwards. But keep in mind that some important messages might have vanished. Referenced by disconnect(), and setServer(). |
|
This signal is emitted right before the client disconnects. It can be used to this store the id of the client which is about to be lost. |
|
This signal is emitted when this client becomes the admin client or when it loses the admin client status. Connect to this signal if you have to do any initialization or cleanup.
Referenced by processMessage(). |
|
This signal is emitted when another client has connected to the server. Connect to this method if that clients needs initialization. This should usually only be done in one client, e.g. the admin client.
Referenced by processMessage(). |
|
This signal is emitted when the server has lost the connection to one of the clients (This could be because of a bad internet connection or because the client disconnected on purpose).
Referenced by processMessage(). |
|
This signal is emitted on every message that came from the server. You can connect to this signal to see the messages directly. They are in the format specified in KMessageServer.
Referenced by processMessage(). |
|
This slot is called from processIncomingMessage or processFirstMessage, depending on whether the client is locked or a delayed message is still here (see lock). It processes the message and analyses it. If it is a broadcast or a forward message from another client, it emits the signal processBroadcast or processForward accordingly. If you want to treat additional server messages, you can overwrite this method. Don't forget to call processIncomingMessage of your superclass! At the moment, the following server messages are interpreted: MSG_BROADCAST, MSG_FORWARD, ANS_CLIENT_ID, ANS_ADMIN_ID, ANS_CLIENT_LIST
Definition at line 202 of file kmessageclient.cpp. References adminStatusChanged(), broadcastReceived(), eventClientConnected(), eventClientDisconnected(), forwardReceived(), isAdmin(), processMessage(), and serverMessageReceived(). Referenced by processFirstMessage(), processIncomingMessage(), and processMessage(). |
|
This slot is called from the signal KMessageIO::received whenever a message from the KMessageServer arrives. It processes the message and analyses it. If it is a broadcast or a forward message from another client, it emits the signal processBroadcast or processForward accordingly. If you want to treat additional server messages, you can overwrite this method. Don't forget to call processIncomingMessage() of your superclass! At the moment, the following server messages are interpreted: MSG_BROADCAST, MSG_FORWARD, ANS_CLIENT_ID, ANS_ADMIN_ID, ANS_CLIENT_LIST
Definition at line 182 of file kmessageclient.cpp. References processIncomingMessage(), and processMessage(). Referenced by processIncomingMessage(), and setServer(). |
|
Called from unlock() (using QTimer::singleShot) until all delayed messages are delivered.
Definition at line 305 of file kmessageclient.cpp. References processMessage(). Referenced by unlock(). |
|
This slot is called from the signal KMessageIO::connectionBroken. It deletes the internal KMessageIO object, and resets the client to default values. To connect again to another server, use setServer. Definition at line 321 of file kmessageclient.cpp. Referenced by setServer(). |
The documentation for this class was generated from the following files: