完善网络调试 功能

master
zcy 2021-05-03 16:49:33 +08:00
parent 1a3840dca6
commit 7619a0cefc
6 changed files with 76 additions and 28 deletions

View File

@ -23,12 +23,22 @@ void LuaDelegate::PrintError(lua_State *L)
qDebug()<<"\nFATAL ERROR:%s\n\n"<< lua_tostring(L, -1);
}
void LuaDelegate::OnDataRecv(QString data){
void LuaDelegate::OnSerialData(QString data){
int i = lua_getglobal(mVM,"OnDataReady");
lua_pushstring(mVM,data.toStdString().data());
lua_call(mVM,1,0);
}
void LuaDelegate::OnNetworkData(char *addr, char *data, uint32_t port)
{
int i = lua_getglobal(mVM,"OnNetworkData");
lua_pushstring(mVM,addr);
lua_pushstring(mVM,data);
lua_pushnumber(mVM,port);
lua_call(mVM,3,0);
}
void LuaDelegate::DumpStack()
{
static int count = 0;

View File

@ -44,7 +44,8 @@ public:
}
void PrintError(lua_State *L);
/* 收到数据发送给lua层进行处理*/
void OnDataRecv(QString);
void OnSerialData(QString);
void OnNetworkData(char*,char*,uint32_t port);
void DumpStack();
~LuaDelegate();

View File

@ -1,7 +1,12 @@
#include "network_controller.h"
NetworkController::NetworkController(NetworkController::NetWorkType type,
NetworkController::NetworkType NetworkController::Type()
{
return mType;
}
NetworkController::NetworkController(NetworkController::NetworkType type,
QString ip,
uint16_t port):mTcp(nullptr),
mUDP(nullptr),
@ -12,7 +17,7 @@ NetworkController::NetworkController(NetworkController::NetWorkType type,
if(_checkType(type) == TYPE_UNKOWN){
}
if(type == NetWorkType::TYPE_TCP_CLIENT){
if(type == NetworkType::TYPE_TCP_CLIENT){
mTcp = new QTcpSocket();
mCnn = mTcp;
@ -22,7 +27,7 @@ NetworkController::NetworkController(NetworkController::NetWorkType type,
mTcp->connectToHost(ip,port,QIODevice::ReadWrite);
}
if(type == NetWorkType::TYPE_TCP_SERVER){
if(type == NetworkType::TYPE_TCP_SERVER){
mTcpServer = new QTcpServer();
connect(mTcpServer,SIGNAL(newConnection()),
@ -35,7 +40,7 @@ NetworkController::NetworkController(NetworkController::NetWorkType type,
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_TCP_CLIENT)){
mUDP = new QUdpSocket(this);
mUDP->bind(QHostAddress::Any, port);
mCnn = mUDP;
@ -53,12 +58,28 @@ int NetworkController::SendData(int8_t *data, uint32_t len)
return mCnn->write((const char *)data,len);
}
int NetworkController::ReadData(int8_t *data)
NetworkController::RecvResult NetworkController::ReadData(int8_t *data)
{
if(nullptr != data){
return -1;
RecvResult ret;
if(nullptr == data){
ret.len = -1;
return ret;
}
memcpy(data,mCnn->readAll().data(),mCnn->size());
if((mType == TYPE_UDP_CLIENT )||(mType == TYPE_UDP_SERVER)){
int len = 0;
while(mUDP->hasPendingDatagrams()){
QNetworkDatagram datagram = mUDP->receiveDatagram();
memcpy(data + len,datagram.data().data(),datagram.data().size());
len += datagram.data().size();
ret.addr = datagram.senderAddress().toString();
ret.port = datagram.senderPort();
qDebug()<<datagram.data().length();
}
}
else{
memcpy(data,mCnn->readAll().data(),mCnn->size());
}
return ret;
}
int NetworkController::Close()
@ -87,12 +108,7 @@ NetworkController::~NetworkController()
void NetworkController::on_ready_read()
{
if((mType == TYPE_UDP_CLIENT )||(mType == TYPE_UDP_SERVER)){
QNetworkDatagram datagram = mUDP->receiveDatagram();
qDebug()<<datagram.senderAddress()<<datagram.senderPort()<<datagram.data();
}else{
qDebug()<<QString::fromStdString(mCnn->readAll().toStdString());
}
qDebug()<<"data read:";
emit(on_data_recv());
}
@ -126,10 +142,9 @@ void NetworkController::on_server_accept()
void NetworkController::on_accept_error(QAbstractSocket::SocketError socketError)
{
qDebug()<<socketError;
}
NetworkController::NetWorkType NetworkController::_checkType(NetWorkType type){
NetworkController::NetworkType NetworkController::_checkType(NetworkType type){
if(type < TYPE_UNKOWN){
return type;
}else{

View File

@ -20,11 +20,17 @@ public:
TYPE_UDP_CLIENT = 2,
TYPE_TCP_CLIENT = 3,
TYPE_UNKOWN = 4,
}NetWorkType;
}NetworkType;
typedef struct{
uint32_t len;
QString addr;
QString port;
}RecvResult;
NetworkController(NetWorkType type,QString ip,uint16_t port);
NetworkType Type();
NetworkController(NetworkType type,QString ip,uint16_t port);
int SendData(int8_t *data,uint32_t len);
int ReadData(int8_t *data);
RecvResult ReadData(int8_t *data);
int Close();
~NetworkController();
@ -39,9 +45,9 @@ signals:
void on_conection_close();
void on_send_data(QByteArray);
private:
NetWorkType _checkType(NetWorkType);
NetworkType _checkType(NetworkType);
NetWorkType mType;
NetworkType mType;
QUdpSocket *mUDP;
QTcpSocket *mTcp;
QTcpServer *mTcpServer;

View File

@ -50,7 +50,7 @@ int ShareData::SetQuickView(QQuickWidget *view)
int ShareData::OnDataRecv(QByteArray arr)
{
if(m_luavm_status_)
m_lua_.OnDataRecv(QString(arr));
m_lua_.OnSerialData(QString(arr));
}
int ShareData::ShowDataInQML(QString x)
@ -71,7 +71,6 @@ int ShareData::SendUartData(const char *data)
int ShareData::openUart(QString port, QString baudRate, QString dataBits, QString stopBits, QString flow)
{
qDebug()<<port<<baudRate<<dataBits<<stopBits<<flow;
if(m_serial_controller_->OpenSerial(port,baudRate,dataBits,stopBits,flow) == 0){
m_serial_controller_->SetListener(this);
return 0;
@ -82,17 +81,23 @@ int ShareData::openUart(QString port, QString baudRate, QString dataBits, QStrin
int ShareData::openNetwork(QString ip, unsigned int port, bool is_ws,int type)
{
if(type == NetworkController::TYPE_TCP_CLIENT){
m_network_ = new NetworkController(NetworkController::TYPE_TCP_CLIENT,ip,port);
connect(m_network_,SIGNAL(on_data_recv()),this,SLOT(on_network_data_recv()));
return 0;
}
if((type == NetworkController::TYPE_UDP_CLIENT) ||
(type == NetworkController::TYPE_UDP_SERVER)){
m_network_ = new NetworkController((NetworkController::NetWorkType)type,ip,port);
m_network_ = new NetworkController((NetworkController::NetworkType)type,ip,port);
connect(m_network_,SIGNAL(on_data_recv()),this,SLOT(on_network_data_recv()));
return 0;
}
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()));
return 0;
}
return -1;
@ -115,7 +120,7 @@ int ShareData::closeSerial()
int ShareData::TestLua()
{
m_lua_.OnDataRecv("ss");
m_lua_.OnSerialData("ss");
}
int ShareData::TestShowData()
@ -162,4 +167,14 @@ bool ShareData::luaStatus()
return m_luavm_status_;
}
void ShareData::on_network_data_recv(){
qDebug()<<"recv data";
char dat[4096] = {0};
auto ret = this->m_network_->ReadData((int8_t * )dat);
qDebug()<<dat;
if(ret.len > 0){
m_lua_.OnNetworkData((char*)ret.addr.toStdString().data(),dat,ret.port.toUInt());
}
}

View File

@ -54,7 +54,8 @@ public:
signals:
void txtchanged();
void valueFromCpp(int val);
public slots:
void on_network_data_recv();
private:
NetworkController *m_network_;
SerialController *m_serial_controller_;