void Socket::dataReadyToRead()
{
/*
* Let us lock this slot so that further hitting on this cause wait while it is
* already hitted.
*/
/*
* We will only read data when at least one command is arrived. Here is the logic...
* We will continue till the length of the next command is arrived. If only length is
* arrived (not content yet), just read it in d->mnBlockSize (NOTE: LSB come first, i.e, little endian),
* leave this method. But if content arrived too, let us read it and process this command.
*/
while(bytesAvailable() >= (int)sizeof(quint32)){
if (0 == mnBlockSize){
/*
* Read the length bytes first. And as LSB come first, need to calc length like following.
*/
char length_byte[4];
read(length_byte,4);
mnBlockSize = length_byte[0];
mnBlockSize += (length_byte[1] * 256);
mnBlockSize += (length_byte[2] * 256 * 256);
mnBlockSize += (length_byte[3] * 256 * 256 * 256);
}
/*
* Content is not available yet, so there is nothing to do.
*/
if (bytesAvailable() < mnBlockSize){
return;
}
/*
* Content arrived, so get it, copy it to a buffer, analyze the packet by calling
* prepareCommand and them emit the prepared packet to the system so that
* contorller get it and process it.
*/
char dataArrived[MAX_DATA_LENGTH];
read(dataArrived,mnBlockSize);
if(mnBlockSize <= MAX_DATA_LENGTH){
emit commandCame(com);
}
mnBlockSize = 0;
}
}
void Socket::dataReadyToRead()
{
/*
* Let us lock this slot so that further hitting on this cause wait while it is
* already hitted.
*/
QMutexLocker locker(&mMutex);
/*
* We will only read data when at least one command is arrived. Here is the logic...
* We will continue till the length of the next command is arrived. If only length is
* arrived (not content yet), just read it in d->mnBlockSize (NOTE: LSB come first, i.e, little endian),
* leave this method. But if content arrived too, let us read it and process this command.
*/
while(bytesAvailable() >= (int)sizeof(quint32)){
if (0 == mnBlockSize){
/*
* Read the length bytes first. And as LSB come first, need to calc length like following.
*/
char length_byte[4];
read(length_byte,4);
mnBlockSize = length_byte[0];
mnBlockSize += (length_byte[1] * 256);
mnBlockSize += (length_byte[2] * 256 * 256);
mnBlockSize += (length_byte[3] * 256 * 256 * 256);
}
/*
* Content is not available yet, so there is nothing to do.
*/
if (bytesAvailable() < mnBlockSize){
return;
}
/*
* Content arrived, so get it, copy it to a buffer, analyze the packet by calling
* prepareCommand and them emit the prepared packet to the system so that
* contorller get it and process it.
*/
char dataArrived[MAX_DATA_LENGTH];
read(dataArrived,mnBlockSize);
if(mnBlockSize <= MAX_DATA_LENGTH){
QByteArray com(dataArrived,mnBlockSize);
emit commandCame(com);
}
mnBlockSize = 0;
}
}
To copy to clipboard, switch view to plain text mode
void SocketThread::run()
{
if(mTcpHandler){
Socket tcpClient;
connect(mTcpHandler,
SIGNAL(connectToBobHost
(QString,
int)),
&tcpClient,
SLOT(connectToBobHost
(QString,
int)));
connect(mTcpHandler, SIGNAL(disconnectFromBobHost()), &tcpClient, SLOT(disconnectFromBobHost()), Qt::BlockingQueuedConnection);
connect(&tcpClient, SIGNAL(hostConnected()), mTcpHandler, SLOT(hostConnected()));
connect(&tcpClient, SIGNAL(hostDisconnected(bool )), mTcpHandler, SLOT(hostDisconnected(bool)), Qt::BlockingQueuedConnection);
connect(&tcpClient, SIGNAL(hostNotConnected(const QString&)), mTcpHandler, SLOT(hostNotConnected(const QString&)));
connect(&tcpClient,
SIGNAL(commandCame
(QByteArray)), mTcpHandler,
SLOT(analyzeCommand
(QByteArray)));
exec();
}
}
void SocketThread::run()
{
if(mTcpHandler){
Socket tcpClient;
connect(mTcpHandler, SIGNAL(connectToBobHost(QString, int)), &tcpClient, SLOT(connectToBobHost(QString, int)));
connect(mTcpHandler, SIGNAL(disconnectFromBobHost()), &tcpClient, SLOT(disconnectFromBobHost()), Qt::BlockingQueuedConnection);
connect(mTcpHandler, SIGNAL(dataReadyToSend(QByteArray)), &tcpClient, SLOT(sendData(QByteArray)));
connect(&tcpClient, SIGNAL(hostConnected()), mTcpHandler, SLOT(hostConnected()));
connect(&tcpClient, SIGNAL(hostDisconnected(bool )), mTcpHandler, SLOT(hostDisconnected(bool)), Qt::BlockingQueuedConnection);
connect(&tcpClient, SIGNAL(hostNotConnected(const QString&)), mTcpHandler, SLOT(hostNotConnected(const QString&)));
connect(&tcpClient, SIGNAL(commandCame(QByteArray)), mTcpHandler, SLOT(analyzeCommand(QByteArray)));
exec();
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks