From 9402451aa201bc06be8b8ec02d0ed9b7af998b91 Mon Sep 17 00:00:00 2001 From: tianduanrui Date: Sun, 29 Apr 2018 20:05:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0QQt=20Server=E7=9A=84?= =?UTF-8?q?=E4=BE=8B=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- QQtExample.pro | 11 +- .../AppRoot/templete/qqtuserprotocol.cpp | 16 +- .../AppRoot/templete/qqtuserprotocol.h | 7 +- .../templete/qqtuserserverprotocol.cpp | 39 +++ .../AppRoot/templete/qqtuserserverprotocol.h | 131 ++++++++ demo/QQtServerCreator/QQtServerCreator.pro | 16 + demo/QQtServerCreator/mainwindow.ui | 280 +++++++++++++++++- .../qqtclientexample/qqtclientexample.pro | 50 +--- .../qqtserver2protocolmanager.h | 2 + src/network/qqtnetwork.h | 28 ++ src/network/qqtprotocolmanager.cpp | 42 ++- src/network/qqtprotocolmanager.h | 50 +--- src/network/qqtudpserverprotocol.cpp | 1 - src/network/qqtudpserverprotocol.h | 77 ----- 14 files changed, 562 insertions(+), 188 deletions(-) create mode 100644 demo/QQtServerCreator/AppRoot/templete/qqtuserserverprotocol.cpp create mode 100644 demo/QQtServerCreator/AppRoot/templete/qqtuserserverprotocol.h delete mode 100644 src/network/qqtudpserverprotocol.cpp delete mode 100644 src/network/qqtudpserverprotocol.h diff --git a/QQtExample.pro b/QQtExample.pro index 21241856..3de4ab6e 100644 --- a/QQtExample.pro +++ b/QQtExample.pro @@ -45,14 +45,15 @@ SUBDIRS = #SUBDIRS += examples/tabwidgetexamples #need QZXing, default closed. #SUBDIRS += examples/qrcodeexample -#必开 客户端的 basic -#SUBDIRS += examples/qqtnetworkexample +#网络创建工具 +SUBDIRS += demo/QQtClientCreator +SUBDIRS += demo/QQtServerCreator #这边是个组合项,客户端和服务器一起的。 SUBDIRS += examples/qqtclientexample -#SUBDIRS += demo/QQtClientCreator -#服务器的 highgrade SUBDIRS += examples/qqtserverexample -#SUBDIRS += demo/QQtServerCreator +#通信协议的复杂的例子 +SUBDIRS += examples/qqtnetworkexample + #greaterThan(QT_VERSION, 4.6.0):SUBDIRS += test/voicetest #mac:lessThan(QT_MAJOR_VERSION , 5):SUBDIRS -= test/voicetest diff --git a/demo/QQtClientCreator/AppRoot/templete/qqtuserprotocol.cpp b/demo/QQtClientCreator/AppRoot/templete/qqtuserprotocol.cpp index 009d8106..c577c44b 100644 --- a/demo/QQtClientCreator/AppRoot/templete/qqtuserprotocol.cpp +++ b/demo/QQtClientCreator/AppRoot/templete/qqtuserprotocol.cpp @@ -9,23 +9,23 @@ QDebug& operator << ( QDebug& dbg, const QQtUserMessage& msg ) return dbg.space(); } -QQtUserProtocol* QQtUserConnectionInstance ( QObject* parent ) +QQtTcpClient* QQtUserInstance ( QQtUserProtocol*& protocol, QObject* parent ) { static QQtUserProtocol* p0 = NULL; if ( !p0 ) { p0 = new QQtUserProtocol ( parent ); - - } + protocol = p0; - static QQtTcpServer* s0 = NULL; + static QQtTcpClient* s0 = NULL; if ( !s0 ) { - s0 = new QQtTcpServer ( parent ); - //... s0->installProtocol ( p0 ); - s0->listen ( QHostAddress::Any, 8000 ); + s0 = new QQtTcpClient ( parent ); + s0->installProtocol ( p0 ); + //s0->setServer + //s0->sendConnectToHost(); } - return p0; + return s0; } diff --git a/demo/QQtClientCreator/AppRoot/templete/qqtuserprotocol.h b/demo/QQtClientCreator/AppRoot/templete/qqtuserprotocol.h index 362b8ec5..5eeb448e 100644 --- a/demo/QQtClientCreator/AppRoot/templete/qqtuserprotocol.h +++ b/demo/QQtClientCreator/AppRoot/templete/qqtuserprotocol.h @@ -3,7 +3,7 @@ #include #include -#include +#include class QQtUserMessage : public QQtMessage { @@ -69,6 +69,9 @@ public: void recvCommand2 ( const QQtUserMessage& msg ) { //what do you want to do? } + void sendCommand1() { + //what do you want to do? + } signals: //给业务层发的信号 @@ -122,6 +125,6 @@ protected: }; //业务层初始化一下这个实例,总是从这里获取协议句柄进行对外读写。 -QQtUserProtocol* QQtUserConnectionInstance ( QObject* parent = 0 ); +QQtTcpClient* QQtUserInstance ( QQtUserProtocol*& protocol, QObject* parent = 0 ); #endif // QQTUSERPROTOCOL_H diff --git a/demo/QQtServerCreator/AppRoot/templete/qqtuserserverprotocol.cpp b/demo/QQtServerCreator/AppRoot/templete/qqtuserserverprotocol.cpp new file mode 100644 index 00000000..5811f647 --- /dev/null +++ b/demo/QQtServerCreator/AppRoot/templete/qqtuserserverprotocol.cpp @@ -0,0 +1,39 @@ +#include "qqtuserserverprotocol.h" + +QDebug& operator << ( QDebug& dbg, const QQtUserServerMessage& msg ) +{ + //这里打印一下,报文里面到底有什么信息, + //一般到这里的,都是被解析好的message。 + + dbg.nospace() << "{" << hex << msg.size() << "}"; + return dbg.space(); +} + +QQtTcpServer* QQtUserServerInstance ( QQtProtocolManager*& protocolManager, QObject* parent ) +{ + static QQtProtocolManager* m0 = 0; + if ( !m0 ) + { + //创建Protocol管理者 + m0 = new QQtProtocolManager ( parent ); + //注册我实现的Protocol + m0->registerProtocol ( ); + //初始化Protocol管理者完成。 + } + protocolManager = m0; + + static QQtTcpServer* s0 = 0; + if ( !s0 ) + { + //新建服务器 + s0 = new QQtTcpServer ( parent ); + //安装协议管理者 + s0->installProtocolManager ( m0 ); + //开始监听 + s0->listen ( QHostAddress::Any, 8001 ); + //服务器初始化完成。 + } + + //等待客户端发消息过来,Protocol就处理了,去业务层看看。 + return s0; +} diff --git a/demo/QQtServerCreator/AppRoot/templete/qqtuserserverprotocol.h b/demo/QQtServerCreator/AppRoot/templete/qqtuserserverprotocol.h new file mode 100644 index 00000000..39539685 --- /dev/null +++ b/demo/QQtServerCreator/AppRoot/templete/qqtuserserverprotocol.h @@ -0,0 +1,131 @@ +#ifndef QQTUSERSERVERPROTOCOL_H +#define QQTUSERSERVERPROTOCOL_H + +#include +#include +#include +#include + +class QQtUserServerMessage : public QQtMessage +{ + Q_OBJECT +public: + explicit QQtUserServerMessage ( QObject* parent = nullptr ) { + mSize = 0x03;//报文定长 + } + ~QQtUserServerMessage() { + + } + + quint8& size() { return mSize; } + const quint8& size() const { return mSize; } + quint8& cmd() { return mCmd; } + const quint8& cmd() const { return mCmd; } + quint8& data() { return mData; } + const quint8& data() const { return mData; } + +private: + //格式 + //|quint8 size|quint8 cmd|quint8 data| + quint8 mSize; + quint8 mCmd; + quint8 mData; + + // QQtMessage interface +public: + //把报文这条流解析出字段 + virtual void parser ( const QByteArray& l ) override { + QByteArray _l = l; + _l >> mSize; + _l >> mCmd; + _l >> mData; + } + //把报文字段组装成流 + virtual void packer ( QByteArray& l ) const override { + l << mSize; + l << mCmd; + l << mData; + } +}; + +QDebug& operator << ( QDebug& dbg, const QQtUserServerMessage& msg ); + +//业务层总是用这个协议工作,读来到的,写出去的。 +class QQtUserServerProtocol : public QQtProtocol +{ + Q_OBJECT +public: + explicit QQtUserServerProtocol ( QObject* parent = nullptr ) { + + } + ~QQtUserServerProtocol() { + + } + + //收到外部发来的很多命令,处理一下告诉业务层干点什么。 + void recvCommand1 ( const QQtUserServerMessage& msg ) { + //what do you want to do? + } + void recvCommand2 ( const QQtUserServerMessage& msg ) { + //what do you want to do? + } + + void sendCommand1() { + //what do you want to do? + } + +signals: + //给业务层发的信号 + void signalSendtoLogicLevelCode(); + +public slots: + + // QQtProtocol interface +protected: + //报文的最小长度 + virtual quint16 minlength() override { + return 0x0a; + } + //报文的最大长度 + virtual quint16 maxlength() override { + return 0x07FF; + } + //报文现在在流里,第一个字节,就是size,读出来,通过返回值告诉QQtProtocol + virtual quint16 splitter ( const QByteArray& l ) override { //stream + QByteArray s0 = l.left ( 1 ); + quint8 size = 0; + s0 >> size; + return size; + } + + //报文现在被切开,发了进来,第二个字节是cmd,解析出来,在函数里处理处理数据,告诉业务层,拿到数据了干点什么。 + virtual bool dispatcher ( const QByteArray& m ) override { //message + bool ret = true; + + QQtUserServerMessage qMsg; + qMsg.parser ( m ); + pline() << qMsg; + + switch ( qMsg.cmd() ) { + case 0x0a://protocol command 1 + recvCommand1 ( qMsg ); + break; + + case 0x0b://protocol command 2 + recvCommand2 ( qMsg ); + break; + + default: + ret = false; + pline() << "receive unknown command:" << hex << qMsg.cmd(); + break; + } + + return ret; + } +}; + +//用户使用这里的ProtocolManager进行必要的和客户端的通信。一般在QQtUserServerProtocol里面解决。 +QQtTcpServer* QQtUserServerInstance ( QQtProtocolManager*& protocolManager, QObject* parent ); + +#endif // QQTUSERSERVERPROTOCOL_H diff --git a/demo/QQtServerCreator/QQtServerCreator.pro b/demo/QQtServerCreator/QQtServerCreator.pro index e06692c3..682bf380 100644 --- a/demo/QQtServerCreator/QQtServerCreator.pro +++ b/demo/QQtServerCreator/QQtServerCreator.pro @@ -22,6 +22,12 @@ DEFINES += QT_DEPRECATED_WARNINGS # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 +#server +#instance protocol message +SOURCES += \ + AppRoot/templete/qqtuserserverprotocol.cpp +HEADERS += \ + AppRoot/templete/qqtuserserverprotocol.h SOURCES += \ main.cpp \ @@ -32,3 +38,13 @@ HEADERS += \ FORMS += \ mainwindow.ui + +#促使qqt_deploy_config配置执行,没有这个变量不执行。 +APP_CONFIG_PWD = $${PWD}/AppRoot +equals(QMAKE_HOST.os, Windows) { + APP_CONFIG_PWD ~=s,/,\\,g +} + +#促使编译源代码,qmake pri配置里面的QMAKE_XX_LINK命令就会执行。 +system("touch main.cpp") +include(../../src/app_base_manager.pri) diff --git a/demo/QQtServerCreator/mainwindow.ui b/demo/QQtServerCreator/mainwindow.ui index 7de574d7..7b26309c 100644 --- a/demo/QQtServerCreator/mainwindow.ui +++ b/demo/QQtServerCreator/mainwindow.ui @@ -1,24 +1,282 @@ + MainWindow - - + + 0 0 - 400 - 300 + 542 + 324 - + MainWindow - - - - + + + + + + Qt::Horizontal + + + + 355 + 20 + + + + + + + + Next + + + + + + + Generate + + + + + + + 0 + + + + Message + + + + + + Name: + + + + + + + QQtUser + + + + + + + Generate + + + + + + + Qt::Horizontal + + + + 427 + 20 + + + + + + + + + + + + Protocol + + + + + + Name: + + + + + + + QQtUser + + + + + + + + + + Generate + + + + + + + Qt::Horizontal + + + + 427 + 20 + + + + + + + + + Client + + + + + + Name: + + + + + + + QQtUser + + + + + + + + + + Generate + + + + + + + Qt::Horizontal + + + + 427 + 20 + + + + + + + + + Server + + + + + + Name: + + + + + + + QQtUser + + + + + + + + + + Generate + + + + + + + Qt::Horizontal + + + + 427 + 20 + + + + + + + + + Readme + + + + + + <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> +<html><head><meta name="qrichtext" content="1" /><style type="text/css"> +p, li { white-space: pre-wrap; } +</style></head><body style=" font-family:'SimSun'; font-size:8.25pt; font-weight:400; font-style:normal;"> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:12pt; font-weight:600;">Message</span></p> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:10pt;">这个是报文的格式,很多协议会使用相同的报文格式。</span></p> +<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:10pt;"><br /></p> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:12pt; font-weight:600;">Protocol</span></p> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:10pt;">这是命令集合、功能集合、send、recvXXXMessage全在这里。基本上所有的通信功能在这里实现。注意:客户端和服务端的协议分开的实现。</span></p> +<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:10pt;"><br /></p> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:12pt; font-weight:600;">Client</span></p> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:10pt;">这是通信句柄。客户使用这里的protocol句柄进行通信。</span></p> +<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:10pt;"><br /></p> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:12pt; font-weight:600;">Server</span></p> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:10pt;">这是通信句柄。客户使用这里的protocolManager句柄进行某些必要通信。这里的server句柄提供客户端列表。</span></p> +<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:10pt;"><br /></p></body></html> + + + + + + + + + + + + + 0 + 0 + 542 + 17 + + + + + + TopToolBarArea + + + false + + + - - + diff --git a/examples/qqtclientexample/qqtclientexample.pro b/examples/qqtclientexample/qqtclientexample.pro index fa3f5f92..b9ae1d40 100644 --- a/examples/qqtclientexample/qqtclientexample.pro +++ b/examples/qqtclientexample/qqtclientexample.pro @@ -22,56 +22,10 @@ DEFINES += QT_DEPRECATED_WARNINGS #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES -= \ - qqtlanprotocol.cpp \ - qqtuserprotocol1.cpp \ - qqtuserprotocol2.cpp \ - qqtuserprotocol3.cpp \ - usernode0protocol.cpp \ - usernode1protocol.cpp \ - usernode2protocol.cpp \ - usernode3protocol.cpp \ - usernode4protocol.cpp \ - usernode5protocol.cpp \ - usernode6protocol.cpp \ - usernode7protocol.cpp \ - usernode8protocol.cpp \ - usernode9protocol.cpp \ - usertest0protocol.cpp \ - usertest1protocol.cpp \ - usertest2protocol.cpp \ - usertest3protocol.cpp \ - usertest4protocol.cpp \ - usertest5protocol.cpp \ - usertest6protocol.cpp \ - usertest7protocol.cpp \ - usertest8protocol.cpp \ - usertest9protocol.cpp + qqtlanprotocol.cpp HEADERS -= \ - qqtlanprotocol.h \ - qqtuserprotocol1.h \ - qqtuserprotocol2.h \ - qqtuserprotocol3.h \ - usernode0protocol.h \ - usernode1protocol.h \ - usernode2protocol.h \ - usernode3protocol.h \ - usernode4protocol.h \ - usernode5protocol.h \ - usernode6protocol.h \ - usernode7protocol.h \ - usernode8protocol.h \ - usernode9protocol.h \ - usertest0protocol.h \ - usertest1protocol.h \ - usertest2protocol.h \ - usertest3protocol.h \ - usertest4protocol.h \ - usertest5protocol.h \ - usertest6protocol.h \ - usertest7protocol.h \ - usertest8protocol.h \ - usertest9protocol.h + qqtlanprotocol.h SOURCES += \ main.cpp \ diff --git a/examples/qqtserverexample/qqtserver2protocolmanager.h b/examples/qqtserverexample/qqtserver2protocolmanager.h index c679c24e..38e9a1e9 100644 --- a/examples/qqtserverexample/qqtserver2protocolmanager.h +++ b/examples/qqtserverexample/qqtserver2protocolmanager.h @@ -5,6 +5,8 @@ #include #include +//这个有难度,不调试了。 +//C -> S 和 S -> C的报文格式不一样。 class QQtClient2Message : public QQtMessage { Q_OBJECT diff --git a/src/network/qqtnetwork.h b/src/network/qqtnetwork.h index b7629cb2..acc03360 100644 --- a/src/network/qqtnetwork.h +++ b/src/network/qqtnetwork.h @@ -1,4 +1,32 @@ #ifndef QQTNETWORKDEFINE_H #define QQTNETWORKDEFINE_H +#include + +#include +#include + +#include +#include + +#include +#include + +#include + +#ifdef __BLUETOOTH__ +#include +#include +#include +#endif + +#ifdef __WEBSOCKETSUPPORT__ +#include +#include +#endif + +#ifdef __WEBACCESSSUPPORT__ +#include +#endif + #endif // QQTNETWORKDEFINE_H diff --git a/src/network/qqtprotocolmanager.cpp b/src/network/qqtprotocolmanager.cpp index 533df1bb..dc2d7a58 100644 --- a/src/network/qqtprotocolmanager.cpp +++ b/src/network/qqtprotocolmanager.cpp @@ -1 +1,41 @@ -#include "qqtprotocolmanager.h" +#include + + +QQtProtocolManager::QQtProtocolManager ( QObject* parent ) : QObject ( parent ) +{ +} + +QQtProtocolManager::~QQtProtocolManager() +{ + +} + +QQtProtocol* QQtProtocolManager::createProtocol() +{ + if ( m_protocol_list.isEmpty() ) + return NULL; + + //无论如何,使用对象工厂也一样,都不能正确生成。对象工厂崩溃退出。 + //QQtProtocol* p0 = ( QQtProtocol* ) mProtocol->metaObject()->newInstance ( Q_ARG(QQtProtocolManager*, this) ); + QQtProtocol* p0 = findDetachedInstance(); + if ( p0 == 0 ) + return NULL; + pmeta ( p0 ) << p0; + + //帮助Protocol给用户发数据。 + connect ( p0, SIGNAL ( notifyToProtocolManager ( const QQtProtocol*, const QQtMessage* ) ), + this, SIGNAL ( notifyToBusinessLevel ( const QQtProtocol*, const QQtMessage* ) ) ); + return p0; +} + +QQtProtocol* QQtProtocolManager::findDetachedInstance() +{ + QListIterator itor ( m_protocol_list ); + while ( itor.hasNext() ) + { + QQtProtocol* p = itor.next(); + if ( p->detached() ) + return p; + } + return NULL; +} diff --git a/src/network/qqtprotocolmanager.h b/src/network/qqtprotocolmanager.h index c2cf2d6c..c27ebfbc 100644 --- a/src/network/qqtprotocolmanager.h +++ b/src/network/qqtprotocolmanager.h @@ -26,17 +26,20 @@ * 用户的客户端可以发个人信息上来,Protocol保存在句柄内部。 * * 定位: - * 对同种类型的协议句柄,可以管理1024个。 + * 只可以安装同一种类型的句柄。 + * 可以通过多次注册改变内部的句柄数量。 + * + * 模型: + * 业 PM S 业 + * 务 | | 务 + * 层 P - C :: C - P 层 */ class QQTSHARED_EXPORT QQtProtocolManager : public QObject { Q_OBJECT public: - explicit QQtProtocolManager ( QObject* parent = 0 ) : QObject ( parent ) { - } - virtual ~QQtProtocolManager() { - - } + explicit QQtProtocolManager ( QObject* parent = 0 ); + virtual ~QQtProtocolManager(); //获取Protocol列表 //这里列举的函数是给BusinessLevel用的,Protocol里面不要用 @@ -63,11 +66,12 @@ public: * 用于ProtocolManager内部生成用户协议实例。 * 这个用户在生成ProtocolManager对象的时候,需要注册一下自己的协议类,需要调用一次。 * 注意: - * registerProtocol(); + * registerProtocol(1); + * 可以根据Protocol ObjectName区分Protocol 或者metaObject()->className() */ template - bool registerProtocol () { - for ( int i = 0; i < 1024; i++ ) { + bool registerProtocol ( int count = 1024 ) { + for ( int i = 0; i < count; i++ ) { QQtProtocol* p0 = new T ( this ); m_protocol_list.push_back ( p0 ); } @@ -85,33 +89,9 @@ public: * @param protocolTypeName * @return */ - QQtProtocol* createProtocol () { - if ( m_protocol_list.isEmpty() ) - return NULL; - - //无论如何,使用对象工厂也一样,都不能正确生成。对象工厂崩溃退出。 - //QQtProtocol* p0 = ( QQtProtocol* ) mProtocol->metaObject()->newInstance ( Q_ARG(QQtProtocolManager*, this) ); - QQtProtocol* p0 = findDetachedInstance(); - if ( p0 == 0 ) - return NULL; - pmeta ( p0 ) << p0; - - //帮助Protocol给用户发数据。 - connect ( p0, SIGNAL ( notifyToProtocolManager ( const QQtProtocol*, const QQtMessage* ) ), - this, SIGNAL ( notifyToBusinessLevel ( const QQtProtocol*, const QQtMessage* ) ) ); - return p0; - } - + QQtProtocol* createProtocol (); protected: - QQtProtocol* findDetachedInstance() { - QListIterator itor ( m_protocol_list ); - while ( itor.hasNext() ) { - QQtProtocol* p = itor.next(); - if ( p->detached() ) - return p; - } - return NULL; - } + QQtProtocol* findDetachedInstance(); private: QList m_protocol_list; }; diff --git a/src/network/qqtudpserverprotocol.cpp b/src/network/qqtudpserverprotocol.cpp deleted file mode 100644 index 6da6a079..00000000 --- a/src/network/qqtudpserverprotocol.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "qqtudpserverprotocol.h" diff --git a/src/network/qqtudpserverprotocol.h b/src/network/qqtudpserverprotocol.h deleted file mode 100644 index f75a3116..00000000 --- a/src/network/qqtudpserverprotocol.h +++ /dev/null @@ -1,77 +0,0 @@ -#ifndef QQTUDPSERVELPROTOCOL_H -#define QQTUDPSERVELPROTOCOL_H - -#include -#include -#include "qqtmessage.h" -#include "qqtcore.h" - -#define QT_VERSION_DATAGRAM QT_VERSION_CHECK(5,8,0) - -#if QT_VERSION > QT_VERSION_DATAGRAM -#include -#endif - -/* -*/ -class QQTSHARED_EXPORT QQtUdpServerProtocol : public QObject -{ - Q_OBJECT -public: - explicit QQtUdpServerProtocol ( QObject* parent = nullptr ) : QObject ( parent ) { - - } - virtual ~QQtUdpServerProtocol() {} - -#if QT_VERSION > QT_VERSION_DATAGRAM - qint64 writeDatagram ( const QNetworkDatagram& datagram ) { - QByteArray dg = datagram.data(); - QHostAddress addr = datagram.destinationAddress(); - int port = datagram.destinationPort(); - emit writeDatagram ( dg, addr, ( quint16 ) port ); - } -#endif - -signals: - qint64 writeDatagram ( const QByteArray& datagram, - const QHostAddress& host, quint16 port ); - -public slots: - -protected: - /** - * @brief 语义解析器 - * @brief 用户必须继承下去,重写,need override - * @param 数据包 - * @return 0 no dispatched(others) 1 dispatched(own) - */ - inline virtual bool dispatcher ( const QByteArray& datagram, - const QHostAddress& host, quint16 port ) { - return 0; - } - /* - * 这两个dispatcher,任选其一重写。 - */ -#if QT_VERSION > QT_VERSION_DATAGRAM - inline virtual bool dispatcher ( const QNetworkDatagram& ) { return 0; } -#endif - -public: - /** - * @brief 协议处理器 - * @brief 这个处理器是给QQtUdpSocket用的,不需要用户管理。 - * @param Qt通讯口readAll()读到的bytes - * @return - */ - void translator ( const QByteArray& datagram, - const QHostAddress& host, quint16 port ) { - dispatcher ( datagram, host, port ); - } -#if QT_VERSION > QT_VERSION_DATAGRAM - void translator ( const QNetworkDatagram& datagram ) { - dispatcher ( datagram ); - } -#endif -}; - -#endif // QQTUDPSERVELPROTOCOL_H