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
{
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 * );
};
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 * );
};
To copy to clipboard, switch view to plain text mode
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;
};
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;
};
To copy to clipboard, switch view to plain text mode
server class
{
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();
}
};
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();
}
};
To copy to clipboard, switch view to plain text mode
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
Bookmarks