Python 官方文档:入门教程 => 点击学习
#ifndef EVANETWORK_H #define EVANETWORK_H #include <qobject.h> #include <qhostaddress.h> class EvaSocket; c
#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
#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函
--结束END--
本文标题: eva0.4.1源码看看3
本文链接: https://lsjlt.com/news/183442.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0