libkdegames/libkdegamesprivate/kgame
#include <KGame/KMessageClient>
Signals | |
void | aboutToDisconnect (quint32 id) |
void | adminStatusChanged (bool isAdmin) |
void | broadcastReceived (const QByteArray &msg, quint32 senderID) |
void | connectionBroken () |
void | eventClientConnected (quint32 clientID) |
void | eventClientDisconnected (quint32 clientID, bool broken) |
void | forwardReceived (const QByteArray &msg, quint32 senderID, const QList< quint32 > &receivers) |
void | serverMessageReceived (const QByteArray &msg, bool &unknown) |
Public Member Functions | |
KMessageClient (QObject *parent=0) | |
~KMessageClient () | |
quint32 | adminId () const |
QList< quint32 > | clientList () const |
unsigned int | delayedMessageCount () const |
void | disconnect () |
quint32 | id () const |
bool | isAdmin () const |
bool | isConnected () const |
bool | isNetwork () const |
void | lock () |
QString | peerName () const |
quint16 | peerPort () const |
void | sendBroadcast (const QByteArray &msg) |
void | sendForward (const QByteArray &msg, const QList< quint32 > &clients) |
void | sendForward (const QByteArray &msg, quint32 client) |
void | sendServerMessage (const QByteArray &msg) |
void | setServer (const QString &host, quint16 port) |
void | setServer (KMessageServer *server) |
virtual void | setServer (KMessageIO *connection) |
void | unlock () |
Public Member Functions inherited from QObject | |
QObject (QObject *parent) | |
QObject (QObject *parent, const char *name) | |
virtual | ~QObject () |
bool | blockSignals (bool block) |
QObject * | child (const char *objName, const char *inheritsClass, bool recursiveSearch) const |
const QObjectList & | children () const |
const char * | className () const |
bool | connect (const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type) const |
void | deleteLater () |
void | destroyed (QObject *obj) |
bool | disconnect (const QObject *receiver, const char *method) |
bool | disconnect (const char *signal, const QObject *receiver, const char *method) |
void | dumpObjectInfo () |
void | dumpObjectTree () |
QList< QByteArray > | dynamicPropertyNames () const |
virtual bool | event (QEvent *e) |
virtual bool | eventFilter (QObject *watched, QEvent *event) |
T | findChild (const QString &name) const |
QList< T > | findChildren (const QRegExp ®Exp) const |
QList< T > | findChildren (const QString &name) const |
bool | inherits (const char *className) const |
void | insertChild (QObject *object) |
void | installEventFilter (QObject *filterObj) |
bool | isA (const char *className) const |
bool | isWidgetType () const |
void | killTimer (int id) |
virtual const QMetaObject * | metaObject () const |
void | moveToThread (QThread *targetThread) |
const char * | name () const |
const char * | name (const char *defaultName) const |
QString | objectName () const |
QObject * | parent () const |
QVariant | property (const char *name) const |
void | removeChild (QObject *object) |
void | removeEventFilter (QObject *obj) |
void | setName (const char *name) |
void | setObjectName (const QString &name) |
void | setParent (QObject *parent) |
bool | setProperty (const char *name, const QVariant &value) |
bool | signalsBlocked () const |
int | startTimer (int interval) |
QThread * | thread () const |
Protected Slots | |
void | processFirstMessage () |
virtual void | processIncomingMessage (const QByteArray &msg) |
virtual void | removeBrokenConnection () |
void | removeBrokenConnection2 () |
Protected Member Functions | |
virtual void | processMessage (const QByteArray &msg) |
Protected Member Functions inherited from QObject | |
bool | checkConnectArgs (const char *signal, const QObject *object, const char *method) |
virtual void | childEvent (QChildEvent *event) |
virtual void | connectNotify (const char *signal) |
virtual void | customEvent (QEvent *event) |
virtual void | disconnectNotify (const char *signal) |
int | receivers (const char *signal) const |
QObject * | sender () const |
int | senderSignalIndex () const |
virtual void | timerEvent (QTimerEvent *event) |
Additional Inherited Members | |
Static Public Member Functions inherited from QObject | |
bool | connect (const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type) |
bool | connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type) |
bool | disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *method) |
bool | disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method) |
QString | tr (const char *sourceText, const char *disambiguation, int n) |
QString | trUtf8 (const char *sourceText, const char *disambiguation, int n) |
Static Protected Member Functions inherited from QObject | |
QByteArray | normalizeSignalSlot (const char *signalSlot) |
Properties inherited from QObject | |
objectName | |
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 analyze 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.
Definition at line 52 of file kmessageclient.h.
Constructor & Destructor Documentation
KMessageClient::KMessageClient | ( | QObject * | parent = 0 | ) |
Constructor.
Creates an unconnected KMessageClient object. Use setServer() later to connect to a KMessageServer object.
Definition at line 52 of file kmessageclient.cpp.
KMessageClient::~KMessageClient | ( | ) |
Destructor.
Disconnects from the server, if any connection was established.
Definition at line 59 of file kmessageclient.cpp.
Member Function Documentation
|
signal |
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.
quint32 KMessageClient::adminId | ( | ) | const |
- Returns
- The ID of the admin client on the message server.
Definition at line 109 of file kmessageclient.cpp.
|
signal |
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.
- Parameters
-
isAdmin Whether we are now admin or not
|
signal |
This signal is emitted when the client receives a broadcast message from the KMessageServer, sent by another client.
Connect to this signal to analyze 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:
- Parameters
-
msg The message that has been sent to us senderID The ID of the client which sent the message
QList< quint32 > KMessageClient::clientList | ( | ) | const |
- Returns
- The list of the IDs of all the message clients connected to the message server.
Definition at line 114 of file kmessageclient.cpp.
|
signal |
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.
unsigned int KMessageClient::delayedMessageCount | ( | ) | const |
- Returns
- The number of messages that got delayed since lock() was called
Definition at line 370 of file kmessageclient.cpp.
void KMessageClient::disconnect | ( | ) |
Corresponds to setServer(0); but also emits the connectionBroken signal.
Definition at line 344 of file kmessageclient.cpp.
|
signal |
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.
- Parameters
-
clientID The ID of the client that has newly connectd.
|
signal |
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).
- Parameters
-
clientID The ID of the client that has disconnected broken true if it was disconnected because of a network error
|
signal |
This signal is emitted when the client receives a forward message from the KMessageServer, sent by another client.
Connect to this signal to analyze 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.
- Parameters
-
msg The message that has been sent to us senderID The ID of the client which sent the message receivers All clients which receive this message
quint32 KMessageClient::id | ( | ) | const |
- Returns
- The client ID of this client. Every client that is connected to a KMessageServer has a unique ID number.
NOTE: As long as the object is not yet connected to the server, and as long as the server hasn't sent the client ID, this method returns 0.
Definition at line 99 of file kmessageclient.cpp.
bool KMessageClient::isAdmin | ( | ) | const |
- Returns
- Whether or not this client is the server admin. One of the clients connected to the server is the admin and can administrate the server (set maximum number of clients, remove clients, ...).
If you use admin commands without being the admin, these commands are simply ignored by the server.
NOTE: As long as you are not connected to a server, this method returns false.
Definition at line 104 of file kmessageclient.cpp.
bool KMessageClient::isConnected | ( | ) | const |
- Returns
- True, if a connection to a KMessageServer has been started, and if the connection is ready for transferring data. (It will return false e.g. as long as a socket connection hasn't been established, and it will also return false after a socket connection is broken.)
Definition at line 119 of file kmessageclient.cpp.
bool KMessageClient::isNetwork | ( | ) | const |
- Returns
- TRUE if isConnected() is true AND this is not a local (like KMessageDirect) connection.
Definition at line 124 of file kmessageclient.cpp.
void KMessageClient::lock | ( | ) |
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 356 of file kmessageclient.cpp.
QString KMessageClient::peerName | ( | ) | const |
- Returns
- "localhost" if isConnected() is FALSE, otherwise the hostname this client is connected to. See also KMessageIO::peerName() and QSocket::peerName().
Definition at line 134 of file kmessageclient.cpp.
quint16 KMessageClient::peerPort | ( | ) | const |
- Returns
- 0 if isConnected() is FALSE, otherwise the port number this client is connected to. See also KMessageIO::peerPort and QSocket::peerPort.
Definition at line 129 of file kmessageclient.cpp.
|
protectedslot |
Called from unlock() (using QTimer::singleShot) until all delayed messages are delivered.
Definition at line 307 of file kmessageclient.cpp.
|
protectedvirtualslot |
This slot is called from the signal KMessageIO::received whenever a message from the KMessageServer arrives.
It processes the message and analyzes 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
- Parameters
-
msg The incoming message
Definition at line 183 of file kmessageclient.cpp.
|
protectedvirtual |
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 analyzes 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
- Parameters
-
msg The incoming message
Definition at line 203 of file kmessageclient.cpp.
|
protectedvirtualslot |
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 323 of file kmessageclient.cpp.
|
protectedslot |
Definition at line 332 of file kmessageclient.cpp.
void KMessageClient::sendBroadcast | ( | const QByteArray & | msg | ) |
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.
- Parameters
-
msg The message to be sent to the clients
Definition at line 151 of file kmessageclient.cpp.
void KMessageClient::sendForward | ( | const QByteArray & | msg, |
const QList< quint32 > & | clients | ||
) |
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.
- Parameters
-
msg The message to be sent to the clients clients A list of clients the message should be sent to
Definition at line 163 of file kmessageclient.cpp.
void KMessageClient::sendForward | ( | const QByteArray & | msg, |
quint32 | client | ||
) |
Sends a message to a single client.
This is a convenieance method. It calls sendForward (const QByteArray &msg, const QValueList <quint32> &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.
- Parameters
-
msg The message to be sent to the client client The id of the client the message shall be sent to
Definition at line 175 of file kmessageclient.cpp.
void KMessageClient::sendServerMessage | ( | const QByteArray & | msg | ) |
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().
- Parameters
-
msg The message to be sent to the server. Must be in the format specified in KMessageServer.
Definition at line 141 of file kmessageclient.cpp.
|
signal |
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.
- Parameters
-
msg The message that has been sent to us unknown True when KMessageClient didn't recognize the message, i.e. it contained an unknown message ID. If you want to add additional message types to the client, connect to this signal, and if unknown is true, analyze the message by yourself. If you recognized the message, set unknown to false (Otherwise a debug message will be printed).
void KMessageClient::setServer | ( | const QString & | host, |
quint16 | port | ||
) |
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.
- Parameters
-
host The name of the host to connect to. Must be either a hostname which can be resolved to an IP or just an IP port The port to connect to
Definition at line 67 of file kmessageclient.cpp.
void KMessageClient::setServer | ( | KMessageServer * | server | ) |
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.
- Parameters
-
server The KMessageServer to connect to
Definition at line 72 of file kmessageclient.cpp.
|
virtual |
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 79 of file kmessageclient.cpp.
void KMessageClient::unlock | ( | ) |
Deliver every message that was delayed by lock() and actually deliver all messages that get received from now on.
Definition at line 361 of file kmessageclient.cpp.
The documentation for this class was generated from the following files:
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:18:54 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.