#include "clientthread.h"
#include <QByteArray>
#include <QDebug>
#include <QDataStream>
#include <QFile>
#include <QDebug>
ClientThread::ClientThread(int h,QMap<QString,char> mapFilePath,QObject *parent) :
QThread(parent),m_tcpSocket(parent)
{
this->m_mapFilePath = mapFilePath;
m_tcpSocket.setSocketDescriptor(h);
connect(&m_tcpSocket, SIGNAL(disconnected()), this, SLOT(on_socket_disconnected()));
connect(&m_tcpSocket,SIGNAL(connected()),this,SLOT(sendFile()));
connect(&m_tcpSocket, SIGNAL(readyRead()), this, SLOT(on_socket_readyRead()));
}
void ClientThread::sendFile()
{
qDebug()<<"Send Message to Client";
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
qint64 size = 0;
QMap<QString,char>::iterator it;
it = m_mapFilePath.begin();
while(it != m_mapFilePath.end())
{
QFile file(it.key());
if (!file.open(QIODevice::ReadOnly))
{
qDebug()<< "Unable to open the file" << endl;
this->m_tcpSocket.disconnectFromHost();
return;
}
QString fileName = file.fileName();
qDebug()<<"File Name is :"<<fileName;
fileName = fileName.remove(0, fileName.lastIndexOf('/')+1);
block.clear();
out.device()->seek(0);
out << qint16(0x0001) << fileName.toUtf8();
size = block.size();
this->m_tcpSocket.write((char*)&size, sizeof(qint64));
this->m_tcpSocket.write(block.data(), size);
qDebug()<<"Send File is :"<<fileName;
if(!this->m_tcpSocket.waitForBytesWritten(-1))
{
qDebug() << "writen Bytes error ";
}
DataPackage dataPackage;
dataPackage.dwPackageIdentifyNum2 = 0;
do
{
char szData[1024];
char* szDataPackage = new char[1024*8];
memset(szDataPackage,0,1024*8);
block.clear();
out.device()->seek(0);
dataPackage.dwPackageHead = 0xAAAAAAAA;
dataPackage.unPackageEdition = 0x0001;
dataPackage.unPackageIdentifyNum1 = 0x0001;
dataPackage.dwPackageIdentifyNum2++;
int i = 0;
for ( ; i < 6; i++)
{
memset(szData,0,1024);
if( 0 == (dataPackage.pData[i].unDataLen = file.read(szData,1024)))
{
break;
}
else
{
dataPackage.pData[i].chDataType = it.value();
memcpy(dataPackage.pData[i].data,szData,dataPackage.pData[i].unDataLen);
}
}
dataPackage.uchDataAmount = i;
dataPackage.unPackageLen = sizeof(DataPackage) - 4;
memcpy(szDataPackage,&dataPackage,sizeof(DataPackage)-4);
dataPackage.uchCheck = 0;
for (unsigned int j = 0; j < sizeof(DataPackage)-4; j++)
{
dataPackage.uchCheck ^= szDataPackage[j];
}
memset(szDataPackage,0,1024*8);
memcpy(szDataPackage,&dataPackage,sizeof(DataPackage));
QByteArray byte(szDataPackage, sizeof(DataPackage));
out << qint16(0x0002)<<byte;
size = block.size();
this->m_tcpSocket.write((char*)&size, sizeof(qint64));
this->m_tcpSocket.write(block.data(), size);
qDebug()<<"Send File Data to Client";
if(!this->m_tcpSocket.waitForBytesWritten(-1))
{
qDebug() << "writen Bytes error ";
}
delete []szDataPackage;
szDataPackage = NULL;
}while(!file.atEnd());
block.clear();
out.device()->seek(0);
out << qint16(0x0003)<<"FileSendEnd";
size = block.size();
this->m_tcpSocket.write((char*)&size, sizeof(qint64));
this->m_tcpSocket.write(block.data(), size);
qDebug()<<"Send File:"<<file.fileName()<<" End";
if(!this->m_tcpSocket.waitForBytesWritten(-1))
{
qDebug() << "writen Bytes error ";
}
file.close();
it++;
}
block.clear();
out.device()->seek(0);
out<<qint16(0x0004)<<"AllSendEnd";
size = block.size();
this->m_tcpSocket.write((char*)&size, sizeof(qint64));
this->m_tcpSocket.write(block.data(), size);
qDebug()<<"All File Send End";
if(!this->m_tcpSocket.waitForBytesWritten(-1))
{
qDebug() << "writen Bytes error ";
}
this->m_tcpSocket.disconnectFromHost();
}
void ClientThread::run()
{
qDebug() << "New connect from " << this->m_tcpSocket.peerAddress().toString() << ":" << this->m_tcpSocket.peerPort() << ".";
exec();
}
void ClientThread::on_socket_disconnected()
{
qDebug() << "socket disconnected.";
exit();
}
void ClientThread::on_socket_readyRead()
{
qDebug()<<"new Request from Client";
sendFile();
}
评论0
最新资源