• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdegames API Reference
  • KDE Home
  • Contact Us
 

libkdegames/libkdegamesprivate/kgame

Public Types | Public Slots | Signals | Public Member Functions | Protected Slots | Protected Member Functions | List of all members
KMessageServer Class Reference

#include <KGame/KMessageServer>

Inheritance diagram for KMessageServer:
Inheritance graph
[legend]

Public Types

enum  {
  REQ_BROADCAST = 1, REQ_FORWARD, REQ_CLIENT_ID, REQ_ADMIN_ID,
  REQ_ADMIN_CHANGE, REQ_REMOVE_CLIENT, REQ_MAX_NUM_CLIENTS, REQ_CLIENT_LIST,
  REQ_MAX_REQ = 0xffff
}
 
enum  {
  MSG_BROADCAST = 101, MSG_FORWARD, ANS_CLIENT_ID, ANS_ADMIN_ID,
  ANS_CLIENT_LIST, EVNT_CLIENT_CONNECTED, EVNT_CLIENT_DISCONNECTED, EVNT_MAX_EVNT = 0xffff
}
 

Public Slots

void addClient (KMessageIO *)
 
void deleteClients ()
 
void removeClient (KMessageIO *io, bool broken)
 

Signals

void clientConnected (KMessageIO *client)
 
void connectionLost (KMessageIO *client)
 
void messageReceived (const QByteArray &data, quint32 clientID, bool &unknown)
 

Public Member Functions

 KMessageServer (quint16 cookie=42, QObject *parent=0)
 
 ~KMessageServer ()
 
quint32 adminID () const
 
virtual void broadcastMessage (const QByteArray &msg)
 
int clientCount () const
 
QList< quint32 > clientIDs () const
 
virtual void Debug ()
 
KMessageIO * findClient (quint32 no) const
 
bool initNetwork (quint16 port=0)
 
bool isOfferingConnections () const
 
int maxClients () const
 
virtual void sendMessage (quint32 id, const QByteArray &msg)
 
virtual void sendMessage (const QList< quint32 > &ids, const QByteArray &msg)
 
quint16 serverPort () const
 
void setAdmin (quint32 adminID)
 
void setMaxClients (int maxnumber)
 
void stopNetwork ()
 
- 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 &regExp) 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

virtual void getReceivedMessage (const QByteArray &msg)
 
virtual void processOneMessage ()
 

Protected Member Functions

quint32 uniqueClientNumber () const
 
- 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 server for message sending and broadcasting, using TCP/IP connections.

An object of this class listens for incoming connections via TCP/IP sockets and creates KMessageSocket objects for every established connection. It receives messages from the "clients", analyzes them and processes an appropriate reaction.

You can also use other KMessageIO objects with KMessageServer, not only TCP/IP socket based ones. Use addClient to connect via an object of any KMessageIO subclass. (For clients within the same process, you can e.g. use KMessageDirect.) This object already has to be connected.

The messages are always packages of an arbitrary length. The format of the messages is given below. All the data is stored and received with QDataStream, to be platform independent.

Setting up a KMessageServer can be done like this:

KMessageServer *server = new KMessageServer ();
server->initNetwork (TCP/IP-Portnumber);

Usually that is everything you will do. There are a lot of public methods to administrate the object (maximum number of clients, finding clients, removing clients, setting the admin client, ...), but this functionality can also be done by messages from the clients. So you can administrate the object completely on remote.

