You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

crecievethread.cpp 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /***************************************************************************
  2. * Copyright (C) 2008 by Oliver Groß *
  3. * z.o.gross@gmx.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #include "crecievethread.h"
  21. #include <QTcpSocket>
  22. #include <QByteArray>
  23. #include <QDataStream>
  24. #include <QFile>
  25. #include <QDir>
  26. namespace qftrans {
  27. CRecieveThread::CRecieveThread(QString & destinationDir, QTcpSocket * socket, QObject * parent) :
  28. QThread(parent), m_Socket(socket), m_DestinationDir(destinationDir)
  29. {
  30. }
  31. CRecieveThread::~CRecieveThread() {
  32. }
  33. void CRecieveThread::setDestinationDir(QString & value) {
  34. m_DestinationDir = value;
  35. }
  36. // TODO removeDownload(...)
  37. void CRecieveThread::removeDownload(TransferData * /*data*/) {
  38. }
  39. void CRecieveThread::readData() {
  40. QDataStream in(m_Socket);
  41. in.setVersion(QDataStream::Qt_4_0);
  42. if (m_Socket->bytesAvailable() >= (qint64)(sizeof(TransferHeader))) {
  43. in.readRawData((char *)(&m_LastHeader), sizeof(TransferHeader));
  44. m_LastHeaderValid = (qstrcmp(m_LastHeader.descriptor, DESCRIPTOR_STRING) == 0);
  45. qDebug() << "got header" << m_LastHeader.type;
  46. if (m_LastHeaderValid)
  47. qDebug() << "valid";
  48. else
  49. qDebug() << "invalid";
  50. qDebug() << "id" << m_LastHeader.id;
  51. qDebug() << "length" << m_LastHeader.length;
  52. }
  53. else
  54. m_LastHeaderValid = false;
  55. TransferData * target = NULL;
  56. while (m_LastHeaderValid) {
  57. qDebug() << "available Bytes" << m_Socket->bytesAvailable();
  58. if (m_Socket->bytesAvailable() >= m_LastHeader.length) {
  59. //look for existing id
  60. if (m_LastHeader.type == HT_ACK) {
  61. qDebug() << "got HT_ACK .. requesting to start upload";
  62. emit uploadStartRequest(m_LastHeader.id);
  63. }
  64. else {
  65. if (m_DataDict.contains(m_LastHeader.id)) {
  66. if (m_LastHeader.type == HT_CLOSE) {
  67. //finished or canceled by peer
  68. qDebug() << "download closed py peer";
  69. emit downloadRemoved(m_DataDict.take(m_LastHeader.id));
  70. }
  71. else
  72. //assign target
  73. target = m_DataDict[m_LastHeader.id];
  74. }
  75. //id not in dict
  76. else if (m_LastHeader.type == HT_ID) {
  77. //new file announced
  78. qDebug() << "download announced";
  79. TransferData * newFile = new TransferData;
  80. m_DataDict.insert(m_LastHeader.id, newFile);
  81. newFile->transfered = 0;
  82. newFile->size = 0;
  83. newFile->localFile = false;
  84. newFile->status = TS_WAITING;
  85. newFile->fileDir = m_DestinationDir;
  86. }
  87. //check valid target and data available
  88. if (target && m_LastHeader.length) {
  89. switch (m_LastHeader.type) {
  90. case HT_ID:
  91. target->status = TS_WAITING;
  92. QFile::remove(target->fileDir + QDir::separator() + target->fileName);
  93. emit downloadUpdated(target);
  94. break;
  95. case HT_SIZE:
  96. qDebug() << "reading size..";
  97. in >> target->size;
  98. qDebug() << "got size:" << target->size;
  99. break;
  100. case HT_NAME:
  101. qDebug() << "reading filename..";
  102. in >> target->fileName;
  103. qDebug() << "got filename:" << target->fileName;
  104. qDebug() << "requesting to send HT_ACK";
  105. emit downloadAcknowledged(m_LastHeader.id);
  106. break;
  107. case HT_DATA: {
  108. // qDebug() << "got data" << target->transfered << '/' << target->size;
  109. if (target->status == TS_WAITING) {
  110. target->status = TS_TRANSFERING;
  111. emit downloadAdded(target);
  112. }
  113. char * buffer = new char[m_LastHeader.length];
  114. in.readRawData(buffer, m_LastHeader.length);
  115. // QFile file(target->fileDir + QDir::separator() + target->fileName);
  116. // file.open(QIODevice::WriteOnly | QIODevice::Append);
  117. // QDataStream out(&file);
  118. // out.writeRawData((const char *)(buffer), m_LastHeader.length);
  119. target->transfered += m_LastHeader.length;
  120. delete[] buffer;
  121. emit downloadUpdated(target);
  122. }
  123. break;
  124. default:
  125. in.skipRawData(m_LastHeader.length);
  126. break;
  127. }
  128. }
  129. }
  130. }
  131. if (m_Socket->bytesAvailable() >= (qint64)(sizeof(TransferHeader))) {
  132. in.readRawData((char *)(&m_LastHeader), sizeof(TransferHeader));
  133. m_LastHeaderValid = (qstrcmp(m_LastHeader.descriptor, DESCRIPTOR_STRING) == 0);
  134. qDebug() << "got header" << m_LastHeader.type;
  135. if (m_LastHeaderValid)
  136. qDebug() << "valid";
  137. else
  138. qDebug() << "invalid";
  139. qDebug() << "id" << m_LastHeader.id;
  140. qDebug() << "length" << m_LastHeader.length;
  141. }
  142. else
  143. m_LastHeaderValid = false;
  144. }
  145. }
  146. void CRecieveThread::run() {
  147. qDebug() << "start reciever thread";
  148. connect(m_Socket, SIGNAL(readyRead()), this, SLOT(readData()));
  149. exec();
  150. disconnect(m_Socket, SIGNAL(readyRead()), this, SLOT(readData()));
  151. }
  152. }