1
0
mirror of https://gitee.com/drabel/LibQQt.git synced 2025-01-04 10:18:44 +08:00
This commit is contained in:
tianduanrui 2018-02-12 10:16:31 +08:00
parent 5c08d81bed
commit 1cffd4d4be
7 changed files with 116 additions and 68 deletions

View File

@ -1,4 +1,4 @@
#include "qqtapplication.h"
#include "qqtapplication.h"
#ifdef __PLUGINWATCHER__
#include "qqtpluginwatcher.h"
#endif
@ -135,7 +135,7 @@ void QQtApplication::setUPanAutorun ( bool run )
void QQtApplication::setWriteLogSystem ( bool open )
{
/*设置日志系统*/
#ifdef __QQTLOGFILESUPPORT__
#ifdef __QQTLOGSYSTEMSUPPORT__
if ( open )
{

View File

@ -1,5 +1,4 @@
#include "qqtsocketudpclient.h"
#include <QNetworkDatagram>
#include "qqtsocketudpclient.h"
QQtSocketUdpClient::QQtSocketUdpClient ( QObject* parent ) : QUdpSocket ( parent )
{
@ -30,8 +29,12 @@ void QQtSocketUdpClient::installProtocol ( QQtUdpProtocol* stack )
return;
m_protocol = stack;
connect ( m_protocol, SIGNAL ( writeDatagram ( QByteArray, QHostAddress, quint16 ) ),
this, SLOT ( slotWriteDatagram ( QByteArray, QHostAddress, quint16 ) ) );
#if QT_VERSION > QT_VERSION_CHECK(5,0,0)
connect ( m_protocol, SIGNAL ( writeDatagram ( const QNetworkDatagram& ) ),
this, SLOT ( slotWriteDatagram ( const QNetworkDatagram& ) ) );
#endif
}
void QQtSocketUdpClient::uninstallProtocol ( QQtUdpProtocol* stack )
@ -41,8 +44,12 @@ void QQtSocketUdpClient::uninstallProtocol ( QQtUdpProtocol* stack )
if ( !m_protocol )
return;
disconnect ( m_protocol, SIGNAL ( writeDatagram ( QByteArray, QHostAddress, quint16 ) ),
this, SLOT ( slotWriteDatagram ( QByteArray, QHostAddress, quint16 ) ) );
#if QT_VERSION > QT_VERSION_CHECK(5,0,0)
disconnect ( m_protocol, SIGNAL ( writeDatagram ( const QNetworkDatagram& ) ),
this, SLOT ( slotWriteDatagram ( const QNetworkDatagram& ) ) );
#endif
m_protocol = NULL;
}
@ -67,21 +74,21 @@ void QQtSocketUdpClient::socketStateChanged ( QAbstractSocket::SocketState eSock
switch ( eSocketState )
{
case QAbstractSocket::HostLookupState:
case QAbstractSocket::ConnectingState:
break;
case QAbstractSocket::HostLookupState:
case QAbstractSocket::ConnectingState:
break;
case QAbstractSocket::ConnectedState:
break;
case QAbstractSocket::ConnectedState:
break;
case QAbstractSocket::ClosingState:
break;
case QAbstractSocket::ClosingState:
break;
case QAbstractSocket::UnconnectedState:
break;
case QAbstractSocket::UnconnectedState:
break;
default:
break;
default:
break;
}
}
@ -99,13 +106,13 @@ void QQtSocketUdpClient::socketErrorOccured ( QAbstractSocket::SocketError e )
switch ( e )
{
case QAbstractSocket::RemoteHostClosedError:
break;
case QAbstractSocket::RemoteHostClosedError:
break;
case QAbstractSocket::HostNotFoundError:
default:
emit signalConnectFail();
break;
case QAbstractSocket::HostNotFoundError:
default:
emit signalConnectFail();
break;
}
}
@ -137,28 +144,41 @@ void QQtSocketUdpClient::updateProgress ( qint64 bytes )
//pline() << bytes;
}
qint64 QQtSocketUdpClient::slotWriteDatagram ( const QNetworkDatagram& datagram )
{
return writeDatagram ( datagram );
}
void QQtSocketUdpClient::readyReadData()
{
/*为什么用while?*/
/*为什么用while?*/ //Qt4 没有那么高级的一次性读取的接口
while ( hasPendingDatagrams() )
{
QByteArray bytes;
qint64 maxlen = 0;
QHostAddress host;
quint16 port;
qint64 size = pendingDatagramSize();
//这里的buf用完, 已经释放。
char* data = new char[size + 1]();
qint64 len = readDatagram ( data, size, &host, &port );
pline() << len;
bytes.setRawData ( data, size );
delete[] data;
m_protocol->translator ( bytes, host, port );
#if QT_VERSION > QT_VERSION_CHECK(5,0,0)
/*能够一次收够一条报文?测试的能。*/
QNetworkDatagram da = receiveDatagram();
QNetworkDatagram datagram;
//datagrame = receiveDatagram();
/*由于添加了兼容Qt4的代码以上注释起来。*/
datagram.setData ( bytes );
datagram.setSender ( host, port );
/*数据无意义 "" -1 在此设置*/
da.setDestination(this->localAddress(), this->localPort());
//pline() << "udp sender:" << da.senderAddress() << da.senderPort();
//pline() << "udp receiver:" << da.destinationAddress() << da.destinationPort();
datagram.setDestination ( this->localAddress(), this->localPort() );
//pline() << "udp sender:" << datagram.senderAddress() << datagram.senderPort();
//pline() << "udp receiver:" << datagram.destinationAddress() << datagram.destinationPort();
//pline() << "udp new msg size:" << size;
m_protocol->translator ( da );
m_protocol->translator ( datagram );
#endif
}
}

