mirror of
https://gitee.com/drabel/LibQQt.git
synced 2025-01-04 10:18:44 +08:00
添加第二个Server
This commit is contained in:
parent
739ca661c7
commit
98fb218626
@ -48,13 +48,11 @@ SUBDIRS += examples/qqtaudioexample
|
||||
#必开 客户端的 basic
|
||||
SUBDIRS += examples/qqtnetworkexample
|
||||
#这边是个组合项,客户端和服务器一起的。
|
||||
contains(DEFINES, __HIGHGRADE__) {
|
||||
SUBDIRS += examples/qqtclientexample
|
||||
SUBDIRS += demo/QQtClientCreator
|
||||
#服务器的 highgrade
|
||||
SUBDIRS += examples/qqtserverexample
|
||||
SUBDIRS += demo/QQtServerCreator
|
||||
}
|
||||
SUBDIRS += examples/qqtclientexample
|
||||
SUBDIRS += demo/QQtClientCreator
|
||||
#服务器的 highgrade
|
||||
SUBDIRS += examples/qqtserverexample
|
||||
SUBDIRS += demo/QQtServerCreator
|
||||
#greaterThan(QT_VERSION, 4.6.0):SUBDIRS += test/voicetest
|
||||
#mac:lessThan(QT_MAJOR_VERSION , 5):SUBDIRS -= test/voicetest
|
||||
|
||||
|
@ -15,6 +15,9 @@ MainWindow::MainWindow ( QWidget* parent ) :
|
||||
p = QQtClientConnectionInstance ( this );
|
||||
connect ( p, SIGNAL ( signalServerSay ( QString ) ),
|
||||
this, SLOT ( on_recv_say_hello ( QString ) ) );
|
||||
|
||||
QQtClient2Protocol* p0 = QQtClient2ConnectionInstance ( this );
|
||||
p0->sendCommand1();
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow()
|
||||
@ -30,5 +33,5 @@ void MainWindow::on_pushButton_clicked()
|
||||
void MainWindow::on_recv_say_hello ( QString a )
|
||||
{
|
||||
pline() << a;
|
||||
ui->textBrowser->append(a + "\r\n");
|
||||
ui->textBrowser->append ( a + "\r\n" );
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
|
||||
#include <QMainWindow>
|
||||
#include "qqtclientprotocol.h"
|
||||
#include "qqtclient2protocol.h"
|
||||
|
||||
namespace Ui {
|
||||
class MainWindow;
|
||||
|
35
examples/qqtclientexample/qqtclient2protocol.cpp
Normal file
35
examples/qqtclientexample/qqtclient2protocol.cpp
Normal file
@ -0,0 +1,35 @@
|
||||
#include "qqtclient2protocol.h"
|
||||
|
||||
QDebug& operator << ( QDebug& dbg, const QQtClient2Message& msg )
|
||||
{
|
||||
//这里打印一下,报文里面到底有什么信息,
|
||||
//一般到这里的,都是被解析好的message。
|
||||
|
||||
dbg.nospace() << "{" << hex << msg.size() << "}";
|
||||
return dbg.space();
|
||||
}
|
||||
|
||||
QQtClient2Protocol* QQtClient2ConnectionInstance ( QObject* parent )
|
||||
{
|
||||
static QQtClient2Protocol* p0 = NULL;
|
||||
if ( !p0 )
|
||||
{
|
||||
p0 = new QQtClient2Protocol ( parent );
|
||||
}
|
||||
|
||||
static QQtTcpClient* s0 = NULL;
|
||||
if ( !s0 )
|
||||
{
|
||||
s0 = new QQtTcpClient ( parent );
|
||||
s0->installProtocol ( p0 );
|
||||
|
||||
QStringList ip;
|
||||
ip << "192.168.0.100";
|
||||
s0->SetServerIPAddress ( ip );
|
||||
s0->SetServerPort ( 8500 );
|
||||
|
||||
s0->SendConnectMessage();
|
||||
}
|
||||
|
||||
return p0;
|
||||
}
|
144
examples/qqtclientexample/qqtclient2protocol.h
Normal file
144
examples/qqtclientexample/qqtclient2protocol.h
Normal file
@ -0,0 +1,144 @@
|
||||
#ifndef QQTCLIENT2PROTOCOL_H
|
||||
#define QQTCLIENT2PROTOCOL_H
|
||||
|
||||
#include <qqtmessage.h>
|
||||
#include <qqtprotocol.h>
|
||||
#include <qqttcpclient.h>
|
||||
|
||||
class QQtClient2Message : public QQtMessage
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit QQtClient2Message ( QObject* parent = nullptr ) {
|
||||
|
||||
}
|
||||
~QQtClient2Message() {
|
||||
|
||||
}
|
||||
|
||||
quint16& size() { return mSize; }
|
||||
const quint16& size() const { return mSize; }
|
||||
quint8& cmd() { return mCmd; }
|
||||
const quint8& cmd() const { return mCmd; }
|
||||
QByteArray& data() { return mData; }
|
||||
const QByteArray& data() const { return mData; }
|
||||
|
||||
void translate() {
|
||||
mSize = 3 + mData.size();
|
||||
}
|
||||
|
||||
private:
|
||||
//格式
|
||||
//|quint8 size|quint8 cmd|quint8 data|
|
||||
quint16 mSize;
|
||||
quint8 mCmd;
|
||||
QByteArray mData;
|
||||
|
||||
// QQtMessage interface
|
||||
public:
|
||||
//把报文这条流解析出字段
|
||||
virtual void parser ( const QByteArray& l ) override {
|
||||
QByteArray _l = l;
|
||||
_l >> mSize;
|
||||
_l >> mCmd;
|
||||
mData.resize ( mSize - 3 );
|
||||
_l >> mData;
|
||||
}
|
||||
//把报文字段组装成流
|
||||
virtual void packer ( QByteArray& l ) const override {
|
||||
l << mSize;
|
||||
l << mCmd;
|
||||
l << mData;
|
||||
}
|
||||
};
|
||||
|
||||
QDebug& operator << ( QDebug&, const QQtClient2Message& msg );
|
||||
|
||||
|
||||
//业务层总是用这个协议工作,读来到的,写出去的。
|
||||
class QQtClient2Protocol : public QQtProtocol
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit QQtClient2Protocol ( QObject* parent = nullptr ) {
|
||||
|
||||
}
|
||||
~QQtClient2Protocol() {
|
||||
|
||||
}
|
||||
|
||||
//收到外部发来的很多命令,处理一下告诉业务层干点什么。
|
||||
void recvCommand1 ( const QQtClient2Message& msg ) {
|
||||
//what do you want to do?
|
||||
pline() << msg;
|
||||
}
|
||||
void recvCommand2 ( const QQtClient2Message& msg ) {
|
||||
//what do you want to do?
|
||||
}
|
||||
|
||||
void sendCommand1() {
|
||||
QQtClient2Message msg;
|
||||
msg.cmd() = 0x0a;
|
||||
msg.data() = "EEEE";
|
||||
msg.translate();
|
||||
|
||||
QByteArray l;
|
||||
msg.packer ( l );
|
||||
write ( l );
|
||||
}
|
||||
|
||||
signals:
|
||||
//给业务层发的信号
|
||||
void signalSendtoLogicLevelCode();
|
||||
|
||||
public slots:
|
||||
|
||||
// QQtProtocol interface
|
||||
protected:
|
||||
//报文的最小长度
|
||||
virtual quint16 minlength() override {
|
||||
return 0x03;
|
||||
}
|
||||
//报文的最大长度
|
||||
virtual quint16 maxlength() override {
|
||||
return 0x07FF;
|
||||
}
|
||||
//报文现在在流里,第一个字节,就是size,读出来,通过返回值告诉QQtProtocol
|
||||
virtual quint16 splitter ( const QByteArray& l ) override { //stream
|
||||
QByteArray s0 = l.left ( 2 );
|
||||
quint16 size = 0;
|
||||
s0 >> size;
|
||||
return size;
|
||||
}
|
||||
|
||||
//报文现在被切开,发了进来,第二个字节是cmd,解析出来,在函数里处理处理数据,告诉业务层,拿到数据了干点什么。
|
||||
virtual bool dispatcher ( const QByteArray& m ) override { //message
|
||||
bool ret = true;
|
||||
|
||||
QQtClient2Message 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;
|
||||
}
|
||||
};
|
||||
|
||||
//业务层初始化一下这个实例,总是从这里获取协议句柄进行对外读写。
|
||||
QQtClient2Protocol* QQtClient2ConnectionInstance ( QObject* parent = 0 );
|
||||
|
||||
#endif // QQTCLIENT2PROTOCOL_H
|
@ -81,7 +81,8 @@ SOURCES += \
|
||||
qqtnetworkmessage.cpp \
|
||||
qqtserialmessage.cpp \
|
||||
qqtuserserialprotocol.cpp \
|
||||
qqtsubprotocoltest.cpp
|
||||
qqtsubprotocoltest.cpp \
|
||||
qqtclient2protocol.cpp
|
||||
|
||||
|
||||
HEADERS += \
|
||||
@ -91,7 +92,8 @@ HEADERS += \
|
||||
qqtnetworkmessage.h \
|
||||
qqtserialmessage.h \
|
||||
qqtuserserialprotocol.h \
|
||||
qqtsubprotocoltest.h
|
||||
qqtsubprotocoltest.h \
|
||||
qqtclient2protocol.h
|
||||
|
||||
FORMS += \
|
||||
mainwindow.ui
|
||||
|
@ -20,7 +20,7 @@ QQtClientProtocol* QQtClientConnectionInstance ( QObject* parent )
|
||||
s0 = new QQtTcpClient ( parent );
|
||||
s0->installProtocol ( p0 );
|
||||
QStringList ip;
|
||||
ip << "192.168.0.103";
|
||||
ip << "192.168.0.100";
|
||||
s0->SetServerIPAddress ( ip );
|
||||
s0->SetServerPort ( 8001 );
|
||||
|
||||
|
@ -56,7 +56,7 @@ public:
|
||||
}
|
||||
|
||||
void translate() {
|
||||
asize = 2 + 1 + adata.length();
|
||||
asize = 2 + 1 + adata.size();
|
||||
}
|
||||
|
||||
|
||||
|
@ -9,6 +9,8 @@ MainWindow::MainWindow ( QWidget* parent ) :
|
||||
pm0 = QQtServerInstance ( this );
|
||||
connect ( pm0, SIGNAL ( notifyToBusinessLevel ( const QQtProtocol*, const QQtMessage* ) ),
|
||||
this, SLOT ( recvANotify ( const QQtProtocol*, const QQtMessage* ) ) );
|
||||
|
||||
QQtServer2ConnectionInstance ( this );
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow()
|
||||
|
@ -3,6 +3,7 @@
|
||||
|
||||
#include <QMainWindow>
|
||||
#include "qqtserverprotocolmanager.h"
|
||||
#include "qqtserver2protocolmanager.h"
|
||||
|
||||
namespace Ui {
|
||||
class MainWindow;
|
||||
|
38
examples/qqtserverexample/qqtserver2protocolmanager.cpp
Normal file
38
examples/qqtserverexample/qqtserver2protocolmanager.cpp
Normal file
@ -0,0 +1,38 @@
|
||||
#include "qqtserver2protocolmanager.h"
|
||||
|
||||
QDebug& operator << ( QDebug& dbg, const QQtClient2Message& msg )
|
||||
{
|
||||
//这里打印一下,报文里面到底有什么信息,
|
||||
//一般到这里的,都是被解析好的message。
|
||||
|
||||
dbg.nospace() << "{" << hex << msg.size() << "}";
|
||||
return dbg.space();
|
||||
}
|
||||
|
||||
QQtServer2Protocol* QQtServer2ConnectionInstance ( QObject* parent )
|
||||
{
|
||||
static QQtProtocolManager* m0 = 0;
|
||||
if ( !m0 )
|
||||
{
|
||||
//创建Protocol管理者
|
||||
m0 = new QQtProtocolManager ( parent );
|
||||
//注册我实现的Protocol
|
||||
m0->registerProtocol<QQtServer2Protocol> ( "QQtServer2Protocol" );
|
||||
//初始化Protocol管理者完成。
|
||||
}
|
||||
|
||||
static QQtTcpServer* s0 = 0;
|
||||
if ( !s0 )
|
||||
{
|
||||
//新建服务器
|
||||
s0 = new QQtTcpServer ( parent );
|
||||
//安装协议管理者
|
||||
s0->installProtocolManager ( m0 );
|
||||
//开始监听
|
||||
s0->listen ( QHostAddress::Any, 8500 );
|
||||
//服务器初始化完成。
|
||||
}
|
||||
|
||||
//等待客户端发消息过来,Protocol就处理了,去业务层看看。
|
||||
return m0;
|
||||
}
|
147
examples/qqtserverexample/qqtserver2protocolmanager.h
Normal file
147
examples/qqtserverexample/qqtserver2protocolmanager.h
Normal file
@ -0,0 +1,147 @@
|
||||
#ifndef QQTSERVER2PROTOCOLMANAGER_H
|
||||
#define QQTSERVER2PROTOCOLMANAGER_H
|
||||
|
||||
#include <qqtmessage.h>
|
||||
#include <qqtprotocol.h>
|
||||
#include <qqttcpserver.h>
|
||||
|
||||
class QQtClient2Message : public QQtMessage
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit QQtClient2Message ( QObject* parent = nullptr ) {
|
||||
|
||||
}
|
||||
~QQtClient2Message() {
|
||||
|
||||
}
|
||||
|
||||
quint16& size() { return mSize; }
|
||||
const quint16& size() const { return mSize; }
|
||||
quint8& cmd() { return mCmd; }
|
||||
const quint8& cmd() const { return mCmd; }
|
||||
QByteArray& data() { return mData; }
|
||||
const QByteArray& data() const { return mData; }
|
||||
|
||||
void translate() {
|
||||
mSize = 3 + mData.size();
|
||||
}
|
||||
|
||||
private:
|
||||
//格式
|
||||
//|quint8 size|quint8 cmd|quint8 data|
|
||||
quint16 mSize;
|
||||
quint8 mCmd;
|
||||
QByteArray mData;
|
||||
|
||||
// QQtMessage interface
|
||||
public:
|
||||
//把报文这条流解析出字段
|
||||
virtual void parser ( const QByteArray& l ) override {
|
||||
QByteArray _l = l;
|
||||
_l >> mSize;
|
||||
_l >> mCmd;
|
||||
mData.resize ( mSize - 3 );
|
||||
_l >> mData;
|
||||
}
|
||||
//把报文字段组装成流
|
||||
virtual void packer ( QByteArray& l ) const override {
|
||||
l << mSize;
|
||||
l << mCmd;
|
||||
l << mData;
|
||||
}
|
||||
};
|
||||
|
||||
QDebug& operator << ( QDebug&, const QQtClient2Message& msg );
|
||||
|
||||
|
||||
|
||||
|
||||
//业务层总是用这个协议工作,读来到的,写出去的。
|
||||
class QQtServer2Protocol : public QQtProtocol
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit QQtServer2Protocol ( QObject* parent = nullptr ) {
|
||||
|
||||
}
|
||||
~QQtServer2Protocol() {
|
||||
|
||||
}
|
||||
|
||||
//收到外部发来的很多命令,处理一下告诉业务层干点什么。
|
||||
void recvCommand1 ( const QQtClient2Message& msg ) {
|
||||
//what do you want to do?
|
||||
pline() << "recv msg 1" << msg.data();
|
||||
sendCommand1();
|
||||
}
|
||||
void recvCommand2 ( const QQtClient2Message& msg ) {
|
||||
//what do you want to do?
|
||||
}
|
||||
|
||||
void sendCommand1() {
|
||||
QQtClient2Message msg;
|
||||
msg.cmd() = 0x0a;
|
||||
msg.data() = "FFFF";
|
||||
msg.translate();
|
||||
|
||||
QByteArray l;
|
||||
msg.packer ( l );
|
||||
write ( l );
|
||||
}
|
||||
|
||||
signals:
|
||||
//给业务层发的信号
|
||||
void signalSendtoLogicLevelCode();
|
||||
|
||||
public slots:
|
||||
|
||||
// QQtProtocol interface
|
||||
protected:
|
||||
//报文的最小长度
|
||||
virtual quint16 minlength() override {
|
||||
return 0x02;
|
||||
}
|
||||
//报文的最大长度
|
||||
virtual quint16 maxlength() override {
|
||||
return 0x07FF;
|
||||
}
|
||||
//报文现在在流里,第一个字节,就是size,读出来,通过返回值告诉QQtProtocol
|
||||
virtual quint16 splitter ( const QByteArray& l ) override { //stream
|
||||
QByteArray s0 = l.left ( 2 );
|
||||
quint16 size = 0;
|
||||
s0 >> size;
|
||||
return size;
|
||||
}
|
||||
|
||||
//报文现在被切开,发了进来,第二个字节是cmd,解析出来,在函数里处理处理数据,告诉业务层,拿到数据了干点什么。
|
||||
virtual bool dispatcher ( const QByteArray& m ) override { //message
|
||||
bool ret = true;
|
||||
|
||||
QQtClient2Message 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;
|
||||
}
|
||||
};
|
||||
|
||||
//业务层初始化一下这个实例,总是从这里获取协议句柄进行对外读写。
|
||||
QQtServer2Protocol* QQtServer2ConnectionInstance ( QObject* parent = 0 );
|
||||
|
||||
#endif // QQTSERVER2PROTOCOL_H
|
@ -26,11 +26,13 @@ DEFINES += QT_DEPRECATED_WARNINGS
|
||||
SOURCES += \
|
||||
main.cpp \
|
||||
mainwindow.cpp \
|
||||
qqtserverprotocolmanager.cpp
|
||||
qqtserverprotocolmanager.cpp \
|
||||
qqtserver2protocolmanager.cpp
|
||||
|
||||
HEADERS += \
|
||||
mainwindow.h \
|
||||
qqtserverprotocolmanager.h
|
||||
qqtserverprotocolmanager.h \
|
||||
qqtserver2protocolmanager.h
|
||||
|
||||
FORMS += \
|
||||
mainwindow.ui
|
||||
|
Loading…
x
Reference in New Issue
Block a user