ttsthreads.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "ttsthreads.h"
  2. ttsThreads::ttsThreads(QObject *parent, quint16 idx, QString ip, QString path, quint16 port) :
  3. QThread(parent)
  4. {
  5. Idx = idx;
  6. DestIp = ip;
  7. DestPath = path;
  8. DestPort = port;
  9. DataList.clear();
  10. }
  11. void ttsThreads::appendData(QString data)
  12. {
  13. QString Data = QString("POST %1 HTTP/1.1\r\nContent-Type: application/x-www-form-urlencoded\r\nAccept: */*\r\nConnection: keep-alive\r\nCache-Control: no-cache\r\nContent-Length: %2\r\nAccept-Encoding: gzip\r\nAccept-Language: zh-CN,en,*\r\nUser-Agent: Mozilla/5.0\r\nHost: %3:%4\r\n\r\n%5")
  14. .arg(DestPath).arg(data.length()).arg(DestIp).arg(DestPort).arg(data);
  15. DataList.append(Data);
  16. }
  17. void ttsThreads::stoptts()
  18. {
  19. keepWorking = false;
  20. }
  21. void ttsThreads::run()
  22. {
  23. quint8 cur;
  24. for(int i=0;i<16;i++){
  25. so[i] = new QTcpSocket;
  26. }
  27. emit ttslog(Idx,QDateTime::currentDateTime(),QString("start"),true);
  28. keepWorking = true;
  29. cur=0;
  30. while(keepWorking){
  31. if(DataList.length()>0){
  32. QString data = DataList.first();
  33. so[cur]->connectToHost(QHostAddress(DestIp),DestPort,QIODevice::ReadWrite);
  34. if(so[cur]->waitForConnected()){
  35. so[cur]->write(data.toUtf8());
  36. so[cur]->flush();
  37. emit ttslog(Idx,QDateTime::currentDateTime(),data,true);
  38. DataList.removeFirst();
  39. if(so[cur]->waitForReadyRead()){
  40. QByteArray ret = so[cur]->readAll();
  41. if(ret.length()>512)
  42. ret = ret.mid(0,512);
  43. emit ttslog(Idx,QDateTime::currentDateTime(),QString(ret),false);
  44. so[cur]->close();
  45. }else{
  46. emit ttslog(Idx,QDateTime::currentDateTime(),QString("timeout"),false);
  47. so[cur]->abort();
  48. }
  49. }
  50. cur++;
  51. cur &= 0x0f;
  52. }
  53. usleep(100000);
  54. }
  55. for(int i=0;i<16;i++){
  56. if(so[i]->isOpen())
  57. so[i]->abort();
  58. so[i]->deleteLater();
  59. }
  60. }