• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • libtdegames
 

libtdegames

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

#include <kmessageclient.h>

Inherits TQObject.

Signals

void broadcastReceived (const TQByteArray &msg, TQ_UINT32 senderID)
 
void forwardReceived (const TQByteArray &msg, TQ_UINT32 senderID, const TQValueList< TQ_UINT32 > &receivers)
 
void connectionBroken ()
 
void aboutToDisconnect (TQ_UINT32 id)
 
void adminStatusChanged (bool isAdmin)
 
void eventClientConnected (TQ_UINT32 clientID)
 
void eventClientDisconnected (TQ_UINT32 clientID, bool broken)
 
void serverMessageReceived (const TQByteArray &msg, bool &unknown)
 

Public Member Functions

 KMessageClient (TQObject *parent=0, const char *name=0)
 
 ~KMessageClient ()
 
TQ_UINT32 id () const
 
bool isAdmin () const
 
TQ_UINT32 adminId () const
 
const TQValueList< TQ_UINT32 > & clientList () const
 
void setServer (const TQString &host, TQ_UINT16 port)
 
void setServer (KMessageServer *server)
 
void disconnect ()
 
virtual void setServer (KMessageIO *connection)
 
bool isConnected () const
 
bool isNetwork () const
 
TQ_UINT16 peerPort () const
 
TQString peerName () const
 
void sendServerMessage (const TQByteArray &msg)
 
void sendBroadcast (const TQByteArray &msg)
 
void sendForward (const TQByteArray &msg, const TQValueList< TQ_UINT32 > &clients)
 
void sendForward (const TQByteArray &msg, TQ_UINT32 client)
 
void lock ()
 
void unlock ()
 
unsigned int delayedMessageCount () const
 

Protected Slots

virtual void processIncomingMessage (const TQByteArray &msg)
 
void processFirstMessage ()
 
virtual void removeBrokenConnection ()
 
void removeBrokenConnection2 ()
 

Protected Member Functions

virtual void processMessage (const TQByteArray &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 Burkh.nosp@m.ard..nosp@m.Lehne.nosp@m.r@gm.nosp@m.x.de

Definition at line 50 of file kmessageclient.h.

Constructor & Destructor Documentation

◆ KMessageClient()

KMessageClient::KMessageClient ( TQObject *  parent = 0,
const char *  name = 0 
)

Constructor.

Creates an unconnected KMessageClient object. Use setServer() later to connect to a KMessageServer object.

Definition at line 51 of file kmessageclient.cpp.

◆ ~KMessageClient()

KMessageClient::~KMessageClient ( )

Destructor.

Disconnects from the server, if any connection was established.

Definition at line 58 of file kmessageclient.cpp.

Member Function Documentation

◆ aboutToDisconnect

void KMessageClient::aboutToDisconnect ( TQ_UINT32  id)
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.

◆ adminId()

TQ_UINT32 KMessageClient::adminId ( ) const
Returns
The ID of the admin client on the message server.

Definition at line 108 of file kmessageclient.cpp.

◆ adminStatusChanged

void KMessageClient::adminStatusChanged ( bool  isAdmin)
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
isAdminWhether we are now admin or not

◆ broadcastReceived

void KMessageClient::broadcastReceived ( const TQByteArray &  msg,
TQ_UINT32  senderID 
)
signal

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:

void myObject::myBroadcastSlot (const TQByteArray &msg, TQ_UINT32 senderID)
{
if (senderID == ((KMessageClient *)sender())->id())
return;
...
}
KMessageClient
A client to connect to a KMessageServer.
Definition: kmessageclient.h:51
Parameters
msgThe message that has been sent to us
senderIDThe ID of the client which sent the message

◆ clientList()

const TQValueList< TQ_UINT32 > & KMessageClient::clientList ( ) const
Returns
The list of the IDs of all the message clients connected to the message server.

Definition at line 113 of file kmessageclient.cpp.

◆ connectionBroken

void KMessageClient::connectionBroken ( )
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.

◆ delayedMessageCount()

unsigned int KMessageClient::delayedMessageCount ( ) const
Returns
The number of messages that got delayed since lock() was called

Definition at line 368 of file kmessageclient.cpp.

◆ disconnect()

void KMessageClient::disconnect ( )

Corresponds to setServer(0); but also emits the connectionBroken signal.

Definition at line 342 of file kmessageclient.cpp.

◆ eventClientConnected

void KMessageClient::eventClientConnected ( TQ_UINT32  clientID)
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
clientIDThe ID of the client that has newly connectd.

◆ eventClientDisconnected

void KMessageClient::eventClientDisconnected ( TQ_UINT32  clientID,
bool  broken 
)
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
clientIDThe ID of the client that has disconnected
brokentrue if it was disconnected because of a network error

◆ forwardReceived

void KMessageClient::forwardReceived ( const TQByteArray &  msg,
TQ_UINT32  senderID,
const TQValueList< TQ_UINT32 > &  receivers 
)
signal

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 TQt signal to a TQt signal, and the second one can have less parameters.)

