/*************************************************************************** * Copyright (C) 2004 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. * ***************************************************************************/ #ifndef EVASERVERS_H #define EVASERVERS_H #include <qobject.h> #include <qvaluelist.h> //qt这么多奇怪的list #include <qhostaddress.h> /** This class loads all Tencent's tcp and udp servers, and return a random server IP address This class also involves some DNS operations. 看样子,是直接用qq的服务器,他从哪里知道的qq的服务器,还能有dns操作。未接触的知识 @author yunfan */ class QDns; class EvaServers : public QObject { Q_OBJECT public: EvaServers(QString &dataRoot); virtual ~EvaServers(); void fetchAddress(bool isUdp); signals: void isReady(QHostAddress ip); private: typedef struct serverItem{ int type; QString addr; } serverItem; enum { UDP, TCP, Addr_URL, Addr_IP}; QString filename; bool gotIP; bool isLoaded; int fetchType; QDns *dns; //qt还有dns类。无所不能 QValueList<serverItem> TCPServers; QValueList<serverItem> UDPServers; bool loadServers(); void defaultAddress(); private slots: void getResultsSlot(); }; #endif
/*************************************************************************** * Copyright (C) 2004 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 "evaservers.h" #include <qdns.h> #include <qfile.h> #include <qdatastream.h> #include <qdatetime.h> // seed for rand() #include <stdlib.h> // rand() function EvaServers::EvaServers(QString &dataRoot): gotIP(FALSE), isLoaded(FALSE) { filename = dataRoot + "/servers"; QTime t = QTime::currentTime(); srand( t.hour()*12+t.minute()*60+t.second()*60 ); isLoaded = this->loadServers(); dns = NULL; } EvaServers::~EvaServers() { delete dns; } void EvaServers::fetchAddress( bool isUdp ) { int num = 0; if(isUdp){ num = UDPServers.count(); fetchType = UDP; } else{ num = TCPServers.count(); fetchType = TCP; } if(num == 0 ){ defaultAddress(); return; } int index = rand() % num; serverItem addr; if(isUdp) addr = UDPServers[index]; else addr = TCPServers[index]; if(addr.type == Addr_IP){ emit isReady(QHostAddress(addr.addr.latin1())); // this way, Red hat 9 might work properly return; } // the address should be a URL now, so we try to get the IP if(dns!=NULL) { QObject::disconnect(dns, 0,0,0); delete dns; } dns = new QDns(addr.addr, QDns::A); QObject::connect(dns, SIGNAL(resultsReady()), this, SLOT(getResultsSlot())); } bool EvaServers::loadServers( ) { QFile file(filename); if(!file.open(IO_ReadOnly)){ return FALSE; } QTextStream stream(&file); QString line; QStringList lineList; int nextType = 0; while(!stream.atEnd()){ line = stream.readLine().stripWhiteSpace(); if(line == "UDP"){ nextType = UDP; continue; }else if(line == "TCP"){ nextType = TCP; continue; } else if(line == "" ){ //nextType = 0; continue; } lineList = QStringList::split(":", line); if(lineList.size() != 2) continue; lineList[0].stripWhiteSpace(); serverItem *item = new serverItem(); if(lineList[0]=="URL"){ item->type = Addr_URL; }else if(lineList[0]=="IP"){ item->type = Addr_IP; }else continue; item->addr = lineList[1].stripWhiteSpace(); if(nextType == UDP){ UDPServers.append(*item);//从file里面获得server地址列表 } if(nextType == TCP){ TCPServers.append(*item); } } file.close(); return TRUE; } void EvaServers::defaultAddress() { if(fetchType == TCP){ emit isReady(QHostAddress("218.17.209.23")); //这个是缺省地址。 }else{ emit isReady(QHostAddress("218.17.209.20")); //又是tcp又是udp,腾讯真正的是怎么个逻辑通信的? } } void EvaServers::getResultsSlot( ) { QValueList<QHostAddress> list = dns->addresses();//域名解析出来ip地址 if(list.count() == 0 ){ defaultAddress(); return; } QHostAddress addr = list[0]; emit isReady(addr); }
管中窥豹啊。
转载于:https://blog.51cto.com/no001/1317447
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/109982.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...