代码语言:javascript复制
/***************************************************************************
* 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 EVANETWORK_H
#define EVANETWORK_H
#include <qobject.h>
#include <qhostaddress.h>
class EvaSocket;
class EvaHttpProxy;
class EvaNetwork : public QObject{
Q_OBJECT
public:
enum Type { UDP, TCP, HTTP_Proxy}; //3种方式,可以好好看看http代理,这方面技术没研究过
enum Event { Init, Connecting, Ready, Failed, None, BytesReadWrong,
Proxy_None, Proxy_TCP_Ready, Proxy_Connecting, Proxy_Ready,
Proxy_Need_Auth, Proxy_Read_Error, Proxy_Error };
EvaNetwork(const QHostAddress &host, const short port, const Type type = UDP);//默认是udp
~EvaNetwork();
//哦。有server,那么服务器部分的程序,有吗
void setServer(const QHostAddress &address, const short port);
const QHostAddress &getHostAddress() const; // if it's Http Proxy, return the proxy's address
const short getHostPort() const;
void setDestinationServer(const QString &server, const short port); // for Http Proxy only;
void setAuthParameter(const QString &username, const QString &password);//代理认证?
void setAuthParameter(const QCString ¶m);
void newURLRequest();
void connect();
bool read(char *buf, int len);
bool write(const char *buf, const int len);
void setWriteNotifierEnabled(bool enabled);
void close();
const Type connectionType() { return type; }
const QHostAddress getSocketIp();
const unsigned int getSocketPort();
signals:
void isReady();
void dataComming(int);
void exceptionEvent(int); // all in enum type Event;
void writeReady();
private:
EvaSocket *socket; //基本socket
Type type;
private slots:
void processProxyEvent(int);
};
#endif
代码语言:javascript复制/*********************************************************************
继续看cpp文件
代码语言:javascript复制/***************************************************************************
* 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 "evanetwork.h"
#include "evasocket.h"
EvaNetwork::EvaNetwork(const QHostAddress &host, const short port, const Type type)
:socket(NULL)
{
this->type = type;
switch(type){
case UDP:
socket = new EvaSocket(host, port); // default is UDP
QObject::connect(socket, SIGNAL(isReady()), this, SIGNAL(isReady()));//socket--->this
QObject::connect(socket, SIGNAL(writeReady()), SIGNAL(writeReady()));
QObject::connect(socket, SIGNAL(receivedData(int)), this, SIGNAL(dataComming(int)));
QObject::connect(socket, SIGNAL(exceptionEvent(int)), this, SLOT(processProxyEvent(int)));
break;
case TCP:
socket = new EvaSocket(host, port, EvaSocket::TCP);
QObject::connect(socket, SIGNAL(isReady()), this, SIGNAL(isReady()));
QObject::connect(socket, SIGNAL(writeReady()), SIGNAL(writeReady()));
QObject::connect(socket, SIGNAL(receivedData(int)), this, SIGNAL(dataComming(int)));
QObject::connect(socket, SIGNAL(exceptionEvent(int)), this, SLOT(processProxyEvent(int)));
break;
case HTTP_Proxy:
socket = new EvaHttpProxy(host, port);
QObject::connect(socket, SIGNAL(proxyWriteReady()), SIGNAL(writeReady()));
QObject::connect(socket, SIGNAL(proxyEvent(int)), this, SLOT(processProxyEvent(int)));
QObject::connect(socket, SIGNAL(dataArrived(int)), this, SIGNAL(dataComming(int)));
QObject::connect(socket, SIGNAL(socketException(int)), this, SIGNAL(exceptionEvent(int)));
break;
default:
socket = new EvaSocket(host, port); // default is UDP
break;
}
}
EvaNetwork::~EvaNetwork()
{
if(socket) delete socket;
}
void EvaNetwork::setServer(const QHostAddress &address, const short port)
{
socket->setHost(address, port);
}
const QHostAddress &EvaNetwork::getHostAddress() const
{
return socket->getHostAddress();
}
const short EvaNetwork::getHostPort() const
{
return socket->getHostPort();
}
void EvaNetwork::setDestinationServer(const QString &server, const short port) // for Http Proxy only;
{
if(type != HTTP_Proxy) return;
//看来在这个类EvaHttpProxy里,实现了http代理,得详细看看
((EvaHttpProxy*)(socket))->setDestinationServer(server, port);
}
void EvaNetwork::setAuthParameter(const QString &username, const QString &password)
{
((EvaHttpProxy*)(socket))->setAuthParameter(username, password);
}
void EvaNetwork::setAuthParameter(const QCString ¶m)
{
((EvaHttpProxy*)(socket))->setBase64AuthParam(param);
}
void EvaNetwork::newURLRequest()
{
((EvaHttpProxy*)(socket))->tcpReady();
}
void EvaNetwork::connect()
{
socket->startConnecting();
}
bool EvaNetwork::read(char *buf, int len)
{
return socket->read(buf, len);
}
bool EvaNetwork::write(const char *buf, const int len)
{
return socket->write(buf, len);
}
//什么东西都是socket实现的,这里只是封装了一下
void EvaNetwork::setWriteNotifierEnabled(bool enabled)
{
socket->setWriteNotifierEnabled(enabled);
}
void EvaNetwork::processProxyEvent(int num)
{
if(type != HTTP_Proxy){
switch(num){
case EvaSocket::Init:
case EvaSocket::Connecting:
case EvaSocket::Ready:
case EvaSocket::Failed:
case EvaSocket::None:
case EvaSocket::BytesReadWrong:
emit exceptionEvent(num);
break;
}
}else{
switch(num){
case EvaHttpProxy::Proxy_None:
case EvaHttpProxy::Proxy_TCP_Ready:
case EvaHttpProxy::Proxy_Connecting:
break;
case EvaHttpProxy::Proxy_Ready:
emit isReady();
break;
case EvaHttpProxy::Proxy_Need_Auth:
emit exceptionEvent(Proxy_Need_Auth);
break;
case EvaHttpProxy::Proxy_Read_Error:
emit exceptionEvent(Proxy_Read_Error);
break;
case EvaHttpProxy::Proxy_Error:
emit exceptionEvent(Proxy_Error);
break;
}
}
}
void EvaNetwork::close( )
{
socket->closeConnection();
}
const QHostAddress EvaNetwork::getSocketIp( )
{
if(socket) return socket->getSocketAddress();
return QHostAddress();
}
const unsigned int EvaNetwork::getSocketPort( )
{
if(socket) return socket->getSocketPort();
return 0;
}
还是没啥具体实现,还是继续找最底层的socket函