View File

@ -1,4 +1,4 @@
#ifndef QQTUDPCLIENT_H
#ifndef QQTUDPCLIENT_H
#define QQTUDPCLIENT_H
#include <QUdpSocket>
@ -40,7 +40,17 @@ private slots:
void socketConnected();
void socketDisconnect();
void updateProgress ( qint64 bytes );
qint64 slotWriteDatagram ( const QNetworkDatagram& datagram );
//如果有一个同名的槽参数不同并且被用宏控制起来Qt编译不过。
//Qt的元对象系统解析信号和槽函数不支持宏。
qint64 slotWriteDatagram ( const QByteArray& datagram,
const QHostAddress& host, quint16 port ) {
return writeDatagram ( datagram, host, port );
}
#if QT_VERSION > QT_VERSION_CHECK(5,0,0)
qint64 slotWriteDatagram ( const QNetworkDatagram& datagram ) {
return writeDatagram ( datagram );
}
#endif
private:
QQtUdpProtocol* m_protocol;

View File

@ -1,11 +1 @@
#include "qqtudpprotocol.h"
QQtUdpProtocol::QQtUdpProtocol ( QObject* parent ) : QObject ( parent )
{
}
void QQtUdpProtocol::translator ( const QNetworkDatagram& dg )
{
dispatcher ( dg );
}
#include "qqtudpprotocol.h"

View File

@ -1,11 +1,14 @@
#ifndef QQTUDPPROTOCOL_H
#ifndef QQTUDPPROTOCOL_H
#define QQTUDPPROTOCOL_H
#include <QObject>
#include <qqt-local.h>
#include "qqtmessage.h"
#include "qqtcore.h"
#if QT_VERSION > QT_VERSION_CHECK(5,0,0)
#include <QNetworkDatagram>
#endif
/*
*/
@ -13,11 +16,20 @@ class QQTSHARED_EXPORT QQtUdpProtocol : public QObject
{
Q_OBJECT
public:
explicit QQtUdpProtocol ( QObject* parent = nullptr );
explicit QQtUdpProtocol ( QObject* parent = nullptr ) : QObject ( parent ) {
}
virtual ~QQtUdpProtocol() {}
signals:
qint64 writeDatagram ( const QNetworkDatagram& );
qint64 writeDatagram ( const QByteArray& datagram,
const QHostAddress& host, quint16 port );
#if QT_VERSION > QT_VERSION_CHECK(5,0,0)
qint64 writeDatagram ( const QNetworkDatagram& datagram ) {
//emit writeDatagram();
}
#endif
public slots:
protected:
/**
@ -26,7 +38,13 @@ protected:
* @param
* @return 0 no dispatched(others) 1 dispatched(own)
*/
inline virtual bool dispatcher ( const QByteArray& datagram,
const QHostAddress& host, quint16 port ) {
return 0;
}
#if QT_VERSION > QT_VERSION_CHECK(5,0,0)
inline virtual bool dispatcher ( const QNetworkDatagram& ) { return 0; }
#endif
public:
/**
* @brief
@ -34,7 +52,15 @@ public:
* @param Qt通讯口readAll()bytes
* @return
*/
void translator ( const QNetworkDatagram& dg );
void translator ( const QByteArray& datagram,
const QHostAddress& host, quint16 port ) {
dispatcher ( datagram, host, port );
}
#if QT_VERSION > QT_VERSION_CHECK(5,0,0)
void translator ( const QNetworkDatagram& datagram ) {
dispatcher ( datagram );
}
#endif
};
#endif // QQTUDPPROTOCOL_H

