1
0
mirror of https://gitee.com/drabel/LibQQt.git synced 2025-01-04 10:18:44 +08:00

更新QQt Server的例子

This commit is contained in:
tianduanrui 2018-04-29 20:05:46 +08:00
parent 55060cab01
commit 9402451aa2
14 changed files with 562 additions and 188 deletions

View File

@ -45,14 +45,15 @@ SUBDIRS =
#SUBDIRS += examples/tabwidgetexamples
#need QZXing, default closed.
#SUBDIRS += examples/qrcodeexample
#必开 客户端的 basic
#SUBDIRS += examples/qqtnetworkexample
#网络创建工具
SUBDIRS += demo/QQtClientCreator
SUBDIRS += demo/QQtServerCreator
#这边是个组合项,客户端和服务器一起的
SUBDIRS += examples/qqtclientexample
#SUBDIRS += demo/QQtClientCreator
#服务器的 highgrade
SUBDIRS += examples/qqtserverexample
#SUBDIRS += demo/QQtServerCreator
#通信协议的复杂的例子
SUBDIRS += examples/qqtnetworkexample
#greaterThan(QT_VERSION, 4.6.0):SUBDIRS += test/voicetest
#mac:lessThan(QT_MAJOR_VERSION , 5):SUBDIRS -= test/voicetest

View File

@ -9,23 +9,23 @@ QDebug& operator << ( QDebug& dbg, const QQtUserMessage& msg )
return dbg.space();
}
QQtUserProtocol* QQtUserConnectionInstance ( QObject* parent )
QQtTcpClient* QQtUserInstance ( QQtUserProtocol*& protocol, QObject* parent )
{
static QQtUserProtocol* p0 = NULL;
if ( !p0 )
{
p0 = new QQtUserProtocol ( parent );
}
protocol = p0;
static QQtTcpServer* s0 = NULL;
static QQtTcpClient* s0 = NULL;
if ( !s0 )
{
s0 = new QQtTcpServer ( parent );
//... s0->installProtocol ( p0 );
s0->listen ( QHostAddress::Any, 8000 );
s0 = new QQtTcpClient ( parent );
s0->installProtocol ( p0 );
//s0->setServer
//s0->sendConnectToHost();
}
return p0;
return s0;
}

View File

@ -3,7 +3,7 @@
#include <qqtmessage.h>
#include <qqtprotocol.h>
#include <qqttcpserver.h>
#include <qqttcpclient.h>
class QQtUserMessage : public QQtMessage
{
@ -69,6 +69,9 @@ public:
void recvCommand2 ( const QQtUserMessage& msg ) {
//what do you want to do?
}
void sendCommand1() {
//what do you want to do?
}
signals:
//给业务层发的信号
@ -122,6 +125,6 @@ protected:
};
//业务层初始化一下这个实例,总是从这里获取协议句柄进行对外读写。
QQtUserProtocol* QQtUserConnectionInstance ( QObject* parent = 0 );
QQtTcpClient* QQtUserInstance ( QQtUserProtocol*& protocol, QObject* parent = 0 );
#endif // QQTUSERPROTOCOL_H

View File

@ -0,0 +1,39 @@
#include "qqtuserserverprotocol.h"
QDebug& operator << ( QDebug& dbg, const QQtUserServerMessage& msg )
{
//这里打印一下,报文里面到底有什么信息,
//一般到这里的都是被解析好的message。
dbg.nospace() << "{" << hex << msg.size() << "}";
return dbg.space();
}
QQtTcpServer* QQtUserServerInstance ( QQtProtocolManager*& protocolManager, QObject* parent )
{
static QQtProtocolManager* m0 = 0;
if ( !m0 )
{
//创建Protocol管理者
m0 = new QQtProtocolManager ( parent );
//注册我实现的Protocol
m0->registerProtocol<QQtUserServerProtocol> ( );
//初始化Protocol管理者完成。
}
protocolManager = m0;
static QQtTcpServer* s0 = 0;
if ( !s0 )
{
//新建服务器
s0 = new QQtTcpServer ( parent );
//安装协议管理者
s0->installProtocolManager ( m0 );
//开始监听
s0->listen ( QHostAddress::Any, 8001 );
//服务器初始化完成。
}
//等待客户端发消息过来Protocol就处理了去业务层看看。
return s0;
}

View File

@ -0,0 +1,131 @@
#ifndef QQTUSERSERVERPROTOCOL_H
#define QQTUSERSERVERPROTOCOL_H
#include <qqtmessage.h>
#include <qqtprotocol.h>
#include <qqtprotocolmanager.h>
#include <qqttcpserver.h>
class QQtUserServerMessage : public QQtMessage
{
Q_OBJECT
public:
explicit QQtUserServerMessage ( QObject* parent = nullptr ) {
mSize = 0x03;//报文定长
}
~QQtUserServerMessage() {
}
quint8& size() { return mSize; }
const quint8& size() const { return mSize; }
quint8& cmd() { return mCmd; }
const quint8& cmd() const { return mCmd; }
quint8& data() { return mData; }
const quint8& data() const { return mData; }
private:
//格式
//|quint8 size|quint8 cmd|quint8 data|
quint8 mSize;
quint8 mCmd;
quint8 mData;
// QQtMessage interface
public:
//把报文这条流解析出字段
virtual void parser ( const QByteArray& l ) override {
QByteArray _l = l;
_l >> mSize;
_l >> mCmd;
_l >> mData;
}
//把报文字段组装成流
virtual void packer ( QByteArray& l ) const override {
l << mSize;
l << mCmd;
l << mData;
}
};
QDebug& operator << ( QDebug& dbg, const QQtUserServerMessage& msg );
//业务层总是用这个协议工作,读来到的,写出去的。
class QQtUserServerProtocol : public QQtProtocol
{
Q_OBJECT
public:
explicit QQtUserServerProtocol ( QObject* parent = nullptr ) {
}
~QQtUserServerProtocol() {
}
//收到外部发来的很多命令,处理一下告诉业务层干点什么。
void recvCommand1 ( const QQtUserServerMessage& msg ) {
//what do you want to do?
}
void recvCommand2 ( const QQtUserServerMessage& msg ) {
//what do you want to do?
}
void sendCommand1() {
//what do you want to do?
}
signals:
//给业务层发的信号
void signalSendtoLogicLevelCode();
public slots:
// QQtProtocol interface
protected:
//报文的最小长度
virtual quint16 minlength() override {
return 0x0a;
}
//报文的最大长度
virtual quint16 maxlength() override {
return 0x07FF;
}
//报文现在在流里第一个字节就是size读出来通过返回值告诉QQtProtocol
virtual quint16 splitter ( const QByteArray& l ) override { //stream
QByteArray s0 = l.left ( 1 );
quint8 size = 0;
s0 >> size;
return size;
}
//报文现在被切开发了进来第二个字节是cmd解析出来在函数里处理处理数据告诉业务层拿到数据了干点什么。
virtual bool dispatcher ( const QByteArray& m ) override { //message
bool ret = true;
QQtUserServerMessage qMsg;
qMsg.parser ( m );
pline() << qMsg;
switch ( qMsg.cmd() ) {
case 0x0a://protocol command 1
recvCommand1 ( qMsg );
break;
case 0x0b://protocol command 2
recvCommand2 ( qMsg );
break;
default:
ret = false;
pline() << "receive unknown command:" << hex << qMsg.cmd();
break;
}
return ret;
}
};
//用户使用这里的ProtocolManager进行必要的和客户端的通信。一般在QQtUserServerProtocol里面解决。
QQtTcpServer* QQtUserServerInstance ( QQtProtocolManager*& protocolManager, QObject* parent );
#endif // QQTUSERSERVERPROTOCOL_H

View File

@ -22,6 +22,12 @@ DEFINES += QT_DEPRECATED_WARNINGS
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
#server
#instance protocol message
SOURCES += \
AppRoot/templete/qqtuserserverprotocol.cpp
HEADERS += \
AppRoot/templete/qqtuserserverprotocol.h
SOURCES += \
main.cpp \
@ -32,3 +38,13 @@ HEADERS += \
FORMS += \
mainwindow.ui
#促使qqt_deploy_config配置执行没有这个变量不执行
APP_CONFIG_PWD = $${PWD}/AppRoot
equals(QMAKE_HOST.os, Windows) {
APP_CONFIG_PWD ~=s,/,\\,g
}
#促使编译源代码qmake pri配置里面的QMAKE_XX_LINK命令就会执行
system("touch main.cpp")
include(../../src/app_base_manager.pri)

View File

