2018-04-23 20:37:21 +08:00
|
|
|
|
#ifndef QQTSERVERPROTOCOLMANAGER_H
|
2018-04-22 23:00:42 +08:00
|
|
|
|
#define QQTSERVERPROTOCOLMANAGER_H
|
|
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
|
#include <qqtmessage.h>
|
|
|
|
|
#include <qqtprotocol.h>
|
|
|
|
|
#include <qqtprotocolmanager.h>
|
|
|
|
|
#include <qqtcore.h>
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief The QQtServerMessage class
|
|
|
|
|
* 这是协议使用的报文,报文的接收和发送格式不一样。
|
|
|
|
|
*/
|
|
|
|
|
class QQtServerMessage : public QQtMessage
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
|
|
|
|
explicit QQtServerMessage ( QObject* parent = 0 ) {
|
2018-04-23 20:37:21 +08:00
|
|
|
|
bstart = 0xAA;
|
|
|
|
|
bend = 0xBB;
|
2018-04-22 23:00:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-23 10:00:09 +08:00
|
|
|
|
quint16& getASize() {
|
|
|
|
|
return asize;
|
|
|
|
|
}
|
|
|
|
|
const quint16& getASize() const {
|
|
|
|
|
return asize;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-22 23:00:42 +08:00
|
|
|
|
quint8& getACmd() {
|
|
|
|
|
return acmd;
|
|
|
|
|
}
|
2018-04-23 10:00:09 +08:00
|
|
|
|
const quint8& getACmd() const {
|
|
|
|
|
return acmd;
|
|
|
|
|
}
|
2018-04-22 23:00:42 +08:00
|
|
|
|
QByteArray& getAData() {
|
|
|
|
|
return adata;
|
|
|
|
|
}
|
2018-04-23 10:00:09 +08:00
|
|
|
|
const QByteArray& getAData() const {
|
|
|
|
|
return adata;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
quint16& getBSize() {
|
|
|
|
|
return bsize;
|
|
|
|
|
}
|
|
|
|
|
const quint16& getBSize() const {
|
|
|
|
|
return bsize;
|
|
|
|
|
}
|
2018-04-22 23:00:42 +08:00
|
|
|
|
|
|
|
|
|
quint8& getBCmd() {
|
|
|
|
|
return bcmd;
|
|
|
|
|
}
|
2018-04-23 10:00:09 +08:00
|
|
|
|
const quint8& getBCmd() const {
|
|
|
|
|
return bcmd;
|
|
|
|
|
}
|
2018-04-22 23:00:42 +08:00
|
|
|
|
QByteArray& getBData() {
|
|
|
|
|
return bdata;
|
|
|
|
|
}
|
2018-04-23 10:00:09 +08:00
|
|
|
|
const QByteArray& getBData() const {
|
|
|
|
|
return bdata;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void translate() {
|
2018-04-23 20:37:21 +08:00
|
|
|
|
bsize = 1 + 2 + 1 + bdata.size() + 1;
|
2018-04-23 10:00:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-22 23:00:42 +08:00
|
|
|
|
|
|
|
|
|
// QQtMessage interface
|
|
|
|
|
public:
|
|
|
|
|
virtual void parser ( const QByteArray& l0 ) override {
|
|
|
|
|
QByteArray l = l0;
|
|
|
|
|
l >> asize;
|
|
|
|
|
l >> acmd;
|
|
|
|
|
adata.resize ( asize - 2 - 1 );
|
|
|
|
|
l >> adata;
|
|
|
|
|
}
|
|
|
|
|
virtual void packer ( QByteArray& l ) const override {
|
|
|
|
|
l << bstart;
|
|
|
|
|
l << bsize;
|
|
|
|
|
l << bcmd;
|
|
|
|
|
l << bdata;
|
|
|
|
|
l << bend;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
//客户端发过来的。
|
|
|
|
|
quint16 asize;
|
|
|
|
|
quint8 acmd;
|
|
|
|
|
QByteArray adata;
|
|
|
|
|
|
|
|
|
|
//服务器发回去的
|
|
|
|
|
quint8 bstart;
|
|
|
|
|
quint16 bsize;
|
|
|
|
|
quint8 bcmd;
|
|
|
|
|
QByteArray bdata;
|
|
|
|
|
quint8 bend;
|
|
|
|
|
};
|
|
|
|
|
|
2018-04-23 20:37:21 +08:00
|
|
|
|
QDebug& operator << ( QDebug& dbg, const QQtServerMessage& msg );
|
2018-04-23 16:45:54 +08:00
|
|
|
|
|
2018-04-22 23:00:42 +08:00
|
|
|
|
/**
|
2018-04-29 12:36:11 +08:00
|
|
|
|
* @brief The QQtServerProtocol class
|
2018-04-22 23:00:42 +08:00
|
|
|
|
* 这是我定义的服务器和客户端的协议
|
|
|
|
|
* 在这里我实现服务器要干的事情。
|
|
|
|
|
* 具体的:这是我按照服务器和客户端的通信协议,实现的服务器端通信功能。
|
|
|
|
|
*/
|
2018-04-29 12:36:11 +08:00
|
|
|
|
class QQtServerProtocol : public QQtProtocol
|
2018-04-22 23:00:42 +08:00
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
2018-04-29 12:36:11 +08:00
|
|
|
|
explicit QQtServerProtocol ( QObject* parent = nullptr ) : QQtProtocol ( parent ) {
|
2018-04-22 23:00:42 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void recvA1Command ( const QQtServerMessage& msg ) {
|
|
|
|
|
pline() << msg.getAData();
|
|
|
|
|
emit notifyToProtocolManager ( this, &msg );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sendB1Command() {
|
2018-04-23 10:00:09 +08:00
|
|
|
|
QQtServerMessage msg;
|
|
|
|
|
msg.getBCmd() = 0x01;
|
|
|
|
|
msg.getBData() = "Recived, Also hello to you.";
|
|
|
|
|
msg.translate();
|
|
|
|
|
|
2018-04-23 16:45:54 +08:00
|
|
|
|
//pline() << msg.getBSize() << msg.getBCmd() << msg.getBData();
|
|
|
|
|
pline() << msg;
|
2018-04-23 14:44:08 +08:00
|
|
|
|
|
2018-04-23 10:00:09 +08:00
|
|
|
|
QByteArray l;
|
|
|
|
|
msg.packer ( l );
|
|
|
|
|
write ( l );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sendB10Command() {
|
|
|
|
|
QQtServerMessage msg;
|
2018-04-23 14:44:08 +08:00
|
|
|
|
msg.getBCmd() = 0x0A;
|
2018-04-23 10:00:09 +08:00
|
|
|
|
msg.getBData() = "Please, say hello to me.";
|
|
|
|
|
msg.translate();
|
|
|
|
|
|
2018-04-23 16:45:54 +08:00
|
|
|
|
//pline() << msg.getBSize() << msg.getBCmd() << msg.getBData();
|
|
|
|
|
pline() << msg;
|
2018-04-23 14:44:08 +08:00
|
|
|
|
|
2018-04-23 10:00:09 +08:00
|
|
|
|
QByteArray l;
|
|
|
|
|
msg.packer ( l );
|
|
|
|
|
write ( l );
|
2018-04-22 23:00:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// QQtProtocol interface
|
|
|
|
|
protected:
|
|
|
|
|
virtual quint16 minlength() override {
|
2018-04-23 14:44:08 +08:00
|
|
|
|
return 0x02;
|
2018-04-22 23:00:42 +08:00
|
|
|
|
}
|
|
|
|
|
virtual quint16 maxlength() override {
|
|
|
|
|
return 0x07FF;
|
|
|
|
|
}
|
|
|
|
|
virtual quint16 splitter ( const QByteArray& l0 ) override {
|
|
|
|
|
//过来的数据流,按照报文格式,取出size字段。
|
|
|
|
|
QByteArray l = l0.left ( 2 );
|
|
|
|
|
quint16 size;
|
|
|
|
|
l >> size;
|
|
|
|
|
return size;
|
|
|
|
|
}
|
|
|
|
|
virtual bool dispatcher ( const QByteArray& l0 ) override {
|
|
|
|
|
QQtServerMessage msg;
|
|
|
|
|
msg.parser ( l0 );
|
|
|
|
|
//
|
|
|
|
|
switch ( msg.getACmd() ) {
|
|
|
|
|
case 0x01:
|
|
|
|
|
recvA1Command ( msg );
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2018-04-29 12:36:11 +08:00
|
|
|
|
* @brief The QQtServerProtocolManager class
|
2018-04-22 23:00:42 +08:00
|
|
|
|
* ...
|
|
|
|
|
*/
|
2018-04-29 12:36:11 +08:00
|
|
|
|
class QQtServerProtocolManager : public QQtProtocolManager
|
2018-04-22 23:00:42 +08:00
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
2018-04-29 12:36:11 +08:00
|
|
|
|
explicit QQtServerProtocolManager ( QObject* parent = nullptr );
|
2018-04-22 23:00:42 +08:00
|
|
|
|
|
|
|
|
|
signals:
|
|
|
|
|
|
|
|
|
|
public slots:
|
|
|
|
|
};
|
|
|
|
|
|
2018-04-29 12:36:11 +08:00
|
|
|
|
QQtServerProtocolManager* QQtServerInstance ( QObject* parent = 0 );
|
2018-04-29 23:13:40 +08:00
|
|
|
|
QQtServerProtocolManager* QQtServer2Instance ( QObject* parent = 0 );
|
2018-04-22 23:00:42 +08:00
|
|
|
|
|
|
|
|
|
#endif // QQTSERVERPROTOCOLMANAGER_H
|