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

update QQt server module

This commit is contained in:
tianduanrui 2018-04-23 10:00:09 +08:00
parent 317b19a8ef
commit b29482f4b2
12 changed files with 371 additions and 18 deletions

View File

@ -12,16 +12,20 @@ QDebug& operator << ( QDebug& dbg, const QQtUserMessage& msg )
QQtUserProtocol* QQtUserConnectionInstance ( QObject* parent )
{
static QQtUserProtocol* p0 = NULL;
static QQtTcpServer* s0 = NULL;
if ( !p0 && !s0 )
if ( !p0 )
{
p0 = new QQtUserProtocol ( parent );
s0 = new QQtTcpServer ( parent );
s0->installProtocol ( p0 );
s0->listen ( QHostAddress::Any, 8000 );
}
static QQtTcpServer* s0 = NULL;
if ( !s0 )
{
s0 = new QQtTcpServer ( parent );
//... s0->installProtocol ( p0 );
s0->listen ( QHostAddress::Any, 8000 );
}
return p0;
}

View File

@ -1,6 +1,7 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow ( QWidget* parent ) :
QMainWindow ( parent ),
ui ( new Ui::MainWindow )
@ -11,9 +12,22 @@ MainWindow::MainWindow ( QWidget* parent ) :
// tcpsocket example
// websocket?
//
p = QQtClientConnectionInstance ( this );
connect ( p, SIGNAL ( signalServerSay ( QString ) ),
this, SLOT ( on_recv_say_hello ( QString ) ) );
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
p->sendA1Command ( ui->lineEdit->text() );
}
void MainWindow::on_recv_say_hello ( QString a )
{
ui->plainTextEdit->appendPlainText ( a );
}

View File

@ -2,6 +2,7 @@
#define MAINWINDOW_H
#include <QMainWindow>
#include "qqtclientprotocol.h"
namespace Ui {
class MainWindow;
@ -15,8 +16,13 @@ public:
explicit MainWindow ( QWidget* parent = 0 );
~MainWindow();
private slots:
void on_pushButton_clicked();
void on_recv_say_hello ( QString );
private:
Ui::MainWindow* ui;
QQtClientProtocol* p;
};
#endif // MAINWINDOW_H

View File

@ -6,15 +6,47 @@
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>480</height>
<width>536</width>
<height>307</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QHBoxLayout" name="horizontalLayout"/>
<widget class="QPlainTextEdit" name="plainTextEdit">
<property name="geometry">
<rect>
<x>40</x>
<y>30</y>
<width>251</width>
<height>141</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>350</x>
<y>220</y>
<width>111</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit">
<property name="geometry">
<rect>
<x>90</x>
<y>230</y>
<width>201</width>
<height>21</height>
</rect>
</property>
</widget>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>

View File

@ -0,0 +1,32 @@
#include "qqtclientprotocol.h"
QDebug& operator << ( QDebug& dbg, const QQtClientMessage& msg )
{
//这里打印一下,报文里面到底有什么信息,
//一般到这里的都是被解析好的message。
dbg.nospace() << "{" << hex << msg.getBSize() << "}";
return dbg.space();
}
QQtClientProtocol* QQtClientConnectionInstance ( QObject* parent )
{
static QQtClientProtocol* p0 = NULL;
static QQtTcpClient* s0 = NULL;
if ( !p0 && !s0 )
{
p0 = new QQtClientProtocol ( parent );
s0 = new QQtTcpClient ( parent );
s0->installProtocol ( p0 );
QStringList ip;
ip << "192.168.0.101";
s0->SetServerIPAddress ( ip );
s0->SetServerPort ( 8001 );
s0->SendConnectMessage();
}
return p0;
}

View File

@ -0,0 +1,190 @@
#ifndef QQTCLIENTPROTOCOL_H
#define QQTCLIENTPROTOCOL_H
#include <qqtmessage.h>
#include <qqtprotocol.h>
#include <qqttcpserver.h>
class QQtClientMessage : public QQtMessage
{
Q_OBJECT
public:
explicit QQtClientMessage ( QObject* parent = nullptr ) {
}
~QQtClientMessage() {
}
quint16& getASize() {
return asize;
}
const quint16& getASize() const {
return asize;
}
quint8& getACmd() {
return acmd;
}
const quint8& getACmd() const {
return acmd;
}
QByteArray& getAData() {
return adata;
}
const QByteArray& getAData() const {
return adata;
}
quint16& getBSize() {
return bsize;
}
const quint16& getBSize() const {
return bsize;
}
quint8& getBCmd() {
return bcmd;
}
const quint8& getBCmd() const {
return bcmd;
}
QByteArray& getBData() {
return bdata;
}
const QByteArray& getBData() const {
return bdata;
}
void translate() {
asize = 2 + 1 + adata.length();
}
private:
//发送格式
quint16 asize;
quint8 acmd;
QByteArray adata;
//接收格式
quint8 bstart;
quint16 bsize;
quint8 bcmd;
QByteArray bdata;
quint8 bend;
// QQtMessage interface
public:
//把报文这条流解析出字段
virtual void parser ( const QByteArray& l0 ) override {
QByteArray l = l0;
l >> bstart;
l >> bsize;
l >> bcmd;
bdata.resize ( bsize - 1 - 2 - 1 - 1 );
l >> bdata;
l >> bend;
}
//把报文字段组装成流
virtual void packer ( QByteArray& l ) const override {
l << asize;
l << acmd;
l << adata;
}
};
QDebug& operator << ( QDebug&, const QQtClientMessage& msg );
//业务层总是用这个协议工作,读来到的,写出去的。
class QQtClientProtocol : public QQtProtocol
{
Q_OBJECT
public:
explicit QQtClientProtocol ( QObject* parent = nullptr ) {
}
~QQtClientProtocol() {
}
//收到外部发来的很多命令,处理一下告诉业务层干点什么。
void recvCommand1 ( const QQtClientMessage& msg ) {
//what do you want to do?
pline() << msg.getBData();
emit signalServerSay ( msg.getBData() );
}
void recvCommand2 ( const QQtClientMessage& msg ) {
//what do you want to do?
pline() << msg.getBData();
emit signalServerSay ( msg.getBData() );
}
void sendA1Command ( QString data ) {
QQtClientMessage msg;
msg.getBCmd() = 0x01;
msg.getBData() = data.toLocal8Bit();
msg.translate();
QByteArray l;
msg.packer ( l );
write ( l );
}
signals:
//给业务层发的信号
void signalServerSay ( QString h0 );
public slots:
// QQtProtocol interface
protected:
//报文的最小长度
virtual quint16 minlength() override {
return 0x00;
}
//报文的最大长度
virtual quint16 maxlength() override {
return 0x07FF;
}
virtual quint16 splitter ( const QByteArray& l ) override { //stream
QByteArray s0 = l.left ( 3 );
quint8 start = 0;
quint16 size = 0;
s0 << start;
s0 << size;
return size;
}
//报文现在被切开发了进来第二个字节是cmd解析出来在函数里处理处理数据告诉业务层拿到数据了干点什么。
virtual bool dispatcher ( const QByteArray& m ) override { //message
bool ret = true;
QQtClientMessage qMsg;
qMsg.parser ( m );
pline() << qMsg;
switch ( qMsg.getBCmd() ) {
case 0x01://protocol command 1
recvCommand1 ( qMsg );
break;
case 0x0100://protocol command 2
recvCommand2 ( qMsg );
break;
default:
ret = false;
pline() << "receive unknown command:" << hex << qMsg.getBCmd();
break;
}
return ret;
}
};
//业务层初始化一下这个实例,总是从这里获取协议句柄进行对外读写。
QQtClientProtocol* QQtClientConnectionInstance ( QObject* parent = 0 );
#endif // QQTCLIENTPROTOCOL_H

View File

@ -21,7 +21,8 @@ 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
SOURCES += \
SOURCES -= \
qqtlanprotocol.cpp \
qqtuserprotocol1.cpp \
qqtuserprotocol2.cpp \
qqtuserprotocol3.cpp \
@ -46,7 +47,8 @@ SOURCES += \
usertest8protocol.cpp \
usertest9protocol.cpp
HEADERS += \
HEADERS -= \
qqtlanprotocol.h \
qqtuserprotocol1.h \
qqtuserprotocol2.h \
qqtuserprotocol3.h \
@ -74,8 +76,8 @@ HEADERS += \
SOURCES += \
main.cpp \
mainwindow.cpp \
qqtclientprotocol.cpp \
qqtcloudprotocol.cpp \
qqtlanprotocol.cpp \
qqtnetworkmessage.cpp \
qqtserialmessage.cpp \
qqtuserserialprotocol.cpp \
@ -84,8 +86,8 @@ SOURCES += \
HEADERS += \
mainwindow.h \
qqtclientprotocol.h \
qqtcloudprotocol.h \
qqtlanprotocol.h \
qqtnetworkmessage.h \
qqtserialmessage.h \
qqtuserserialprotocol.h \

View File

@ -7,6 +7,8 @@ MainWindow::MainWindow ( QWidget* parent ) :
{
ui->setupUi ( this );
pm0 = QQtServerInstance ( this );
connect ( pm0, SIGNAL ( notifyToBusinessLevel ( const QQtProtocol*, const QQtMessage* ) ),
this, SLOT ( recvANotify ( const QQtProtocol*, const QQtMessage* ) ) );
}
MainWindow::~MainWindow()
@ -16,5 +18,22 @@ MainWindow::~MainWindow()
void MainWindow::recvANotify ( const QQtProtocol* protocol, const QQtMessage* message )
{
pline() << "";
QQtServerProtocol* p = ( QQtServerProtocol* ) protocol;
QQtServerMessage* m = ( QQtServerMessage* ) message;
pline() << m->getAData();
switch ( m->getBCmd() )
{
case 0x01:
{
if ( m->getAData().contains ( "hello" ) )
p->sendB1Command();
else
p->sendB10Command();
break;
}
default:
break;
}
}

View File

@ -21,19 +21,50 @@ public:
bend = 0x01;
}
quint16& getASize() {
return asize;
}
const quint16& getASize() const {
return asize;
}
quint8& getACmd() {
return acmd;
}
const quint8& getACmd() const {
return acmd;
}
QByteArray& getAData() {
return adata;
}
const QByteArray& getAData() const {
return adata;
}
quint16& getBSize() {
return bsize;
}
const quint16& getBSize() const {
return bsize;
}
quint8& getBCmd() {
return bcmd;
}
const quint8& getBCmd() const {
return bcmd;
}
QByteArray& getBData() {
return bdata;
}
const QByteArray& getBData() const {
return bdata;
}
void translate() {
bsize = 1 + 2 + 1 + bdata.length() + 1;
}
// QQtMessage interface
public:
@ -45,7 +76,6 @@ public:
l >> adata;
}
virtual void packer ( QByteArray& l ) const override {
bsize = 1 + 2 + 1 + bdata.length() + 1;
l << bstart;
l << bsize;
l << bcmd;
@ -87,7 +117,25 @@ public:
}
void sendB1Command() {
//write ( "Recived, Thanks." );
QQtServerMessage msg;
msg.getBCmd() = 0x01;
msg.getBData() = "Recived, Also hello to you.";
msg.translate();
QByteArray l;
msg.packer ( l );
write ( l );
}
void sendB10Command() {
QQtServerMessage msg;
msg.getBCmd() = 0x0100;
msg.getBData() = "Please, say hello to me.";
msg.translate();
QByteArray l;
msg.packer ( l );
write ( l );
}
// QQtProtocol interface

View File

@ -60,7 +60,7 @@ public:
*/
template <typename T>
void registerProtocol ( QString protocolTypeName ) {
pline() << typeid ( T ) << typeid ( T ).name();
//pline() << typeid ( T ) << typeid ( T ).name();
mProtocolName = protocolTypeName;
QQtObjectFactory::registerObject<T>();
}

View File

@ -54,7 +54,7 @@ void QQtTcpClient::installProtocol ( QQtProtocol* stack )
m_protocol = stack;
connect ( m_protocol, SIGNAL ( write ( const QByteArray& ) ),
this, SLOT ( write ( const QByteArray& ) ) );
this, SLOT ( writeData ( const QByteArray& ) ) );
}
void QQtTcpClient::uninstallProtocol ( QQtProtocol* stack )
@ -65,7 +65,7 @@ void QQtTcpClient::uninstallProtocol ( QQtProtocol* stack )
return;
disconnect ( m_protocol, SIGNAL ( write ( const QByteArray& ) ),
this, SLOT ( write ( const QByteArray& ) ) );
this, SLOT ( writeData ( const QByteArray& ) ) );
m_protocol = NULL;
}
@ -222,3 +222,8 @@ void QQtTcpClient::readyReadData()
bytes = readAll();
m_protocol->translator ( bytes );
}
void QQtTcpClient::writeData ( const QByteArray& data )
{
write ( data );
}

View File

@ -61,6 +61,7 @@ private slots:
protected slots:
void readyReadData();
void writeData ( const QByteArray& );
private:
void connectToSingelHost();