KMessageClient *client = new KMessageClient ();
connect (client, TQ_SIGNAL (forwardReceived (const TQByteArray &, TQ_UINT32, const TQValueList <TQ_UINT32>&)),
client, TQ_SIGNAL (broadcastReceived (const TQByteArray &, TQ_UINT32)));
KMessageClient::forwardReceived
void forwardReceived(const TQByteArray &msg, TQ_UINT32 senderID, const TQValueList< TQ_UINT32 > &receivers)
This signal is emitted when the client receives a forward message from the KMessageServer,...
KMessageClient::broadcastReceived
void broadcastReceived(const TQByteArray &msg, TQ_UINT32 senderID)
This signal is emitted when the client receives a broadcast message from the KMessageServer,...
KMessageClient::KMessageClient
KMessageClient(TQObject *parent=0, const char *name=0)
Constructor.
Definition: kmessageclient.cpp:51

Then connect the broadcast signal to your slot that analyzes the message.

Parameters
msgThe message that has been sent to us
senderIDThe ID of the client which sent the message
receiversAll clients which receive this message

◆ id()

TQ_UINT32 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 98 of file kmessageclient.cpp.

◆ isAdmin()

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 103 of file kmessageclient.cpp.

◆ isConnected()

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 118 of file kmessageclient.cpp.

◆ isNetwork()

bool KMessageClient::isNetwork ( ) const
Returns
true if isConnected() is true AND this is not a local (like KMessageDirect) connection.

Definition at line 123 of file kmessageclient.cpp.

◆ lock()

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 354 of file kmessageclient.cpp.

◆ peerName()

TQString KMessageClient::peerName ( ) const
Since
3.2
Returns
"localhost" if isConnected() is false, otherwise the hostname this client is connected to. See also KMessageIO::peerName() and TQSocket::peerName().

Definition at line 133 of file kmessageclient.cpp.

◆ peerPort()

TQ_UINT16 KMessageClient::peerPort ( ) const
Returns
0 if isConnected() is false, otherwise the port number this client is connected to. See also KMessageIO::peerPort and TQSocket::peerPort.
Since
3.2

Definition at line 128 of file kmessageclient.cpp.

◆ processFirstMessage

void KMessageClient::processFirstMessage ( )
protectedslot

Called from unlock() (using TQTimer::singleShot) until all delayed messages are delivered.

Definition at line 305 of file kmessageclient.cpp.

◆ processIncomingMessage

void KMessageClient::processIncomingMessage ( const TQByteArray &  msg)
protectedvirtualslot

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

Parameters
msgThe incoming message

Definition at line 182 of file kmessageclient.cpp.

◆ processMessage()

void KMessageClient::processMessage ( const TQByteArray &  msg)
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 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

Parameters
msgThe incoming message

Definition at line 202 of file kmessageclient.cpp.

◆ removeBrokenConnection

void KMessageClient::removeBrokenConnection ( )
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 321 of file kmessageclient.cpp.

◆ removeBrokenConnection2

void KMessageClient::removeBrokenConnection2 ( )
protectedslot

Definition at line 330 of file kmessageclient.cpp.

◆ sendBroadcast()

void KMessageClient::sendBroadcast ( const TQByteArray &  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
msgThe message to be sent to the clients

Definition at line 150 of file kmessageclient.cpp.

◆ sendForward() [1/2]

void KMessageClient::sendForward ( const TQByteArray &  msg,
const TQValueList< TQ_UINT32 > &  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
msgThe message to be sent to the clients
clientsA list of clients the message should be sent to

Definition at line 162 of file kmessageclient.cpp.

◆ sendForward() [2/2]

void KMessageClient::sendForward ( const TQByteArray &  msg,
TQ_UINT32  client 
)

Sends a message to a single client.

This is a convenieance method. It calls sendForward (const TQByteArray &msg, const TQValueList &ltTQ_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.

Parameters
msgThe message to be sent to the client
clientThe id of the client the message shall be sent to

Definition at line 174 of file kmessageclient.cpp.

◆ sendServerMessage()

void KMessageClient::sendServerMessage ( const TQByteArray &  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
msgThe message to be sent to the server. Must be in the format specified in KMessageServer.

Definition at line 140 of file kmessageclient.cpp.

◆ serverMessageReceived

void KMessageClient::serverMessageReceived ( const TQByteArray &  msg,
bool &  unknown 
)
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
msgThe message that has been sent to us
unknownTrue 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, analyse the message by yourself. If you recognized the message, set unknown to false (Otherwise a debug message will be printed).

◆ setServer() [1/3]

void KMessageClient::setServer ( const TQString &  host,
TQ_UINT16  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
hostThe name of the host to connect to. Must be either a hostname which can be resolved to an IP or just an IP
portThe port to connect to

Definition at line 66 of file kmessageclient.cpp.

◆ setServer() [2/3]

void KMessageClient::setServer ( KMessageIO *  connection)
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 78 of file kmessageclient.cpp.

◆ setServer() [3/3]

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
serverThe KMessageServer to connect to

Definition at line 71 of file kmessageclient.cpp.

◆ unlock()

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 359 of file kmessageclient.cpp.


The documentation for this class was generated from the following files:
  • kmessageclient.h
  • kmessageclient.cpp

libtdegames

Skip menu "libtdegames"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

libtdegames

Skip menu "libtdegames"
  • libtdegames
Generated for libtdegames by doxygen 1.9.4
This website is maintained by Timothy Pearson.