#include <kmessageserver.h>
Inherits TQObject.
Public Slots | |
void | addClient (KMessageIO *) |
void | removeClient (KMessageIO *io, bool broken) |
void | deleteClients () |
Signals | |
void | clientConnected (KMessageIO *client) |
void | connectionLost (KMessageIO *client) |
void | messageReceived (const TQByteArray &data, TQ_UINT32 clientID, bool &unknown) |
Public Member Functions | |
KMessageServer (TQ_UINT16 cookie=42, TQObject *parent=0) | |
virtual void | Debug () |
bool | initNetwork (TQ_UINT16 port=0) |
TQ_UINT16 | serverPort () const |
void | stopNetwork () |
bool | isOfferingConnections () const |
void | setMaxClients (int maxnumber) |
int | maxClients () const |
int | clientCount () const |
TQValueList< TQ_UINT32 > | clientIDs () const |
KMessageIO * | findClient (TQ_UINT32 no) const |
TQ_UINT32 | adminID () const |
void | setAdmin (TQ_UINT32 adminID) |
virtual void | broadcastMessage (const TQByteArray &msg) |
virtual void | sendMessage (TQ_UINT32 id, const TQByteArray &msg) |
virtual void | sendMessage (const TQValueList< TQ_UINT32 > &ids, const TQByteArray &msg) |
Protected Slots | |
virtual void | getReceivedMessage (const TQByteArray &msg) |
virtual void | processOneMessage () |
Protected Member Functions | |
TQ_UINT32 | uniqueClientNumber () const |
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", analyses 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 TQDataStream, to be platform independant.
Setting up a KMessageServer can be done like this:
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 analyse 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, independant of its real clientID.
Here is a list of the messages the KMessageServer understands: << means, the value is inserted into the TQByteArray using TQDataStream. The messageIDs (REQ_BROADCAST, ...) are of type TQ_UINT32.
TQByteArray << static_cast<TQ_UINT32>( 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: TQByteArray << static_cast <TQ_UINT32>( MSG_BROADCAST ) << clientID << raw_data TQ_UINT32 clientID; // the ID of the client that sent the broadcast request
TQByteArray << static_cast<TQ_UINT32>( REQ_FORWARD ) << client_list << raw_data TQValueList <TQ_UINT32> client_list; // list of receivers
When the server receives this message, it sends the following message to the clients in client_list: TQByteArray << static_cast<TQ_UINT32>( MSG_FORWARD ) << senderID << client_list << raw_data TQ_UINT32 senderID; // the sender of the forward request TQValueList <TQ_UINT32> 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.
TQByteArray << static_cast<TQ_UINT32>( REQ_CLIENT_ID )
When the server receives this message, it sends the following message to the asking client: TQByteArray << static_cast<TQ_UINT32>( ANS_CLIENT_ID ) << clientID TQ_UINT32 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.
TQByteArray << static_cast<TQ_UINT32>( REQ_ADMIN_ID )
When the server receives this message, it sends the following message to the asking client: TQByteArray << ANS_ADMIN_ID << adminID TQ_UINT32 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).
TQByteArray << static_cast<TQ_UINT32>( REQ_ADMIN_CHANGE ) << new_admin TQ_UINT32 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.
TQByteArray << static_cast<TQ_UINT32>( REQ_REMOVE_CLIENT ) << client_list TQValueList <TQ_UINT32> 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.
TQByteArray << static_cast<TQ_UINT32>( REQ_MAX_NUM_CLIENTS ) << maximum_clients TQ_INT32 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)
TQByteArray << static_cast<TQ_UINT32>( 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: TQByteArray << static_cast<TQ_UINT32>( ANS_CLIENT_LIST ) << clientList TQValueList <TQ_UINT32> 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:
TQByteArray << static_cast<TQ_UINT32>( EVNT_CLIENT_CONNECTED ) << clientID; TQ_UINT32 clientID; // the ID of the new connected client TQByteArray << static_cast<TQ_UINT32>( EVNT_CLIENT_DISCONNECTED ) << clientID; TQ_UINT32 clientID; // the ID of the client that lost the connection TQ_UINT8 broken; // 1 if the network connection was closed, 0 if it was disconnected
on purpose
- Version
- $Id$
Definition at line 175 of file kmessageserver.h.
Member Enumeration Documentation
◆ anonymous enum
anonymous enum |
MessageIDs for messages from a client to the message server.
Definition at line 184 of file kmessageserver.h.
◆ anonymous enum
anonymous enum |
MessageIDs for messages from the message server to a client.
Definition at line 198 of file kmessageserver.h.
Constructor & Destructor Documentation
◆ KMessageServer()
KMessageServer::KMessageServer | ( | TQ_UINT16 | cookie = 42 , |
TQObject * | parent = 0 |
||
) |
Create a KGameNetwork object.
Definition at line 89 of file kmessageserver.cpp.
◆ ~KMessageServer()
KMessageServer::~KMessageServer | ( | ) |
Definition at line 106 of file kmessageserver.cpp.
Member Function Documentation
◆ addClient
|
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 170 of file kmessageserver.cpp.
◆ adminID()
TQ_UINT32 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 306 of file kmessageserver.cpp.
◆ broadcastMessage()
|
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 342 of file kmessageserver.cpp.
◆ clientConnected
|
signal |
A new client connected to the game.
- Parameters
-
client the client object that connected
◆ clientCount()
int KMessageServer::clientCount | ( | ) | const |
returns the current number of connected clients.
- Returns
- the number of clients
Definition at line 278 of file kmessageserver.cpp.
◆ clientIDs()
TQValueList< TQ_UINT32 > KMessageServer::clientIDs | ( | ) | const |
returns a list of the unique IDs of all clients.
Definition at line 283 of file kmessageserver.cpp.
◆ connectionLost
|
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
-
client the client which left the game
◆ Debug()
|
virtual |
Gives debug output of the game status.
Definition at line 503 of file kmessageserver.cpp.
◆ deleteClients
|
slot |
Deletes all connections to the clients.
Definition at line 247 of file kmessageserver.cpp.
◆ findClient()
KMessageIO * KMessageServer::findClient | ( | TQ_UINT32 | no | ) | const |
Find the KMessageIO object to the given client number.
- Parameters
-
no the 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 291 of file kmessageserver.cpp.
◆ getReceivedMessage
|
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 361 of file kmessageserver.cpp.
◆ initNetwork()
bool KMessageServer::initNetwork | ( | TQ_UINT16 | port = 0 | ) |
Starts the Communication server to listen for incoming TCP/IP connections.
- Parameters
-
port The 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 118 of file kmessageserver.cpp.
◆ isOfferingConnections()
bool KMessageServer::isOfferingConnections | ( | ) | const |
Are we still offer offering server connections?
- Returns
- true, if we are still listening to connections requests
Definition at line 163 of file kmessageserver.cpp.
◆ maxClients()
int KMessageServer::maxClients | ( | ) | const |
returns the maximum number of clients
- Returns
- the number of clients
Definition at line 273 of file kmessageserver.cpp.
◆ messageReceived
|
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 analyse 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
-
data the message data clientID the ID of the KMessageIO object that received the message unknown true, if the message type is not known by the 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 analyses 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 382 of file kmessageserver.cpp.
◆ removeClient
|
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
-
io the object to delete and to remove from the client list broken true if the client has lost connection Mostly used internally. You will probably not need this.
Definition at line 223 of file kmessageserver.cpp.
◆ sendMessage() [1/2]
|
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 355 of file kmessageserver.cpp.
◆ sendMessage() [2/2]
|
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 348 of file kmessageserver.cpp.
◆ serverPort()
TQ_UINT16 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 146 of file kmessageserver.cpp.
◆ setAdmin()
void KMessageServer::setAdmin | ( | TQ_UINT32 | 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 311 of file kmessageserver.cpp.
◆ setMaxClients()
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
-
maxnumber the number of clients
Definition at line 268 of file kmessageserver.cpp.
◆ stopNetwork()
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 154 of file kmessageserver.cpp.
◆ uniqueClientNumber()
|
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 335 of file kmessageserver.cpp.
The documentation for this class was generated from the following files: