master
zcy 2021-09-02 00:16:47 +08:00
parent 7de228601d
commit c447084ba5
7 changed files with 53 additions and 14 deletions

View File

@ -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

View File

@ -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()<<ret;
lua_pushstring(mVM,addr);
lua_pushstring(mVM,data);
lua_pushnumber(mVM,port);
lua_call(mVM,3,0);
}

View File

@ -23,6 +23,8 @@ public:
int DoFile(QString);
void Stop();
int DoString(QString);
int UpdateScript(QString);
void pushstack() {
// const std::type_info &t1 = std::bad_typeid(arg1);
// std::cout << t1.name() << std::endl;

View File

@ -41,8 +41,8 @@ NetworkController::NetworkController(NetworkController::NetworkType type,
mTcpServer = new QTcpServer();
connect(mTcpServer,SIGNAL(newConnection()),
this,SLOT(on_server_accept()));
connect(mTcpServer, SIGNAL(acceptError(QAbstractSocket::SocketError socketError)),
this, SLOT( on_accept_error(QAbstractSocket::SocketError socketError)));
connect(mTcpServer, SIGNAL(acceptError(QAbstractSocket::SocketError )),
this, SLOT( on_accept_error(QAbstractSocket::SocketError )));
if (!mTcpServer->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());
}
}

View File

@ -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;
};

View File

@ -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()<<QThread::currentThreadId();
m_lua_string = str;
qDebug()<<str;
int ret = m_lua_.DoString(m_lua_string);
int ret = m_lua_.UpdateScript(m_lua_string);
if(ret < 0){
qDebug()<<"更新lua脚本失败";
m_luavm_status_ = false;
@ -179,6 +180,11 @@ bool QmlShareData::luaStatus()
return m_luavm_status_;
}
bool QmlShareData::DoLuaCmd(QString)
{
}
void QmlShareData::on_network_data_recv(){
qDebug()<<"recv data";
char dat[4096] = {0};

View File

@ -47,6 +47,7 @@ public:
Q_INVOKABLE int updateLuaScript(QString);
Q_INVOKABLE int saveLuaScript(QString);
Q_INVOKABLE bool luaStatus();
Q_INVOKABLE bool DoLuaCmd(QString);
QString _txt = "hello world\r\n";