Compare commits
10 Commits
7de228601d
...
e10f75b7ce
Author | SHA1 | Date |
---|---|---|
zcy | e10f75b7ce | |
zcy | 15084716e8 | |
zcy | d29d688b55 | |
zcy | 8e0176a5c0 | |
zcy | 62c9df87c8 | |
zcy | d7c5d0e889 | |
zcy | c33530ce1d | |
zcy | 6e80dda35b | |
zcy | ab55618406 | |
zcy | c447084ba5 |
28
README.md
28
README.md
|
@ -25,17 +25,39 @@ lua代码例子:
|
|||
```
|
||||
require("string")
|
||||
|
||||
function OnDataReady(data) # 接收串口数据回调
|
||||
|
||||
function OnDataReady(data) # 串口收到数据
|
||||
showbuffer(data)
|
||||
serial_send(data)
|
||||
end
|
||||
|
||||
function OnNetworkData(addr,data,len) # 接收网络数据回调
|
||||
showbuffer(addr)
|
||||
function OnNetworkData(addr,data,len) # tcp客户端模式接收到数据
|
||||
print(addr,data,len)
|
||||
print(len)
|
||||
print(data)
|
||||
showbuffer("recv network data: " .. data .. "\r\n")
|
||||
end
|
||||
|
||||
function OnUdpData(addr,data,len) # udp客户端模式接收到数据
|
||||
print(addr,data,len)
|
||||
showbuffer("recv data: ",data,"\r\n")
|
||||
end
|
||||
|
||||
|
||||
|
||||
function OnNewClient(addr,port,sock) # 新连接,tcp服务端模式
|
||||
print(addr,port,sock)
|
||||
showbuffer("tcp connected: "
|
||||
..addr.."\r\n"..port.."\r\n"..sock.."\r\n")
|
||||
end
|
||||
|
||||
|
||||
function OnClientLeave(addr,port,sock) # 连接断开,tcp服务端模式
|
||||
print(addr,port,sock)
|
||||
showbuffer("tcp close: "
|
||||
..addr.."\r\n"..port.."\r\n"..sock.."\r\n")
|
||||
end
|
||||
|
||||
|
||||
serial_send("test") # 在脚本中主动发送串口数据
|
||||
|
||||
|
|
|
@ -28,6 +28,7 @@ set(HEADERS # 待预编译的cpp头文件
|
|||
sharedata.h
|
||||
network_controller.h
|
||||
mainwindow.h
|
||||
tcp_server_libevent.h
|
||||
|
||||
)
|
||||
|
||||
|
@ -40,7 +41,7 @@ set(SOURCES # 待预编译的cpp代码
|
|||
network_controller.cpp
|
||||
mainwindow.cpp
|
||||
tcp_swarm_libevent.cpp
|
||||
|
||||
tcp_server_libevent.cpp
|
||||
)
|
||||
|
||||
set(RESOURCES
|
||||
|
|
|
@ -179,7 +179,8 @@ Item {
|
|||
}
|
||||
if(proto_combox.currentText === "tcp" && (conn_type.currentText === "server")){
|
||||
type_network = 1
|
||||
button_network.text = "正在连接"
|
||||
button_network.text = "正在监听"
|
||||
button_close.visible = true
|
||||
}
|
||||
if(proto_combox.currentText === "tcp" && (conn_type.currentText === "client")){
|
||||
type_network = 3
|
||||
|
@ -197,10 +198,33 @@ Item {
|
|||
type_network)
|
||||
|
||||
|
||||
}else if ((button_network.text === "关闭连接") && (proto_combox.currentText !== "udp"))
|
||||
}else if ((button_network.text === "关闭连接") && (proto_combox.currentText === "udp"))
|
||||
{
|
||||
console.log("dfdfads")
|
||||
DataWrap.closeNetwork();
|
||||
DataWrap.closeNetwork("");
|
||||
button_network.text = "打开网络"
|
||||
}
|
||||
else if ((button_network.text === "关闭连接") && (proto_combox.currentText === "tcp"))
|
||||
{
|
||||
console.log("dfdfads")
|
||||
DataWrap.closeNetwork("");
|
||||
button_network.text = "打开网络"
|
||||
}
|
||||
}
|
||||
}
|
||||
Button {
|
||||
id: button_close
|
||||
visible: false
|
||||
Layout.leftMargin: 20
|
||||
Layout.topMargin: 20
|
||||
width: 128
|
||||
height: 52
|
||||
text: qsTr("停止监听")
|
||||
|
||||
onClicked: {
|
||||
if(button_network.text == "正在监听"){
|
||||
button_close.visible = false
|
||||
DataWrap.closeNetwork("tcpserver");
|
||||
button_network.text = "打开网络"
|
||||
}
|
||||
}
|
||||
|
@ -252,7 +276,7 @@ Item {
|
|||
Connections {
|
||||
target: DataWrap
|
||||
onDisConnected: {
|
||||
button_network.text = "打开连接"
|
||||
button_network.text = "打开网络"
|
||||
tip.success("网络连接失败")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import QtQuick.Controls.Material 2.12
|
|||
|
||||
|
||||
Item {
|
||||
id: debug_form
|
||||
objectName: "ProtoDebug"
|
||||
width: 800
|
||||
height: 600
|
||||
|
@ -52,7 +53,7 @@ Item {
|
|||
font.pixelSize: 30
|
||||
font.bold: true
|
||||
}
|
||||
Flickable {
|
||||
ScrollView {
|
||||
id: flick
|
||||
Layout.preferredHeight: parent.height - 100
|
||||
Layout.preferredWidth: parent.width/2 - 20
|
||||
|
@ -60,7 +61,6 @@ Item {
|
|||
|
||||
contentWidth: lua_script.paintedWidth
|
||||
contentHeight: lua_script.paintedHeight
|
||||
clip: true
|
||||
Layout.leftMargin: 10
|
||||
|
||||
ColorAnimation {
|
||||
|
@ -91,19 +91,28 @@ Item {
|
|||
onCursorRectangleChanged: flick.ensureVisible(cursorRectangle)
|
||||
}
|
||||
}
|
||||
|
||||
TextArea {
|
||||
id: text_output
|
||||
Layout.preferredHeight: parent.height - 100
|
||||
text: qsTr("")
|
||||
font.pixelSize: 20
|
||||
ScrollView{
|
||||
Layout.columnSpan: 1
|
||||
Layout.preferredWidth: parent.width/2 - 20
|
||||
background: Rectangle {
|
||||
border.color: text_output.enabled ? "#21be2b" : "transparent"
|
||||
}
|
||||
Layout.preferredWidth: parent.width/2 - 35
|
||||
Layout.preferredHeight: parent.height - 200
|
||||
Layout.leftMargin: 10
|
||||
height: parent.height
|
||||
|
||||
TextArea {
|
||||
id: text_output
|
||||
height: parent.height
|
||||
text: qsTr("")
|
||||
font.pixelSize: 20
|
||||
cursorVisible: true;
|
||||
focus: true;
|
||||
readOnly: true
|
||||
selectByMouse:true;
|
||||
selectByKeyboard: true
|
||||
background: Rectangle {
|
||||
border.color: text_output.enabled ? "#21be2b" : "transparent"
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
RowLayout{
|
||||
visible: true
|
||||
|
@ -168,10 +177,17 @@ Item {
|
|||
text: qsTr("执行命令")
|
||||
Layout.bottomMargin: 10
|
||||
onClicked: {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Button {
|
||||
id: button3
|
||||
text: qsTr("清空缓冲区")
|
||||
Layout.bottomMargin: 10
|
||||
onClicked: {
|
||||
console.log("dfsd")
|
||||
debug_form.clearbuff()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -195,6 +211,10 @@ Item {
|
|||
showbuf += str;
|
||||
text_output.text = showbuf;
|
||||
}
|
||||
function clearbuff(){
|
||||
showbuf = "";
|
||||
text_output.text = showbuf;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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,13 +60,42 @@ 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);
|
||||
}
|
||||
|
||||
void LuaDelegate::OnNewTcpClient(QString ip, uint32_t port, uint32_t sockptr)
|
||||
{
|
||||
qDebug()<<"call lua network new client callback";
|
||||
int ret = lua_getglobal(mVM,"OnNewClient");
|
||||
if(ret == 0)
|
||||
return;
|
||||
qDebug()<<ret;
|
||||
lua_pushstring(mVM,ip.toStdString().c_str());
|
||||
lua_pushinteger(mVM,port);
|
||||
lua_pushinteger(mVM,sockptr);
|
||||
lua_call(mVM,3,0);
|
||||
}
|
||||
|
||||
void LuaDelegate::OnClientLeave(QString ip, uint32_t port, uint32_t sockptr)
|
||||
{
|
||||
qDebug()<<"call lua network leave callback";
|
||||
int ret = lua_getglobal(mVM,"OnClientLeave");
|
||||
if(ret == 0)
|
||||
return;
|
||||
qDebug()<<ret;
|
||||
lua_pushstring(mVM,ip.toStdString().c_str());
|
||||
lua_pushnumber(mVM,port);
|
||||
lua_pushnumber(mVM,sockptr);
|
||||
lua_call(mVM,3,0);
|
||||
}
|
||||
|
||||
|
||||
void LuaDelegate::DumpStack()
|
||||
{
|
||||
static int count = 0;
|
||||
|
|
|
@ -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;
|
||||
|
@ -46,6 +48,10 @@ public:
|
|||
/* 收到数据发送给lua层进行处理*/
|
||||
void OnSerialData(QString);
|
||||
void OnNetworkData(char*,char*,uint32_t port);
|
||||
void OnNewTcpClient(QString ip,uint32_t port,uint32_t sockptr);
|
||||
void OnClientLeave(QString ip,uint32_t port,uint32_t sockptr);
|
||||
void ClearShowBuffer();
|
||||
|
||||
void DumpStack();
|
||||
|
||||
~LuaDelegate();
|
||||
|
|
|
@ -15,21 +15,50 @@
|
|||
#include "lua_wraper.h"
|
||||
#include "mainwindow.h"
|
||||
#include <windows.h>
|
||||
#include "Qss.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
#ifdef WIN32
|
||||
WORD wdVersion = MAKEWORD(2, 2);//定义自己需要的网络库版本,这里是2.2
|
||||
WSADATA wdSockMsg;//这是一个结构体
|
||||
int nRes = WSAStartup(wdVersion, &wdSockMsg);//打开一个套接字
|
||||
if (0 != nRes) {
|
||||
switch (nRes) {
|
||||
case WSASYSNOTREADY:
|
||||
printf("check your library");
|
||||
break;
|
||||
case WSAVERNOTSUPPORTED:
|
||||
printf("need updated");
|
||||
break;
|
||||
case WSAEINPROGRESS:
|
||||
printf("need reboot");
|
||||
break;
|
||||
case WSAEPROCLIM:
|
||||
printf("sdfsdfsa");
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (2 != HIBYTE(wdSockMsg.wVersion) || 2 != LOBYTE(wdSockMsg.wVersion)) {
|
||||
printf("WSACleanup");
|
||||
WSACleanup();
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
AllocConsole();
|
||||
freopen("CONOUT$","w",stdout);
|
||||
|
||||
QApplication app(argc, argv);
|
||||
QQmlApplicationEngine engine;
|
||||
|
||||
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
|
||||
QApplication::setAttribute(Qt::AA_DisableHighDpiScaling);
|
||||
|
||||
printf("hello world");
|
||||
MainWindow w;
|
||||
w.setWindowTitle("协议调试器");
|
||||
w.show();
|
||||
|
||||
CurrentDPI(&w,0);
|
||||
return app.exec();
|
||||
}
|
||||
|
|
|
@ -12,9 +12,23 @@
|
|||
QmlShareData gGlobal;
|
||||
|
||||
|
||||
static void rangeObjectList(QObject*obj,int indent){
|
||||
QObjectList child = obj->children();
|
||||
QString tmp("");
|
||||
for(int i = 0;i < indent;i++)
|
||||
tmp +=" ";
|
||||
|
||||
for(int i = 0;i < child.size();i++){
|
||||
qDebug()<<tmp + child.at(i)->objectName() + " " + child.at(i)->metaObject()->className();
|
||||
if(child.at(i)->children().size() > 0){
|
||||
rangeObjectList(child.at(i),indent + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent) :
|
||||
QssMainWindow(parent),
|
||||
ui(new Ui::MainWindow)
|
||||
ui(new Ui::MainWindow),
|
||||
QssMainWindow(parent)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
QQuickWidget *m_quickWidget=new QQuickWidget();
|
||||
|
@ -30,6 +44,7 @@ MainWindow::MainWindow(QWidget *parent) :
|
|||
this->setStyleSheet("margin: 0px;");
|
||||
gGlobal.SetQuickView(m_quickWidget);
|
||||
ui->centralwidget->layout()->addWidget(m_quickWidget);
|
||||
rangeObjectList(this,0);
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow()
|
||||
|
|
|
@ -41,6 +41,20 @@
|
|||
<property name="bottomMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<item>
|
||||
<widget class="QLabel" name="label_2">
|
||||
<property name="text">
|
||||
<string>TextLabel</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="text">
|
||||
<string>TextLabel</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QMenuBar" name="menubar">
|
||||
|
@ -49,7 +63,7 @@
|
|||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>1183</width>
|
||||
<height>27</height>
|
||||
<height>20</height>
|
||||
</rect>
|
||||
</property>
|
||||
</widget>
|
||||
|
|
|
@ -17,7 +17,6 @@ NetworkController::NetworkController(NetworkController::NetworkType type,
|
|||
{
|
||||
mType = type;
|
||||
if(_checkType(type) == TYPE_UNKOWN){
|
||||
|
||||
}
|
||||
if(type == NetworkType::TYPE_TCP_CLIENT){
|
||||
mTcp = new QTcpSocket();
|
||||
|
@ -38,11 +37,12 @@ NetworkController::NetworkController(NetworkController::NetworkType type,
|
|||
}
|
||||
|
||||
if(type == NetworkType::TYPE_TCP_SERVER){
|
||||
qDebug()<<"tcp server mode";
|
||||
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,23 +164,24 @@ 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()));
|
||||
QObject::connect(pClientConnection, SIGNAL(disconnected()), this, SLOT(on_disconect()));
|
||||
}
|
||||
mTcp = pClientConnection;
|
||||
mCnn = mTcp;
|
||||
qDebug()<<pClientConnection->socketDescriptor();
|
||||
// mTcp = pClientConnection;
|
||||
// mCnn = mTcp;
|
||||
mState = true;
|
||||
emit(on_connected());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#include <QThread>
|
||||
#include <QTcpServer>
|
||||
#include <QNetworkDatagram>
|
||||
#include <QMap>
|
||||
|
||||
// this is not a thread-safe class,any interface invoked in multi-thread maybe will cause unkown falut
|
||||
class NetworkController : public QObject
|
||||
|
@ -35,7 +36,6 @@ public:
|
|||
~NetworkController();
|
||||
bool State();
|
||||
public slots:
|
||||
|
||||
void on_ready_read();
|
||||
void on_disconect();
|
||||
void on_server_accept();
|
||||
|
@ -52,12 +52,12 @@ signals:
|
|||
void on_send_data(QByteArray);
|
||||
private:
|
||||
NetworkType _checkType(NetworkType);
|
||||
|
||||
NetworkType mType;
|
||||
QUdpSocket *mUDP;
|
||||
QTcpSocket *mTcp;
|
||||
QTcpServer *mTcpServer;
|
||||
QIODevice *mCnn;
|
||||
QMap<qintptr,QTcpSocket *> m_clients;
|
||||
QThread mThread;
|
||||
bool mState;
|
||||
};
|
||||
|
|
|
@ -10,7 +10,8 @@
|
|||
QmlShareData::QmlShareData(QObject *parent) : QObject(parent),
|
||||
m_qml_view_(nullptr),
|
||||
m_luavm_status_(false),
|
||||
m_network_(nullptr)
|
||||
m_network_(nullptr),
|
||||
m_tcp_server(nullptr)
|
||||
{
|
||||
m_serial_controller_ = new SerialController(nullptr);
|
||||
QFile file("Test.lua");
|
||||
|
@ -106,17 +107,30 @@ int QmlShareData::openNetwork(QString ip, unsigned int port, bool is_ws,int type
|
|||
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()));
|
||||
m_tcp_server = new TcpServerLibevent(port,ip.toStdString());
|
||||
m_tcp_server->StartServerAsync();
|
||||
m_tcp_server->moveToThread(this->thread());
|
||||
connect(m_tcp_server,SIGNAL(on_onnected(string , uint )),this,SLOT(on_network_conected(string , uint )),Qt::DirectConnection);
|
||||
connect(m_tcp_server,SIGNAL(on_disconected(string , uint )),this,SLOT(on_network_disconected(string , uint )),Qt::DirectConnection);
|
||||
connect(m_tcp_server,SIGNAL(on_conn_read(string )),this,SLOT(on_network_server_read(string)),Qt::DirectConnection);
|
||||
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int QmlShareData::closeNetwork()
|
||||
int QmlShareData::closeNetwork(QString type)
|
||||
{
|
||||
m_network_->Close();
|
||||
if(type == "tcpserver"){
|
||||
if(nullptr != m_tcp_server){
|
||||
m_tcp_server->StopServer();
|
||||
delete m_tcp_server;
|
||||
m_tcp_server = nullptr;
|
||||
}
|
||||
}
|
||||
else
|
||||
m_network_->Close();
|
||||
|
||||
}
|
||||
|
||||
int QmlShareData::closeSerial()
|
||||
|
@ -148,7 +162,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 +193,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};
|
||||
|
@ -189,6 +208,16 @@ void QmlShareData::on_network_data_recv(){
|
|||
}
|
||||
}
|
||||
|
||||
void QmlShareData::on_network_conected(string ip, uint port)
|
||||
{
|
||||
m_lua_.OnNewTcpClient(QString::fromStdString(ip),port,0);
|
||||
}
|
||||
|
||||
void QmlShareData::on_network_disconected(string ip, uint port)
|
||||
{
|
||||
m_lua_.OnClientLeave(QString::fromStdString(ip),0,port);
|
||||
}
|
||||
|
||||
void QmlShareData::on_udp_data_recv(){
|
||||
char dat[4096] = {0};
|
||||
auto ret = this->m_network_->ReadData((int8_t * )dat);
|
||||
|
@ -198,14 +227,12 @@ void QmlShareData::on_udp_data_recv(){
|
|||
}
|
||||
}
|
||||
|
||||
void QmlShareData::on_network_conected()
|
||||
void QmlShareData::on_network_server_read(string ip)
|
||||
{
|
||||
emit(sendToQml(11));
|
||||
}
|
||||
|
||||
void QmlShareData::on_network_disconected()
|
||||
{
|
||||
emit(disConnected());
|
||||
qDebug()<<ip.c_str();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#include <QDebug>
|
||||
#include "lua_wraper.h"
|
||||
#include <QtQuickWidgets/QQuickWidget>
|
||||
#include "tcp_server_libevent.h"
|
||||
|
||||
class QmlShareData;
|
||||
|
||||
|
@ -31,38 +32,37 @@ public:
|
|||
int OnDataRecv(QByteArray);
|
||||
int ShowDataInQML(QString x);
|
||||
int SendUartData(const char *);
|
||||
|
||||
Q_INVOKABLE void getValFromQml(int v) {
|
||||
emit valueFromCpp(456);
|
||||
emit valueFromCpp(456);
|
||||
}
|
||||
|
||||
Q_INVOKABLE int openUart(QString port,QString baudRate,QString dataBits,QString stopBits,
|
||||
QString flow);
|
||||
Q_INVOKABLE int openNetwork(QString ip,unsigned int port,bool is_ws,int type);
|
||||
Q_INVOKABLE int closeNetwork();
|
||||
|
||||
Q_INVOKABLE int closeNetwork(QString);
|
||||
Q_INVOKABLE int closeSerial();
|
||||
Q_INVOKABLE int TestLua();
|
||||
Q_INVOKABLE int TestShowData();
|
||||
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";
|
||||
|
||||
signals:
|
||||
void txtchanged();
|
||||
void valueFromCpp(int val);
|
||||
void sendToQml(int count);
|
||||
void disConnected();
|
||||
void clearBuffer();
|
||||
|
||||
public slots:
|
||||
void on_network_data_recv();
|
||||
void on_network_conected();
|
||||
void on_network_disconected();
|
||||
void on_network_conected(string,uint);
|
||||
void on_network_disconected(string,uint);
|
||||
void on_udp_data_recv();
|
||||
void on_network_server_read(string);
|
||||
|
||||
private:
|
||||
TcpServerLibevent *m_tcp_server;
|
||||
NetworkController *m_network_;
|
||||
SerialController *m_serial_controller_;
|
||||
LuaDelegate m_lua_;
|
||||
|
|
|
@ -113,7 +113,8 @@ void read_cb(struct bufferevent *bev, void *arg)
|
|||
char buf[1024] = {0};
|
||||
ConnectionLibevent* conn = (ConnectionLibevent*)arg;
|
||||
bufferevent_read(bev, buf, sizeof(buf));
|
||||
|
||||
auto server = conn->Server();
|
||||
emit(server->on_conn_read("12345"));
|
||||
cout << "client " << conn->IpAddress() << " say:" << buf << endl;
|
||||
conn->OnRecv(buf,sizeof(buf));
|
||||
}
|
||||
|
@ -135,7 +136,7 @@ void event_cb(struct bufferevent *bev, short events, void *arg)
|
|||
if (events & BEV_EVENT_EOF)
|
||||
{
|
||||
cout << "connection closed: " << conn->IpAddress() << " " << conn->SocketFd() << endl;
|
||||
conn->OnClose();
|
||||
conn->OnClose();
|
||||
bufferevent_free(bev);
|
||||
server->RemoveConnection(conn->SocketFd());
|
||||
}
|
||||
|
@ -144,6 +145,7 @@ void event_cb(struct bufferevent *bev, short events, void *arg)
|
|||
conn->OnClose();
|
||||
bufferevent_free(bev);
|
||||
server->RemoveConnection(conn->SocketFd());
|
||||
|
||||
cout << "some other error !" << endl;
|
||||
}
|
||||
delete conn;
|
||||
|
@ -166,6 +168,7 @@ void cb_listener(struct evconnlistener *listener, evutil_socket_t fd, struct soc
|
|||
server->AddConnection(ntohs(client->sin_port),conn);
|
||||
bufferevent_setcb(bev, read_cb, write_cb, event_cb, conn);
|
||||
bufferevent_enable(bev, EV_READ);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -176,11 +179,12 @@ void cb_listener(struct evconnlistener *listener, evutil_socket_t fd, struct soc
|
|||
*/
|
||||
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);
|
||||
while(p->m_status == TcpServerLibevent::RUNNING){
|
||||
int ret =event_base_loop(p->m_event_base,EVLOOP_NONBLOCK);
|
||||
if(ret < 0){
|
||||
p->m_status = TcpServerLibevent::FAIL;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -204,6 +208,7 @@ int TcpServerLibevent::AddConnection(uint32_t fd,ConnectionLibevent *p){
|
|||
else
|
||||
return -1;
|
||||
}
|
||||
emit(this->on_onnected(p->IpAddress(),p->SocketFd()));
|
||||
return 0;
|
||||
}
|
||||
/**
|
||||
|
@ -213,6 +218,8 @@ int TcpServerLibevent::AddConnection(uint32_t fd,ConnectionLibevent *p){
|
|||
*/
|
||||
int TcpServerLibevent::RemoveConnection(uint32_t fd){
|
||||
if( m_map_client.find(fd) != m_map_client.end()){
|
||||
emit(on_disconected(m_map_client.at(fd)->IpAddress(),
|
||||
m_map_client.at(fd)->SocketFd()));
|
||||
m_map_client.erase(fd);
|
||||
return 0;
|
||||
}else{
|
||||
|
@ -258,7 +265,7 @@ TcpServerLibevent::TcpServerLibevent(int port,string bindip) :
|
|||
m_event_listener = evconnlistener_new_bind(m_event_base,
|
||||
cb_listener,
|
||||
this,
|
||||
LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE,
|
||||
LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE|LEV_OPT_THREADSAFE,
|
||||
m_backlog,
|
||||
(struct sockaddr*)&m_server_addr,
|
||||
sizeof(m_server_addr));
|
||||
|
@ -276,8 +283,9 @@ TcpServerLibevent::TcpServerLibevent(int port,string bindip) :
|
|||
int TcpServerLibevent::StartServerSync(){
|
||||
if(m_status == STOP){
|
||||
m_status = RUNNING;
|
||||
event_base_dispatch(m_event_base);
|
||||
evconnlistener_free(m_event_listener);
|
||||
if(-1 == event_base_dispatch(m_event_base)){
|
||||
qDebug()<<"error ";
|
||||
}
|
||||
event_base_free(m_event_base);
|
||||
return 0;
|
||||
}
|
||||
|
@ -296,11 +304,37 @@ int TcpServerLibevent::StartServerAsync(){
|
|||
#ifdef linux
|
||||
evthread_use_pthreads();
|
||||
#endif
|
||||
m_thread = new thread(server_run,this);
|
||||
m_status = RUNNING;
|
||||
|
||||
m_thread = new std::thread(server_run,this);
|
||||
m_thread->detach();
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int TcpServerLibevent::StopServer()
|
||||
{
|
||||
struct timeval v;
|
||||
v.tv_usec = 1000;
|
||||
v.tv_sec = 0;
|
||||
|
||||
if(m_status == RUNNING){
|
||||
int ret = event_base_loopexit(m_event_base,&v);
|
||||
if (ret < 0){
|
||||
// todo write log
|
||||
|
||||
}
|
||||
evconnlistener_free(this->m_event_listener);
|
||||
if(ret < 0){
|
||||
// todo write log
|
||||
qDebug()<<"evconnlistener_disable"<<ret;
|
||||
|
||||
}
|
||||
m_status = STOP;
|
||||
return ret;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
/**
|
||||
* @description: start server asynchronous
|
||||
|
@ -310,7 +344,20 @@ int TcpServerLibevent::StartServerAsync(){
|
|||
TcpServerLibevent::~TcpServerLibevent(){
|
||||
if(this->m_status == RUNNING){
|
||||
m_thread->detach();
|
||||
event_base_loopbreak(m_event_base);
|
||||
this->m_status = STOP;
|
||||
struct timeval v;
|
||||
v.tv_usec = 1000;
|
||||
v.tv_sec = 1;
|
||||
|
||||
int ret = event_base_loopexit(m_event_base,&v);
|
||||
this->m_status = STOP;
|
||||
}
|
||||
if(nullptr != m_event_base){
|
||||
event_base_free(m_event_base);
|
||||
delete m_event_base;
|
||||
m_event_base = nullptr;
|
||||
}
|
||||
if(nullptr != m_event_listener){
|
||||
delete m_event_listener;
|
||||
m_event_listener = nullptr;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,11 +36,26 @@ extern "C"{
|
|||
#include <thread>
|
||||
#include <map>
|
||||
|
||||
|
||||
#define Qt_Support
|
||||
|
||||
|
||||
#ifdef Qt_Support
|
||||
#include <QObject>
|
||||
#include <QDebug>
|
||||
|
||||
#endif
|
||||
using namespace std;
|
||||
|
||||
class TcpServerLibevent;
|
||||
// tcp 连接
|
||||
|
||||
#ifdef Qt_Support
|
||||
class ConnectionLibevent : public QObject {
|
||||
Q_OBJECT
|
||||
#else
|
||||
class ConnectionLibevent{
|
||||
#endif
|
||||
public:
|
||||
ConnectionLibevent(TcpServerLibevent *p,
|
||||
struct bufferevent*v,
|
||||
|
@ -64,10 +79,17 @@ private:
|
|||
struct bufferevent *m_event;
|
||||
struct sockaddr_in *m_addr;
|
||||
uint32_t m_fd;
|
||||
|
||||
};
|
||||
|
||||
// 管理服务端
|
||||
class TcpServerLibevent{
|
||||
#ifdef Qt_Support
|
||||
class TcpServerLibevent : public QObject{
|
||||
Q_OBJECT
|
||||
#else
|
||||
class TcpServerLibevent {
|
||||
#endif
|
||||
|
||||
typedef enum{
|
||||
RUNNING,
|
||||
STOP,
|
||||
|
@ -80,6 +102,7 @@ public:
|
|||
~TcpServerLibevent();
|
||||
int StartServerSync(); // 同步启动服务器
|
||||
int StartServerAsync(); // 异步启动服务
|
||||
int StopServer();
|
||||
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);
|
||||
|
@ -87,7 +110,14 @@ public:
|
|||
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);
|
||||
|
||||
|
||||
#ifdef Qt_Support
|
||||
signals:
|
||||
int on_onnected(string ip,uint sock);
|
||||
int on_disconected(string ip,uint sock);
|
||||
int on_conn_read(string ip);
|
||||
|
||||
#endif
|
||||
private:
|
||||
uint32_t m_port; // 监听端口号
|
||||
string m_bind_ip; // 绑定端口号
|
||||
|
@ -97,11 +127,12 @@ private:
|
|||
struct event_base * m_event_base;
|
||||
struct evconnlistener* m_event_listener;
|
||||
SERVER_STATUS m_status;
|
||||
thread *m_thread;
|
||||
std::thread *m_thread;
|
||||
map<uint32_t,ConnectionLibevent*> m_map_client;
|
||||
OnAccept m_handle_accept;
|
||||
int AddConnection(uint32_t fd,ConnectionLibevent *p);
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
|
|
@ -73,7 +73,6 @@ int TcpSwarmClientLibevent::addConection(evutil_socket_t fd,struct bufferevent*
|
|||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int TcpSwarmClientLibevent::removeConection(evutil_socket_t fd){
|
||||
m_clients.erase(fd);
|
||||
return 0;
|
||||
|
|
Loading…
Reference in New Issue