From 2e8a7cbc8aa59041b5ae55e23ad1ad4449d2b217 Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq.com> Date: Thu, 3 Feb 2022 00:22:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BF=9D=E5=AD=98lua?= =?UTF-8?q?=E8=84=9A=E6=9C=AC=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/proto_debuger/tcp_client.lua | 16 +--------------- examples/proto_debuger/tcp_client_form.cpp | 19 ++++++++++++++----- examples/proto_debuger/tcp_client_form.h | 5 ++++- examples/proto_debuger/tcp_server.lua | 17 +---------------- examples/proto_debuger/tcp_server_form.cpp | 19 +++++++++++++++---- examples/proto_debuger/tcp_server_form.h | 4 +++- examples/proto_debuger/uart_form.cpp | 6 +++++- examples/proto_debuger/uart_process.h | 3 ++- .../{script.lua => uart_script.lua} | 0 9 files changed, 45 insertions(+), 44 deletions(-) rename examples/proto_debuger/{script.lua => uart_script.lua} (100%) diff --git a/examples/proto_debuger/tcp_client.lua b/examples/proto_debuger/tcp_client.lua index 04a85e75..3f564540 100644 --- a/examples/proto_debuger/tcp_client.lua +++ b/examples/proto_debuger/tcp_client.lua @@ -1,15 +1 @@ -require("string") - -function OnNewClient(ip,port) - showdata("on new client from lua " .. ip..port) -end - -function OnClientRecvData(data) - showdata("client recieve data from lua "..data) -end - - -function OnClientDisconnect(ip,port) - showdata("client leave from lua " .. ip..port) -end - +require("string") function OnNewClient(ip,port) showdata("on new client from lua " .. ip..port.."32232323\r\n") end function OnClientRecvData(data) showdata("client recieve data from lua "..data.."\r\n") end function OnClientDisconnect(ip,port) showdata("client leave from lua " .. ip..port) end \ No newline at end of file diff --git a/examples/proto_debuger/tcp_client_form.cpp b/examples/proto_debuger/tcp_client_form.cpp index a2ea6c5d..bbe6f736 100644 --- a/examples/proto_debuger/tcp_client_form.cpp +++ b/examples/proto_debuger/tcp_client_form.cpp @@ -2,6 +2,12 @@ #include "utils.h" #include "lua_bind.h" + +#define TCP_CLIENT_SCRIPT "tcp_client.lua" +TcpClientForm::~TcpClientForm() { + +} + TcpClientForm::TcpClientForm(ui::Window* hwnd,std::string url, uint32_t port, TcpClientLibevent* p): m_connected(false), mLua(nullptr) @@ -13,14 +19,14 @@ TcpClientForm::TcpClientForm(ui::Window* hwnd,std::string url, uint32_t port, Tc } mLua = new LuaDelegate; - mLuaFile.open("tcp_client.lua"); + mLuaFileRead.open(TCP_CLIENT_SCRIPT); std::string lua_script; - if (mLuaFile.is_open()) { + if (mLuaFileRead.is_open()) { std::string s; - while (getline(mLuaFile, s)) { + while (getline(mLuaFileRead, s)) { lua_script += s + "\r\n"; } - mLuaFile.close(); + mLuaFileRead.close(); } mLuaScript = lua_script; mLua->DoString(lua_script); @@ -28,7 +34,6 @@ TcpClientForm::TcpClientForm(ui::Window* hwnd,std::string url, uint32_t port, Tc std::cout << "lua script is " << lua_script << std::endl; m_url = url; m_port = port; - mLua = new LuaDelegate(); this->mLua->BindFunction("showdata", LuaShowData); } @@ -81,6 +86,10 @@ void TcpClientForm::Init() std::string lua = wstring2string(m_rich_edit_2->GetText()); if (0 == this->mLua->UpdateScript(lua)) { this->mLuaScript = lua; + mLuaFileEdit = std::ofstream(TCP_CLIENT_SCRIPT, std::ios::out | std::ios::trunc); + mLuaFileEdit.write(lua.c_str(), lua.size()); + mLuaFileEdit.flush(); + mLuaFileEdit.close(); } else { MessageBox(0, L"lua½Å±¾´íÎó", L"", 0); diff --git a/examples/proto_debuger/tcp_client_form.h b/examples/proto_debuger/tcp_client_form.h index 00786eeb..1cb77d4c 100644 --- a/examples/proto_debuger/tcp_client_form.h +++ b/examples/proto_debuger/tcp_client_form.h @@ -29,6 +29,7 @@ class TcpClientForm : public: TcpClientForm(ui::Window* hwnd,string url, uint32_t port, TcpClientLibevent* p); + ~TcpClientForm(); virtual void Init() override; TcpClientLibevent* ClientEvent(); LuaDelegate* LuaVM(); @@ -52,7 +53,9 @@ private: ui::CheckBox* m_check_box_3; ui::CheckBox* m_check_box_4; TcpClientLibevent* mClient; - std::ifstream mLuaFile; + std::fstream mLuaFileRead; + std::ofstream mLuaFileEdit; + virtual void HandleMessage(ui::EventArgs& msg); std::string mLuaScript; std::string m_url; diff --git a/examples/proto_debuger/tcp_server.lua b/examples/proto_debuger/tcp_server.lua index dae452fd..a1defaf0 100644 --- a/examples/proto_debuger/tcp_server.lua +++ b/examples/proto_debuger/tcp_server.lua @@ -1,16 +1 @@ -require("string") -local file = io.open("writetest.txt", "w+") - -function OnNewClient(ip,port) - showdata("on new client from lua " .. ip..port) -end - -function OnClientRecvData(data,ip,port) - showdata("client recieve data from lua "..data.. ip..port) -end - - -function OnClientDisconnect(ip,port) - showdata("client leave from lua " .. ip..port) -end - +require("string") local file = io.open("writetest.txt", "w+") function OnNewClient(ip,port) showdata("on new client from lua " .. ip..port) end function OnClientRecvData(data,ip,port) showdata("client recieve data from lua "..data.. ip..port) end function OnClientDisconnect(ip,port) showdata("client leave from lua " .. ip..port) end \ No newline at end of file diff --git a/examples/proto_debuger/tcp_server_form.cpp b/examples/proto_debuger/tcp_server_form.cpp index 0e2483fd..1f69143d 100644 --- a/examples/proto_debuger/tcp_server_form.cpp +++ b/examples/proto_debuger/tcp_server_form.cpp @@ -12,6 +12,13 @@ extern "C" { #include "event2/thread.h" }; + +#define TCP_SERVER_SCRIPT "tcp_server.lua" +TcpServerFrom::~TcpServerFrom() { + +} + + TcpServerFrom::TcpServerFrom(ui::Window* hwnd, string url, uint32_t port, TcpServerLibevent* p): mLua(nullptr) @@ -22,14 +29,14 @@ TcpServerFrom::TcpServerFrom(ui::Window* hwnd, string url, mFlagSelectClient = true; mLua = new LuaDelegate; - mLuaFile.open("tcp_server.lua"); + mLuaFileRead.open(TCP_SERVER_SCRIPT); std::string lua_script; - if (mLuaFile.is_open()) { + if (mLuaFileRead.is_open()) { std::string s; - while (getline(mLuaFile, s)) { + while (getline(mLuaFileRead, s)) { lua_script += s + "\r\n"; } - mLuaFile.close(); + mLuaFileRead.close(); } mLuaScript = lua_script; mLua->DoString(lua_script); @@ -102,6 +109,10 @@ void TcpServerFrom::Init() std::string lua = wstring2string(mEditLua->GetText()); if (0 == this->mLua->UpdateScript(lua)) { this->mLuaScript = lua; + mLuaFileEdit = std::ofstream(TCP_SERVER_SCRIPT, std::ios::out | std::ios::trunc); + mLuaFileEdit.write(lua.c_str(), lua.size()); + mLuaFileEdit.flush(); + mLuaFileEdit.close(); } else { MessageBox(0, L"lua½Å±¾´íÎó", L"", 0); diff --git a/examples/proto_debuger/tcp_server_form.h b/examples/proto_debuger/tcp_server_form.h index e6665f88..37344e8e 100644 --- a/examples/proto_debuger/tcp_server_form.h +++ b/examples/proto_debuger/tcp_server_form.h @@ -17,6 +17,7 @@ class TcpServerFrom : { public: TcpServerFrom(ui::Window* hwnd, string url, uint32_t port, TcpServerLibevent* p); + ~TcpServerFrom(); TcpServerLibevent* ServerP(); virtual void Init() override; void OnNewConnAccept(ConnectionLibevent*); @@ -47,7 +48,8 @@ private: ui::Label* m_label_2; ui::Combo* m_combo_1; ui::Button* m_button_3; - std::ifstream mLuaFile; + std::ifstream mLuaFileRead; + std::ofstream mLuaFileEdit; bool mFlagSelectClient; LuaDelegate *mLua; std::string mLuaScript; diff --git a/examples/proto_debuger/uart_form.cpp b/examples/proto_debuger/uart_form.cpp index ddc8b878..e86bad73 100644 --- a/examples/proto_debuger/uart_form.cpp +++ b/examples/proto_debuger/uart_form.cpp @@ -5,6 +5,8 @@ #include #include "lua_bind.h" + +#define UART_LUA_SCRIPT "uart_script.lua" UartForm::UartForm(ui::Window* hwnd,std::wstring name, uint32_t baurate,UINT portnum, uint8_t data_bits, uint8_t stop_bits, @@ -24,7 +26,7 @@ UartForm::UartForm(ui::Window* hwnd,std::wstring name, m_runing = true; mLua = new LuaDelegate; - mLuaFile.open("script.lua"); + mLuaFile.open(UART_LUA_SCRIPT); std::string lua_script; if (mLuaFile.is_open()) { std::string s; @@ -69,6 +71,8 @@ UartForm::UartForm(ui::Window* hwnd,std::wstring name, UartForm::~UartForm() { SerialPort::ClosePort(m_portnum); + mLuaFile.clear(); + mLuaFile.close(); } void UartForm::ShowDataInEdit(const char*p) { diff --git a/examples/proto_debuger/uart_process.h b/examples/proto_debuger/uart_process.h index edf0474f..63b18915 100644 --- a/examples/proto_debuger/uart_process.h +++ b/examples/proto_debuger/uart_process.h @@ -29,7 +29,7 @@ public: uint8_t verify, uint8_t flow_control); ~UartForm(); - + void ShowDataInEdit(const char*) override; void OnUpdateUart(); LuaDelegate* LuaVM(); @@ -55,6 +55,7 @@ public: LuaDelegate* mLua; std::string mLuaScript; std::ifstream mLuaFile; + std::ofstream mLuaFileEdit; private: virtual void HandleMessage(ui::EventArgs& msg); diff --git a/examples/proto_debuger/script.lua b/examples/proto_debuger/uart_script.lua similarity index 100% rename from examples/proto_debuger/script.lua rename to examples/proto_debuger/uart_script.lua