From c447084ba5e0a1dbce4fe0c77161b1753c978d75 Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq.com> Date: Thu, 2 Sep 2021 00:16:47 +0800 Subject: [PATCH] fix --- protoDebuger/NetSelect.qml | 2 +- protoDebuger/lua_wraper.cpp | 31 ++++++++++++++++++++++++++++- protoDebuger/lua_wraper.h | 2 ++ protoDebuger/network_controller.cpp | 21 +++++++++---------- protoDebuger/network_controller.h | 2 +- protoDebuger/sharedata.cpp | 8 +++++++- protoDebuger/sharedata.h | 1 + 7 files changed, 53 insertions(+), 14 deletions(-) diff --git a/protoDebuger/NetSelect.qml b/protoDebuger/NetSelect.qml index b2503dc..5677fd6 100644 --- a/protoDebuger/NetSelect.qml +++ b/protoDebuger/NetSelect.qml @@ -179,7 +179,7 @@ Item { } if(proto_combox.currentText === "tcp" && (conn_type.currentText === "server")){ type_network = 1 - button_network.text = "正在连接" + button_network.text = "正在监听" } if(proto_combox.currentText === "tcp" && (conn_type.currentText === "client")){ type_network = 3 diff --git a/protoDebuger/lua_wraper.cpp b/protoDebuger/lua_wraper.cpp index 324cf5c..4ffe417 100644 --- a/protoDebuger/lua_wraper.cpp +++ b/protoDebuger/lua_wraper.cpp @@ -18,6 +18,31 @@ int LuaDelegate::DoString(QString scr) return 0; } +int LuaDelegate::UpdateScript(QString scr) +{ + if(nullptr != mVM){ + lua_close(mVM); + } + free(mVM); + mVM = luaL_newstate(); //打开lua + if(nullptr != mVM){ + printf("shit is nullptr"); + } + luaL_openlibs(mVM); //打开标准库 + lua_register(mVM, "showbuffer", LuaShowData); + lua_register(mVM, "serial_send", LuaWriteUart); + + if (mVM != nullptr){ + int ret = luaL_dostring(mVM,scr.toStdString().c_str()); + if (ret > 0){ + printf("lua error"); + PrintError(mVM); + return -1; + } + } + return 0; +} + void LuaDelegate::PrintError(lua_State *L) { qDebug()<<"\nFATAL ERROR:%s\n\n"<< lua_tostring(L, -1); @@ -25,6 +50,8 @@ void LuaDelegate::PrintError(lua_State *L) void LuaDelegate::OnSerialData(QString data){ int i = lua_getglobal(mVM,"OnDataReady"); + if(i == 0) + return; lua_pushstring(mVM,data.toStdString().data()); lua_call(mVM,1,0); } @@ -33,10 +60,12 @@ void LuaDelegate::OnNetworkData(char *addr, char *data, uint32_t port) { qDebug()<<"call lua network callback"; int ret = lua_getglobal(mVM,"OnNetworkData"); + if(ret == 0) + return; + qDebug()<listen(QHostAddress::Any, port)) { @@ -87,8 +87,9 @@ NetworkController::RecvResult NetworkController::ReadData(int8_t *data) } } else{ - memcpy(data,mCnn->readAll().data(),mCnn->size()); - ret.len = mCnn->size(); + auto reads = mCnn->readAll(); + memcpy(data,reads.data(),reads.size()); + ret.len = reads.size(); } return ret; } @@ -100,9 +101,9 @@ int NetworkController::Close() if(nullptr != mCnn) mCnn->close(); delete mTcpServer; - delete mCnn; mTcpServer = nullptr; mCnn = nullptr; + mTcp = nullptr; } if(mType == TYPE_TCP_CLIENT){ qDebug()<<(((QTcpSocket*)mTcp)->state()); @@ -139,7 +140,6 @@ bool NetworkController::State() void NetworkController::on_conected() { - qDebug()<<"connected"; mState = true; emit(on_connected()); } @@ -164,15 +164,14 @@ void NetworkController::on_disconect() mCnn->close(); emit(this->on_conection_close()); } - if(mType == TYPE_TCP_SERVER){ - mTcpServer->resumeAccepting(); - } +// if(mType == TYPE_TCP_SERVER){ +// mTcpServer->resumeAccepting(); +// } } void NetworkController::on_server_accept() { if(mType == TYPE_TCP_SERVER){ - mTcpServer->pauseAccepting(); QTcpSocket* pClientConnection = mTcpServer->nextPendingConnection(); if(nullptr != pClientConnection){ QObject::connect(pClientConnection, SIGNAL(readyRead()), this, SLOT(on_ready_read())); @@ -181,6 +180,8 @@ void NetworkController::on_server_accept() mTcp = pClientConnection; mCnn = mTcp; mState = true; + emit(on_connected()); + } } diff --git a/protoDebuger/network_controller.h b/protoDebuger/network_controller.h index 3c4ea16..612854e 100644 --- a/protoDebuger/network_controller.h +++ b/protoDebuger/network_controller.h @@ -35,7 +35,6 @@ public: ~NetworkController(); bool State(); public slots: - void on_ready_read(); void on_disconect(); void on_server_accept(); @@ -58,6 +57,7 @@ private: QTcpSocket *mTcp; QTcpServer *mTcpServer; QIODevice *mCnn; + QThread mThread; bool mState; }; diff --git a/protoDebuger/sharedata.cpp b/protoDebuger/sharedata.cpp index 440b7a6..97b543c 100644 --- a/protoDebuger/sharedata.cpp +++ b/protoDebuger/sharedata.cpp @@ -108,6 +108,7 @@ int QmlShareData::openNetwork(QString ip, unsigned int port, bool is_ws,int type if(type == NetworkController::TYPE_TCP_SERVER){ m_network_ = new NetworkController(NetworkController::TYPE_TCP_SERVER,ip,port); connect(m_network_,SIGNAL(on_data_recv()),this,SLOT(on_network_data_recv())); + connect(m_network_,SIGNAL(on_connected()),this,SLOT(on_network_conected())); return 0; } @@ -148,7 +149,7 @@ int QmlShareData::updateLuaScript(QString str) qDebug()<