From cf2b1a5b0e7a6f19fe53cbc2853fb51ee110f8bc Mon Sep 17 00:00:00 2001 From: tianduanrui Date: Thu, 19 Apr 2018 23:03:39 +0800 Subject: [PATCH] add qqt network example and creator --- QQt.pro | 1 + .../AppRoot/templete/qqtuserprotocol.cpp | 27 ++ .../AppRoot/templete/qqtuserprotocol.h | 127 ++++++++ demo/QQtNetworkCreator/QQtNetworkCreator.pro | 50 ++++ demo/QQtNetworkCreator/main.cpp | 11 + demo/QQtNetworkCreator/mainwindow.cpp | 77 +++++ demo/QQtNetworkCreator/mainwindow.h | 32 ++ demo/QQtNetworkCreator/mainwindow.ui | 58 ++++ examples/qqtnetworkexample/mainwindow.cpp | 1 - .../qqtnetworkexample/qqtnetworkexample.pro | 281 +++++++++++------- .../qqtnetworkexample/qqtuserprotocol1.cpp | 27 ++ examples/qqtnetworkexample/qqtuserprotocol1.h | 127 ++++++++ .../qqtnetworkexample/qqtuserprotocol2.cpp | 26 ++ examples/qqtnetworkexample/qqtuserprotocol2.h | 129 ++++++++ .../qqtnetworkexample/qqtuserprotocol3.cpp | 33 ++ examples/qqtnetworkexample/qqtuserprotocol3.h | 127 ++++++++ .../qqtnetworkexample/usernode0protocol.cpp | 27 ++ .../qqtnetworkexample/usernode0protocol.h | 127 ++++++++ .../qqtnetworkexample/usernode1protocol.cpp | 27 ++ .../qqtnetworkexample/usernode1protocol.h | 127 ++++++++ .../qqtnetworkexample/usernode2protocol.cpp | 27 ++ .../qqtnetworkexample/usernode2protocol.h | 127 ++++++++ .../qqtnetworkexample/usernode3protocol.cpp | 27 ++ .../qqtnetworkexample/usernode3protocol.h | 127 ++++++++ .../qqtnetworkexample/usernode4protocol.cpp | 27 ++ .../qqtnetworkexample/usernode4protocol.h | 127 ++++++++ .../qqtnetworkexample/usernode5protocol.cpp | 27 ++ .../qqtnetworkexample/usernode5protocol.h | 127 ++++++++ .../qqtnetworkexample/usernode6protocol.cpp | 27 ++ .../qqtnetworkexample/usernode6protocol.h | 127 ++++++++ .../qqtnetworkexample/usernode7protocol.cpp | 27 ++ .../qqtnetworkexample/usernode7protocol.h | 127 ++++++++ .../qqtnetworkexample/usernode8protocol.cpp | 27 ++ .../qqtnetworkexample/usernode8protocol.h | 127 ++++++++ .../qqtnetworkexample/usernode9protocol.cpp | 27 ++ .../qqtnetworkexample/usernode9protocol.h | 127 ++++++++ .../qqtnetworkexample/usertest0protocol.cpp | 27 ++ .../qqtnetworkexample/usertest0protocol.h | 127 ++++++++ .../qqtnetworkexample/usertest1protocol.cpp | 27 ++ .../qqtnetworkexample/usertest1protocol.h | 127 ++++++++ .../qqtnetworkexample/usertest2protocol.cpp | 27 ++ .../qqtnetworkexample/usertest2protocol.h | 127 ++++++++ .../qqtnetworkexample/usertest3protocol.cpp | 27 ++ .../qqtnetworkexample/usertest3protocol.h | 127 ++++++++ .../qqtnetworkexample/usertest4protocol.cpp | 27 ++ .../qqtnetworkexample/usertest4protocol.h | 127 ++++++++ .../qqtnetworkexample/usertest5protocol.cpp | 27 ++ .../qqtnetworkexample/usertest5protocol.h | 127 ++++++++ .../qqtnetworkexample/usertest6protocol.cpp | 27 ++ .../qqtnetworkexample/usertest6protocol.h | 127 ++++++++ .../qqtnetworkexample/usertest7protocol.cpp | 27 ++ .../qqtnetworkexample/usertest7protocol.h | 127 ++++++++ .../qqtnetworkexample/usertest8protocol.cpp | 27 ++ .../qqtnetworkexample/usertest8protocol.h | 127 ++++++++ .../qqtnetworkexample/usertest9protocol.cpp | 27 ++ .../qqtnetworkexample/usertest9protocol.h | 127 ++++++++ 56 files changed, 4109 insertions(+), 105 deletions(-) create mode 100644 demo/QQtNetworkCreator/AppRoot/templete/qqtuserprotocol.cpp create mode 100644 demo/QQtNetworkCreator/AppRoot/templete/qqtuserprotocol.h create mode 100644 demo/QQtNetworkCreator/QQtNetworkCreator.pro create mode 100644 demo/QQtNetworkCreator/main.cpp create mode 100644 demo/QQtNetworkCreator/mainwindow.cpp create mode 100644 demo/QQtNetworkCreator/mainwindow.h create mode 100644 demo/QQtNetworkCreator/mainwindow.ui create mode 100644 examples/qqtnetworkexample/qqtuserprotocol1.cpp create mode 100644 examples/qqtnetworkexample/qqtuserprotocol1.h create mode 100644 examples/qqtnetworkexample/qqtuserprotocol2.cpp create mode 100644 examples/qqtnetworkexample/qqtuserprotocol2.h create mode 100644 examples/qqtnetworkexample/qqtuserprotocol3.cpp create mode 100644 examples/qqtnetworkexample/qqtuserprotocol3.h create mode 100644 examples/qqtnetworkexample/usernode0protocol.cpp create mode 100644 examples/qqtnetworkexample/usernode0protocol.h create mode 100644 examples/qqtnetworkexample/usernode1protocol.cpp create mode 100644 examples/qqtnetworkexample/usernode1protocol.h create mode 100644 examples/qqtnetworkexample/usernode2protocol.cpp create mode 100644 examples/qqtnetworkexample/usernode2protocol.h create mode 100644 examples/qqtnetworkexample/usernode3protocol.cpp create mode 100644 examples/qqtnetworkexample/usernode3protocol.h create mode 100644 examples/qqtnetworkexample/usernode4protocol.cpp create mode 100644 examples/qqtnetworkexample/usernode4protocol.h create mode 100644 examples/qqtnetworkexample/usernode5protocol.cpp create mode 100644 examples/qqtnetworkexample/usernode5protocol.h create mode 100644 examples/qqtnetworkexample/usernode6protocol.cpp create mode 100644 examples/qqtnetworkexample/usernode6protocol.h create mode 100644 examples/qqtnetworkexample/usernode7protocol.cpp create mode 100644 examples/qqtnetworkexample/usernode7protocol.h create mode 100644 examples/qqtnetworkexample/usernode8protocol.cpp create mode 100644 examples/qqtnetworkexample/usernode8protocol.h create mode 100644 examples/qqtnetworkexample/usernode9protocol.cpp create mode 100644 examples/qqtnetworkexample/usernode9protocol.h create mode 100644 examples/qqtnetworkexample/usertest0protocol.cpp create mode 100644 examples/qqtnetworkexample/usertest0protocol.h create mode 100644 examples/qqtnetworkexample/usertest1protocol.cpp create mode 100644 examples/qqtnetworkexample/usertest1protocol.h create mode 100644 examples/qqtnetworkexample/usertest2protocol.cpp create mode 100644 examples/qqtnetworkexample/usertest2protocol.h create mode 100644 examples/qqtnetworkexample/usertest3protocol.cpp create mode 100644 examples/qqtnetworkexample/usertest3protocol.h create mode 100644 examples/qqtnetworkexample/usertest4protocol.cpp create mode 100644 examples/qqtnetworkexample/usertest4protocol.h create mode 100644 examples/qqtnetworkexample/usertest5protocol.cpp create mode 100644 examples/qqtnetworkexample/usertest5protocol.h create mode 100644 examples/qqtnetworkexample/usertest6protocol.cpp create mode 100644 examples/qqtnetworkexample/usertest6protocol.h create mode 100644 examples/qqtnetworkexample/usertest7protocol.cpp create mode 100644 examples/qqtnetworkexample/usertest7protocol.h create mode 100644 examples/qqtnetworkexample/usertest8protocol.cpp create mode 100644 examples/qqtnetworkexample/usertest8protocol.h create mode 100644 examples/qqtnetworkexample/usertest9protocol.cpp create mode 100644 examples/qqtnetworkexample/usertest9protocol.h diff --git a/QQt.pro b/QQt.pro index 0b5aa1c4..6126eb21 100644 --- a/QQt.pro +++ b/QQt.pro @@ -45,6 +45,7 @@ SUBDIRS += examples/qqtaudioexample #SUBDIRS += examples/qrcodeexample # SUBDIRS += examples/qqtnetworkexample +SUBDIRS += demo/QQtNetworkCreator #greaterThan(QT_VERSION, 4.6.0):SUBDIRS += test/voicetest #mac:lessThan(QT_MAJOR_VERSION , 5):SUBDIRS -= test/voicetest diff --git a/demo/QQtNetworkCreator/AppRoot/templete/qqtuserprotocol.cpp b/demo/QQtNetworkCreator/AppRoot/templete/qqtuserprotocol.cpp new file mode 100644 index 00000000..0b4cb5b2 --- /dev/null +++ b/demo/QQtNetworkCreator/AppRoot/templete/qqtuserprotocol.cpp @@ -0,0 +1,27 @@ +#include "qqtuserprotocol.h" + +QDebug& operator << ( QDebug& dbg, const QQtUserMessage& msg ) +{ + //这里打印一下,报文里面到底有什么信息, + //一般到这里的,都是被解析好的message。 + + dbg.nospace() << "{" << hex << msg.size() << "}"; + return dbg.space(); +} + +QQtUserProtocol* QQtUserConnectionInstance ( QObject* parent ) +{ + static QQtUserProtocol* p0 = NULL; + static QQtSocketTcpServer* s0 = NULL; + if ( !p0 && !s0 ) + { + p0 = new QQtUserProtocol ( parent ); + + s0 = new QQtSocketTcpServer ( parent ); + s0->installProtocol ( p0 ); + s0->listen ( QHostAddress::Any, 8000 ); + + } + + return p0; +} diff --git a/demo/QQtNetworkCreator/AppRoot/templete/qqtuserprotocol.h b/demo/QQtNetworkCreator/AppRoot/templete/qqtuserprotocol.h new file mode 100644 index 00000000..5b5dca78 --- /dev/null +++ b/demo/QQtNetworkCreator/AppRoot/templete/qqtuserprotocol.h @@ -0,0 +1,127 @@ +#ifndef QQTUSERPROTOCOL_H +#define QQTUSERPROTOCOL_H + +#include +#include +#include + +class QQtUserMessage : public QQtMessage +{ + Q_OBJECT +public: + explicit QQtUserMessage ( QObject* parent = nullptr ) { + mSize = 0x03;//报文定长 + } + ~QQtUserMessage() { + + } + + 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&, const QQtUserMessage& msg ); + + +//业务层总是用这个协议工作,读来到的,写出去的。 +class QQtUserProtocol : public QQtProtocol +{ + Q_OBJECT +public: + explicit QQtUserProtocol ( QObject* parent = nullptr ) { + + } + ~QQtUserProtocol() { + + } + + //收到外部发来的很多命令,处理一下告诉业务层干点什么。 + void recvCommand1 ( const QQtUserMessage& msg ) { + //what do you want to do? + } + void recvCommand2 ( const QQtUserMessage& msg ) { + //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; + + QQtUserMessage 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; + } +}; + +//业务层初始化一下这个实例,总是从这里获取协议句柄进行对外读写。 +QQtUserProtocol* QQtUserConnectionInstance ( QObject* parent = 0 ); + +#endif // QQTUSERPROTOCOL_H diff --git a/demo/QQtNetworkCreator/QQtNetworkCreator.pro b/demo/QQtNetworkCreator/QQtNetworkCreator.pro new file mode 100644 index 00000000..98d61033 --- /dev/null +++ b/demo/QQtNetworkCreator/QQtNetworkCreator.pro @@ -0,0 +1,50 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2018-04-19T20:04:17 +# +#------------------------------------------------- + +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = QQtNetworkCreator +TEMPLATE = app + +# The following define makes your compiler emit warnings if you use +# any feature of Qt which has been marked as deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if you use deprecated APIs. +# In order to do so, uncomment the following line. +# 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 + + +SOURCES += \ + main.cpp \ + mainwindow.cpp \ + #测试这个模板是不是正确的。 + AppRoot/templete/qqtuserprotocol.cpp + +HEADERS += \ + mainwindow.h \ + AppRoot/templete/qqtuserprotocol.h + +FORMS += \ + mainwindow.ui + +CONFIG += mobility +MOBILITY = + +#促使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/QQtNetworkCreator/main.cpp b/demo/QQtNetworkCreator/main.cpp new file mode 100644 index 00000000..aab39bb8 --- /dev/null +++ b/demo/QQtNetworkCreator/main.cpp @@ -0,0 +1,11 @@ +#include "mainwindow.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + + return a.exec(); +} diff --git a/demo/QQtNetworkCreator/mainwindow.cpp b/demo/QQtNetworkCreator/mainwindow.cpp new file mode 100644 index 00000000..a6ef15be --- /dev/null +++ b/demo/QQtNetworkCreator/mainwindow.cpp @@ -0,0 +1,77 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" + +#include +#include + +MainWindow::MainWindow ( QWidget* parent ) : + QMainWindow ( parent ), + ui ( new Ui::MainWindow ) +{ + ui->setupUi ( this ); + + QFile file ( "templete/qqtuserprotocol.h" ); + file.open ( QFile::ReadOnly ); + headerFileBytes = file.readAll(); + file.close(); + + file.setFileName ( "templete/qqtuserprotocol.cpp" ); + file.open ( QFile::ReadOnly ); + sourceFileBytes = file.readAll(); + file.close(); + + ui->lineEdit->installEventFilter ( this ); +} + +MainWindow::~MainWindow() +{ + delete ui; +} + +void MainWindow::on_pushButton_clicked() +{ + if ( ui->lineEdit->text().isEmpty() ) + return; + + QByteArray className = ui->lineEdit->text().toLocal8Bit(); + QByteArray HongName = className.toUpper(); + QByteArray fileName = className.toLower(); + + QByteArray headerFileBytes = this->headerFileBytes; + QByteArray sourceFileBytes = this->sourceFileBytes; + + headerFileBytes.replace ( "QQtUser", className ); + headerFileBytes.replace ( "QQTUSER", HongName ); + + sourceFileBytes.replace ( "QQtUser", className ); + sourceFileBytes.replace ( "qqtuser", fileName ); + + QFile headerFile ( QString ( "%1protocol.h" ).arg ( QString ( fileName ) ) ); + QFile sourceFile ( QString ( "%1protocol.cpp" ).arg ( QString ( fileName ) ) ); + + headerFile.open ( QFile::Truncate | QFile::WriteOnly ); + headerFile.write ( headerFileBytes ); + headerFile.close(); + + sourceFile.open ( QFile::Truncate | QFile::WriteOnly ); + sourceFile.write ( sourceFileBytes ); + sourceFile.close(); + +} + + +bool MainWindow::eventFilter ( QObject* watched, QEvent* event ) +{ + if ( watched == ui->lineEdit ) + { + if ( event->type() == QEvent::KeyPress ) + { + QKeyEvent* e = ( QKeyEvent* ) event; + if ( e->key() == Qt::Key_Enter + || e->key() == Qt::Key_Return ) + { + on_pushButton_clicked(); + } + } + } +} diff --git a/demo/QQtNetworkCreator/mainwindow.h b/demo/QQtNetworkCreator/mainwindow.h new file mode 100644 index 00000000..d69c86fc --- /dev/null +++ b/demo/QQtNetworkCreator/mainwindow.h @@ -0,0 +1,32 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +namespace Ui { +class MainWindow; +} + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow ( QWidget* parent = 0 ); + ~MainWindow(); + +private slots: + void on_pushButton_clicked(); + +private: + Ui::MainWindow* ui; + QByteArray headerFileBytes; + QByteArray sourceFileBytes; + + + // QObject interface +public: + virtual bool eventFilter ( QObject* watched, QEvent* event ) override; +}; + +#endif // MAINWINDOW_H diff --git a/demo/QQtNetworkCreator/mainwindow.ui b/demo/QQtNetworkCreator/mainwindow.ui new file mode 100644 index 00000000..f2242a77 --- /dev/null +++ b/demo/QQtNetworkCreator/mainwindow.ui @@ -0,0 +1,58 @@ + + + MainWindow + + + + 0 + 0 + 546 + 326 + + + + MainWindow + + + + + + 370 + 240 + 111 + 21 + + + + Create + + + + + + 60 + 40 + 271 + 16 + + + + Protocol Class Name: + + + + + + 140 + 100 + 281 + 31 + + + + + + + + + diff --git a/examples/qqtnetworkexample/mainwindow.cpp b/examples/qqtnetworkexample/mainwindow.cpp index fdef4bfd..63e4eec7 100644 --- a/examples/qqtnetworkexample/mainwindow.cpp +++ b/examples/qqtnetworkexample/mainwindow.cpp @@ -1,6 +1,5 @@ #include "mainwindow.h" #include "ui_mainwindow.h" -#include "qqtuserserialprotocol.h" MainWindow::MainWindow ( QWidget* parent ) : QMainWindow ( parent ), diff --git a/examples/qqtnetworkexample/qqtnetworkexample.pro b/examples/qqtnetworkexample/qqtnetworkexample.pro index 060d4d48..0fdbe2b7 100644 --- a/examples/qqtnetworkexample/qqtnetworkexample.pro +++ b/examples/qqtnetworkexample/qqtnetworkexample.pro @@ -1,104 +1,177 @@ -#------------------------------------------------- -# -# Project created by QtCreator 2017-10-17T21:33:06 -# -#------------------------------------------------- -QT += core gui - -greaterThan(QT_MAJOR_VERSION, 4): QT += widgets - -TARGET = qqtnetworkexample -TEMPLATE = app - -# The following define makes your compiler emit warnings if you use -# any feature of Qt which as been marked as deprecated (the exact warnings -# depend on your compiler). Please consult the documentation of the -# deprecated API in order to know how to port your code away from it. -DEFINES += QT_DEPRECATED_WARNINGS - -# You can also make your code fail to compile if you use deprecated APIs. -# In order to do so, uncomment the following line. -# 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 - -SOURCES += \ - main.cpp \ - mainwindow.cpp \ - qqtcloudprotocol.cpp \ - qqtlanprotocol.cpp \ - qqtnetworkmessage.cpp \ - qqtserialmessage.cpp \ - qqtuserserialprotocol.cpp \ - qqtsubprotocoltest.cpp - -HEADERS += \ - mainwindow.h \ - qqtcloudprotocol.h \ - qqtlanprotocol.h \ - qqtnetworkmessage.h \ - qqtserialmessage.h \ - qqtuserserialprotocol.h \ - qqtsubprotocoltest.h - -FORMS += \ - mainwindow.ui - -#qmake_pre/post_link will work after source changed but not pro pri changed. -system("touch main.cpp") - -#------------------------------------------------- -#link qqt library -#if you link a library to your app, on android you must select the running kit to the app, not LibQQt e.g. -#user can modify any infomation under this annotation -#------------------------------------------------- -include(../../src/app_base_manager.pri) - -contains (DEFINES, __BLUETOOTH__){ - SOURCES += \ - quserbluetoothprotocol.cpp - HEADERS += \ - quserbluetoothprotocol.h -} - -#------------------------------------------------- -#user app may use these these settings prefertly -#------------------------------------------------- -#------------------------------------------------- -#install app -#------------------------------------------------- -#CONFIG += can_install -can_install:equals(QKIT_PRIVATE, EMBEDDED) { - target.path = /Application - INSTALLS += target -} else: unix { - equals(QKIT_PRIVATE, macOS) { - target.path = /Applications - INSTALLS += target - } -} - -############ -##config defination -############ -equals(QKIT_PRIVATE, macOS) { - CONFIG += app_bundle -} - -contains(QKIT_PRIVATE, ANDROID|ANDROIDX86) { - CONFIG += mobility - MOBILITY = - DISTFILES += \ - android/AndroidManifest.xml - - ANDROID_PACKAGE_SOURCE_DIR = $${PWD}/android -} - - -#------------------------------------------------- -##project environ -#------------------------------------------------- -#default -message ($${TARGET} config $${CONFIG}) -message ($${TARGET} define $${DEFINES}) - - +#------------------------------------------------- +# +# Project created by QtCreator 2017-10-17T21:33:06 +# +#------------------------------------------------- +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = qqtnetworkexample +TEMPLATE = app + +# The following define makes your compiler emit warnings if you use +# any feature of Qt which as been marked as deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if you use deprecated APIs. +# In order to do so, uncomment the following line. +# 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 + +SOURCES += \ + main.cpp \ + mainwindow.cpp \ + qqtcloudprotocol.cpp \ + qqtlanprotocol.cpp \ + qqtnetworkmessage.cpp \ + qqtserialmessage.cpp \ + qqtuserserialprotocol.cpp \ + qqtsubprotocoltest.cpp \ + qqtuserprotocol1.cpp \ + qqtuserprotocol2.cpp \ + qqtuserprotocol3.cpp \ + main.cpp \ + mainwindow.cpp \ + qqtcloudprotocol.cpp \ + qqtlanprotocol.cpp \ + qqtnetworkmessage.cpp \ + qqtserialmessage.cpp \ + qqtsubprotocoltest.cpp \ + qqtuserprotocol1.cpp \ + qqtuserprotocol2.cpp \ + qqtuserprotocol3.cpp \ + qqtuserserialprotocol.cpp \ + quserbluetoothprotocol.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 + +HEADERS += \ + mainwindow.h \ + qqtcloudprotocol.h \ + qqtlanprotocol.h \ + qqtnetworkmessage.h \ + qqtserialmessage.h \ + qqtuserserialprotocol.h \ + qqtsubprotocoltest.h \ + qqtuserprotocol1.h \ + qqtuserprotocol2.h \ + qqtuserprotocol3.h \ + mainwindow.h \ + qqtcloudprotocol.h \ + qqtlanprotocol.h \ + qqtnetworkmessage.h \ + qqtserialmessage.h \ + qqtsubprotocoltest.h \ + qqtuserprotocol1.h \ + qqtuserprotocol2.h \ + qqtuserprotocol3.h \ + qqtuserserialprotocol.h \ + quserbluetoothprotocol.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 + +FORMS += \ + mainwindow.ui \ + mainwindow.ui + +#qmake_pre/post_link will work after source changed but not pro pri changed. +system("touch main.cpp") + +#------------------------------------------------- +#link qqt library +#if you link a library to your app, on android you must select the running kit to the app, not LibQQt e.g. +#user can modify any infomation under this annotation +#------------------------------------------------- +include(../../src/app_base_manager.pri) + +contains (DEFINES, __BLUETOOTH__){ + SOURCES += \ + quserbluetoothprotocol.cpp + HEADERS += \ + quserbluetoothprotocol.h +} + +#------------------------------------------------- +#user app may use these these settings prefertly +#------------------------------------------------- +#------------------------------------------------- +#install app +#------------------------------------------------- +#CONFIG += can_install +can_install:equals(QKIT_PRIVATE, EMBEDDED) { + target.path = /Application + INSTALLS += target +} else: unix { + equals(QKIT_PRIVATE, macOS) { + target.path = /Applications + INSTALLS += target + } +} + +############ +##config defination +############ +equals(QKIT_PRIVATE, macOS) { + CONFIG += app_bundle +} + +contains(QKIT_PRIVATE, ANDROID|ANDROIDX86) { + CONFIG += mobility + MOBILITY = + DISTFILES += \ + android/AndroidManifest.xml + + ANDROID_PACKAGE_SOURCE_DIR = $${PWD}/android +} + + +#------------------------------------------------- +##project environ +#------------------------------------------------- +#default +message ($${TARGET} config $${CONFIG}) +message ($${TARGET} define $${DEFINES}) + +SUBDIRS += \ + qqtnetworkexample.pro + + diff --git a/examples/qqtnetworkexample/qqtuserprotocol1.cpp b/examples/qqtnetworkexample/qqtuserprotocol1.cpp new file mode 100644 index 00000000..924d9006 --- /dev/null +++ b/examples/qqtnetworkexample/qqtuserprotocol1.cpp @@ -0,0 +1,27 @@ +#include "qqtuserprotocol1.h" + +QDebug& operator << ( QDebug& dbg, const QQtUserMessage1& msg ) +{ + //这里打印一下,报文里面到底有什么信息, + //一般到这里的,都是被解析好的message。 + + dbg.nospace() << "{" << hex << msg.size() << "}"; + return dbg.space(); +} + +QQtUserProtocol1* QQtUserConnectionInstance1 ( QObject* parent ) +{ + static QQtUserProtocol1* p0 = NULL; + static QQtSocketTcpServer* s0 = NULL; + if ( !p0 && !s0 ) + { + p0 = new QQtUserProtocol1 ( parent ); + + s0 = new QQtSocketTcpServer ( parent ); + s0->installProtocol ( p0 ); + s0->listen ( QHostAddress::Any, 8000 ); + + } + + return p0; +} diff --git a/examples/qqtnetworkexample/qqtuserprotocol1.h b/examples/qqtnetworkexample/qqtuserprotocol1.h new file mode 100644 index 00000000..48a0cdac --- /dev/null +++ b/examples/qqtnetworkexample/qqtuserprotocol1.h @@ -0,0 +1,127 @@ +#ifndef QQTUSERPROTOCOL1_H +#define QQTUSERPROTOCOL1_H + +#include +#include +#include + +class QQtUserMessage1 : public QQtMessage +{ + Q_OBJECT +public: + explicit QQtUserMessage1 ( QObject* parent = nullptr ) { + mSize = 0x03;//报文定长 + } + ~QQtUserMessage1() { + + } + + 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&, const QQtUserMessage1& msg ); + + +//业务层总是用这个协议工作,读来到的,写出去的。 +class QQtUserProtocol1 : public QQtProtocol +{ + Q_OBJECT +public: + explicit QQtUserProtocol1 ( QObject* parent = nullptr ) { + + } + ~QQtUserProtocol1() { + + } + + //收到外部发来的很多命令,处理一下告诉业务层干点什么。 + void recvCommand1 ( const QQtUserMessage1& msg ) { + //what do you want to do? + } + void recvCommand2 ( const QQtUserMessage1& msg ) { + //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; + + QQtUserMessage1 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; + } +}; + +//业务层初始化一下这个实例,总是从这里获取协议句柄进行对外读写。 +QQtUserProtocol1* QQtUserConnectionInstance1 ( QObject* parent = 0 ); + +#endif // QQTUSERPROTOCOL1_H diff --git a/examples/qqtnetworkexample/qqtuserprotocol2.cpp b/examples/qqtnetworkexample/qqtuserprotocol2.cpp new file mode 100644 index 00000000..8696fffc --- /dev/null +++ b/examples/qqtnetworkexample/qqtuserprotocol2.cpp @@ -0,0 +1,26 @@ +#include "qqtuserprotocol2.h" + +QDebug& operator << ( QDebug& dbg, const QQtUserMessage2& msg ) +{ + //这里打印一下,报文里面到底有什么信息, + //一般到这里的,都是被解析好的message。 + + dbg.nospace() << "{" << hex << msg.size() << "}"; + return dbg.space(); +} + +QQtUserProtocol2* QQtUserConnectionInstance2 ( QObject* parent ) +{ + static QQtUserProtocol2* p0 = NULL; + static QQtSocketTcpClient* s0 = NULL; + if ( !p0 && !s0 ) + { + p0 = new QQtUserProtocol2 ( parent ); + + s0 = new QQtSocketTcpClient ( parent ); + s0->installProtocol ( p0 ); + s0->connectToHost ( QHostAddress ( "192.168.0.100" ), 8000 ); + } + + return p0; +} diff --git a/examples/qqtnetworkexample/qqtuserprotocol2.h b/examples/qqtnetworkexample/qqtuserprotocol2.h new file mode 100644 index 00000000..47c398de --- /dev/null +++ b/examples/qqtnetworkexample/qqtuserprotocol2.h @@ -0,0 +1,129 @@ +#ifndef QQTUSERPROTOCOL2_H +#define QQTUSERPROTOCOL2_H + +#include +#include +#include + +#include + +class QQtUserMessage2 : public QQtMessage +{ + Q_OBJECT +public: + explicit QQtUserMessage2 ( QObject* parent = nullptr ) { + mSize = 0x03;//报文定长 + } + ~QQtUserMessage2() { + + } + + 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&, const QQtUserMessage2& msg ); + + +//业务层总是用这个协议工作,读来到的,写出去的。 +class QQtUserProtocol2 : public QQtProtocol +{ + Q_OBJECT +public: + explicit QQtUserProtocol2 ( QObject* parent = nullptr ) { + + } + ~QQtUserProtocol2() { + + } + + //收到外部发来的很多命令,处理一下告诉业务层干点什么。 + void recvCommand1 ( const QQtUserMessage2& msg ) { + //what do you want to do? + } + void recvCommand2 ( const QQtUserMessage2& msg ) { + //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; + + QQtUserMessage2 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; + } +}; + +//业务层初始化一下这个实例,总是从这里获取协议句柄进行对外读写。 +QQtUserProtocol2* QQtUserConnectionInstance2 ( QObject* parent = 0 ); + +#endif // QQTUSERPROTOCOL2_H diff --git a/examples/qqtnetworkexample/qqtuserprotocol3.cpp b/examples/qqtnetworkexample/qqtuserprotocol3.cpp new file mode 100644 index 00000000..8beb66f5 --- /dev/null +++ b/examples/qqtnetworkexample/qqtuserprotocol3.cpp @@ -0,0 +1,33 @@ +#include "qqtuserprotocol3.h" + +QDebug& operator << ( QDebug& dbg, const QQtUserMessage3& msg ) +{ + //这里打印一下,报文里面到底有什么信息, + //一般到这里的,都是被解析好的message。 + + dbg.nospace() << "{" << hex << msg.size() << "}"; + return dbg.space(); +} + +QQtUserProtocol3* QQtUserConnectionInstance3 ( QObject* parent ) +{ + static QQtUserProtocol3* p0 = NULL; + static QQtSerialPort* s0 = NULL; + if ( !p0 && !s0 ) + { + p0 = new QQtUserProtocol3 ( parent ); + + s0 = new QQtSerialPort ( parent ); + s0->setPortName ( "/dev/ttyUSB0" ); + s0->setBaudRate ( QQtSerialPort::Baud57600 ); + s0->setDataBits ( QQtSerialPort::Data8 ); + s0->setParity ( QQtSerialPort::NoParity ); + s0->setStopBits ( QQtSerialPort::OneStop ); + s0->setFlowControl ( QQtSerialPort::NoFlowControl ); + s0->open ( QQtSerialPort::ReadWrite ); + s0->installProtocol ( p0 ); + + } + + return p0; +} diff --git a/examples/qqtnetworkexample/qqtuserprotocol3.h b/examples/qqtnetworkexample/qqtuserprotocol3.h new file mode 100644 index 00000000..a4615c9a --- /dev/null +++ b/examples/qqtnetworkexample/qqtuserprotocol3.h @@ -0,0 +1,127 @@ +#ifndef QQTUSERPROTOCOL3_H +#define QQTUSERPROTOCOL3_H + +#include +#include +#include + +class QQtUserMessage3 : public QQtMessage +{ + Q_OBJECT +public: + explicit QQtUserMessage3 ( QObject* parent = nullptr ) { + mSize = 0x03;//报文定长 + } + ~QQtUserMessage3() { + + } + + 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&, const QQtUserMessage3& msg ); + + +//业务层总是用这个协议工作,读来到的,写出去的。 +class QQtUserProtocol3 : public QQtProtocol +{ + Q_OBJECT +public: + explicit QQtUserProtocol3 ( QObject* parent = nullptr ) { + + } + ~QQtUserProtocol3() { + + } + + //收到外部发来的很多命令,处理一下告诉业务层干点什么。 + void recvCommand1 ( const QQtUserMessage3& msg ) { + //what do you want to do? + } + void recvCommand2 ( const QQtUserMessage3& msg ) { + //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; + + QQtUserMessage3 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; + } +}; + +//业务层初始化一下这个实例,总是从这里获取协议句柄进行对外读写。 +QQtUserProtocol3* QQtUserConnectionInstance3 ( QObject* parent = 0 ); + +#endif // QQTUSERPROTOCOL3_H diff --git a/examples/qqtnetworkexample/usernode0protocol.cpp b/examples/qqtnetworkexample/usernode0protocol.cpp new file mode 100644 index 00000000..fa0a52f5 --- /dev/null +++ b/examples/qqtnetworkexample/usernode0protocol.cpp @@ -0,0 +1,27 @@ +#include "usernode0protocol.h" + +QDebug& operator << ( QDebug& dbg, const UserNode0Message& msg ) +{ + //这里打印一下,报文里面到底有什么信息, + //一般到这里的,都是被解析好的message。 + + dbg.nospace() << "{" << hex << msg.size() << "}"; + return dbg.space(); +} + +UserNode0Protocol* UserNode0ConnectionInstance ( QObject* parent ) +{ + static UserNode0Protocol* p0 = NULL; + static QQtSocketTcpServer* s0 = NULL; + if ( !p0 && !s0 ) + { + p0 = new UserNode0Protocol ( parent ); + + s0 = new QQtSocketTcpServer ( parent ); + s0->installProtocol ( p0 ); + s0->listen ( QHostAddress::Any, 8000 ); + + } + + return p0; +} diff --git a/examples/qqtnetworkexample/usernode0protocol.h b/examples/qqtnetworkexample/usernode0protocol.h new file mode 100644 index 00000000..ada51023 --- /dev/null +++ b/examples/qqtnetworkexample/usernode0protocol.h @@ -0,0 +1,127 @@ +#ifndef USERNODE0PROTOCOL_H +#define USERNODE0PROTOCOL_H + +#include +#include +#include + +class UserNode0Message : public QQtMessage +{ + Q_OBJECT +public: + explicit UserNode0Message ( QObject* parent = nullptr ) { + mSize = 0x03;//报文定长 + } + ~UserNode0Message() { + + } + + 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&, const UserNode0Message& msg ); + + +//业务层总是用这个协议工作,读来到的,写出去的。 +class UserNode0Protocol : public QQtProtocol +{ + Q_OBJECT +public: + explicit UserNode0Protocol ( QObject* parent = nullptr ) { + + } + ~UserNode0Protocol() { + + } + + //收到外部发来的很多命令,处理一下告诉业务层干点什么。 + void recvCommand1 ( const UserNode0Message& msg ) { + //what do you want to do? + } + void recvCommand2 ( const UserNode0Message& msg ) { + //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; + + UserNode0Message 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; + } +}; + +//业务层初始化一下这个实例,总是从这里获取协议句柄进行对外读写。 +UserNode0Protocol* UserNode0ConnectionInstance ( QObject* parent = 0 ); + +#endif // USERNODE0PROTOCOL_H diff --git a/examples/qqtnetworkexample/usernode1protocol.cpp b/examples/qqtnetworkexample/usernode1protocol.cpp new file mode 100644 index 00000000..1254f594 --- /dev/null +++ b/examples/qqtnetworkexample/usernode1protocol.cpp @@ -0,0 +1,27 @@ +#include "usernode1protocol.h" + +QDebug& operator << ( QDebug& dbg, const UserNode1Message& msg ) +{ + //这里打印一下,报文里面到底有什么信息, + //一般到这里的,都是被解析好的message。 + + dbg.nospace() << "{" << hex << msg.size() << "}"; + return dbg.space(); +} + +UserNode1Protocol* UserNode1ConnectionInstance ( QObject* parent ) +{ + static UserNode1Protocol* p0 = NULL; + static QQtSocketTcpServer* s0 = NULL; + if ( !p0 && !s0 ) + { + p0 = new UserNode1Protocol ( parent ); + + s0 = new QQtSocketTcpServer ( parent ); + s0->installProtocol ( p0 ); + s0->listen ( QHostAddress::Any, 8000 ); + + } + + return p0; +} diff --git a/examples/qqtnetworkexample/usernode1protocol.h b/examples/qqtnetworkexample/usernode1protocol.h new file mode 100644 index 00000000..6ef13f87 --- /dev/null +++ b/examples/qqtnetworkexample/usernode1protocol.h @@ -0,0 +1,127 @@ +#ifndef USERNODE1PROTOCOL_H +#define USERNODE1PROTOCOL_H + +#include +#include +#include + +class UserNode1Message : public QQtMessage +{ + Q_OBJECT +public: + explicit UserNode1Message ( QObject* parent = nullptr ) { + mSize = 0x03;//报文定长 + } + ~UserNode1Message() { + + } + + 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&, const UserNode1Message& msg ); + + +//业务层总是用这个协议工作,读来到的,写出去的。 +class UserNode1Protocol : public QQtProtocol +{ + Q_OBJECT +public: + explicit UserNode1Protocol ( QObject* parent = nullptr ) { + + } + ~UserNode1Protocol() { + + } + + //收到外部发来的很多命令,处理一下告诉业务层干点什么。 + void recvCommand1 ( const UserNode1Message& msg ) { + //what do you want to do? + } + void recvCommand2 ( const UserNode1Message& msg ) { + //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; + + UserNode1Message 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; + } +}; + +//业务层初始化一下这个实例,总是从这里获取协议句柄进行对外读写。 +UserNode1Protocol* UserNode1ConnectionInstance ( QObject* parent = 0 ); + +#endif // USERNODE1PROTOCOL_H diff --git a/examples/qqtnetworkexample/usernode2protocol.cpp b/examples/qqtnetworkexample/usernode2protocol.cpp new file mode 100644 index 00000000..3ff4516e --- /dev/null +++ b/examples/qqtnetworkexample/usernode2protocol.cpp @@ -0,0 +1,27 @@ +#include "usernode2protocol.h" + +QDebug& operator << ( QDebug& dbg, const UserNode2Message& msg ) +{ + //这里打印一下,报文里面到底有什么信息, + //一般到这里的,都是被解析好的message。 + + dbg.nospace() << "{" << hex << msg.size() << "}"; + return dbg.space(); +} + +UserNode2Protocol* UserNode2ConnectionInstance ( QObject* parent ) +{ + static UserNode2Protocol* p0 = NULL; + static QQtSocketTcpServer* s0 = NULL; + if ( !p0 && !s0 ) + { + p0 = new UserNode2Protocol ( parent ); + + s0 = new QQtSocketTcpServer ( parent ); + s0->installProtocol ( p0 ); + s0->listen ( QHostAddress::Any, 8000 ); + + } + + return p0; +} diff --git a/examples/qqtnetworkexample/usernode2protocol.h b/examples/qqtnetworkexample/usernode2protocol.h new file mode 100644 index 00000000..455b86e1 --- /dev/null +++ b/examples/qqtnetworkexample/usernode2protocol.h @@ -0,0 +1,127 @@ +#ifndef USERNODE2PROTOCOL_H +#define USERNODE2PROTOCOL_H + +#include +#include +#include + +class UserNode2Message : public QQtMessage +{ + Q_OBJECT +public: + explicit UserNode2Message ( QObject* parent = nullptr ) { + mSize = 0x03;//报文定长 + } + ~UserNode2Message() { + + } + + 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&, const UserNode2Message& msg ); + + +//业务层总是用这个协议工作,读来到的,写出去的。 +class UserNode2Protocol : public QQtProtocol +{ + Q_OBJECT +public: + explicit UserNode2Protocol ( QObject* parent = nullptr ) { + + } + ~UserNode2Protocol() { + + } + + //收到外部发来的很多命令,处理一下告诉业务层干点什么。 + void recvCommand1 ( const UserNode2Message& msg ) { + //what do you want to do? + } + void recvCommand2 ( const UserNode2Message& msg ) { + //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; + + UserNode2Message 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; + } +}; + +//业务层初始化一下这个实例,总是从这里获取协议句柄进行对外读写。 +UserNode2Protocol* UserNode2ConnectionInstance ( QObject* parent = 0 ); + +#endif // USERNODE2PROTOCOL_H diff --git a/examples/qqtnetworkexample/usernode3protocol.cpp b/examples/qqtnetworkexample/usernode3protocol.cpp new file mode 100644 index 00000000..e63afc7b --- /dev/null +++ b/examples/qqtnetworkexample/usernode3protocol.cpp @@ -0,0 +1,27 @@ +#include "usernode3protocol.h" + +QDebug& operator << ( QDebug& dbg, const UserNode3Message& msg ) +{ + //这里打印一下,报文里面到底有什么信息, + //一般到这里的,都是被解析好的message。 + + dbg.nospace() << "{" << hex << msg.size() << "}"; + return dbg.space(); +} + +UserNode3Protocol* UserNode3ConnectionInstance ( QObject* parent ) +{ + static UserNode3Protocol* p0 = NULL; + static QQtSocketTcpServer* s0 = NULL; + if ( !p0 && !s0 ) + { + p0 = new UserNode3Protocol ( parent ); + + s0 = new QQtSocketTcpServer ( parent ); + s0->installProtocol ( p0 ); + s0->listen ( QHostAddress::Any, 8000 ); + + } + + return p0; +} diff --git a/examples/qqtnetworkexample/usernode3protocol.h b/examples/qqtnetworkexample/usernode3protocol.h new file mode 100644 index 00000000..29b1e4d8 --- /dev/null +++ b/examples/qqtnetworkexample/usernode3protocol.h @@ -0,0 +1,127 @@ +#ifndef USERNODE3PROTOCOL_H +#define USERNODE3PROTOCOL_H + +#include +#include +#include + +class UserNode3Message : public QQtMessage +{ + Q_OBJECT +public: + explicit UserNode3Message ( QObject* parent = nullptr ) { + mSize = 0x03;//报文定长 + } + ~UserNode3Message() { + + } + + 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&, const UserNode3Message& msg ); + + +//业务层总是用这个协议工作,读来到的,写出去的。 +class UserNode3Protocol : public QQtProtocol +{ + Q_OBJECT +public: + explicit UserNode3Protocol ( QObject* parent = nullptr ) { + + } + ~UserNode3Protocol() { + + } + + //收到外部发来的很多命令,处理一下告诉业务层干点什么。 + void recvCommand1 ( const UserNode3Message& msg ) { + //what do you want to do? + } + void recvCommand2 ( const UserNode3Message& msg ) { + //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; + + UserNode3Message 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; + } +}; + +//业务层初始化一下这个实例,总是从这里获取协议句柄进行对外读写。 +UserNode3Protocol* UserNode3ConnectionInstance ( QObject* parent = 0 ); + +#endif // USERNODE3PROTOCOL_H diff --git a/examples/qqtnetworkexample/usernode4protocol.cpp b/examples/qqtnetworkexample/usernode4protocol.cpp new file mode 100644 index 00000000..eac16727 --- /dev/null +++ b/examples/qqtnetworkexample/usernode4protocol.cpp @@ -0,0 +1,27 @@ +#include "usernode4protocol.h" + +QDebug& operator << ( QDebug& dbg, const UserNode4Message& msg ) +{ + //这里打印一下,报文里面到底有什么信息, + //一般到这里的,都是被解析好的message。 + + dbg.nospace() << "{" << hex << msg.size() << "}"; + return dbg.space(); +} + +UserNode4Protocol* UserNode4ConnectionInstance ( QObject* parent ) +{ + static UserNode4Protocol* p0 = NULL; + static QQtSocketTcpServer* s0 = NULL; + if ( !p0 && !s0 ) + { + p0 = new UserNode4Protocol ( parent ); + + s0 = new QQtSocketTcpServer ( parent ); + s0->installProtocol ( p0 ); + s0->listen ( QHostAddress::Any, 8000 ); + + } + + return p0; +} diff --git a/examples/qqtnetworkexample/usernode4protocol.h b/examples/qqtnetworkexample/usernode4protocol.h new file mode 100644 index 00000000..e6ab881b --- /dev/null +++ b/examples/qqtnetworkexample/usernode4protocol.h @@ -0,0 +1,127 @@ +#ifndef USERNODE4PROTOCOL_H +#define USERNODE4PROTOCOL_H + +#include +#include +#include + +class UserNode4Message : public QQtMessage +{ + Q_OBJECT +public: + explicit UserNode4Message ( QObject* parent = nullptr ) { + mSize = 0x03;//报文定长 + } + ~UserNode4Message() { + + } + + 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&, const UserNode4Message& msg ); + + +//业务层总是用这个协议工作,读来到的,写出去的。 +class UserNode4Protocol : public QQtProtocol +{ + Q_OBJECT +public: + explicit UserNode4Protocol ( QObject* parent = nullptr ) { + + } + ~UserNode4Protocol() { + + } + + //收到外部发来的很多命令,处理一下告诉业务层干点什么。 + void recvCommand1 ( const UserNode4Message& msg ) { + //what do you want to do? + } + void recvCommand2 ( const UserNode4Message& msg ) { + //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; + + UserNode4Message 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; + } +}; + +//业务层初始化一下这个实例,总是从这里获取协议句柄进行对外读写。 +UserNode4Protocol* UserNode4ConnectionInstance ( QObject* parent = 0 ); + +#endif // USERNODE4PROTOCOL_H diff --git a/examples/qqtnetworkexample/usernode5protocol.cpp b/examples/qqtnetworkexample/usernode5protocol.cpp new file mode 100644 index 00000000..7212c3eb --- /dev/null +++ b/examples/qqtnetworkexample/usernode5protocol.cpp @@ -0,0 +1,27 @@ +#include "usernode5protocol.h" + +QDebug& operator << ( QDebug& dbg, const UserNode5Message& msg ) +{ + //这里打印一下,报文里面到底有什么信息, + //一般到这里的,都是被解析好的message。 + + dbg.nospace() << "{" << hex << msg.size() << "}"; + return dbg.space(); +} + +UserNode5Protocol* UserNode5ConnectionInstance ( QObject* parent ) +{ + static UserNode5Protocol* p0 = NULL; + static QQtSocketTcpServer* s0 = NULL; + if ( !p0 && !s0 ) + { + p0 = new UserNode5Protocol ( parent ); + + s0 = new QQtSocketTcpServer ( parent ); + s0->installProtocol ( p0 ); + s0->listen ( QHostAddress::Any, 8000 ); + + } + + return p0; +} diff --git a/examples/qqtnetworkexample/usernode5protocol.h b/examples/qqtnetworkexample/usernode5protocol.h new file mode 100644 index 00000000..003738cf --- /dev/null +++ b/examples/qqtnetworkexample/usernode5protocol.h @@ -0,0 +1,127 @@ +#ifndef USERNODE5PROTOCOL_H +#define USERNODE5PROTOCOL_H + +#include +#include +#include + +class UserNode5Message : public QQtMessage +{ + Q_OBJECT +public: + explicit UserNode5Message ( QObject* parent = nullptr ) { + mSize = 0x03;//报文定长 + } + ~UserNode5Message() { + + } + + 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&, const UserNode5Message& msg ); + + +//业务层总是用这个协议工作,读来到的,写出去的。 +class UserNode5Protocol : public QQtProtocol +{ + Q_OBJECT +public: + explicit UserNode5Protocol ( QObject* parent = nullptr ) { + + } + ~UserNode5Protocol() { + + } + + //收到外部发来的很多命令,处理一下告诉业务层干点什么。 + void recvCommand1 ( const UserNode5Message& msg ) { + //what do you want to do? + } + void recvCommand2 ( const UserNode5Message& msg ) { + //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; + + UserNode5Message 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; + } +}; + +//业务层初始化一下这个实例,总是从这里获取协议句柄进行对外读写。 +UserNode5Protocol* UserNode5ConnectionInstance ( QObject* parent = 0 ); + +#endif // USERNODE5PROTOCOL_H diff --git a/examples/qqtnetworkexample/usernode6protocol.cpp b/examples/qqtnetworkexample/usernode6protocol.cpp new file mode 100644 index 00000000..ae5fedc8 --- /dev/null +++ b/examples/qqtnetworkexample/usernode6protocol.cpp @@ -0,0 +1,27 @@ +#include "usernode6protocol.h" + +QDebug& operator << ( QDebug& dbg, const UserNode6Message& msg ) +{ + //这里打印一下,报文里面到底有什么信息, + //一般到这里的,都是被解析好的message。 + + dbg.nospace() << "{" << hex << msg.size() << "}"; + return dbg.space(); +} + +UserNode6Protocol* UserNode6ConnectionInstance ( QObject* parent ) +{ + static UserNode6Protocol* p0 = NULL; + static QQtSocketTcpServer* s0 = NULL; + if ( !p0 && !s0 ) + { + p0 = new UserNode6Protocol ( parent ); + + s0 = new QQtSocketTcpServer ( parent ); + s0->installProtocol ( p0 ); + s0->listen ( QHostAddress::Any, 8000 ); + + } + + return p0; +} diff --git a/examples/qqtnetworkexample/usernode6protocol.h b/examples/qqtnetworkexample/usernode6protocol.h new file mode 100644 index 00000000..5c0ab3ca --- /dev/null +++ b/examples/qqtnetworkexample/usernode6protocol.h @@ -0,0 +1,127 @@ +#ifndef USERNODE6PROTOCOL_H +#define USERNODE6PROTOCOL_H + +#include +#include +#include + +class UserNode6Message : public QQtMessage +{ + Q_OBJECT +public: + explicit UserNode6Message ( QObject* parent = nullptr ) { + mSize = 0x03;//报文定长 + } + ~UserNode6Message() { + + } + + 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&, const UserNode6Message& msg ); + + +//业务层总是用这个协议工作,读来到的,写出去的。 +class UserNode6Protocol : public QQtProtocol +{ + Q_OBJECT +public: + explicit UserNode6Protocol ( QObject* parent = nullptr ) { + + } + ~UserNode6Protocol() { + + } + + //收到外部发来的很多命令,处理一下告诉业务层干点什么。 + void recvCommand1 ( const UserNode6Message& msg ) { + //what do you want to do? + } + void recvCommand2 ( const UserNode6Message& msg ) { + //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; + + UserNode6Message 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; + } +}; + +//业务层初始化一下这个实例,总是从这里获取协议句柄进行对外读写。 +UserNode6Protocol* UserNode6ConnectionInstance ( QObject* parent = 0 ); + +#endif // USERNODE6PROTOCOL_H diff --git a/examples/qqtnetworkexample/usernode7protocol.cpp b/examples/qqtnetworkexample/usernode7protocol.cpp new file mode 100644 index 00000000..c6206b29 --- /dev/null +++ b/examples/qqtnetworkexample/usernode7protocol.cpp @@ -0,0 +1,27 @@ +#include "usernode7protocol.h" + +QDebug& operator << ( QDebug& dbg, const UserNode7Message& msg ) +{ + //这里打印一下,报文里面到底有什么信息, + //一般到这里的,都是被解析好的message。 + + dbg.nospace() << "{" << hex << msg.size() << "}"; + return dbg.space(); +} + +UserNode7Protocol* UserNode7ConnectionInstance ( QObject* parent ) +{ + static UserNode7Protocol* p0 = NULL; + static QQtSocketTcpServer* s0 = NULL; + if ( !p0 && !s0 ) + { + p0 = new UserNode7Protocol ( parent ); + + s0 = new QQtSocketTcpServer ( parent ); + s0->installProtocol ( p0 ); + s0->listen ( QHostAddress::Any, 8000 ); + + } + + return p0; +} diff --git a/examples/qqtnetworkexample/usernode7protocol.h b/examples/qqtnetworkexample/usernode7protocol.h new file mode 100644 index 00000000..95cc0f0c --- /dev/null +++ b/examples/qqtnetworkexample/usernode7protocol.h @@ -0,0 +1,127 @@ +#ifndef USERNODE7PROTOCOL_H +#define USERNODE7PROTOCOL_H + +#include +#include +#include + +class UserNode7Message : public QQtMessage +{ + Q_OBJECT +public: + explicit UserNode7Message ( QObject* parent = nullptr ) { + mSize = 0x03;//报文定长 + } + ~UserNode7Message() { + + } + + 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&, const UserNode7Message& msg ); + + +//业务层总是用这个协议工作,读来到的,写出去的。 +class UserNode7Protocol : public QQtProtocol +{ + Q_OBJECT +public: + explicit UserNode7Protocol ( QObject* parent = nullptr ) { + + } + ~UserNode7Protocol() { + + } + + //收到外部发来的很多命令,处理一下告诉业务层干点什么。 + void recvCommand1 ( const UserNode7Message& msg ) { + //what do you want to do? + } + void recvCommand2 ( const UserNode7Message& msg ) { + //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; + + UserNode7Message 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; + } +}; + +//业务层初始化一下这个实例,总是从这里获取协议句柄进行对外读写。 +UserNode7Protocol* UserNode7ConnectionInstance ( QObject* parent = 0 ); + +#endif // USERNODE7PROTOCOL_H diff --git a/examples/qqtnetworkexample/usernode8protocol.cpp b/examples/qqtnetworkexample/usernode8protocol.cpp new file mode 100644 index 00000000..d3304ddd --- /dev/null +++ b/examples/qqtnetworkexample/usernode8protocol.cpp @@ -0,0 +1,27 @@ +#include "usernode8protocol.h" + +QDebug& operator << ( QDebug& dbg, const UserNode8Message& msg ) +{ + //这里打印一下,报文里面到底有什么信息, + //一般到这里的,都是被解析好的message。 + + dbg.nospace() << "{" << hex << msg.size() << "}"; + return dbg.space(); +} + +UserNode8Protocol* UserNode8ConnectionInstance ( QObject* parent ) +{ + static UserNode8Protocol* p0 = NULL; + static QQtSocketTcpServer* s0 = NULL; + if ( !p0 && !s0 ) + { + p0 = new UserNode8Protocol ( parent ); + + s0 = new QQtSocketTcpServer ( parent ); + s0->installProtocol ( p0 ); + s0->listen ( QHostAddress::Any, 8000 ); + + } + + return p0; +} diff --git a/examples/qqtnetworkexample/usernode8protocol.h b/examples/qqtnetworkexample/usernode8protocol.h new file mode 100644 index 00000000..7d2bd704 --- /dev/null +++ b/examples/qqtnetworkexample/usernode8protocol.h @@ -0,0 +1,127 @@ +#ifndef USERNODE8PROTOCOL_H +#define USERNODE8PROTOCOL_H + +#include +#include +#include + +class UserNode8Message : public QQtMessage +{ + Q_OBJECT +public: + explicit UserNode8Message ( QObject* parent = nullptr ) { + mSize = 0x03;//报文定长 + } + ~UserNode8Message() { + + } + + 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&, const UserNode8Message& msg ); + + +//业务层总是用这个协议工作,读来到的,写出去的。 +class UserNode8Protocol : public QQtProtocol +{ + Q_OBJECT +public: + explicit UserNode8Protocol ( QObject* parent = nullptr ) { + + } + ~UserNode8Protocol() { + + } + + //收到外部发来的很多命令,处理一下告诉业务层干点什么。 + void recvCommand1 ( const UserNode8Message& msg ) { + //what do you want to do? + } + void recvCommand2 ( const UserNode8Message& msg ) { + //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; + + UserNode8Message 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; + } +}; + +//业务层初始化一下这个实例,总是从这里获取协议句柄进行对外读写。 +UserNode8Protocol* UserNode8ConnectionInstance ( QObject* parent = 0 ); + +#endif // USERNODE8PROTOCOL_H diff --git a/examples/qqtnetworkexample/usernode9protocol.cpp b/examples/qqtnetworkexample/usernode9protocol.cpp new file mode 100644 index 00000000..0393c849 --- /dev/null +++ b/examples/qqtnetworkexample/usernode9protocol.cpp @@ -0,0 +1,27 @@ +#include "usernode9protocol.h" + +QDebug& operator << ( QDebug& dbg, const UserNode9Message& msg ) +{ + //这里打印一下,报文里面到底有什么信息, + //一般到这里的,都是被解析好的message。 + + dbg.nospace() << "{" << hex << msg.size() << "}"; + return dbg.space(); +} + +UserNode9Protocol* UserNode9ConnectionInstance ( QObject* parent ) +{ + static UserNode9Protocol* p0 = NULL; + static QQtSocketTcpServer* s0 = NULL; + if ( !p0 && !s0 ) + { + p0 = new UserNode9Protocol ( parent ); + + s0 = new QQtSocketTcpServer ( parent ); + s0->installProtocol ( p0 ); + s0->listen ( QHostAddress::Any, 8000 ); + + } + + return p0; +} diff --git a/examples/qqtnetworkexample/usernode9protocol.h b/examples/qqtnetworkexample/usernode9protocol.h new file mode 100644 index 00000000..2fe0a23e --- /dev/null +++ b/examples/qqtnetworkexample/usernode9protocol.h @@ -0,0 +1,127 @@ +#ifndef USERNODE9PROTOCOL_H +#define USERNODE9PROTOCOL_H + +#include +#include +#include + +class UserNode9Message : public QQtMessage +{ + Q_OBJECT +public: + explicit UserNode9Message ( QObject* parent = nullptr ) { + mSize = 0x03;//报文定长 + } + ~UserNode9Message() { + + } + + 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&, const UserNode9Message& msg ); + + +//业务层总是用这个协议工作,读来到的,写出去的。 +class UserNode9Protocol : public QQtProtocol +{ + Q_OBJECT +public: + explicit UserNode9Protocol ( QObject* parent = nullptr ) { + + } + ~UserNode9Protocol() { + + } + + //收到外部发来的很多命令,处理一下告诉业务层干点什么。 + void recvCommand1 ( const UserNode9Message& msg ) { + //what do you want to do? + } + void recvCommand2 ( const UserNode9Message& msg ) { + //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; + + UserNode9Message 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; + } +}; + +//业务层初始化一下这个实例,总是从这里获取协议句柄进行对外读写。 +UserNode9Protocol* UserNode9ConnectionInstance ( QObject* parent = 0 ); + +#endif // USERNODE9PROTOCOL_H diff --git a/examples/qqtnetworkexample/usertest0protocol.cpp b/examples/qqtnetworkexample/usertest0protocol.cpp new file mode 100644 index 00000000..19c4b9cd --- /dev/null +++ b/examples/qqtnetworkexample/usertest0protocol.cpp @@ -0,0 +1,27 @@ +#include "usertest0protocol.h" + +QDebug& operator << ( QDebug& dbg, const UserTest0Message& msg ) +{ + //这里打印一下,报文里面到底有什么信息, + //一般到这里的,都是被解析好的message。 + + dbg.nospace() << "{" << hex << msg.size() << "}"; + return dbg.space(); +} + +UserTest0Protocol* UserTest0ConnectionInstance ( QObject* parent ) +{ + static UserTest0Protocol* p0 = NULL; + static QQtSocketTcpServer* s0 = NULL; + if ( !p0 && !s0 ) + { + p0 = new UserTest0Protocol ( parent ); + + s0 = new QQtSocketTcpServer ( parent ); + s0->installProtocol ( p0 ); + s0->listen ( QHostAddress::Any, 8000 ); + + } + + return p0; +} diff --git a/examples/qqtnetworkexample/usertest0protocol.h b/examples/qqtnetworkexample/usertest0protocol.h new file mode 100644 index 00000000..a6cb5402 --- /dev/null +++ b/examples/qqtnetworkexample/usertest0protocol.h @@ -0,0 +1,127 @@ +#ifndef USERTEST0PROTOCOL_H +#define USERTEST0PROTOCOL_H + +#include +#include +#include + +class UserTest0Message : public QQtMessage +{ + Q_OBJECT +public: + explicit UserTest0Message ( QObject* parent = nullptr ) { + mSize = 0x03;//报文定长 + } + ~UserTest0Message() { + + } + + 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&, const UserTest0Message& msg ); + + +//业务层总是用这个协议工作,读来到的,写出去的。 +class UserTest0Protocol : public QQtProtocol +{ + Q_OBJECT +public: + explicit UserTest0Protocol ( QObject* parent = nullptr ) { + + } + ~UserTest0Protocol() { + + } + + //收到外部发来的很多命令,处理一下告诉业务层干点什么。 + void recvCommand1 ( const UserTest0Message& msg ) { + //what do you want to do? + } + void recvCommand2 ( const UserTest0Message& msg ) { + //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; + + UserTest0Message 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; + } +}; + +//业务层初始化一下这个实例,总是从这里获取协议句柄进行对外读写。 +UserTest0Protocol* UserTest0ConnectionInstance ( QObject* parent = 0 ); + +#endif // USERTEST0PROTOCOL_H diff --git a/examples/qqtnetworkexample/usertest1protocol.cpp b/examples/qqtnetworkexample/usertest1protocol.cpp new file mode 100644 index 00000000..64aa264e --- /dev/null +++ b/examples/qqtnetworkexample/usertest1protocol.cpp @@ -0,0 +1,27 @@ +#include "usertest1protocol.h" + +QDebug& operator << ( QDebug& dbg, const UserTest1Message& msg ) +{ + //这里打印一下,报文里面到底有什么信息, + //一般到这里的,都是被解析好的message。 + + dbg.nospace() << "{" << hex << msg.size() << "}"; + return dbg.space(); +} + +UserTest1Protocol* UserTest1ConnectionInstance ( QObject* parent ) +{ + static UserTest1Protocol* p0 = NULL; + static QQtSocketTcpServer* s0 = NULL; + if ( !p0 && !s0 ) + { + p0 = new UserTest1Protocol ( parent ); + + s0 = new QQtSocketTcpServer ( parent ); + s0->installProtocol ( p0 ); + s0->listen ( QHostAddress::Any, 8000 ); + + } + + return p0; +} diff --git a/examples/qqtnetworkexample/usertest1protocol.h b/examples/qqtnetworkexample/usertest1protocol.h new file mode 100644 index 00000000..aed36593 --- /dev/null +++ b/examples/qqtnetworkexample/usertest1protocol.h @@ -0,0 +1,127 @@ +#ifndef USERTEST1PROTOCOL_H +#define USERTEST1PROTOCOL_H + +#include +#include +#include + +class UserTest1Message : public QQtMessage +{ + Q_OBJECT +public: + explicit UserTest1Message ( QObject* parent = nullptr ) { + mSize = 0x03;//报文定长 + } + ~UserTest1Message() { + + } + + 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&, const UserTest1Message& msg ); + + +//业务层总是用这个协议工作,读来到的,写出去的。 +class UserTest1Protocol : public QQtProtocol +{ + Q_OBJECT +public: + explicit UserTest1Protocol ( QObject* parent = nullptr ) { + + } + ~UserTest1Protocol() { + + } + + //收到外部发来的很多命令,处理一下告诉业务层干点什么。 + void recvCommand1 ( const UserTest1Message& msg ) { + //what do you want to do? + } + void recvCommand2 ( const UserTest1Message& msg ) { + //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; + + UserTest1Message 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; + } +}; + +//业务层初始化一下这个实例,总是从这里获取协议句柄进行对外读写。 +UserTest1Protocol* UserTest1ConnectionInstance ( QObject* parent = 0 ); + +#endif // USERTEST1PROTOCOL_H diff --git a/examples/qqtnetworkexample/usertest2protocol.cpp b/examples/qqtnetworkexample/usertest2protocol.cpp new file mode 100644 index 00000000..172505e2 --- /dev/null +++ b/examples/qqtnetworkexample/usertest2protocol.cpp @@ -0,0 +1,27 @@ +#include "usertest2protocol.h" + +QDebug& operator << ( QDebug& dbg, const UserTest2Message& msg ) +{ + //这里打印一下,报文里面到底有什么信息, + //一般到这里的,都是被解析好的message。 + + dbg.nospace() << "{" << hex << msg.size() << "}"; + return dbg.space(); +} + +UserTest2Protocol* UserTest2ConnectionInstance ( QObject* parent ) +{ + static UserTest2Protocol* p0 = NULL; + static QQtSocketTcpServer* s0 = NULL; + if ( !p0 && !s0 ) + { + p0 = new UserTest2Protocol ( parent ); + + s0 = new QQtSocketTcpServer ( parent ); + s0->installProtocol ( p0 ); + s0->listen ( QHostAddress::Any, 8000 ); + + } + + return p0; +} diff --git a/examples/qqtnetworkexample/usertest2protocol.h b/examples/qqtnetworkexample/usertest2protocol.h new file mode 100644 index 00000000..31ae29f6 --- /dev/null +++ b/examples/qqtnetworkexample/usertest2protocol.h @@ -0,0 +1,127 @@ +#ifndef USERTEST2PROTOCOL_H +#define USERTEST2PROTOCOL_H + +#include +#include +#include + +class UserTest2Message : public QQtMessage +{ + Q_OBJECT +public: + explicit UserTest2Message ( QObject* parent = nullptr ) { + mSize = 0x03;//报文定长 + } + ~UserTest2Message() { + + } + + 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&, const UserTest2Message& msg ); + + +//业务层总是用这个协议工作,读来到的,写出去的。 +class UserTest2Protocol : public QQtProtocol +{ + Q_OBJECT +public: + explicit UserTest2Protocol ( QObject* parent = nullptr ) { + + } + ~UserTest2Protocol() { + + } + + //收到外部发来的很多命令,处理一下告诉业务层干点什么。 + void recvCommand1 ( const UserTest2Message& msg ) { + //what do you want to do? + } + void recvCommand2 ( const UserTest2Message& msg ) { + //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; + + UserTest2Message 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; + } +}; + +//业务层初始化一下这个实例,总是从这里获取协议句柄进行对外读写。 +UserTest2Protocol* UserTest2ConnectionInstance ( QObject* parent = 0 ); + +#endif // USERTEST2PROTOCOL_H diff --git a/examples/qqtnetworkexample/usertest3protocol.cpp b/examples/qqtnetworkexample/usertest3protocol.cpp new file mode 100644 index 00000000..91040541 --- /dev/null +++ b/examples/qqtnetworkexample/usertest3protocol.cpp @@ -0,0 +1,27 @@ +#include "usertest3protocol.h" + +QDebug& operator << ( QDebug& dbg, const UserTest3Message& msg ) +{ + //这里打印一下,报文里面到底有什么信息, + //一般到这里的,都是被解析好的message。 + + dbg.nospace() << "{" << hex << msg.size() << "}"; + return dbg.space(); +} + +UserTest3Protocol* UserTest3ConnectionInstance ( QObject* parent ) +{ + static UserTest3Protocol* p0 = NULL; + static QQtSocketTcpServer* s0 = NULL; + if ( !p0 && !s0 ) + { + p0 = new UserTest3Protocol ( parent ); + + s0 = new QQtSocketTcpServer ( parent ); + s0->installProtocol ( p0 ); + s0->listen ( QHostAddress::Any, 8000 ); + + } + + return p0; +} diff --git a/examples/qqtnetworkexample/usertest3protocol.h b/examples/qqtnetworkexample/usertest3protocol.h new file mode 100644 index 00000000..df3fcbc0 --- /dev/null +++ b/examples/qqtnetworkexample/usertest3protocol.h @@ -0,0 +1,127 @@ +#ifndef USERTEST3PROTOCOL_H +#define USERTEST3PROTOCOL_H + +#include +#include +#include + +class UserTest3Message : public QQtMessage +{ + Q_OBJECT +public: + explicit UserTest3Message ( QObject* parent = nullptr ) { + mSize = 0x03;//报文定长 + } + ~UserTest3Message() { + + } + + 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&, const UserTest3Message& msg ); + + +//业务层总是用这个协议工作,读来到的,写出去的。 +class UserTest3Protocol : public QQtProtocol +{ + Q_OBJECT +public: + explicit UserTest3Protocol ( QObject* parent = nullptr ) { + + } + ~UserTest3Protocol() { + + } + + //收到外部发来的很多命令,处理一下告诉业务层干点什么。 + void recvCommand1 ( const UserTest3Message& msg ) { + //what do you want to do? + } + void recvCommand2 ( const UserTest3Message& msg ) { + //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; + + UserTest3Message 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; + } +}; + +//业务层初始化一下这个实例,总是从这里获取协议句柄进行对外读写。 +UserTest3Protocol* UserTest3ConnectionInstance ( QObject* parent = 0 ); + +#endif // USERTEST3PROTOCOL_H diff --git a/examples/qqtnetworkexample/usertest4protocol.cpp b/examples/qqtnetworkexample/usertest4protocol.cpp new file mode 100644 index 00000000..8aae42b7 --- /dev/null +++ b/examples/qqtnetworkexample/usertest4protocol.cpp @@ -0,0 +1,27 @@ +#include "usertest4protocol.h" + +QDebug& operator << ( QDebug& dbg, const UserTest4Message& msg ) +{ + //这里打印一下,报文里面到底有什么信息, + //一般到这里的,都是被解析好的message。 + + dbg.nospace() << "{" << hex << msg.size() << "}"; + return dbg.space(); +} + +UserTest4Protocol* UserTest4ConnectionInstance ( QObject* parent ) +{ + static UserTest4Protocol* p0 = NULL; + static QQtSocketTcpServer* s0 = NULL; + if ( !p0 && !s0 ) + { + p0 = new UserTest4Protocol ( parent ); + + s0 = new QQtSocketTcpServer ( parent ); + s0->installProtocol ( p0 ); + s0->listen ( QHostAddress::Any, 8000 ); + + } + + return p0; +} diff --git a/examples/qqtnetworkexample/usertest4protocol.h b/examples/qqtnetworkexample/usertest4protocol.h new file mode 100644 index 00000000..e5a246d6 --- /dev/null +++ b/examples/qqtnetworkexample/usertest4protocol.h @@ -0,0 +1,127 @@ +#ifndef USERTEST4PROTOCOL_H +#define USERTEST4PROTOCOL_H + +#include +#include +#include + +class UserTest4Message : public QQtMessage +{ + Q_OBJECT +public: + explicit UserTest4Message ( QObject* parent = nullptr ) { + mSize = 0x03;//报文定长 + } + ~UserTest4Message() { + + } + + 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&, const UserTest4Message& msg ); + + +//业务层总是用这个协议工作,读来到的,写出去的。 +class UserTest4Protocol : public QQtProtocol +{ + Q_OBJECT +public: + explicit UserTest4Protocol ( QObject* parent = nullptr ) { + + } + ~UserTest4Protocol() { + + } + + //收到外部发来的很多命令,处理一下告诉业务层干点什么。 + void recvCommand1 ( const UserTest4Message& msg ) { + //what do you want to do? + } + void recvCommand2 ( const UserTest4Message& msg ) { + //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; + + UserTest4Message 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; + } +}; + +//业务层初始化一下这个实例,总是从这里获取协议句柄进行对外读写。 +UserTest4Protocol* UserTest4ConnectionInstance ( QObject* parent = 0 ); + +#endif // USERTEST4PROTOCOL_H diff --git a/examples/qqtnetworkexample/usertest5protocol.cpp b/examples/qqtnetworkexample/usertest5protocol.cpp new file mode 100644 index 00000000..1da6e042 --- /dev/null +++ b/examples/qqtnetworkexample/usertest5protocol.cpp @@ -0,0 +1,27 @@ +#include "usertest5protocol.h" + +QDebug& operator << ( QDebug& dbg, const UserTest5Message& msg ) +{ + //这里打印一下,报文里面到底有什么信息, + //一般到这里的,都是被解析好的message。 + + dbg.nospace() << "{" << hex << msg.size() << "}"; + return dbg.space(); +} + +UserTest5Protocol* UserTest5ConnectionInstance ( QObject* parent ) +{ + static UserTest5Protocol* p0 = NULL; + static QQtSocketTcpServer* s0 = NULL; + if ( !p0 && !s0 ) + { + p0 = new UserTest5Protocol ( parent ); + + s0 = new QQtSocketTcpServer ( parent ); + s0->installProtocol ( p0 ); + s0->listen ( QHostAddress::Any, 8000 ); + + } + + return p0; +} diff --git a/examples/qqtnetworkexample/usertest5protocol.h b/examples/qqtnetworkexample/usertest5protocol.h new file mode 100644 index 00000000..255313bf --- /dev/null +++ b/examples/qqtnetworkexample/usertest5protocol.h @@ -0,0 +1,127 @@ +#ifndef USERTEST5PROTOCOL_H +#define USERTEST5PROTOCOL_H + +#include +#include +#include + +class UserTest5Message : public QQtMessage +{ + Q_OBJECT +public: + explicit UserTest5Message ( QObject* parent = nullptr ) { + mSize = 0x03;//报文定长 + } + ~UserTest5Message() { + + } + + 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&, const UserTest5Message& msg ); + + +//业务层总是用这个协议工作,读来到的,写出去的。 +class UserTest5Protocol : public QQtProtocol +{ + Q_OBJECT +public: + explicit UserTest5Protocol ( QObject* parent = nullptr ) { + + } + ~UserTest5Protocol() { + + } + + //收到外部发来的很多命令,处理一下告诉业务层干点什么。 + void recvCommand1 ( const UserTest5Message& msg ) { + //what do you want to do? + } + void recvCommand2 ( const UserTest5Message& msg ) { + //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; + + UserTest5Message 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; + } +}; + +//业务层初始化一下这个实例,总是从这里获取协议句柄进行对外读写。 +UserTest5Protocol* UserTest5ConnectionInstance ( QObject* parent = 0 ); + +#endif // USERTEST5PROTOCOL_H diff --git a/examples/qqtnetworkexample/usertest6protocol.cpp b/examples/qqtnetworkexample/usertest6protocol.cpp new file mode 100644 index 00000000..f1851f57 --- /dev/null +++ b/examples/qqtnetworkexample/usertest6protocol.cpp @@ -0,0 +1,27 @@ +#include "usertest6protocol.h" + +QDebug& operator << ( QDebug& dbg, const UserTest6Message& msg ) +{ + //这里打印一下,报文里面到底有什么信息, + //一般到这里的,都是被解析好的message。 + + dbg.nospace() << "{" << hex << msg.size() << "}"; + return dbg.space(); +} + +UserTest6Protocol* UserTest6ConnectionInstance ( QObject* parent ) +{ + static UserTest6Protocol* p0 = NULL; + static QQtSocketTcpServer* s0 = NULL; + if ( !p0 && !s0 ) + { + p0 = new UserTest6Protocol ( parent ); + + s0 = new QQtSocketTcpServer ( parent ); + s0->installProtocol ( p0 ); + s0->listen ( QHostAddress::Any, 8000 ); + + } + + return p0; +} diff --git a/examples/qqtnetworkexample/usertest6protocol.h b/examples/qqtnetworkexample/usertest6protocol.h new file mode 100644 index 00000000..f1d909f3 --- /dev/null +++ b/examples/qqtnetworkexample/usertest6protocol.h @@ -0,0 +1,127 @@ +#ifndef USERTEST6PROTOCOL_H +#define USERTEST6PROTOCOL_H + +#include +#include +#include + +class UserTest6Message : public QQtMessage +{ + Q_OBJECT +public: + explicit UserTest6Message ( QObject* parent = nullptr ) { + mSize = 0x03;//报文定长 + } + ~UserTest6Message() { + + } + + 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&, const UserTest6Message& msg ); + + +//业务层总是用这个协议工作,读来到的,写出去的。 +class UserTest6Protocol : public QQtProtocol +{ + Q_OBJECT +public: + explicit UserTest6Protocol ( QObject* parent = nullptr ) { + + } + ~UserTest6Protocol() { + + } + + //收到外部发来的很多命令,处理一下告诉业务层干点什么。 + void recvCommand1 ( const UserTest6Message& msg ) { + //what do you want to do? + } + void recvCommand2 ( const UserTest6Message& msg ) { + //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; + + UserTest6Message 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; + } +}; + +//业务层初始化一下这个实例,总是从这里获取协议句柄进行对外读写。 +UserTest6Protocol* UserTest6ConnectionInstance ( QObject* parent = 0 ); + +#endif // USERTEST6PROTOCOL_H diff --git a/examples/qqtnetworkexample/usertest7protocol.cpp b/examples/qqtnetworkexample/usertest7protocol.cpp new file mode 100644 index 00000000..5fbfc4b8 --- /dev/null +++ b/examples/qqtnetworkexample/usertest7protocol.cpp @@ -0,0 +1,27 @@ +#include "usertest7protocol.h" + +QDebug& operator << ( QDebug& dbg, const UserTest7Message& msg ) +{ + //这里打印一下,报文里面到底有什么信息, + //一般到这里的,都是被解析好的message。 + + dbg.nospace() << "{" << hex << msg.size() << "}"; + return dbg.space(); +} + +UserTest7Protocol* UserTest7ConnectionInstance ( QObject* parent ) +{ + static UserTest7Protocol* p0 = NULL; + static QQtSocketTcpServer* s0 = NULL; + if ( !p0 && !s0 ) + { + p0 = new UserTest7Protocol ( parent ); + + s0 = new QQtSocketTcpServer ( parent ); + s0->installProtocol ( p0 ); + s0->listen ( QHostAddress::Any, 8000 ); + + } + + return p0; +} diff --git a/examples/qqtnetworkexample/usertest7protocol.h b/examples/qqtnetworkexample/usertest7protocol.h new file mode 100644 index 00000000..9b8aa95d --- /dev/null +++ b/examples/qqtnetworkexample/usertest7protocol.h @@ -0,0 +1,127 @@ +#ifndef USERTEST7PROTOCOL_H +#define USERTEST7PROTOCOL_H + +#include +#include +#include + +class UserTest7Message : public QQtMessage +{ + Q_OBJECT +public: + explicit UserTest7Message ( QObject* parent = nullptr ) { + mSize = 0x03;//报文定长 + } + ~UserTest7Message() { + + } + + 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&, const UserTest7Message& msg ); + + +//业务层总是用这个协议工作,读来到的,写出去的。 +class UserTest7Protocol : public QQtProtocol +{ + Q_OBJECT +public: + explicit UserTest7Protocol ( QObject* parent = nullptr ) { + + } + ~UserTest7Protocol() { + + } + + //收到外部发来的很多命令,处理一下告诉业务层干点什么。 + void recvCommand1 ( const UserTest7Message& msg ) { + //what do you want to do? + } + void recvCommand2 ( const UserTest7Message& msg ) { + //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; + + UserTest7Message 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; + } +}; + +//业务层初始化一下这个实例,总是从这里获取协议句柄进行对外读写。 +UserTest7Protocol* UserTest7ConnectionInstance ( QObject* parent = 0 ); + +#endif // USERTEST7PROTOCOL_H diff --git a/examples/qqtnetworkexample/usertest8protocol.cpp b/examples/qqtnetworkexample/usertest8protocol.cpp new file mode 100644 index 00000000..ab4b30bf --- /dev/null +++ b/examples/qqtnetworkexample/usertest8protocol.cpp @@ -0,0 +1,27 @@ +#include "usertest8protocol.h" + +QDebug& operator << ( QDebug& dbg, const UserTest8Message& msg ) +{ + //这里打印一下,报文里面到底有什么信息, + //一般到这里的,都是被解析好的message。 + + dbg.nospace() << "{" << hex << msg.size() << "}"; + return dbg.space(); +} + +UserTest8Protocol* UserTest8ConnectionInstance ( QObject* parent ) +{ + static UserTest8Protocol* p0 = NULL; + static QQtSocketTcpServer* s0 = NULL; + if ( !p0 && !s0 ) + { + p0 = new UserTest8Protocol ( parent ); + + s0 = new QQtSocketTcpServer ( parent ); + s0->installProtocol ( p0 ); + s0->listen ( QHostAddress::Any, 8000 ); + + } + + return p0; +} diff --git a/examples/qqtnetworkexample/usertest8protocol.h b/examples/qqtnetworkexample/usertest8protocol.h new file mode 100644 index 00000000..33be4d7a --- /dev/null +++ b/examples/qqtnetworkexample/usertest8protocol.h @@ -0,0 +1,127 @@ +#ifndef USERTEST8PROTOCOL_H +#define USERTEST8PROTOCOL_H + +#include +#include +#include + +class UserTest8Message : public QQtMessage +{ + Q_OBJECT +public: + explicit UserTest8Message ( QObject* parent = nullptr ) { + mSize = 0x03;//报文定长 + } + ~UserTest8Message() { + + } + + 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&, const UserTest8Message& msg ); + + +//业务层总是用这个协议工作,读来到的,写出去的。 +class UserTest8Protocol : public QQtProtocol +{ + Q_OBJECT +public: + explicit UserTest8Protocol ( QObject* parent = nullptr ) { + + } + ~UserTest8Protocol() { + + } + + //收到外部发来的很多命令,处理一下告诉业务层干点什么。 + void recvCommand1 ( const UserTest8Message& msg ) { + //what do you want to do? + } + void recvCommand2 ( const UserTest8Message& msg ) { + //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; + + UserTest8Message 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; + } +}; + +//业务层初始化一下这个实例,总是从这里获取协议句柄进行对外读写。 +UserTest8Protocol* UserTest8ConnectionInstance ( QObject* parent = 0 ); + +#endif // USERTEST8PROTOCOL_H diff --git a/examples/qqtnetworkexample/usertest9protocol.cpp b/examples/qqtnetworkexample/usertest9protocol.cpp new file mode 100644 index 00000000..5fd80e3f --- /dev/null +++ b/examples/qqtnetworkexample/usertest9protocol.cpp @@ -0,0 +1,27 @@ +#include "usertest9protocol.h" + +QDebug& operator << ( QDebug& dbg, const UserTest9Message& msg ) +{ + //这里打印一下,报文里面到底有什么信息, + //一般到这里的,都是被解析好的message。 + + dbg.nospace() << "{" << hex << msg.size() << "}"; + return dbg.space(); +} + +UserTest9Protocol* UserTest9ConnectionInstance ( QObject* parent ) +{ + static UserTest9Protocol* p0 = NULL; + static QQtSocketTcpServer* s0 = NULL; + if ( !p0 && !s0 ) + { + p0 = new UserTest9Protocol ( parent ); + + s0 = new QQtSocketTcpServer ( parent ); + s0->installProtocol ( p0 ); + s0->listen ( QHostAddress::Any, 8000 ); + + } + + return p0; +} diff --git a/examples/qqtnetworkexample/usertest9protocol.h b/examples/qqtnetworkexample/usertest9protocol.h new file mode 100644 index 00000000..40b5b8c1 --- /dev/null +++ b/examples/qqtnetworkexample/usertest9protocol.h @@ -0,0 +1,127 @@ +#ifndef USERTEST9PROTOCOL_H +#define USERTEST9PROTOCOL_H + +#include +#include +#include + +class UserTest9Message : public QQtMessage +{ + Q_OBJECT +public: + explicit UserTest9Message ( QObject* parent = nullptr ) { + mSize = 0x03;//报文定长 + } + ~UserTest9Message() { + + } + + 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&, const UserTest9Message& msg ); + + +//业务层总是用这个协议工作,读来到的,写出去的。 +class UserTest9Protocol : public QQtProtocol +{ + Q_OBJECT +public: + explicit UserTest9Protocol ( QObject* parent = nullptr ) { + + } + ~UserTest9Protocol() { + + } + + //收到外部发来的很多命令,处理一下告诉业务层干点什么。 + void recvCommand1 ( const UserTest9Message& msg ) { + //what do you want to do? + } + void recvCommand2 ( const UserTest9Message& msg ) { + //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; + + UserTest9Message 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; + } +}; + +//业务层初始化一下这个实例,总是从这里获取协议句柄进行对外读写。 +UserTest9Protocol* UserTest9ConnectionInstance ( QObject* parent = 0 ); + +#endif // USERTEST9PROTOCOL_H