If you want to extend the Server for your own needs (e.g. additional message types), you can either create a subclass and overwrite the method processOneMessage. (But don't forget to call the method of the superclass!) Or you can connect to the signal messageReceived, and analyze the messages there.

Every client has a unique ID, so that messages can be sent to another dedicated client or a list of clients.

One of the clients (the admin) has a special administration right. Some of the administration messages can only be used with him. The admin can give the admin status to another client. You can send a message to the admin by using clientID 0. This is always interpreted as the admin client, of its real clientID.

Here is a list of the messages the KMessageServer understands: << means, the value is inserted into the QByteArray using QDataStream. The messageIDs (REQ_BROADCAST, ...) are of type quint32.

  • QByteArray << static_cast<quint32>( REQ_BROADCAST ) << raw_data

    When the server receives this message, it sends the following message to ALL connected clients (a broadcast), where the raw_data is left unchanged: QByteArray << static_cast <quint32>( MSG_BROADCAST ) << clientID << raw_data quint32 clientID; // the ID of the client that sent the broadcast request

  • QByteArray << static_cast<quint32>( REQ_FORWARD ) << client_list << raw_data QValueList <quint32> client_list; // list of receivers

    When the server receives this message, it sends the following message to the clients in client_list: QByteArray << static_cast<quint32>( MSG_FORWARD ) << senderID << client_list << raw_data quint32 senderID; // the sender of the forward request QValueList <quint32> client_list; // a copy of the receiver list

    Note: Every client receives the message as many times as he is in the client_list. Note: Since the client_list is sent to all the clients, every client can see who else got the message. If you want to prevent this, send a single REQ_FORWARD message for every receiver.

  • QByteArray << static_cast<quint32>( REQ_CLIENT_ID )

    When the server receives this message, it sends the following message to the asking client: QByteArray << static_cast<quint32>( ANS_CLIENT_ID ) << clientID quint32 clientID; // The ID of the client who asked for it

    Note: This answer is also automatically sent to a new connected client, so that he can store his ID. The ID of a client doesn't change during his lifetime, and is unique for this KMessageServer.

  • QByteArray << static_cast<quint32>( REQ_ADMIN_ID )

    When the server receives this message, it sends the following message to the asking client: QByteArray << ANS_ADMIN_ID << adminID quint32 adminID; // The ID of the admin

    Note: This answer is also automatically sent to a new connected client, so that he can see if he is the admin or not. It will also be sent to all connected clients when a new admin is set (see REQ_ADMIN_CHANGE).

  • QByteArray << static_cast<quint32>( REQ_ADMIN_CHANGE ) << new_admin quint32 new_admin; // the ID of the new admin, or 0 for no admin

    When the server receives this message, it sets the admin to the new ID. If no client with that ID exists, nothing happens. With new_admin == 0 no client is a admin. ONLY THE ADMIN ITSELF CAN USE THIS MESSAGE!

    Note: The server sends a ANS_ADMIN_ID message to every connected client.

  • QByteArray << static_cast<quint32>( REQ_REMOVE_CLIENT ) << client_list QValueList <quint32> client_list; // The list of clients to be removed

    When the server receives this message, it removes the clients with the ids stored in client_list, disconnecting the connection to them. ONLY THE ADMIN CAN USE THIS MESSAGE!

    Note: If one of the clients is the admin himself, he will also be deleted. Another client (if any left) will become the new admin.

  • QByteArray << static_cast<quint32>( REQ_MAX_NUM_CLIENTS ) << maximum_clients qint32 maximum_clients; // The maximum of clients connected, or infinite if -1

    When the server receives this message, it limits the number of clients to the number given, or sets it unlimited for maximum_clients == -1. ONLY THE ADMIN CAN USE THIS MESSAGE!

    Note: If there are already more clients, they are not affected. It only prevents new Clients to be added. To assure this limit, remove clients afterwards (REQ_REMOVE_CLIENT)

  • QByteArray << static_cast<quint32>( REQ_CLIENT_LIST )

    When the server receives this message, it answers by sending a list of IDs of all the clients that are connected at the moment. So it sends the following message to the asking client: QByteArray << static_cast<quint32>( ANS_CLIENT_LIST ) << clientList QValueList <quint32> clientList; // The IDs of the connected clients

    Note: This message is also sent to every new connected client, so that he knows the other clients.

There are two more messages that are sent from the server to the every client automatically when a new client connects or a connection to a client is lost:

  QByteArray << static_cast&lt;quint32>( EVNT_CLIENT_CONNECTED ) << clientID;
  quint32 clientID;   // the ID of the new connected client

  QByteArray << static_cast&lt;quint32>( EVNT_CLIENT_DISCONNECTED ) << clientID;
  quint32 clientID;   // the ID of the client that lost the connection
  quint8 broken;      // 1 if the network connection was closed, 0 if it was disconnected

on purpose

Author
Andreas Beckermann b_man.nosp@m.n@gm.nosp@m.x.de, Burkhard Lehner Burkh.nosp@m.ard..nosp@m.Lehne.nosp@m.r@gm.nosp@m.x.de

Definition at line 175 of file kmessageserver.h.

Member Enumeration Documentation

anonymous enum

MessageIDs for messages from a client to the message server.

Enumerator
REQ_BROADCAST 
REQ_FORWARD 
REQ_CLIENT_ID 
REQ_ADMIN_ID 
REQ_ADMIN_CHANGE 
REQ_REMOVE_CLIENT 
REQ_MAX_NUM_CLIENTS 
REQ_CLIENT_LIST 
REQ_MAX_REQ 

Definition at line 183 of file kmessageserver.h.

anonymous enum

MessageIDs for messages from the message server to a client.

Enumerator
MSG_BROADCAST 
MSG_FORWARD 
ANS_CLIENT_ID 
ANS_ADMIN_ID 
ANS_CLIENT_LIST 
EVNT_CLIENT_CONNECTED 
EVNT_CLIENT_DISCONNECTED 
EVNT_MAX_EVNT 

Definition at line 197 of file kmessageserver.h.

Constructor & Destructor Documentation

KMessageServer::KMessageServer ( quint16  cookie = 42,
QObject *  parent = 0 
)
explicit

Create a KGameNetwork object.

Definition at line 99 of file kmessageserver.cpp.

KMessageServer::~KMessageServer ( )

Definition at line 115 of file kmessageserver.cpp.

Member Function Documentation

void KMessageServer::addClient ( KMessageIO *  client)
slot

Adds a new KMessageIO object to the communication server.

This "client" gets a unique ID.

This slot method is automatically called for any incoming TCP/IP connection. You can use it to add other types of connections, e.g. local connections (KMessageDirect) to the server manually.

NOTE: The KMessageIO object gets owned by the KMessageServer, so don't delete or manipulate it afterwards. It is automatically deleted when the connection is broken or the communication server is deleted. So, add a KMessageIO object to just ONE KMessageServer.

Definition at line 180 of file kmessageserver.cpp.

quint32 KMessageServer::adminID ( ) const

Returns the clientID of the admin, if there is a admin, 0 otherwise.

NOTE: Most often you don't need to know that id, since you can use clientID 0 to specify the admin.

Definition at line 316 of file kmessageserver.cpp.

void KMessageServer::broadcastMessage ( const QByteArray &  msg)
virtual

Sends a message to all connected clients.

The message is NOT translated in any way. This method calls KMessageIO::send for every client added.

Definition at line 352 of file kmessageserver.cpp.

void KMessageServer::clientConnected ( KMessageIO *  client)
signal

A new client connected to the game.

Parameters
clientthe client object that connected
int KMessageServer::clientCount ( ) const

returns the current number of connected clients.

Returns
the number of clients

Definition at line 288 of file kmessageserver.cpp.

QList< quint32 > KMessageServer::clientIDs ( ) const

returns a list of the unique IDs of all clients.

Definition at line 293 of file kmessageserver.cpp.

void KMessageServer::connectionLost ( KMessageIO *  client)
signal

A network connection got broken.

Note that the client will automatically get deleted after this signal is emitted. The signal is not emitted when the client was removed regularly.

Parameters
clientthe client which left the game
void KMessageServer::Debug ( )
virtual

Gives debug output of the game status.

Definition at line 517 of file kmessageserver.cpp.

void KMessageServer::deleteClients ( )
slot

Deletes all connections to the clients.

Definition at line 257 of file kmessageserver.cpp.

KMessageIO * KMessageServer::findClient ( quint32  no) const

Find the KMessageIO object to the given client number.

Parameters
nothe client number to look for, or 0 to look for the admin
Returns
address of the client, or 0 if no client with that number exists

Definition at line 301 of file kmessageserver.cpp.

void KMessageServer::getReceivedMessage ( const QByteArray &  msg)
protectedvirtualslot

This slot receives all the messages from the KMessageIO::received signals.

It stores the messages in a queue. The messages are later taken out of the queue by getReceivedMessage.

NOTE: It is important that this slot may only be called from the signal KMessageIO::received, since the sender() object is used to find out the client that sent the message!

Definition at line 371 of file kmessageserver.cpp.

bool KMessageServer::initNetwork ( quint16  port = 0)

Starts the Communication server to listen for incoming TCP/IP connections.

Parameters
portThe port on which the service is offered, or 0 to let the system pick a free port
Returns
true if it worked

Definition at line 127 of file kmessageserver.cpp.

bool KMessageServer::isOfferingConnections ( ) const

Are we still offer offering server connections?

Returns
true, if we are still listening to connections requests

Definition at line 173 of file kmessageserver.cpp.

int KMessageServer::maxClients ( ) const

returns the maximum number of clients

Returns
the number of clients

Definition at line 283 of file kmessageserver.cpp.

void KMessageServer::messageReceived ( const QByteArray &  data,
quint32  clientID,
bool &  unknown 
)
signal

This signal is always emitted when a message from a client is received.

You can use this signal to extend the communication server without subclassing. Just connect to this signal and analyze the message, if unknown is true. If you recognize a message and process it, set unknown to false, otherwise a warning message is printed.

Parameters
datathe message data
clientIDthe ID of the KMessageIO object that received the message
unknowntrue, if the message type is not known by the KMessageServer
void KMessageServer::processOneMessage ( )
protectedvirtualslot

This slot is called whenever there are elements in the message queue.

This queue is filled by getReceivedMessage. This slot takes one message out of the queue and analyzes processes it, if it recognizes it. (See message types in the description of the class.) After that, the signal messageReceived is emitted. Connect to that signal if you want to process other types of messages.

Definition at line 392 of file kmessageserver.cpp.

void KMessageServer::removeClient ( KMessageIO *  io,
bool  broken 
)
slot

Removes the KMessageIO object from the client list and deletes it.

This destroys the connection, if it already was up. Does NOT emit connectionLost. Sends an info message to the other clients, that contains the ID of the removed client and the value of the parameter broken.

Parameters
iothe object to delete and to remove from the client list
brokentrue if the client has lost connection Mostly used internally. You will probably not need this.

Definition at line 233 of file kmessageserver.cpp.

void KMessageServer::sendMessage ( quint32  id,
const QByteArray &  msg 
)
virtual

Sends a message to a single client with the given ID.

The message is NOT translated in any way. If no client with the given id exists, nothing is done. This is just a convenience method. You could also call findClient (id)->send(msg) manually, but this method checks for errors.

Definition at line 358 of file kmessageserver.cpp.

void KMessageServer::sendMessage ( const QList< quint32 > &  ids,
const QByteArray &  msg 
)
virtual

Sends a message to a list of clients.

Their ID is given in ids. If a client id is given more than once in the list, the message is also sent several times to that client. This is just a convenience method. You could also iterate over the list of IDs.

Definition at line 365 of file kmessageserver.cpp.

quint16 KMessageServer::serverPort ( ) const

Returns the TCP/IP port number we are listening to for incoming connections.

(This has to be known by other clients so that they can connect to us. It's especially necessary if you used 0 as port number in initNetwork().

Returns
the port number

Definition at line 156 of file kmessageserver.cpp.

void KMessageServer::setAdmin ( quint32  adminID)

Sets the admin to a new client with the given ID.

The old admin (if existed) and the new admin will get the ANS_ADMIN message. If you use 0 as new adminID, no client will be admin.

Definition at line 321 of file kmessageserver.cpp.

void KMessageServer::setMaxClients ( int  maxnumber)

sets the maximum number of clients which can connect.

If this number is reached, no more clients can be added. Setting this number to -1 means unlimited number of clients.

NOTE: Existing connections are not affected. So, clientCount > maxClients is possible, if there were already more clients than allowed before reducing this value.

Parameters
maxnumberthe number of clients

Definition at line 278 of file kmessageserver.cpp.

void KMessageServer::stopNetwork ( )

Stops listening for connections.

The already running connections are not affected. To listen for connections again call initNetwork again.

Definition at line 164 of file kmessageserver.cpp.

quint32 KMessageServer::uniqueClientNumber ( ) const
protected
Returns
A unique number which can be used as the id of a KMessageIO. It is incremented after every call so if you need the id twice you have to save it anywhere. It's currently used to initialize newly connected clints only.

Definition at line 345 of file kmessageserver.cpp.


The documentation for this class was generated from the following files:
  • kmessageserver.h
  • kmessageserver.cpp
This file is part of the KDE documentation.
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.

libkdegames/libkdegamesprivate/kgame

Skip menu "libkdegames/libkdegamesprivate/kgame"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdegames API Reference

Skip menu "kdegames API Reference"
  • granatier
  • kapman
  • kblackbox
  • kgoldrunner
  • kigo
  • kmahjongg
  • KShisen
  • ksquares
  • libkdegames
  •   highscore
  •   libkdegamesprivate
  •     kgame
  • libkmahjongg
  • palapeli
  •   libpala

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal