📄 evapicmanager.cpp
字号:
/*************************************************************************** * Copyright (C) 2005 by yunfan * * yunfan_zg@163.com * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "evapicmanager.h"#include "evauser.h"#include "evausersetting.h"#include "evanetwork.h"#include "evaqtutil.h"#include "libeva.h"#include <stdio.h>#include <unistd.h>#include <qevent.h>#include <qhostaddress.h>#include <qtextcodec.h>#include <qfile.h>#include <qdatastream.h>#include <qmutex.h>#include <qtimer.h>#define QUEUE_INTERVAL 10000EvaPicManager::EvaPicManager(EvaUser *u, bool useProxy){ user = u; usingProxy = useProxy; isBusy = false; connecter = NULL; currentIndex = 0; codec = QTextCodec::codecForName("GB18030"); EvaPicPacket::setQQ(user->getQQ()); bufLength = 0; currentFile.buf = NULL; expectedSequence = 0; isSend = false; isAppending = false; isRemoving = false; outPool.setAutoDelete(true); timer = new QTimer(this, "sendOutQueue"); QObject::connect(timer, SIGNAL(timeout()), this, SLOT(packetMonitor()));}EvaPicManager::~EvaPicManager(){ if(timer->isActive()) timer->stop(); delete timer; clearManager();}void EvaPicManager::setProxyServer( const QString ip, const short port, QCString proxyParam ){ proxyIP = ip; proxyPort = port; proxyAuthParam = proxyParam;}void EvaPicManager::customEvent( QCustomEvent * e ){ if(e->type() == EvaRequestCustomizedPicEvent){ EvaAskForCustomizedPicEvent *se = (EvaAskForCustomizedPicEvent *)e; Session session; session.qunID = se->getQunID(); session.list = se->getPicList(); downloadList.push_back(session); //if(isBusy) printf("EvaPicManager::customEvent -- isBusy \n"); if(!isBusy) doProcessEvent(); }else if(e->type() == EvaSendPictureReadyEvent){ EvaSendCustomizedPicEvent *se = (EvaSendCustomizedPicEvent *) e; OutSession session; session.qunID = se->getQunID(); session.list = se->getPicList(); sendList.push_back(session); if(!isBusy) doProcessOutEvent(); }}void EvaPicManager::doProcessEvent(){ if(!downloadList.size()) { isBusy = false; if(sendList.size()) doProcessOutEvent(); clearManager(); printf("EvaPicManager::doProcessEvent -- downloadList size is ZERO, return\n"); return; } isBusy = true; isSend = false; Session session = downloadList.front(); downloadList.pop_front(); qunID = session.qunID; picList = session.list; //printf("EvaPicManager::doProcessEvent \n"); if(!picList.size()){ isBusy = false; doProcessEvent(); printf("EvaPicManager::doProcessEvent -- picList size is ZERO, return\n"); return; } currentIndex = -1; if(!Packet::getFileAgentKey()) { clearManager(); return; } EvaPicPacket::setFileAgentKey(Packet::getFileAgentKey()); currentPic = *(picList.begin()); initConnection(currentPic.ip, currentPic.port);}void EvaPicManager::initConnection(const int ip, const short port){ //printf("EvaPicManager::initConnection ip:%s, port:%d\n", QHostAddress(ip).toString().ascii(), port); QHostAddress host; if(ip==-1) host.setAddress(QString(GROUP_FILE_AGENT)); else host.setAddress(ip); sendIP = host.toIPv4Address(); sendPort = port; if(connecter){ delete connecter; } if(usingProxy){ connecter = new EvaNetwork(QHostAddress(proxyIP), proxyPort, EvaNetwork::HTTP_Proxy); connecter->setDestinationServer(host.toString(), port); connecter->setAuthParameter(proxyAuthParam); }else connecter = new EvaNetwork(host, port, EvaNetwork::TCP); QObject::connect(connecter, SIGNAL(isReady()), SLOT(slotReady())); QObject::connect(connecter, SIGNAL(dataComming(int)), SLOT(slotDataComming(int))); QObject::connect(connecter, SIGNAL(exceptionEvent(int)), SLOT(slotException(int))); //printf("EvaPicManager::initConnection -- connecting....\n"); connecter->connect();}void EvaPicManager::slotReady(){ //printf("EvaPicManager::slotReady -- connection ready\n"); if(!isSend) doRequestNextPic(); else doRequestAgent();}void EvaPicManager::sendPacket(EvaPicOutPacket *packet){ if(!connecter) { printf("EvaPicManager::sendPacket -- connecter NULL\n"); return; } QMutex mutex; mutex.lock(); unsigned char *buf = new unsigned char[MAX_PACKET_SIZE]; int len; packet->fill(buf, &len); bool result = connecter->write((char *)buf, len); delete buf; mutex.unlock(); if(!result) slotException(-2);}void EvaPicManager::append(EvaPicOutPacket *packet){ isAppending = true; sendPacket(packet); // force to send if(packet->needAck()) outPool.append(packet); else delete packet; isAppending = false; if(!timer->isActive()) timer->start(QUEUE_INTERVAL, false);}void EvaPicManager::slotDataComming(int len){ QMutex mutex; mutex.lock(); char * rawData = new char [len+1]; if(!connecter->read(rawData, len)){ delete rawData; mutex.unlock(); slotException(-1); return; } memcpy(buf + bufLength, rawData, len); bufLength += len; delete rawData; slotProcessBuffer(); mutex.unlock();}void EvaPicManager::slotProcessBuffer( ){ if(buf[0] == FAMILY_05_TAG && buf[bufLength-1] == FAMILY_05_TAIL){ EvaPicInPacket *packet = new EvaPicInPacket(buf, bufLength); unsigned int pLen = packet->getPacketLength(); while(pLen <= bufLength){ packet->cutOffPacketData(); memcpy(buf, buf+pLen, bufLength-pLen); bufLength -= pLen; removePacket(packet->hashCode()); parseInData(packet); delete packet; packet = new EvaPicInPacket(buf, bufLength); pLen = packet->getPacketLength(); } delete packet; }}void EvaPicManager::parseInData(const EvaPicInPacket *in){ if(in->isValid()){ switch(in->getCommand()){ case QQ_05_CMD_REQUEST_AGENT: processRequestAgentReply(in); break; case QQ_05_CMD_REQUEST_FACE: processRequestFaceReply(in); break; case QQ_05_CMD_TRANSFER: processTransferReply(in); break; case QQ_05_CMD_REQUEST_START: processRequestStartReply(in); break; } } else printf("EvaPicManager::parseInData -- invalid packet!\n");}void EvaPicManager::doRequestPic(CustomizedPic pic){ if( pic.fileName.isEmpty() ){ doRequestNextPic(); return; } if(QFile::exists(user->getSetting()->getPictureCacheDir() + "/" + pic.fileName)){ emit pictureReady(qunID, user->getSetting()->getPictureCacheDir() + "/" + pic.fileName, currentPic.tmpFileName); doRequestNextPic(); return; } EvaRequestFacePacket *packet = new EvaRequestFacePacket(); packet->setQunID(qunID); packet->setKey(pic.fileAgentKey); packet->setFileAgentToken(Packet::getFileAgentToken(), Packet::getFileAgentTokenLength()); packet->setSessionID(pic.sessionID); sessionID = pic.sessionID; append(packet);}void EvaPicManager::doRequestData(CustomizedPic pic, const bool isReply){ EvaPicTransferPacket *packet = new EvaPicTransferPacket(); packet->setSessionID(pic.sessionID); packet->setDataReply(isReply); append(packet);}void EvaPicManager::slotException(int /*type*/){ isRemoving = true; outPool.first(); outPool.remove(); isRemoving = false; //printf("EvaPicManager::slotException -- type %d\n", type); bufLength = 0; doRequestNextPic();}void EvaPicManager::processRequestAgentReply(const EvaPicInPacket *in){ RequestAgentReplyPacket *packet = new RequestAgentReplyPacket(in->getRawBody(), in->getRawBodyLength()); packet->parse(); if(expectedSequence != packet->getSequence()){ delete packet; return; } switch(packet->getReplyCode()){ case QQ_REQUEST_AGENT_REPLY_OK:{ sessionID = packet->getSessionID(); //printf("EvaPicManager::processRequestAgentReply -- \n\tmessage:%s\n", packet->getMessage().c_str());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -