mirror of
https://gitee.com/drabel/LibQQt.git
synced 2025-01-04 10:18:44 +08:00
更新QQt Server的例子
This commit is contained in:
parent
55060cab01
commit
9402451aa2
@ -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
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
131
demo/QQtServerCreator/AppRoot/templete/qqtuserserverprotocol.h
Normal file
131
demo/QQtServerCreator/AppRoot/templete/qqtuserserverprotocol.h
Normal 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
|
@ -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)
|
||||
|
@ -1,3 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>MainWindow</class>
|
||||
<widget class="QMainWindow" name="MainWindow">
|
||||
@ -5,20 +6,277 @@
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>400</width>
|
||||
<height>300</height>
|
||||
<width>542</width>
|
||||
<height>324</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>MainWindow</string>
|
||||
</property>
|
||||
<widget class="QMenuBar" name="menuBar" />
|
||||
<widget class="QToolBar" name="mainToolBar" />
|
||||
<widget class="QWidget" name="centralWidget" />
|
||||
<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><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
|
||||
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
|
||||
p, li { white-space: pre-wrap; }
|
||||
</style></head><body style=" font-family:'SimSun'; font-size:8.25pt; font-weight:400; font-style:normal;">
|
||||
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:12pt; font-weight:600;">Message</span></p>
|
||||
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:10pt;">这个是报文的格式,很多协议会使用相同的报文格式。</span></p>
|
||||
<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:10pt;"><br /></p>
|
||||
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:12pt; font-weight:600;">Protocol</span></p>
|
||||
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:10pt;">这是命令集合、功能集合、send、recvXXXMessage全在这里。基本上所有的通信功能在这里实现。注意:客户端和服务端的协议分开的实现。</span></p>
|
||||
<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:10pt;"><br /></p>
|
||||
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:12pt; font-weight:600;">Client</span></p>
|
||||
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:10pt;">这是通信句柄。客户使用这里的protocol句柄进行通信。</span></p>
|
||||
<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:10pt;"><br /></p>
|
||||
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:12pt; font-weight:600;">Server</span></p>
|
||||
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:10pt;">这是通信句柄。客户使用这里的protocolManager句柄进行某些必要通信。这里的server句柄提供客户端列表。</span></p>
|
||||
<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:10pt;"><br /></p></body></html></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>
|
||||
|
@ -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 \
|
||||
|
@ -5,6 +5,8 @@
|
||||
#include <qqtprotocol.h>
|
||||
#include <qqttcpserver.h>
|
||||
|
||||
//这个有难度,不调试了。
|
||||
//C -> S 和 S -> C的报文格式不一样。
|
||||
class QQtClient2Message : public QQtMessage
|
||||
{
|
||||
Q_OBJECT
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
};
|
||||
|
@ -1 +0,0 @@
|
||||
#include "qqtudpserverprotocol.h"
|
@ -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
|
Loading…
x
Reference in New Issue
Block a user