16,214
社区成员
发帖
与我相关
我的任务
分享
TcpServer::TcpServer(QObject *parent) : QTcpServer(parent)
{
}
void TcpServer::incomingConnection(qintptr socketDescriptor)
{
TcpSocketThread* thread = new TcpSocketThread(socketDescriptor, this);
// connect(this, SIGNAL(broadcasted(QByteArray)), thread, SLOT(socketWrite(QByteArray)));
ci.clientSocket = thread;
g_clientMap[ci.id] = ci;
thread->setId(ci.id);
thread->start();
}
TcpSocketThread::TcpSocketThread(qintptr socketDescriptor, QObject *parent)
: m_socketDescriptor(socketDescriptor), QThread(parent)
{
}
void TcpSocketThread::run()
{
qDebug("[%x][TcpSocketThread::run]", QThread::currentThread());
m_tcpSocket = new QTcpSocket();
connect(m_tcpSocket, SIGNAL(connected()), this, SLOT(socketConnected()), Qt::DirectConnection);
connect(m_tcpSocket, SIGNAL(aboutToClose()), this, SLOT(socketClosed()), Qt::DirectConnection);
connect(m_tcpSocket, SIGNAL(disconnected()), this, SLOT(socketDisconnected()), Qt::DirectConnection);
connect(m_tcpSocket, SIGNAL(disconnected()), this, SLOT(quit()), Qt::DirectConnection);
connect(m_tcpSocket, SIGNAL(readyRead()), this, SLOT(socketRead()), Qt::DirectConnection);
connect(this, SIGNAL(finished()), this, SLOT(threadFinished()), Qt::DirectConnection);
connect(this, SIGNAL(writeRequested(QByteArray)), m_tcpSocket, SLOT(writeData(QByteArray)));
connect(this, SIGNAL(finished()), m_tcpSocket, SLOT(deleteLater()));
connect(this, SIGNAL(finished()), this, SLOT(deleteLater()));
if (!m_tcpSocket->setSocketDescriptor(m_socketDescriptor))
{
return;
}
this->exec();
}