ttsthreads.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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::ttslog(quint16 idx, QDateTime t, QString data, bool dirflag)
  22. {
  23. if(dirflag)
  24. printf("[ %04d-%02d-%02d %02d:%02d:%02d >>> ] %s\n",
  25. t.date().year(),t.date().month(),t.date().day(),
  26. t.time().hour(),t.time().minute(),t.time().second(),
  27. data.toUtf8().data());
  28. else
  29. printf("[ %04d-%02d-%02d %02d:%02d:%02d <<< ] %s\n",
  30. t.date().year(),t.date().month(),t.date().day(),
  31. t.time().hour(),t.time().minute(),t.time().second(),
  32. data.toUtf8().data());
  33. }
  34. void ttsThreads::run()
  35. {
  36. quint8 cur;
  37. for(int i=0;i<16;i++){
  38. so[i] = new QTcpSocket;
  39. }
  40. keepWorking = true;
  41. cur=0;
  42. while(keepWorking){
  43. if(DataList.length()>0){
  44. QString data = DataList.first();
  45. so[cur]->connectToHost(QHostAddress(DestIp),DestPort,QIODevice::ReadWrite);
  46. if(so[cur]->waitForConnected()){
  47. so[cur]->write(data.toUtf8());
  48. so[cur]->flush();
  49. ttslog(Idx,QDateTime::currentDateTime(),data,true);
  50. DataList.removeFirst();
  51. if(so[cur]->waitForReadyRead()){
  52. QByteArray ret = so[cur]->readAll();
  53. if(ret.length()>512)
  54. ret = ret.mid(0,512);
  55. ttslog(Idx,QDateTime::currentDateTime(),QString(ret),false);
  56. so[cur]->close();
  57. }else{
  58. ttslog(Idx,QDateTime::currentDateTime(),QString("timeout"),false);
  59. so[cur]->abort();
  60. }
  61. }
  62. cur++;
  63. cur &= 0x0f;
  64. }
  65. usleep(100000);
  66. }
  67. for(int i=0;i<16;i++){
  68. if(so[i]->isOpen())
  69. so[i]->abort();
  70. so[i]->deleteLater();
  71. }
  72. }