@ -1,24 +1,282 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow" >
<property name="geometry" >
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
<width>542</width>
<height>324</height>
</rect>
</property>
<property name="windowTitle" >
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QMenuBar" name="menuBar" />
<widget class="QToolBar" name="mainToolBar" />
<widget class="QWidget" name="centralWidget" />
<widget class="QStatusBar" name="statusBar" />
<widget class="QWidget" name="centralWidget">
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="1">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>355</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="2">
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>Next</string>
</property>
</widget>
</item>
<item row="1" column="3">
<widget class="QPushButton" name="pushButton_3">
<property name="text">
<string>Generate</string>
</property>
</widget>
</item>
<item row="0" column="0" colspan="4">
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>Message</string>
</attribute>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Name:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="lineEdit">
<property name="text">
<string>QQtUser</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QPushButton" name="pushButton_2">
<property name="text">
<string>Generate</string>
</property>
</widget>
</item>
<item row="2" column="1" colspan="2">
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>427</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="0" colspan="3">
<widget class="QTextBrowser" name="textBrowser"/>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>Protocol</string>
</attribute>
<layout class="QGridLayout" name="gridLayout_5">
<item row="0" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Name:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="lineEdit_4">
<property name="text">
<string>QQtUser</string>
</property>
</widget>
</item>
<item row="1" column="0" colspan="2">
<widget class="QTextBrowser" name="textBrowser_4"/>
</item>
<item row="2" column="0">
<widget class="QPushButton" name="pushButton_6">
<property name="text">
<string>Generate</string>
</property>
</widget>
</item>
<item row="2" column="1">
<spacer name="horizontalSpacer_5">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>427</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_3">
<attribute name="title">
<string>Client</string>
</attribute>
<layout class="QGridLayout" name="gridLayout_4">
<item row="0" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Name:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="lineEdit_3">
<property name="text">
<string>QQtUser</string>
</property>
</widget>
</item>
<item row="1" column="0" colspan="2">
<widget class="QTextBrowser" name="textBrowser_3"/>
</item>
<item row="2" column="0">
<widget class="QPushButton" name="pushButton_5">
<property name="text">
<string>Generate</string>
</property>
</widget>
</item>
<item row="2" column="1">
<spacer name="horizontalSpacer_4">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>427</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_4">
<attribute name="title">
<string>Server</string>
</attribute>
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Name:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="lineEdit_2">
<property name="text">
<string>QQtUser</string>
</property>
</widget>
</item>
<item row="1" column="0" colspan="2">
<widget class="QTextBrowser" name="textBrowser_2"/>
</item>
<item row="2" column="0">
<widget class="QPushButton" name="pushButton_4">
<property name="text">
<string>Generate</string>
</property>
</widget>
</item>
<item row="2" column="1">
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>427</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_5">
<attribute name="title">
<string>Readme</string>
</attribute>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QTextBrowser" name="textBrowser_5">
<property name="html">
<string>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
p, li { white-space: pre-wrap; }
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'SimSun'; font-size:8.25pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:12pt; font-weight:600;&quot;&gt;Message&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:10pt;&quot;&gt;这个是报文的格式,很多协议会使用相同的报文格式。&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:10pt;&quot;&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:12pt; font-weight:600;&quot;&gt;Protocol&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:10pt;&quot;&gt;这是命令集合、功能集合、send、recvXXXMessage全在这里。基本上所有的通信功能在这里实现。注意客户端和服务端的协议分开的实现。&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:10pt;&quot;&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:12pt; font-weight:600;&quot;&gt;Client&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:10pt;&quot;&gt;这是通信句柄。客户使用这里的protocol句柄进行通信。&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:10pt;&quot;&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:12pt; font-weight:600;&quot;&gt;Server&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:10pt;&quot;&gt;这是通信句柄。客户使用这里的protocolManager句柄进行某些必要通信。这里的server句柄提供客户端列表。&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:10pt;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>542</width>
<height>17</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutDefault spacing="6" margin="11" />
<pixmapfunction></pixmapfunction>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>

View File

