void Server::incomingConnection(int id) { QThread* thread = new QThread(); ServerSocket* socket = new ServerSocket(id); socket->moveToThread(thread); thread->start(); connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); connect(socket, SIGNAL(logGenerated(QString)), this, SLOT(on_socket_logGenerated(QString))); connect(socket, SIGNAL(imageRead(QByteArray)), this, SLOT(on_socket_imageRead(QByteArray))); connect(this, SIGNAL(serverTerminated()), socket, SLOT(disconnectFromHostImplementation())); connect(this, SIGNAL(serverTerminated()), socket, SLOT(abortConnection())); connect(socket, SIGNAL(disconnected()), thread, SLOT(quit())); //Seems risky }
void ServerSocket::on_readyRead() { qDebug() << "Reading"; QDataStream stream(this); stream.setVersion(QDataStream::Qt_4_7); while (!stream.atEnd()) { int flag; stream >> flag; if (flag == LOGIN) { QString pd; waitNextMessage(10000); stream>>username; waitNextMessage(10000); stream>>pd; qDebug() << username + " is logining."; emit logGenerated(username + " is logining."); if (!ServerDBInterface::login(username, pd)) { stream << FAILED; abort(); }else stream << SUCCEEDED; } if (flag == IMAGE) { int size; waitNextMessage(20000); stream >> size; qDebug() << "Someone is uploading image, size is " << size; QByteArray bytes; QBuffer buf(&bytes); buf.open(QIODevice::WriteOnly); while (bytes.length() < size) { waitNextMessage(20000); buf.write(this->read(size - bytes.length())); } //stream << PROGRESS << size; emit imageRead(bytes); emit logGenerated("Received an image from " + username + QString(", image size is %1 bytes.").arg(size)); }
static int ipdbRead(IPDB * const pdbP, FILE * const fileP, bool const verbose) { int retval; ipdb_clear(pdbP); pdbP->p = ipdb_pdbhead_alloc(NULL); if (pdbP->p == NULL) retval = ENOMEM; else { int status; status = pdbheadRead(pdbP->p, fileP); if (status != 0) retval = status; else { pdbP->i = ipdb_image_alloc(pdbP->p->name, IMG_GRAY, 0, 0); if (pdbP->i == NULL) retval = ENOMEM; else { int status; status = rechdrRead(pdbP->i->r, fileP); if (status != 0) retval = status; else { if (pdbP->p->num_recs > 1) { pdbP->t = ipdb_text_alloc(NULL); if (pdbP->t == NULL) retval = ENOMEM; else { int status; status = rechdrRead(pdbP->t->r, fileP); if (status != 0) retval = status; else retval = 0; } } else retval = 0; if (retval == 0) { uint32_t const offset = pdbP->t == NULL ? UNKNOWN_OFFSET : pdbP->t->r->offset - 1; int status; status = imageRead(pdbP->i, offset, fileP, verbose); if (status != 0) retval = status; else { if (pdbP->t != NULL) { int status; status = textRead(pdbP->t, fileP); if (status != 0) retval = status; } } } } } } } return retval; }
void Server::on_socket_imageRead(const QByteArray& bytes) { emit imageRead(bytes); }