View File

@ -148,7 +148,7 @@ contains (DEFINES, __CPP11__) {
##################MultiMedia Module###############################
DEFINES += __MULTIMEDIA__
#on mac qt has no multimedia
#on mac qt4 has no multimedia
contains(QKIT_PRIVATE, macOS) {
lessThan(QT_MAJOR_VERSION, 5):DEFINES-=__MULTIMEDIA__
}
@ -220,10 +220,13 @@ contains (DEFINES, __EXQUISITE__) {
##################WebSocket Module###############################
#don't close this macro ...
DEFINES += __TCPUDPSOCKET__
#if you use Qt Service Support ( QtSoap ), open this annotation
DEFINES += __WEBSERVICESUPPORT__
#One Ftp Http 单工...
#Multi 半双工(客户端并发,服务器序列) QNetworkAccessManager
#QNetworkAccessManager 提供多路并发 HTTP session。
#if you use QNetworkAccessManagerSupport , open this annotation
DEFINES += __WEBACCESSSUPPORT__
lessThan(QT_MAJOR_VERSION, 5): DEFINES -= __WEBACCESSSUPPORT__
@ -231,6 +234,7 @@ contains(QKIT_PRIVATE, ARM32||MIPS32||EMBEDDED):DEFINES -= __WEBACCESSSUPPORT__
contains (DEFINES, __WEBACCESSSUPPORT__) {
#QSslError not found, you need recompiler Qt4
}
DEFINES += __WEBKITSUPPORT__
contains (DEFINES, __WEBKITSUPPORT__) {
#TODO: QT += webkit
@ -267,10 +271,10 @@ DEFINES += __CUSTOMPLOT__
##################Mathes Module###############################
DEFINES += __MATHSUPPORT__
##################QQtLogFile Module###############################
DEFINES += __QQTLOGFILESUPPORT__
##################QQtLogSystem Module###############################
DEFINES += __QQTLOGSYSTEMSUPPORT__
##################################################################
#################################################################
##library
##################################################################
equals (QKIT_PRIVATE, iOSSimulator):{

View File

@ -227,30 +227,28 @@ FORMS += \
#network
SOURCES += \
$$PWD/network/qqtprotocol.cpp \
$$PWD/network/qqtudpprotocol.cpp \
$$PWD/network/qqtnetwork.cpp
HEADERS += \
$$PWD/network/qqtmessage.h \
$$PWD/network/qqtprotocol.h \
$$PWD/network/qqtudpprotocol.h \
$$PWD/network/qqtnetwork.h
#tcpsocket
SOURCES += \
$$PWD/network/qqtsockettcpclient.cpp \
$$PWD/network/qqtsockettcpserver.cpp
HEADERS += \
$$PWD/network/qqtsockettcpclient.h \
$$PWD/network/qqtsockettcpserver.h
#udpsocket
!contains(QKIT_PRIVATE, MIPS32||ARM32) {
#tcpudpsocket
contains(DEFINES, __TCPUDPSOCKET__){
SOURCES += \
$$PWD/network/qqtsockettcpclient.cpp \
$$PWD/network/qqtsockettcpserver.cpp
HEADERS += \
$$PWD/network/qqtsockettcpclient.h \
$$PWD/network/qqtsockettcpserver.h
SOURCES += \
$$PWD/network/qqtudpprotocol.cpp \
$$PWD/network/qqtsocketudpclient.cpp
HEADERS += \
$$PWD/network/qqtudpprotocol.h \
$$PWD/network/qqtsocketudpclient.h
}
#serialport
SOURCES += \
$$PWD/network/qqtserialport.cpp