【发布时间】:2013-12-17 18:11:24
【问题描述】:
我在典型场景中遇到了一个奇怪的问题:QTcpServer 的方法incomingConnection 在自定义类中被覆盖,并且任何接收到的连接都计划在 QThreadPool 上的单独线程中处理。
服务器:
void FooS::incomingConnection(qintptr socketDescriptor)
{
QThreadPool *thread_pool = QThreadPool::globalInstance();
FooSocket *fs = new FooSocket();
fs->setSocket(socketDescriptor);
thread_pool->start(fs);
}
任务:
class FooSocket: public QObject, public QRunnable;
...
private slots:
void connectionIncomingData();
...
void FooSocket::run() {
QTcpSocket *socket = new QTcpSocket();
qDebug() << "SD: " << socketDescriptor; // is correct
if (!socket->setSocketDescriptor(socketDescriptor)) {
qDebug() << "Can't set socket descriptor";
emit error(socket->error());
return;
}
// -- had no effect here
// socket->moveToThread(QThread::currentThread());
connect(socket, SIGNAL(readyRead()), this, SLOT(connectionIncomingData()));
connect(socket, SIGNAL(disconnected()), this, SLOT(connectionClosed()));
}
readyRead 信号没有被触发,但是套接字客户端被确认(tcpdump)发送数据..
在使 QRunnable 生成一个内部带有套接字逻辑的 QThread 对象,并玩弄 setAutoDelete、moveToThread 之后 - 仍然没有效果。
【问题讨论】: