diff --git a/src/frame/qqtapplication.cpp b/src/frame/qqtapplication.cpp index b48a5ea0..2b85e581 100644 --- a/src/frame/qqtapplication.cpp +++ b/src/frame/qqtapplication.cpp @@ -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 ) { diff --git a/src/network/qqtsocketudpclient.cpp b/src/network/qqtsocketudpclient.cpp index dd39fa43..2dbcb8a7 100644 --- a/src/network/qqtsocketudpclient.cpp +++ b/src/network/qqtsocketudpclient.cpp @@ -1,5 +1,4 @@ -#include "qqtsocketudpclient.h" -#include +#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 } } diff --git a/src/network/qqtsocketudpclient.h b/src/network/qqtsocketudpclient.h index b7f1e217..315d2db8 100644 --- a/src/network/qqtsocketudpclient.h +++ b/src/network/qqtsocketudpclient.h @@ -1,4 +1,4 @@ -#ifndef QQTUDPCLIENT_H +#ifndef QQTUDPCLIENT_H #define QQTUDPCLIENT_H #include @@ -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; diff --git a/src/network/qqtudpprotocol.cpp b/src/network/qqtudpprotocol.cpp index d182ed46..727f77fb 100644 --- a/src/network/qqtudpprotocol.cpp +++ b/src/network/qqtudpprotocol.cpp @@ -1,11 +1 @@ -#include "qqtudpprotocol.h" - -QQtUdpProtocol::QQtUdpProtocol ( QObject* parent ) : QObject ( parent ) -{ - -} - -void QQtUdpProtocol::translator ( const QNetworkDatagram& dg ) -{ - dispatcher ( dg ); -} +#include "qqtudpprotocol.h" diff --git a/src/network/qqtudpprotocol.h b/src/network/qqtudpprotocol.h index 6c93680c..bd4d8da9 100644 --- a/src/network/qqtudpprotocol.h +++ b/src/network/qqtudpprotocol.h @@ -1,11 +1,14 @@ -#ifndef QQTUDPPROTOCOL_H +#ifndef QQTUDPPROTOCOL_H #define QQTUDPPROTOCOL_H #include #include #include "qqtmessage.h" #include "qqtcore.h" + +#if QT_VERSION > QT_VERSION_CHECK(5,0,0) #include +#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 diff --git a/src/qqt_header.pri b/src/qqt_header.pri index 99c2eb26..758f91dd 100644 --- a/src/qqt_header.pri +++ b/src/qqt_header.pri @@ -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):{ diff --git a/src/qqt_source.pri b/src/qqt_source.pri index 16798f42..4bbe0ed6 100644 --- a/src/qqt_source.pri +++ b/src/qqt_source.pri @@ -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