@ -22,56 +22,10 @@ DEFINES += QT_DEPRECATED_WARNINGS
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES -= \
qqtlanprotocol.cpp \
qqtuserprotocol1.cpp \
qqtuserprotocol2.cpp \
qqtuserprotocol3.cpp \
usernode0protocol.cpp \
usernode1protocol.cpp \
usernode2protocol.cpp \
usernode3protocol.cpp \
usernode4protocol.cpp \
usernode5protocol.cpp \
usernode6protocol.cpp \
usernode7protocol.cpp \
usernode8protocol.cpp \
usernode9protocol.cpp \
usertest0protocol.cpp \
usertest1protocol.cpp \
usertest2protocol.cpp \
usertest3protocol.cpp \
usertest4protocol.cpp \
usertest5protocol.cpp \
usertest6protocol.cpp \
usertest7protocol.cpp \
usertest8protocol.cpp \
usertest9protocol.cpp
qqtlanprotocol.cpp
HEADERS -= \
qqtlanprotocol.h \
qqtuserprotocol1.h \
qqtuserprotocol2.h \
qqtuserprotocol3.h \
usernode0protocol.h \
usernode1protocol.h \
usernode2protocol.h \
usernode3protocol.h \
usernode4protocol.h \
usernode5protocol.h \
usernode6protocol.h \
usernode7protocol.h \
usernode8protocol.h \
usernode9protocol.h \
usertest0protocol.h \
usertest1protocol.h \
usertest2protocol.h \
usertest3protocol.h \
usertest4protocol.h \
usertest5protocol.h \
usertest6protocol.h \
usertest7protocol.h \
usertest8protocol.h \
usertest9protocol.h
qqtlanprotocol.h
SOURCES += \
main.cpp \

View File

