我对设计一个简单的 QLocalServer-QLocalSocket IPC 系统的问题感到困惑。
QLocalServer 等待新连接并将信号连接到正确的插槽。
void CommandProcessor::onNewConnection()
{
QLocalSocket* pLocal = _server->nextPendingConnection();
connect(pLocal,SIGNAL(disconnected()),this,SLOT(onSocketDisconnected()));
connect(pLocal,SIGNAL(readyRead()),this,SLOT(onSocketReadyRead()));
connect(pLocal,SIGNAL(error(QLocalSocket::LocalSocketError)),this, SLOT(onSocketError(QLocalSocket::LocalSocketError)));
qDebug("Socket connected. addr=%p", pLocal);
}
The readyRead slot implementation is:
void CommandProcessor::onSocketReadyRead()
{
QLocalSocket* pLocalSocket = (QLocalSocket *) sender();
qDebug("SocketReadyRead. addr=%p", pLocalSocket);
QDataStream in(pLocalSocket);
in.setVersion(QDataStream::Qt_5_2);
pLocalSocket->readAll();
qDebug("%s pLocalSocket->bytesAvailable() = %d", Q_FUNC_INFO, pLocalSocket->bytesAvailable());
}
这个 readAll 是有意完成的,以检查我如何按顺序获得两个 readyRead 信号(来自同一个插槽指针,我验证了这一点)。
客户端操作相当简单:
QByteArray data;
QDataStream out(&data, QIODevice::ReadWrite);
out.setVersion(QDataStream::Qt_5_2);
cmd.toDataStream(out);
// write blocksize at first field
out.device()->seek(0);
out << data.size() - sizeof(BLOCKSIZE_T);
qint64 bw = _socket->write(data);
_socket->write(data)调用会在服务器上触发重复的 readyRead(即使服务器端已通过 ReadAll 调用读取了所有数据)。
任何迹象表明我应该在哪里看?