#include "connection.h" #include #include #include #include #include #include #include using namespace std; int Connection::n = 0; Connection::Connection(qintptr handle_, int ID, QObject *parent) : QThread(parent) { handle = handle_; two = 0; id = ID; } void Connection::init() { socket = new QTcpSocket(this); mutex = new QMutex; socket->setSocketDescriptor(handle); connect(socket, SIGNAL(readyRead()), this, SLOT(recv())); connect(socket, SIGNAL(disconnected()), this, SLOT(logout())); qDebug() << "init " << id; } void Connection::recv() { /*QByteArray arr = socket->readAll(); QDataStream in(arr); QString st; in >> st; qDebug() << "receive " << st;*/ QMutexLocker locker(mutex); QDataStream info(socket); qint64 size, cur = 0; QString name; int ty; info >> ty >> size; qDebug() << "receive " << currentThreadId(); qDebug() << tr("get %1 %2").arg(name).arg(size); QString savePath = QString::number(n)+".txt"; n++; emit startReceive(savePath, id); QFile file(savePath); if (!file.open(QFile::WriteOnly)) { qDebug() << "can not open"; } while (cur < size) { socket->waitForReadyRead(150); QByteArray arr = socket->readAll(); cur += arr.size(); file.write(arr); emit refreshReceive(100.*cur/size, id); } file.close(); emit endReceive(savePath, id); std::ifstream IF(savePath.toStdString()); std::vector a; int x; while (!IF.eof()) { IF >> x; a.push_back(x); } double ans = 0; if (ty == 0) { ans = a[0]; for (int i=1; iwrite(arr); } void Connection::logout() { qDebug() << myName << " logout"; }