@ -5,6 +5,8 @@
#include <qqtprotocol.h>
#include <qqttcpserver.h>
//这个有难度,不调试了。
//C -> S 和 S -> C的报文格式不一样。
class QQtClient2Message : public QQtMessage
{
Q_OBJECT

View File

@ -1,4 +1,32 @@
#ifndef QQTNETWORKDEFINE_H
#define QQTNETWORKDEFINE_H
#include <qqtprotocolmanager.h>
#include <qqtprotocol.h>
#include <qqtmessage.h>
#include <qqttcpclient.h>
#include <qqttcpserver.h>
#include <qqtudpclient.h>
#include <qqtudpserver.h>
#include <qqtserialport.h>
#ifdef __BLUETOOTH__
#include <qqtbluetoothclient.h>
#include <qqtbluetoothserver.h>
#include <qqtbluetoothmanager.h>
#endif
#ifdef __WEBSOCKETSUPPORT__
#include <qqtwebsocketclient.h>
#include <qqtwebsocketserver.h>
#endif
#ifdef __WEBACCESSSUPPORT__
#include <qqtwebaccessmanager.h>
#endif
#endif // QQTNETWORKDEFINE_H

View File

@ -1 +1,41 @@
#include "qqtprotocolmanager.h"
#include <qqtprotocolmanager.h>
QQtProtocolManager::QQtProtocolManager ( QObject* parent ) : QObject ( parent )
{
}
QQtProtocolManager::~QQtProtocolManager()
{
}
QQtProtocol* QQtProtocolManager::createProtocol()
{
if ( m_protocol_list.isEmpty() )
return NULL;
//无论如何,使用对象工厂也一样,都不能正确生成。对象工厂崩溃退出。
//QQtProtocol* p0 = ( QQtProtocol* ) mProtocol->metaObject()->newInstance ( Q_ARG(QQtProtocolManager*, this) );
QQtProtocol* p0 = findDetachedInstance();
if ( p0 == 0 )
return NULL;
pmeta ( p0 ) << p0;
//帮助Protocol给用户发数据。
connect ( p0, SIGNAL ( notifyToProtocolManager ( const QQtProtocol*, const QQtMessage* ) ),
this, SIGNAL ( notifyToBusinessLevel ( const QQtProtocol*, const QQtMessage* ) ) );
return p0;
}
QQtProtocol* QQtProtocolManager::findDetachedInstance()
{
QListIterator<QQtProtocol*> itor ( m_protocol_list );
while ( itor.hasNext() )
{
QQtProtocol* p = itor.next();
if ( p->detached() )
return p;
}
return NULL;
}

View File

@ -26,17 +26,20 @@
* Protocol保存在句柄内部
*
*
* 1024
*
*
*
*
* PM S
* | |
* P - C :: C - P
*/
class QQTSHARED_EXPORT QQtProtocolManager : public QObject
{
Q_OBJECT
public:
explicit QQtProtocolManager ( QObject* parent = 0 ) : QObject ( parent ) {
}
virtual ~QQtProtocolManager() {
}
explicit QQtProtocolManager ( QObject* parent = 0 );
virtual ~QQtProtocolManager();
//获取Protocol列表
//这里列举的函数是给BusinessLevel用的Protocol里面不要用
@ -63,11 +66,12 @@ public:
* ProtocolManager内部生成用户协议实例
* ProtocolManager对象的时候
*
* registerProtocol<QQtXXXProtocol>();
* registerProtocol<QQtXXXProtocol>(1);
* Protocol ObjectName区分Protocol metaObject()->className()
*/
template <typename T>
bool registerProtocol () {
for ( int i = 0; i < 1024; i++ ) {
bool registerProtocol ( int count = 1024 ) {
for ( int i = 0; i < count; i++ ) {
QQtProtocol* p0 = new T ( this );
m_protocol_list.push_back ( p0 );
}
@ -85,33 +89,9 @@ public:
* @param protocolTypeName
* @return
*/
QQtProtocol* createProtocol () {
if ( m_protocol_list.isEmpty() )
return NULL;
//无论如何,使用对象工厂也一样,都不能正确生成。对象工厂崩溃退出。
//QQtProtocol* p0 = ( QQtProtocol* ) mProtocol->metaObject()->newInstance ( Q_ARG(QQtProtocolManager*, this) );
QQtProtocol* p0 = findDetachedInstance();
if ( p0 == 0 )
return NULL;
pmeta ( p0 ) << p0;
//帮助Protocol给用户发数据。
connect ( p0, SIGNAL ( notifyToProtocolManager ( const QQtProtocol*, const QQtMessage* ) ),
this, SIGNAL ( notifyToBusinessLevel ( const QQtProtocol*, const QQtMessage* ) ) );
return p0;
}
QQtProtocol* createProtocol ();
protected:
QQtProtocol* findDetachedInstance() {
QListIterator<QQtProtocol*> itor ( m_protocol_list );
while ( itor.hasNext() ) {
QQtProtocol* p = itor.next();
if ( p->detached() )
return p;
}
return NULL;
}
QQtProtocol* findDetachedInstance();
private:
QList<QQtProtocol*> m_protocol_list;
};

View File

@ -1 +0,0 @@
#include "qqtudpserverprotocol.h"

View File

@ -1,77 +0,0 @@
#ifndef QQTUDPSERVELPROTOCOL_H
#define QQTUDPSERVELPROTOCOL_H
#include <QObject>
#include <qqt-local.h>
#include "qqtmessage.h"
#include "qqtcore.h"
#define QT_VERSION_DATAGRAM QT_VERSION_CHECK(5,8,0)
#if QT_VERSION > QT_VERSION_DATAGRAM
#include <QNetworkDatagram>
#endif
/*
*/
class QQTSHARED_EXPORT QQtUdpServerProtocol : public QObject
{
Q_OBJECT
public:
explicit QQtUdpServerProtocol ( QObject* parent = nullptr ) : QObject ( parent ) {
}
virtual ~QQtUdpServerProtocol() {}
#if QT_VERSION > QT_VERSION_DATAGRAM
qint64 writeDatagram ( const QNetworkDatagram& datagram ) {
QByteArray dg = datagram.data();
QHostAddress addr = datagram.destinationAddress();
int port = datagram.destinationPort();
emit writeDatagram ( dg, addr, ( quint16 ) port );
}
#endif
signals:
qint64 writeDatagram ( const QByteArray& datagram,
const QHostAddress& host, quint16 port );
public slots:
protected:
/**
* @brief
* @brief need override
* @param
* @return 0 no dispatched(others) 1 dispatched(own)
*/
inline virtual bool dispatcher ( const QByteArray& datagram,
const QHostAddress& host, quint16 port ) {
return 0;
}
/*
* dispatcher
*/
#if QT_VERSION > QT_VERSION_DATAGRAM
inline virtual bool dispatcher ( const QNetworkDatagram& ) { return 0; }
#endif
public:
/**
* @brief
* @brief QQtUdpSocket用的
* @param Qt通讯口readAll()bytes
* @return
*/
void translator ( const QByteArray& datagram,
const QHostAddress& host, quint16 port ) {
dispatcher ( datagram, host, port );
}
#if QT_VERSION > QT_VERSION_DATAGRAM
void translator ( const QNetworkDatagram& datagram ) {
dispatcher ( datagram );
}
#endif
};
#endif // QQTUDPSERVELPROTOCOL_H