12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- #include "ttsthreads.h"
- ttsThreads::ttsThreads(QObject *parent, quint16 idx, QString ip, QString path, quint16 port) :
- QThread(parent)
- {
- Idx = idx;
- DestIp = ip;
- DestPath = path;
- DestPort = port;
- DataList.clear();
- }
- void ttsThreads::appendData(QString data)
- {
- 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")
- .arg(DestPath).arg(data.length()).arg(DestIp).arg(DestPort).arg(data);
- DataList.append(Data);
- }
- void ttsThreads::stoptts()
- {
- keepWorking = false;
- }
- void ttsThreads::run()
- {
- quint8 cur;
- for(int i=0;i<16;i++){
- so[i] = new QTcpSocket;
- }
- emit ttslog(Idx,QDateTime::currentDateTime(),QString("start"),true);
- keepWorking = true;
- cur=0;
- while(keepWorking){
- if(DataList.length()>0){
- QString data = DataList.first();
- so[cur]->connectToHost(QHostAddress(DestIp),DestPort,QIODevice::ReadWrite);
- if(so[cur]->waitForConnected()){
- so[cur]->write(data.toUtf8());
- so[cur]->flush();
- emit ttslog(Idx,QDateTime::currentDateTime(),data,true);
- DataList.removeFirst();
- if(so[cur]->waitForReadyRead()){
- QByteArray ret = so[cur]->readAll();
- if(ret.length()>512)
- ret = ret.mid(0,512);
- emit ttslog(Idx,QDateTime::currentDateTime(),QString(ret),false);
- so[cur]->close();
- }else{
- emit ttslog(Idx,QDateTime::currentDateTime(),QString("timeout"),false);
- so[cur]->abort();
- }
- }
- cur++;
- cur &= 0x0f;
- }
- usleep(100000);
- }
- for(int i=0;i<16;i++){
- if(so[i]->isOpen())
- so[i]->abort();
- so[i]->deleteLater();
- }
- }
|