PDA

View Full Version : QThread vs QTcpServer feat. QTcpSocket



ilyagoo
13th December 2009, 11:07
hi there! this is my first post:) let's start!

i've read almost all posts about QThread, QTcpServer & QTcpSocket, but still have some problems.
i wanna advice in the subject & all around:)

the main idea is to write multithreaded app using QTcpServer & QTcpSocket.

here's little bit of code:

client class


class TClient : public QTcpSocket
{
public:
TClient()
{
connect( this, SIGNAL( connected() ), this, SLOT( connectedSlot() ) );
connect( this, SIGNAL( disconnected() ), this, SLOT( disconnectedSlot() ) );
}

private slots:
void connectedSlot() { emit connected( this ); }
void disconnectedSlot() { emit disconnected( this ); }

signals:
void connected( TClient * );
void disconnected( TClient * );
};


thread class


class TClientThread : public QThread
{
public:
TClientThread( TClient *client ) : m_Client( client ) {}
~TClientThread()
{
wait();
}

protected:
void run()
{
connect( m_Client, SIGNAL( destroyed() ), this, SLOT( quit() ) );
exec();
}

private:
TClient *m_Client;
};


server class


class TServer : public QTcpServer
{
protected:
void incomingConnection( int socketDescriptor )
{
TClient *client = new TClient;
// assuming no error
client->setSocketDescriptor( socketDescriptor );
connect( client, SIGNAL( connected( TClient * ) ), this, SLOT( clientConnected( TClient * ) ), Qt::QueuedConnection );
connect( client, SIGNAL( disconnected( TClient * ) ), this, SLOT( clientDisconnected( TClient * ) ), Qt::QueuedConnection );

TClientThread *thread = new TClientThread( client );
connect( thread, SIGNAL( finished() ), this, SLOT( killThread() ) );
client->moveToThread( thread );
thread->start();

emit clientConnected( client );
}

signals:
void clientConnected( TClient * );
void clientDisconnected( TClient * );

private slots:
void killThread()
{
delete sender();
}
};


i call client->deleteLater() in the slot connected to TServer::clientDisconnected( TClient * );

1) is the 13th line in server class good idea or not?

2) may be better to move thread creation & moving client to thread from incomingConnection() to the slot connected to TServer::clientDisconnected( TClient * ) signal?

3) i need to obtain data from clientThread from other thread. data would be represented by char *[1-16 kBytes]? client receives data uninterruptedly, another thread process this data. i don't want client to be able to know anything about extern queue or mutex...
do you know some design pattern to use in this case or with threads in general?

4) i designed client anisochronous because i wanna use it either in main or spawned thread. it has relationship with 2)

thanks for your attention:)

wysota
13th December 2009, 11:30
1) is the 13th line in server class good idea or not?
Bad idea. Never explicitely delete a sender of a signal inside slots connected to that signal. Use deleteLater() instead (it's a slot, by the way).


2) may be better to move thread creation & moving client to thread from incomingConnection() to the slot connected to TServer::clientDisconnected( TClient * ) signal?
The socket has to be created within the thread's run() method.


3) i need to obtain data from clientThread from other thread. data would be represented by char *[1-16 kBytes]? client receives data uninterruptedly, another thread process this data. i don't want client to be able to know anything about extern queue or mutex...
do you know some design pattern to use in this case or with threads in general?
Emit a signal containing a byte array with the data.

ilyagoo
13th December 2009, 11:55
Use deleteLater() instead (it's a slot, by the way).


connect( thread, SIGNAL( finished() ), thread, SLOT( deleteLater() ) );
thanks:)




The socket has to be created within the thread's run() method.


but how to use its functions? using signals? smth like this?



TClientThread::run()
{
TClient client;
client->setSocketDescriptor( socketDescriptor );
// use client's functions via signals
connect( this, SIGNAL( someThreadSignals ), &client, SLOT( someThreadSlots() ) );

exec();
}


could i have any problems in thread's destructor relating to client on the stack?


Emit a signal containing a byte array with the data

to much copies of data: when i emit signal from one thread to another Qt produces QEvent with the copy of QByteArray. i receives 16kBytes about 100 times per second, its too hard to copy all of these arrays.

wysota
13th December 2009, 12:13
QEvent with the copy of QByteArray. i receives 16kBytes about 100 times per second, its too hard to copy all of these arrays.

Copy of a byte array is not the same as copy of the data it carries. QByteArray is an implicitly shared class thus there is only one copy of the data in question.

ilyagoo
13th December 2009, 13:11
i.e. using of QByteArray instead of queues of char* wouldn't decrease app's perfomance?

and would following code be correct to your mind?



void TClient::readyReadSlot()
{
if ( !m_HeaderProcessed )
{
read( reinterpret_cast< char * >( &m_PackageHeader ), sizeof( m_PackageHeader ) );
m_PackageHeaderProcessed = true;
}

int bytesToRead = bytesAvailable();

if ( bytesToRead >= m_PackageHeader.MessageSize )
{
QByteArray message = read( m_PackageHeader.MessageSize );
m_PackageHeaderProcessed = false;

emit dataReceived( message );
}
else
// read remaining data
emit readyRead();

}



and returning to:



TClientThread::run()
{
TClient client;
// little mistake))
client.setSocketDescriptor( socketDescriptor );
// use client's functions via signals
connect( this, SIGNAL( someThreadSignals ), &client, SLOT( someThreadSlots() ) );

exec();
}


i introduced signal TServer::clientDisconnected( TClient * ) because i need some information about disconnected client, for example, peerAddress & peerPort. this suggests me to use following code:



TClientThread::run()
{
m_Client = new TClient;
m_Client->setSocketDescriptor( socketDescriptor );
// use client's functions via signals
connect( this, SIGNAL( someThreadSignals ), m_Client, SLOT( someThreadSlots() ) );

exec();
}


but how can i finish thread correct now?

wysota
13th December 2009, 16:03
i.e. using of QByteArray instead of queues of char* wouldn't decrease app's perfomance?
No. At least not in any noticable way.


and would following code be correct to your mind?
The first "if" block is very C-ish. IMO you should read the header into a byte array. The rest seems fine.


but how can i finish thread correct now?

I don't see your point...

By the way, are you sure you need threads at all? So far I see no reason to use them in your situation.

ilyagoo
14th December 2009, 07:46
returning to my phrase:


i introduced signal TServer::clientDisconnected( TClient * ) because i need some information about disconnected client, for example, peerAddress & peerPort.

would it be correct to call client->peerPort() in the slot connected to aforesaid signal if this call and client are in different threads?