tcp client模式功能实现

master
zcy 2021-08-29 16:13:50 +08:00
parent c32ac8c040
commit bd1b6a7117
18 changed files with 521 additions and 76 deletions

View File

@ -64,23 +64,17 @@ Item {
color: "white" color: "white"
textMargin: 4 textMargin: 4
} }
RadioButton { ComboBox {
id: isServer id: conn_type
Layout.leftMargin: 20 Layout.leftMargin: 20
width: 138 Layout.topMargin: 20
height: 41 width: 143
text: qsTr("服务端") height: 40
checked: true
onClicked:{ model:[
if(isServer.checked == true){ "client",
isServer.checked = false "server"
isServer.checkable = false ]
}else{
isServer.checked = true
isServer.checkable = true
}
}
} }
} }
} }
@ -91,9 +85,9 @@ Item {
Layout.leftMargin: 20 Layout.leftMargin: 20
Label { Label {
id: label7 id: label7
text: "协议选择:"
Layout.leftMargin: 20 Layout.leftMargin: 20
Layout.topMargin: 20 Layout.topMargin: 20
text: "协议选择:"
Layout.preferredHeight: 40 Layout.preferredHeight: 40
renderType: Text.QtRendering renderType: Text.QtRendering
Layout.preferredWidth: 125 Layout.preferredWidth: 125
@ -179,16 +173,13 @@ Item {
onClicked: { onClicked: {
if(button_network.text == "打开网络"){ if(button_network.text == "打开网络"){
let type_network = -1 let type_network = -1
if(proto_combox.currentText == "udp" && isServer.checked){ if(proto_combox.currentText == "udp" ){
type_network = 0 type_network = 0
} }
if(proto_combox.currentText == "udp" && !isServer.checked){ if(proto_combox.currentText == "tcp" && (conn_type.currentText == "server")){
type_network = 2
}
if(proto_combox.currentText == "tcp" && isServer.checked){
type_network = 1 type_network = 1
} }
if(proto_combox.currentText == "tcp" && !isServer.checked){ if(proto_combox.currentText == "tcp" && (conn_type.currentText == "client")){
type_network = 3 type_network = 3
} }
console.log(hostEdit.text, console.log(hostEdit.text,

View File

@ -146,9 +146,7 @@ Item {
tip.text = "更新lua脚本成功" tip.text = "更新lua脚本成功"
tip.visible = true tip.visible = true
} }
} }
Rectangle { Rectangle {
Layout.preferredHeight: 41 Layout.preferredHeight: 41
Layout.preferredWidth: input.contentWidth<300 ? 300 : input.contentWidth + 10 Layout.preferredWidth: input.contentWidth<300 ? 300 : input.contentWidth + 10

View File

@ -198,14 +198,14 @@ Item {
dataBits.currentText, dataBits.currentText,
stopBits.currentText, stopBits.currentText,
flow.currentText) flow.currentText)
if(ret == 0){ if(ret === 0){
uart_open = true uart_open = true
button.text = "关闭串口" button.text = "关闭串口"
serial_select_view.notify("串口已经连接") serial_select_view.notify("串口已经连接")
} }
}else{ }else{
let ret = DataWrap.closeSerial(); let ret = DataWrap.closeSerial();
if(ret == 0){ if(ret === 0){
button.text = "打开串口" button.text = "打开串口"
uart_open = false uart_open = false
serial_select_view.notify("串口已经关闭") serial_select_view.notify("串口已经关闭")

View File

@ -0,0 +1 @@
<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1628004582539" class="icon" viewBox="0 0 1054 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="671" xmlns:xlink="http://www.w3.org/1999/xlink" width="205.859375" height="200"><defs><style type="text/css"></style></defs><path d="M1054.293333 72.40404v650.254223H857.315556v79.54101h-117.423839v151.518384h-425.50303V802.19798H196.964848v-79.54101H0V72.40404h1054.293333z m-25.263838 25.250909H23.983838v598.484041H220.948687v80.808081h117.436768v151.518384h377.522424V776.947071h116.156767v-80.808081h196.964849V97.654949zM193.176566 337.55798h47.980606V97.654949H193.176566v239.903031z m88.384646 0h47.980606V97.654949h-47.980606v239.903031z m88.384647 0h47.980606V97.654949h-47.980606v239.903031z m88.384646 0h47.980606V97.654949h-47.980606v239.903031z m89.638788 0h47.979313V97.654949H547.969293v239.903031z m88.384646 0h47.980606V97.654949h-47.980606v239.903031z m88.384647 0h47.979313V97.654949H724.738586v239.903031z m88.384646 0h47.979313V97.654949H813.123232v239.903031z" p-id="672" fill="#ffffff"></path></svg>

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@ -31,7 +31,7 @@ void LuaDelegate::OnSerialData(QString data){
void LuaDelegate::OnNetworkData(char *addr, char *data, uint32_t port) void LuaDelegate::OnNetworkData(char *addr, char *data, uint32_t port)
{ {
int i = lua_getglobal(mVM,"OnNetworkData"); int ret = lua_getglobal(mVM,"OnNetworkData");
lua_pushstring(mVM,addr); lua_pushstring(mVM,addr);
lua_pushstring(mVM,data); lua_pushstring(mVM,data);
lua_pushnumber(mVM,port); lua_pushnumber(mVM,port);

View File

@ -14,13 +14,17 @@
#include "serialcontroller.h" #include "serialcontroller.h"
#include "lua_wraper.h" #include "lua_wraper.h"
#include "mainwindow.h" #include "mainwindow.h"
#include <windows.h>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
AllocConsole();
freopen("CONOUT$","w",stdout);
QApplication app(argc, argv); QApplication app(argc, argv);
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
printf("hello world");
MainWindow w; MainWindow w;
w.setWindowTitle("协议调试器"); w.setWindowTitle("协议调试器");
w.show(); w.show();

View File

@ -21,7 +21,7 @@ Rectangle {
height: parent.height height: parent.height
Rectangle { Rectangle {
width: 100; width: 130;
height: parent.height height: parent.height
color: "black" color: "black"
Flow{ Flow{
@ -32,14 +32,14 @@ Rectangle {
Button{ Button{
id: btn_serial id: btn_serial
width: parent.width width: parent.width
height:60 height:80
onClicked: { onClicked: {
uart_choose.visible = true uart_choose.visible = true
net_choose.visible = false net_choose.visible = false
proto_debug.visible = false proto_debug.visible = false
} }
background: Image { background: Image {
source: btn_serial.hovered? "qrc:///res/proto_hover.svg": "qrc:///res/serial.svg" source: btn_serial.hovered? "qrc:///res/serial1.svg": "qrc:///res/serial.svg"
} }
} }
Text { Text {
@ -47,15 +47,15 @@ Rectangle {
text: qsTr("串口设置") text: qsTr("串口设置")
width: parent.width width: parent.width
color: "white" color: "white"
font.pointSize: 10 font.pointSize: 15
anchors.top: btn_serial.bottom anchors.top: btn_serial.bottom
leftPadding: 13 leftPadding: 15
} }
Button{ Button{
id: button_net id: button_net
width: parent.width width: parent.width
height:60 height:80
anchors.top: label_serial.bottom anchors.top: label_serial.bottom
onClicked: { onClicked: {
@ -63,30 +63,26 @@ Rectangle {
background: Image { background: Image {
source: button_net.hovered? "qrc:///res/nethover.svg":"qrc:///res/net.svg" source: button_net.hovered? "qrc:///res/nethover.svg":"qrc:///res/net.svg"
} }
onPressed:{ onPressed:{
uart_choose.visible = false uart_choose.visible = false
net_choose.visible = true net_choose.visible = true
proto_debug.visible = false proto_debug.visible = false
} }
} }
Text { Text {
id: label_net id: label_net
text: qsTr("网口设置") text: qsTr("网口设置")
width: parent.width width: parent.width
color: "white" color: "white"
font.pointSize: 10 font.pointSize: 15
anchors.top: button_net.bottom anchors.top: button_net.bottom
leftPadding: 13 leftPadding: 15
} }
Button{ Button{
id: button_proto id: button_proto
width: parent.width width: parent.width
height:60 height:80
anchors.top: label_net.bottom anchors.top: label_net.bottom
onClicked: { onClicked: {
uart_choose.visible = false uart_choose.visible = false
@ -95,7 +91,7 @@ Rectangle {
DataWrap.getValFromQml(1) DataWrap.getValFromQml(1)
} }
background: Image { background: Image {
source: "qrc:///res/proto.svg" source: !button_proto.hovered? "qrc:///res/proto.svg":"qrc:///res/proto_hover.svg"
} }
} }
Text { Text {
@ -103,9 +99,9 @@ Rectangle {
text: qsTr("协议调试") text: qsTr("协议调试")
width: parent.width width: parent.width
color: "white" color: "white"
font.pointSize: 10 font.pointSize: 15
anchors.top: button_proto.bottom anchors.top: button_proto.bottom
leftPadding: 13 leftPadding: 15
} }
Button{ Button{
@ -130,9 +126,9 @@ Rectangle {
text: qsTr("tcp集群") text: qsTr("tcp集群")
width: parent.width width: parent.width
color: "white" color: "white"
font.pointSize: 10 font.pointSize: 15
anchors.top: button_tcp_swarm.bottom anchors.top: button_tcp_swarm.bottom
leftPadding: 13 leftPadding: 15
} }
Text { Text {
@ -140,7 +136,7 @@ Rectangle {
text: qsTr("未连接") text: qsTr("未连接")
width: parent.width width: parent.width
color: "white" color: "white"
font.pointSize: 13 font.pointSize: 10
anchors.bottom: parent.bottom anchors.bottom: parent.bottom
Layout.leftMargin: 10 Layout.leftMargin: 10
Layout.bottomMargin: 10 Layout.bottomMargin: 10
@ -150,25 +146,25 @@ Rectangle {
} }
SerialSelect{ SerialSelect{
id: uart_choose id: uart_choose
width: parent.width - 100 width: parent.width - 130
height: parent.height height: parent.height
} }
NetSelect{ NetSelect{
id: net_choose id: net_choose
width: parent.width - 100 width: parent.width - 130
height: parent.height height: parent.height
} }
ProtoDebug{ ProtoDebug{
id: proto_debug id: proto_debug
width: parent.width - 100 width: parent.width - 130
height: parent.height height: parent.height
visible: false visible: false
} }
TcpSwarm{ TcpSwarm{
id: tcp_swarm id: tcp_swarm
width: parent.width - 100 width: parent.width - 130
height: parent.height height: parent.height
visible: false visible: false
} }

View File

@ -8,7 +8,8 @@ NetworkController::NetworkType NetworkController::Type()
NetworkController::NetworkController(NetworkController::NetworkType type, NetworkController::NetworkController(NetworkController::NetworkType type,
QString ip, QString ip,
uint16_t port):mTcp(nullptr), uint16_t port):
mTcp(nullptr),
mUDP(nullptr), mUDP(nullptr),
mTcpServer(nullptr), mTcpServer(nullptr),
mCnn(nullptr), mCnn(nullptr),
@ -22,10 +23,11 @@ NetworkController::NetworkController(NetworkController::NetworkType type,
mTcp = new QTcpSocket(); mTcp = new QTcpSocket();
mCnn = mTcp; mCnn = mTcp;
QObject::connect(mTcp, SIGNAL(connected()), this, SLOT(on_conected()));
QObject::connect(mTcp, SIGNAL(readyRead()), this, SLOT(on_ready_read())); QObject::connect(mTcp, SIGNAL(readyRead()), this, SLOT(on_ready_read()));
QObject::connect(mTcp, SIGNAL(disconnected()), this, SLOT(on_disconect())); QObject::connect(mTcp, SIGNAL(disconnected()), this, SLOT(on_disconect()));
mTcp->connectToHost(ip,port,QIODevice::ReadWrite); mTcp->connectToHost(ip,port,QIODevice::ReadWrite,QAbstractSocket::IPv4Protocol);
qDebug()<<mTcp->state(); qDebug()<<mTcp->state();
if(mTcp->state() == QAbstractSocket::ConnectedState) if(mTcp->state() == QAbstractSocket::ConnectedState)
mState = true; mState = true;
@ -43,7 +45,7 @@ NetworkController::NetworkController(NetworkController::NetworkType type,
qDebug() << "m_pTcpServer->listen() error"; qDebug() << "m_pTcpServer->listen() error";
} }
} }
if((type == NetworkType::TYPE_UDP_SERVER) || (type == NetworkType::TYPE_TCP_CLIENT)){ if((type == NetworkType::TYPE_UDP_SERVER) || (type == NetworkType::TYPE_UDP_CLIENT)){
mUDP = new QUdpSocket(this); mUDP = new QUdpSocket(this);
mUDP->bind(QHostAddress::Any, port); mUDP->bind(QHostAddress::Any, port);
mCnn = mUDP; mCnn = mUDP;
@ -94,6 +96,8 @@ int NetworkController::Close()
mCnn->close(); mCnn->close();
delete mTcpServer; delete mTcpServer;
delete mCnn; delete mCnn;
mTcpServer = nullptr;
mCnn = nullptr;
} }
if(mType == TYPE_TCP_CLIENT){ if(mType == TYPE_TCP_CLIENT){
qDebug()<<(((QTcpSocket*)mTcp)->state()); qDebug()<<(((QTcpSocket*)mTcp)->state());
@ -102,6 +106,7 @@ int NetworkController::Close()
if(((QTcpSocket*)mTcp)->waitForDisconnected(3000)); if(((QTcpSocket*)mTcp)->waitForDisconnected(3000));
mTcp->close(); mTcp->close();
delete mTcp; delete mTcp;
mTcp = nullptr;
} }
mState = false; mState = false;
return 0; return 0;
@ -110,10 +115,28 @@ int NetworkController::Close()
NetworkController::~NetworkController() NetworkController::~NetworkController()
{ {
delete mTcp; if(mState){
delete mCnn; if(mTcp != nullptr)
delete mTcpServer; delete mTcp;
delete mUDP; if((mCnn != nullptr) && (this->mType != NetworkType::TYPE_TCP_CLIENT))
delete mCnn;
if(mTcpServer != nullptr)
delete mTcpServer;
if(mUDP != nullptr)
delete mUDP;
}
}
bool NetworkController::State()
{
return this->mState;
}
void NetworkController::on_conected()
{
qDebug()<<"connected";
mState = true;
} }
void NetworkController::on_ready_read() void NetworkController::on_ready_read()

View File

@ -33,8 +33,10 @@ public:
RecvResult ReadData(int8_t *data); RecvResult ReadData(int8_t *data);
int Close(); int Close();
~NetworkController(); ~NetworkController();
bool State();
public slots: public slots:
void on_conected();
void on_ready_read(); void on_ready_read();
void on_disconect(); void on_disconect();
void on_server_accept(); void on_server_accept();

View File

@ -10,5 +10,6 @@
<file>TcpSwarm.qml</file> <file>TcpSwarm.qml</file>
<file>res/nethover.svg</file> <file>res/nethover.svg</file>
<file>res/proto_hover.svg</file> <file>res/proto_hover.svg</file>
<file>res/serial1.svg</file>
</qresource> </qresource>
</RCC> </RCC>

View File

@ -1 +0,0 @@
<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1617468834479" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5222" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M888.38144 143.03232h-753.664c-18.14528 0-32.768 14.336-32.768 32.07168V848.2816c0 17.73568 14.66368 32.07168 32.768 32.07168h753.664c18.14528 0 32.768-14.336 32.768-32.07168V175.104c0.04096-17.73568-14.62272-32.07168-32.768-32.07168z m-31.9488 673.13664H166.7072V207.21664h689.72544v608.95232z m-9.0112-7.94624" p-id="5223" fill="#8a8a8a"></path><path d="M275.0464 743.66976h473.088c18.14528 0 32.768-14.21312 32.768-31.78496v-248.34048c0-17.6128-14.62272-31.78496-32.768-31.78496H673.3824V329.5232c0-17.6128-14.62272-31.78496-32.768-31.78496h-258.048c-18.14528 0-32.768 14.21312-32.768 31.78496v102.1952H275.0464c-18.14528 0-32.768 14.21312-32.768 31.78496v248.34048c0 17.6128 14.62272 31.82592 32.768 31.82592z m29.98272-249.52832h109.44512V360.73472h194.19136v133.3248h109.44512v186.53184H305.02912v-186.44992z m10.97728 9.13408" p-id="5224" fill="#8a8a8a"></path><path d="M404.56192 533.17632h-24.576c-4.5056 0-8.192 3.60448-8.192 7.9872v92.9792c0 4.42368 3.6864 7.9872 8.192 7.9872h24.576c4.5056 0 8.192-3.60448 8.192-7.9872v-92.9792c0-4.38272-3.6864-7.9872-8.192-7.9872z m77.53728 0h-24.576c-4.5056 0-8.192 3.60448-8.192 7.9872v92.9792c0 4.42368 3.6864 7.9872 8.192 7.9872h24.576c4.5056 0 8.192-3.60448 8.192-7.9872v-92.9792c0-4.38272-3.6864-7.9872-8.192-7.9872z m77.49632 0h-24.576c-4.5056 0-8.192 3.60448-8.192 7.9872v92.9792c0 4.42368 3.6864 7.9872 8.192 7.9872h24.576c4.5056 0 8.192-3.60448 8.192-7.9872v-92.9792c0-4.38272-3.6864-7.9872-8.192-7.9872z m77.49632 0h-24.576c-4.5056 0-8.192 3.60448-8.192 7.9872v92.9792c0 4.42368 3.6864 7.9872 8.192 7.9872h24.576c4.5056 0 8.192-3.60448 8.192-7.9872v-92.9792c0.04096-4.38272-3.56352-7.9872-8.192-7.9872z m0 0" p-id="5225" fill="#8a8a8a"></path></svg>

Before

Width:  |  Height:  |  Size: 2.0 KiB

View File

@ -1 +1 @@
<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1617468350377" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1442" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M961.9 522.7L851.4 412.2l135.4-135.4c20-20 20-52.8 0-72.8s-52.8-20-72.8 0L778.6 339.4l-99-99L815 105c20-20 20-52.8 0-72.8s-52.8-20-72.8 0L606.8 167.6 496.3 57.1c-19.9-19.9-52.4-19.9-72.3 0s-19.9 52.4 0 72.3L889.6 595c19.9 19.9 52.4 19.9 72.3 0s19.9-52.4 0-72.3zM848.2 603L415.3 170.1c-1.9-1.9-5-1.9-6.9 0l-59.8 59.8-113.5 113.5-0.2 0.2-0.6 0.6c-39.4 76.5-39.4 167.4-43.2 239.4L435.5 828c71.5-5.4 175.2-2.5 238.5-44.1l0.8-0.8 0.1 0.1 173.3-173.3c1.9-1.9 1.9-5 0-6.9zM386.6 826.9L191.4 631.7c-13.6-13.6-35.7-13.6-49.3 0-13.6 13.6-13.6 35.7 0 49.3l195.3 195.3c13.6 13.6 35.7 13.5 49.3 0 13.5-13.6 13.5-35.8-0.1-49.4zM103.4 769.9c-18.4 18.4-18.4 48.6 0 67l15.4 15.4-98.9 98.9 48.6 48.6 98.9-98.9 15.4 15.4c18.4 18.4 48.6 18.4 67 0l39.5-39.5-146.4-146.4-39.5 39.5z" fill="#ffffff" p-id="1443"></path></svg> <?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1630164907618" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2276" width="200" height="200" xmlns:xlink="http://www.w3.org/1999/xlink"><defs><style type="text/css"></style></defs><path d="M301.3 476.9c-13.7 0-24.8-11.1-24.8-24.8 0-13.7 11.1-24.8 24.8-24.8 13.4 0 24.8 11.4 24.8 24.8s-11.4 24.8-24.8 24.8zM406.3 476.9c-13.7 0-24.8-11.1-24.8-24.8 0-13.7 11.1-24.8 24.8-24.8 13.4 0 24.8 11.4 24.8 24.8s-11.4 24.8-24.8 24.8zM511.3 476.9c-13.7 0-24.8-11.1-24.8-24.8 0-13.7 11.1-24.8 24.8-24.8 13.4 0 24.8 11.4 24.8 24.8s-11.4 24.8-24.8 24.8zM616.2 476.9c-13.7 0-24.8-11.1-24.8-24.8 0-13.7 11.1-24.8 24.8-24.8 13.4 0 24.8 11.4 24.8 24.8 0.1 13.4-11.3 24.8-24.8 24.8zM721.2 476.9c-13.7 0-24.8-11.1-24.8-24.8 0-13.7 11.1-24.8 24.8-24.8 13.4 0 24.8 11.4 24.8 24.8s-11.3 24.8-24.8 24.8z" p-id="2277" fill="#ffffff"></path><path d="M353.8 597.7c-13.7 0-24.8-11.1-24.8-24.8 0-13.7 11.1-24.8 24.8-24.8 13.4 0 24.8 11.4 24.8 24.8 0 13.5-11.4 24.8-24.8 24.8zM458.8 597.7c-13.7 0-24.8-11.1-24.8-24.8 0-13.7 11.1-24.8 24.8-24.8 13.4 0 24.8 11.4 24.8 24.8 0 13.5-11.4 24.8-24.8 24.8zM563.7 597.7c-13.7 0-24.8-11.1-24.8-24.8 0-13.7 11.1-24.8 24.8-24.8 13.4 0 24.8 11.4 24.8 24.8 0.1 13.5-11.3 24.8-24.8 24.8zM668.7 597.7c-13.7 0-24.8-11.1-24.8-24.8 0-13.7 11.1-24.8 24.8-24.8 13.4 0 24.8 11.4 24.8 24.8 0 13.5-11.3 24.8-24.8 24.8z" p-id="2278" fill="#ffffff"></path><path d="M728.7 706.5H284.2c-55.7 0-104.1-33.9-115.2-88.4l-37.6-179.4c-6.1-29.9 1.5-60.5 20.8-84.1 19.3-23.6 47.8-37.1 78.3-37.1h562.9c30.4 0 58.8 13.4 78.1 36.9 19.3 23.5 26.9 54 21.1 83.7l-48.8 180c-10.7 54.3-59.2 88.4-115.1 88.4zM230.6 341.8c-23.1 0-44.8 10.3-59.5 28.2-14.6 17.9-20.4 41.2-15.8 63.9l37.6 179.4c8.8 43.3 47.2 69 91.4 69h444.4c44.4 0 82.9-25.9 91.5-69.5l48.8-180c4.3-21.9-1.5-45.1-16.2-62.9-14.7-17.8-36.3-28-59.3-28H230.6z" p-id="2279" fill="#ffffff"></path></svg>

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 2.0 KiB

View File

@ -0,0 +1 @@
<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1630164907618" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2276" width="200" height="200" xmlns:xlink="http://www.w3.org/1999/xlink"><defs><style type="text/css"></style></defs><path d="M301.3 476.9c-13.7 0-24.8-11.1-24.8-24.8 0-13.7 11.1-24.8 24.8-24.8 13.4 0 24.8 11.4 24.8 24.8s-11.4 24.8-24.8 24.8zM406.3 476.9c-13.7 0-24.8-11.1-24.8-24.8 0-13.7 11.1-24.8 24.8-24.8 13.4 0 24.8 11.4 24.8 24.8s-11.4 24.8-24.8 24.8zM511.3 476.9c-13.7 0-24.8-11.1-24.8-24.8 0-13.7 11.1-24.8 24.8-24.8 13.4 0 24.8 11.4 24.8 24.8s-11.4 24.8-24.8 24.8zM616.2 476.9c-13.7 0-24.8-11.1-24.8-24.8 0-13.7 11.1-24.8 24.8-24.8 13.4 0 24.8 11.4 24.8 24.8 0.1 13.4-11.3 24.8-24.8 24.8zM721.2 476.9c-13.7 0-24.8-11.1-24.8-24.8 0-13.7 11.1-24.8 24.8-24.8 13.4 0 24.8 11.4 24.8 24.8s-11.3 24.8-24.8 24.8z" p-id="2277" fill="#1296db"></path><path d="M353.8 597.7c-13.7 0-24.8-11.1-24.8-24.8 0-13.7 11.1-24.8 24.8-24.8 13.4 0 24.8 11.4 24.8 24.8 0 13.5-11.4 24.8-24.8 24.8zM458.8 597.7c-13.7 0-24.8-11.1-24.8-24.8 0-13.7 11.1-24.8 24.8-24.8 13.4 0 24.8 11.4 24.8 24.8 0 13.5-11.4 24.8-24.8 24.8zM563.7 597.7c-13.7 0-24.8-11.1-24.8-24.8 0-13.7 11.1-24.8 24.8-24.8 13.4 0 24.8 11.4 24.8 24.8 0.1 13.5-11.3 24.8-24.8 24.8zM668.7 597.7c-13.7 0-24.8-11.1-24.8-24.8 0-13.7 11.1-24.8 24.8-24.8 13.4 0 24.8 11.4 24.8 24.8 0 13.5-11.3 24.8-24.8 24.8z" p-id="2278" fill="#1296db"></path><path d="M728.7 706.5H284.2c-55.7 0-104.1-33.9-115.2-88.4l-37.6-179.4c-6.1-29.9 1.5-60.5 20.8-84.1 19.3-23.6 47.8-37.1 78.3-37.1h562.9c30.4 0 58.8 13.4 78.1 36.9 19.3 23.5 26.9 54 21.1 83.7l-48.8 180c-10.7 54.3-59.2 88.4-115.1 88.4zM230.6 341.8c-23.1 0-44.8 10.3-59.5 28.2-14.6 17.9-20.4 41.2-15.8 63.9l37.6 179.4c8.8 43.3 47.2 69 91.4 69h444.4c44.4 0 82.9-25.9 91.5-69.5l48.8-180c4.3-21.9-1.5-45.1-16.2-62.9-14.7-17.8-36.3-28-59.3-28H230.6z" p-id="2279" fill="#1296db"></path></svg>

After

Width:  |  Height:  |  Size: 2.0 KiB

View File

@ -1,6 +1,5 @@
#include "serialcontroller.h" #include "serialcontroller.h"
SerialController::SerialController(QObject *parent) : SerialController::SerialController(QObject *parent) :
QObject(parent), QObject(parent),
mCurrentPort(nullptr), mCurrentPort(nullptr),
@ -11,9 +10,7 @@ SerialController::SerialController(QObject *parent) :
} }
int SerialController::OpenSerial(QString port, QString baudRate, QString dataBits, QString stopBits, QString flow){
int SerialController::OpenSerial(QString port, QString baudRate, QString dataBits, QString stopBits, QString flow)
{
GetSetting(baudRate,dataBits,stopBits,flow); GetSetting(baudRate,dataBits,stopBits,flow);
mCurrentPort = new QSerialPort(); mCurrentPort = new QSerialPort();
if(nullptr == mCurrentPort){ if(nullptr == mCurrentPort){
@ -43,15 +40,12 @@ int SerialController::CloseSerial()
return OK; return OK;
} }
QList<QSerialPortInfo> SerialController::GetPorts() QList<QSerialPortInfo> SerialController::GetPorts()
{ {
this->mPorts = QSerialPortInfo::availablePorts(); this->mPorts = QSerialPortInfo::availablePorts();
return mPorts; return mPorts;
} }
int SerialController::SetProto(QSerialProto * proto) int SerialController::SetProto(QSerialProto * proto)
{ {
this->mProto = proto; this->mProto = proto;

View File

@ -9,7 +9,8 @@
QmlShareData::QmlShareData(QObject *parent) : QObject(parent), QmlShareData::QmlShareData(QObject *parent) : QObject(parent),
m_qml_view_(nullptr), m_qml_view_(nullptr),
m_luavm_status_(false) m_luavm_status_(false),
m_network_(nullptr)
{ {
m_serial_controller_ = new SerialController(nullptr); m_serial_controller_ = new SerialController(nullptr);
QFile file("Test.lua"); QFile file("Test.lua");
@ -82,11 +83,21 @@ int QmlShareData::openUart(QString port, QString baudRate, QString dataBits, QSt
int QmlShareData::openNetwork(QString ip, unsigned int port, bool is_ws,int type) int QmlShareData::openNetwork(QString ip, unsigned int port, bool is_ws,int type)
{ {
if(type == NetworkController::TYPE_TCP_CLIENT){ if(type == NetworkController::TYPE_TCP_CLIENT){
m_network_ = new NetworkController(NetworkController::TYPE_TCP_CLIENT,ip,port); qDebug()<<"connect to server";
if(m_network_ == nullptr)
m_network_ = new NetworkController(NetworkController::TYPE_TCP_CLIENT,ip,port);
else{
delete m_network_;
m_network_ = nullptr;
m_network_ = new NetworkController(NetworkController::TYPE_TCP_CLIENT,ip,port);
}
connect(m_network_,SIGNAL(on_data_recv()),this,SLOT(on_network_data_recv())); connect(m_network_,SIGNAL(on_data_recv()),this,SLOT(on_network_data_recv()));
return 0; if(m_network_->State())
return 0;
else{
return -1;
}
} }
if((type == NetworkController::TYPE_UDP_CLIENT) || if((type == NetworkController::TYPE_UDP_CLIENT) ||
(type == NetworkController::TYPE_UDP_SERVER)){ (type == NetworkController::TYPE_UDP_SERVER)){

View File

@ -0,0 +1,316 @@
#define _WSPIAPI_H_
#define _WINSOCKAPI_
#include "tcp_server_libevent.h"
#include <cstring>
/**
* @description:
* @param {*}
* @return {*}
*/
ConnectionLibevent::ConnectionLibevent(TcpServerLibevent *p,struct bufferevent *ev,uint32_t fd,struct sockaddr_in *p1):
m_parent_server(nullptr),
m_event(nullptr),
m_fd(-1),
m_addr(nullptr)
{
m_parent_server = p;
m_event = ev;
m_fd = fd;
m_addr = p1;
}
/**
* @description:
* @param {*}
* @return {*}
*/
ConnectionLibevent::ConnectionLibevent(struct bufferevent *ev,uint32_t fd,struct sockaddr_in *p1):
m_parent_server(nullptr),
m_event(nullptr),
m_fd(-1),
m_addr(nullptr)
{
m_event = ev;
m_fd = fd;
m_addr = p1;
}
/**
* @description:
* @param {*}
* @return {*}
*/
ConnectionLibevent* defaultConnAccept(struct bufferevent *ev,uint32_t fd,struct sockaddr_in *p1){
return new ConnectionLibevent(ev,fd,p1);
}
/**
* @description:
* @param {*}
* @return {*}
*/
int ConnectionLibevent::OnRecv(char *p,uint32_t len){
std::cout<<"OnRecv "<<p<<std::endl;
m_bytes_recv += len;
return 0;
}
/**
* @description:
* @param {*}
* @return {*}
*/
int ConnectionLibevent::OnClose(){
std::cout<<"close "<<this->m_fd << " "<<this->IpAddress()<<std::endl;
return 0;
}
/**
* @description:
* @param {*}
* @return {*}
*/
int ConnectionLibevent::OnWrite(){
return 0;
}
/**
* @description:
* @param {*}
* @return {*}
*/
int ConnectionLibevent::WriteData(const char *p,uint16_t len){
if(nullptr == p){
return -1;
}
return bufferevent_write(this->m_event,p,len);
}
uint32_t ConnectionLibevent::SocketFd(){
return m_fd;
}
/**
* @description:
* @param {*}
* @return {*}
*/
int ConnectionLibevent::SetServer(TcpServerLibevent *p){
if(nullptr != p){
this->m_parent_server = p;
return 0;
}
return -1;
}
string ConnectionLibevent::IpAddress(){
if(nullptr != m_addr){
return string(inet_ntoa(m_addr->sin_addr));
}
return "";
}
TcpServerLibevent *ConnectionLibevent::Server(){
return m_parent_server;
}
void read_cb(struct bufferevent *bev, void *arg)
{
char buf[1024] = {0};
ConnectionLibevent* conn = (ConnectionLibevent*)arg;
bufferevent_read(bev, buf, sizeof(buf));
cout << "client " << conn->IpAddress() << " say:" << buf << endl;
conn->OnRecv(buf,sizeof(buf));
}
void write_cb(struct bufferevent *bev, void *arg)
{
ConnectionLibevent* conn = (ConnectionLibevent*)arg;
std::cout<<"connection "<<conn->IpAddress()<<" sended data success"<< std::endl;
}
/**
* @description:
* @param {*}
* @return {*}
*/
void event_cb(struct bufferevent *bev, short events, void *arg)
{
ConnectionLibevent *conn = (ConnectionLibevent*)(arg);
TcpServerLibevent *server = conn->Server();
if (events & BEV_EVENT_EOF)
{
cout << "connection closed: " << conn->IpAddress() << " " << conn->SocketFd() << endl;
conn->OnClose();
bufferevent_free(bev);
server->RemoveConnection(conn->SocketFd());
}
else if (events & BEV_EVENT_ERROR)
{
conn->OnClose();
bufferevent_free(bev);
server->RemoveConnection(conn->SocketFd());
cout << "some other error !" << endl;
}
delete conn;
}
/**
* @description:
* @param {*}
* @return {*}
*/
void cb_listener(struct evconnlistener *listener, evutil_socket_t fd, struct sockaddr *addr, int len, void *ptr) {
struct sockaddr_in* client = (sockaddr_in*)addr ;
cout << "connect new client: " << inet_ntoa(client->sin_addr) << " " << fd << " ::"<< ntohs(client->sin_port)<< endl;
TcpServerLibevent *server = (TcpServerLibevent*)ptr;
if(server != nullptr){
struct bufferevent *bev;
bev = bufferevent_socket_new(server->m_event_base, fd, BEV_OPT_CLOSE_ON_FREE);
ConnectionLibevent *conn = server->m_handle_accept(bev,ntohs(client->sin_port),client);
conn->SetServer(server);
server->AddConnection(ntohs(client->sin_port),conn);
bufferevent_setcb(bev, read_cb, write_cb, event_cb, conn);
bufferevent_enable(bev, EV_READ);
}
}
/**
* @description:
* @param {*}
* @return {*}
*/
void server_run(TcpServerLibevent *p){
if(nullptr != p){
if(p->m_status == TcpServerLibevent::STOP){
p->m_status = TcpServerLibevent::RUNNING;
event_base_dispatch(p->m_event_base);
evconnlistener_free(p->m_event_listener);
event_base_free(p->m_event_base);
}
}
}
/**
* @description:
* @param {*}
* @return {*}
*/
TcpServerLibevent::SERVER_STATUS TcpServerLibevent::Status(){
return m_status;
}
/**
* @description:
* @param {*}
* @return {*}
*/
int TcpServerLibevent::AddConnection(uint32_t fd,ConnectionLibevent *p){
if( m_map_client.find(fd) == m_map_client.end()){
if(nullptr != p)
m_map_client[fd] = p;
else
return -1;
}
return 0;
}
/**
* @description:
* @param {*}
* @return {*}
*/
int TcpServerLibevent::RemoveConnection(uint32_t fd){
if( m_map_client.find(fd) != m_map_client.end()){
m_map_client.erase(fd);
return 0;
}else{
return -1;
}
}
/**
* @description:
* @param {*}
* @return {*}
*/
int TcpServerLibevent::SetNewConnectionHandle(OnAccept p){
m_handle_accept = p;
return 0;
}
/**
* @description:
* @param {int} ports
* @param {string} bindip
* @return {*}
*/
TcpServerLibevent::TcpServerLibevent(int port,string bindip) :
m_thread(nullptr),
m_event_base(nullptr),
m_event_listener(nullptr)
{
m_handle_accept = defaultConnAccept;
m_backlog = 10000;
this->m_bind_ip = bindip;
this->m_port = port;
memset(&m_server_addr, 0, sizeof(m_server_addr));
m_server_addr.sin_family = AF_INET;
m_server_addr.sin_port = htons(port);
m_server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
// 创建 event_base
m_event_base = event_base_new();
if(NULL == m_event_base){
return;
}
m_event_listener = evconnlistener_new_bind(m_event_base,
cb_listener,
this,
LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE,
m_backlog,
(struct sockaddr*)&m_server_addr,
sizeof(m_server_addr));
if(NULL == m_event_listener)
{
m_status = FAIL;
}
m_status = STOP;
}
/**
* @description: start server synchronous
* @param {*}
* @return {*}
*/
int TcpServerLibevent::StartServerSync(){
if(m_status == STOP){
m_status = RUNNING;
event_base_dispatch(m_event_base);
evconnlistener_free(m_event_listener);
event_base_free(m_event_base);
return 0;
}
return -1;
}
/**
* @description: start server asynchronous
* @param {*}
* @return {*}
*/
int TcpServerLibevent::StartServerAsync(){
if(m_status == STOP){
#ifdef WIN32
evthread_use_windows_threads();
#endif
#ifdef linux
evthread_use_pthreads();
#endif
m_thread = new thread(server_run,this);
m_thread->detach();
return 0;
}
return -1;
}
/**
* @description: start server asynchronous
* @param {*}
* @return {*}
*/
TcpServerLibevent::~TcpServerLibevent(){
if(this->m_status == RUNNING){
m_thread->detach();
event_base_loopbreak(m_event_base);
this->m_status = STOP;
}
}

View File

@ -0,0 +1,107 @@
/*
* @Author: your name
* @Date: 2021-06-30 16:23:10
* @LastEditTime: 2021-07-15 22:29:01
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: \server\tcp_server_libevent.h
*/
#ifndef GENERAL_TCPSERVER_H
#define GENERAL_TCPSERVER_H
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0500
#endif
#ifdef linux
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#define EVENT__HAVE_PTHREADS
#endif
extern "C"{
#include "event2/bufferevent.h"
#include "event2/buffer.h"
#include "event2/listener.h"
#include "event2/util.h"
#include "event2/event.h"
#include "event2/thread.h"
#include <stdint.h>
};
#include <iostream>
#include <mutex>
#include <thread>
#include <map>
using namespace std;
class TcpServerLibevent;
// tcp 连接
class ConnectionLibevent{
public:
ConnectionLibevent(TcpServerLibevent *p,
struct bufferevent*v,
uint32_t fd,
struct sockaddr_in *p1);
ConnectionLibevent(struct bufferevent*v,
uint32_t fd,
struct sockaddr_in *p1);
virtual int OnRecv(char *p,uint32_t len); // 接收到
virtual int OnClose(); // 接收到
virtual int OnWrite();
int WriteData(const char *p,uint16_t len);
int SetServer(TcpServerLibevent *);
TcpServerLibevent *Server();
string IpAddress();
uint32_t SocketFd();
private:
int m_bytes_send;
int m_bytes_recv;
TcpServerLibevent *m_parent_server;
struct bufferevent *m_event;
struct sockaddr_in *m_addr;
uint32_t m_fd;
};
// 管理服务端
class TcpServerLibevent{
typedef enum{
RUNNING,
STOP,
FAIL
}SERVER_STATUS;
public:
typedef ConnectionLibevent* (*OnAccept)(struct bufferevent *ev,uint32_t fd,struct sockaddr_in *p1);
TcpServerLibevent(int port,string bindip);
SERVER_STATUS Status();
~TcpServerLibevent();
int StartServerSync(); // 同步启动服务器
int StartServerAsync(); // 异步启动服务
int RemoveConnection(uint32_t );
int SetNewConnectionHandle(OnAccept );
friend void cb_listener(struct evconnlistener *listener, evutil_socket_t fd, struct sockaddr *addr, int len, void *ptr);
friend void read_cb(struct bufferevent *bev, void *arg);
friend void event_cb(struct bufferevent *bev, short events, void *arg);
friend void write_cb(struct bufferevent *bev, void *arg);
friend void server_run(TcpServerLibevent *p);
private:
uint32_t m_port; // 监听端口号
string m_bind_ip; // 绑定端口号
int m_current_conection; // 当前连接数目
uint16_t m_backlog;
struct sockaddr_in m_server_addr; // 服务器地址
struct event_base * m_event_base;
struct evconnlistener* m_event_listener;
SERVER_STATUS m_status;
thread *m_thread;
map<uint32_t,ConnectionLibevent*> m_map_client;
OnAccept m_handle_accept;
int AddConnection(uint32_t fd,ConnectionLibevent *p);
};
#endif

View File

@ -1,7 +1,7 @@
/* /*
* @Author: your name * @Author: your name
* @Date: 2021-07-04 16:06:47 * @Date: 2021-07-04 16:06:47
* @LastEditTime: 2021-07-15 22:52:22 * @LastEditTime: 2021-08-29 01:00:36
* @LastEditors: Please set LastEditors * @LastEditors: Please set LastEditors
* @Description: In User Settings Edit * @Description: In User Settings Edit
* @FilePath: \server\tcp_swarm_libevent.cpp * @FilePath: \server\tcp_swarm_libevent.cpp
@ -14,10 +14,9 @@ void conn_writecb(struct bufferevent *, void *);
void conn_readcb(struct bufferevent *, void *); void conn_readcb(struct bufferevent *, void *);
void conn_eventcb(struct bufferevent *, short, void *); void conn_eventcb(struct bufferevent *, short, void *);
void conn_writecb(struct bufferevent *bev, void *user_data) { void conn_writecb(struct bufferevent *bev, void *user_data)
{
} }
// 运行线程 // 运行线程
int thread_dispatch(TcpSwarmClientLibevent *p) { int thread_dispatch(TcpSwarmClientLibevent *p) {
if (nullptr != p) { if (nullptr != p) {
@ -59,6 +58,8 @@ void conn_eventcb(struct bufferevent *bev, short events, void *user_data)
{ {
evutil_socket_t fd = bufferevent_getfd(bev); evutil_socket_t fd = bufferevent_getfd(bev);
server->addConection(fd,bev); server->addConection(fd,bev);
std::cout << "conect new fd " << fd << std::endl;
bufferevent_write(bev, string("hello world").c_str(), strlen("hello world"));
} }
} }