From 1572832de677f61321adce74feeeea12d7f47b2b Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq.com> Date: Sun, 13 Mar 2022 01:50:44 +0800 Subject: [PATCH] add websocket server --- .../themes/default/basic/newmonitor.xml | 2 +- examples/proto_debuger/base_form.cpp | 79 ++++++++++++++++++- examples/proto_debuger/base_form.h | 2 + examples/proto_debuger/main.cpp | 3 +- examples/proto_debuger/new_monitor_form.cpp | 34 +++++++- examples/proto_debuger/proto_debuger.vcxproj | 2 + .../proto_debuger.vcxproj.filters | 9 +++ examples/proto_debuger/udp_group_form.cpp | 7 +- examples/proto_debuger/udp_group_form.h | 5 +- examples/proto_debuger/websocket_client.cpp | 27 +++++-- examples/proto_debuger/websocket_client.h | 1 + .../proto_debuger/websocket_client_form.cpp | 60 ++++++++++++-- .../proto_debuger/websocket_client_form.h | 10 ++- examples/proto_debuger/websocket_server.h | 9 --- 14 files changed, 212 insertions(+), 38 deletions(-) diff --git a/examples/Debug/resources/themes/default/basic/newmonitor.xml b/examples/Debug/resources/themes/default/basic/newmonitor.xml index 26d749bb..550f788b 100644 --- a/examples/Debug/resources/themes/default/basic/newmonitor.xml +++ b/examples/Debug/resources/themes/default/basic/newmonitor.xml @@ -29,7 +29,7 @@ - diff --git a/examples/proto_debuger/base_form.cpp b/examples/proto_debuger/base_form.cpp index ce59be46..d7e4a461 100644 --- a/examples/proto_debuger/base_form.cpp +++ b/examples/proto_debuger/base_form.cpp @@ -112,6 +112,26 @@ LRESULT BasicForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) delete mRightShow; mRightShow = nullptr; } + if (uMsg == WM_ADD_WEBSOCKET_CLIENT_CLOSE) { + wprintf(L"close %s\r\n", ((wstring*)wParam)->c_str()); + mRightShow->SetVisible(false); + mRightSide->SetAutoDestroy(true); + mRightSide->RemoveAll(); + + mWebsocketClientForm.erase(*(wstring*)(wParam)); + int cnt = mMonitor->GetRootNode()->GetChildNode(5)->GetChildNodeCount(); + ui::TreeNode* p = nullptr; + for (int i = 0; i < cnt; i++) { + if (mMonitor->GetRootNode()->GetChildNode(5)->GetChildNode(i)->GetText() + == *(wstring*)(wParam)) { + p = mMonitor->GetRootNode()->GetChildNode(5)->GetChildNode(i); + break; + } + } + mMonitor->GetRootNode()->GetChildNode(5)->RemoveChildNode(p); + // delete mRightShow; + mRightShow = nullptr; + } if (uMsg == WM_USER_TCP_CLIENT_CLOSE) { wprintf(L"close %s\r\n", ((wstring*)wParam)->c_str()); mRightShow->SetVisible(false); @@ -221,7 +241,7 @@ LRESULT BasicForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) client->SetOnReadHandler(std::bind(&WebsocketClientForm::OnReadHandler, form, std::placeholders::_1, std::placeholders::_2)); - form->SetChildLayoutXML(L"basic/tcp_server_form.xml"); + form->SetChildLayoutXML(L"basic/websocket_client_form.xml"); form->SetName(key); form->SetVisible(false); mWebsocketClientForm[key] = form; @@ -256,6 +276,57 @@ LRESULT BasicForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) } ); } + + if (uMsg == WM_ADD_WEBSOCKET_SERVER) { + TcpServerInfo* info = (TcpServerInfo*)wParam; + ui::TreeNode* node = new ui::TreeNode; + + mMonitor->GetRootNode()->GetChildNode(5)->AddChildNode(node); + auto key = info->ip; + node->SetText(key); + node->SetClass(L"listitem"); + + if (mWebsocketServerForm.find(info->ip) == mWebsocketServerForm.end()) + { + WebsocketServer* server = (WebsocketServer*)lParam; + auto form = new WebsocketServerForm(this, wstring2string(info->ip), server); + + form->SetChildLayoutXML(L"basic/tcp_server_form.xml"); + form->SetName(key); + form->SetVisible(false); + mWebsocketServerForm[key] = form; + if (!mRightSide->Add(form)) + printf("error 1"); + } + node->AttachAllEvents( + [this](ui::EventArgs* ev) { + if (ui::EventType::kEventSelect == ev->Type) { + wprintf(L"%s\r\n", dynamic_cast (ev->pSender)->GetText().c_str()); + printf("GetCurSel %d\r\n", mRightSide->GetCurSel()); + } + WebsocketServerForm* p = mWebsocketServerForm[dynamic_cast + (ev->pSender)->GetText()]; + if (p != nullptr) { + printf("GetCurSel %d\r\n", mRightSide->GetCurSel()); + p->SetAutoDestroy(true); + if (mRightShow != nullptr) { + mRightShow->SetVisible(false); + p->SetVisible(true); + mRightShow = p; + wprintf(L"%s", p->GetName()); + mRightSide->SelectItem(p->GetName()); + } + else { + wprintf(L"%s", p->GetName()); + p->SetVisible(true); + mRightSide->SelectItem(p->GetName()); + mRightShow = p; + } + } + return true; + } + ); + } if (uMsg == WM_ADD_TCPSERVER_MONITOR) { TcpServerInfo* info = (TcpServerInfo*)wParam; TcpServerLibevent* server = (TcpServerLibevent*)lParam; @@ -485,6 +556,7 @@ void BasicForm::InitWindow(){ return true; }); } + mMonitor = dynamic_cast(FindControl(L"tree")); mMonitor->SetClass(L"list"); mMonitor->SetIndent(5); @@ -573,6 +645,11 @@ LuaBindInterface* BasicForm::FindCurrentFormByLuaPointer(lua_State* pointer) return (LuaBindInterface*)itr->second; } } + for (auto itr = mWebsocketClientForm.begin(); itr != mWebsocketClientForm.end(); itr++) { + if (itr->second->LuaVM()->VM() == pointer) { + return (LuaBindInterface*)itr->second; + } + } return nullptr; } diff --git a/examples/proto_debuger/base_form.h b/examples/proto_debuger/base_form.h index 2d102333..b3aca7a0 100644 --- a/examples/proto_debuger/base_form.h +++ b/examples/proto_debuger/base_form.h @@ -21,6 +21,7 @@ #include "udp_group_form.h" #include "websocket_client_form.h" #include "websocket_client.h" +#include "websocket_server_form.h" #include #include @@ -64,6 +65,7 @@ private: std::map mUdpForm; std::map mUdpGroupForm; std::map mWebsocketClientForm; + std::map mWebsocketServerForm; ui::Control* mRightShow; }; diff --git a/examples/proto_debuger/main.cpp b/examples/proto_debuger/main.cpp index 55397cc4..39036316 100644 --- a/examples/proto_debuger/main.cpp +++ b/examples/proto_debuger/main.cpp @@ -93,7 +93,8 @@ void MainThread::Init() #endif auto dpiManager = ui::DpiManager::GetInstance(); - dpiManager->SetAdaptDPI(); +// dpiManager->SetAdaptDPI(); + dpiManager->SetScale(1); // 创建一个默认带有阴影的居中窗口 gMainWindow = new BasicForm(); diff --git a/examples/proto_debuger/new_monitor_form.cpp b/examples/proto_debuger/new_monitor_form.cpp index 97a09b91..86a12cc0 100644 --- a/examples/proto_debuger/new_monitor_form.cpp +++ b/examples/proto_debuger/new_monitor_form.cpp @@ -11,6 +11,7 @@ #include "tcp_client.h" #include "udp_libevent.h" #include "websocket_client.h" +#include "websocket_server.h" #include "utils.h" #include @@ -217,7 +218,7 @@ void NewMonitorForm::InitWindow() p->ip = m_ip_select->GetText(); // p->port = port; // p->socket_fd = udp->SocketFD(); - printf("开启服务端 %d \r\n", p->socket_fd); + printf("websocket 客户端 %d \r\n", p->socket_fd); auto succ = ::PostMessage(m_parent->GetHWND(), WM_ADD_WEBSOCKET_CLIENT, (WPARAM)p, (LPARAM)wsclient); @@ -225,6 +226,25 @@ void NewMonitorForm::InitWindow() printf("postmessage error :%d\r\n", GetLastError()); } } + + if (m_combo_type->GetText() == L"websocket server") { + wprintf(L"%s\r\n", m_ip_select->GetText().c_str()); + wprintf(L"%s\r\n", m_port_select->GetText().c_str()); + int port = atoi(wstring2string(m_port_select->GetText()).c_str()); + WebsocketServer* wsserver = new + WebsocketServer(wstring2string(m_ip_select->GetText().c_str()), atoi(wstring2string(m_port_select->GetText()).c_str())); + + TcpServerInfo* p = new TcpServerInfo; + p->ip = m_ip_select->GetText(); + p->port = port; + // p->socket_fd = udp->SocketFD(); + printf("websocket 服务端 %d \r\n", port); + auto succ = ::PostMessage(m_parent->GetHWND(), + WM_ADD_WEBSOCKET_SERVER, (WPARAM)p, (LPARAM)wsserver); + if (!succ) { + printf("postmessage error :%d\r\n", GetLastError()); + } + } return true; }); } @@ -364,15 +384,23 @@ void NewMonitorForm::InitWindow() } } if ((text == L"tcp client")|| (text == L"tcp server") || - (text == L"udp") || (text == L"udp group") || (text == L"websocket server") || (text == L"websocket client") - ) { + (text == L"udp") || (text == L"udp group")) { this->m_ip_config_vbox->SetVisible(true); this->m_uart_config_vbox->SetVisible(false); + m_port_select->SetVisible(true); + m_ip_select->SetText(L"127.0.0.1"); m_port_select->SetText(L"9001"); } + if ( (text == L"websocket server") || (text == L"websocket client")) { + this->m_ip_config_vbox->SetVisible(true); + this->m_uart_config_vbox->SetVisible(false); + m_port_select->SetVisible(false); + m_ip_select->SetText(L"wss://117.50.176.114:8080/echo"); + m_port_select->SetText(L"9001"); + } } } return true; diff --git a/examples/proto_debuger/proto_debuger.vcxproj b/examples/proto_debuger/proto_debuger.vcxproj index 8dffd5ca..e147ed6d 100644 --- a/examples/proto_debuger/proto_debuger.vcxproj +++ b/examples/proto_debuger/proto_debuger.vcxproj @@ -183,6 +183,7 @@ + @@ -204,6 +205,7 @@ + diff --git a/examples/proto_debuger/proto_debuger.vcxproj.filters b/examples/proto_debuger/proto_debuger.vcxproj.filters index 2bdcd261..afa673ab 100644 --- a/examples/proto_debuger/proto_debuger.vcxproj.filters +++ b/examples/proto_debuger/proto_debuger.vcxproj.filters @@ -75,6 +75,9 @@ 婧愭枃浠 + + 婧愭枃浠 + @@ -134,6 +137,9 @@ 澶存枃浠 + + 澶存枃浠 + @@ -157,6 +163,9 @@ 璧勬簮鏂囦欢 + + 璧勬簮鏂囦欢 + diff --git a/examples/proto_debuger/udp_group_form.cpp b/examples/proto_debuger/udp_group_form.cpp index 27441e8d..d0ae6af5 100644 --- a/examples/proto_debuger/udp_group_form.cpp +++ b/examples/proto_debuger/udp_group_form.cpp @@ -11,8 +11,8 @@ UdpGroupForm::UdpGroupForm(ui::Window* hwnd, string url, uint32_t port, UdpDataG this->mUdpPeer = p; this->m_url = url; this->m_port = port; - mLua = new LuaDelegate; + mLua = new LuaDelegate; mLuaFile.open(UDP_LUA_SCRIPT); std::string lua_script; if (mLuaFile.is_open()) { @@ -24,20 +24,19 @@ UdpGroupForm::UdpGroupForm(ui::Window* hwnd, string url, uint32_t port, UdpDataG } mLuaScript = lua_script; mLua->DoString(lua_script); + this->mLua->BindFunction("showdata", LuaShowData); UdpDataGramLibevent::OnReadDataHandle onRecvCallBack = [this](const char* dat, int len, struct sockaddr_in sendarr) { std::cout << "receive udp datagram len" << len << dat << "\r\n"; - this->mLua->CallFuntion("OnUdpData", std::string(dat), inet_ntoa(sendarr.sin_addr), sendarr.sin_port); - }; + if (mUdpPeer != nullptr) { std::cout << "\r\n" << &onRecvCallBack << "\r\n"; mUdpPeer->SetOnReadHandle(onRecvCallBack); } - this->mLua->BindFunction("showdata", LuaShowData); std::cout << "lua script is " << lua_script << std::endl; } diff --git a/examples/proto_debuger/udp_group_form.h b/examples/proto_debuger/udp_group_form.h index f03d9f2d..58f850fd 100644 --- a/examples/proto_debuger/udp_group_form.h +++ b/examples/proto_debuger/udp_group_form.h @@ -45,15 +45,14 @@ private: ui::Button* m_button_3; ui::Button* m_button_4; - + LuaDelegate* mLua; + std::string mLuaScript; std::ifstream mLuaFile; std::ofstream mLuaFileEdit; virtual void HandleMessage(ui::EventArgs& msg); - std::string mLuaScript; std::string m_url; int m_port; - LuaDelegate* mLua; UdpDataGramLibevent* mUdpPeer; }; diff --git a/examples/proto_debuger/websocket_client.cpp b/examples/proto_debuger/websocket_client.cpp index 7c89ca15..66499daf 100644 --- a/examples/proto_debuger/websocket_client.cpp +++ b/examples/proto_debuger/websocket_client.cpp @@ -94,10 +94,18 @@ void on_fail(WebsocketClient* c, websocketpp::connection_hdl hdl) { WebsocketClient::~WebsocketClient() { this->m_status = WebsocketClient::STOP; - m_client.stop(); + if(m_tls) + m_client_tls.stop(); + else + m_client.stop(); m_thread->join(); } +void WebsocketClient::Close() +{ + this->m_status = STOP; +} + std::string WebsocketClient::Url() { return this->m_url; @@ -126,6 +134,11 @@ WebsocketClient::WebsocketClient(std::string url, bool tls) websocketpp::lib::error_code ec; std::cout << "1" << std::endl; m_conn_tls = m_client_tls.get_connection(this->m_url, ec); + if (ec) { + std::cout << "could not create connection because: " << ec.message() << std::endl; + this->m_status = Status::FAIL; + break; + } m_conn_tls->set_open_handler(websocketpp::lib::bind( &on_open, this, @@ -141,11 +154,7 @@ WebsocketClient::WebsocketClient(std::string url, bool tls) this, websocketpp::lib::placeholders::_1 )); - if (ec) { - std::cout << "could not create connection because: " << ec.message() << std::endl; - this->m_status = Status::FAIL; - break; - } + std::cout << "2" << std::endl; // Note that connect here only requests a connection. No network messages are // exchanged until the event loop starts running in the next line. @@ -163,7 +172,8 @@ WebsocketClient::WebsocketClient(std::string url, bool tls) ) { try { int count_of_handler = this->m_client_tls.run(); - // } + std::cout << "4 " << std::endl; + // run搴旇鍙墽琛屼竴娆″氨浼氶鍑 } catch (std::exception e) { @@ -173,7 +183,8 @@ WebsocketClient::WebsocketClient(std::string url, bool tls) Sleep(1000); } std::cout << "close"; - this->m_on_disconnected(this, WebsocketClient::CloseReason::LOCAL_CLOSED); + if(m_on_disconnected) + this->m_on_disconnected(this, WebsocketClient::CloseReason::LOCAL_CLOSED); }); } else { diff --git a/examples/proto_debuger/websocket_client.h b/examples/proto_debuger/websocket_client.h index c0f357be..61e1d449 100644 --- a/examples/proto_debuger/websocket_client.h +++ b/examples/proto_debuger/websocket_client.h @@ -52,6 +52,7 @@ public: std::string Url(); WebsocketClient(std::string url,bool tls); ~WebsocketClient(); + void Close(); int SendMsg(const char * str,uint32_t len,websocketpp::frame::opcode::value); friend void on_fail(WebsocketClient * c, websocketpp::connection_hdl hdl); friend void on_close(WebsocketClient * c, websocketpp::connection_hdl hdl); diff --git a/examples/proto_debuger/websocket_client_form.cpp b/examples/proto_debuger/websocket_client_form.cpp index a6735838..e0f9f817 100644 --- a/examples/proto_debuger/websocket_client_form.cpp +++ b/examples/proto_debuger/websocket_client_form.cpp @@ -1,5 +1,10 @@ #include "websocket_client_form.h" #include "utils.h" +#include "lua_bind.h" + + + +#define WEBSOCKET_CLIENT_LUA "websocket_client.lua" WebsocketClientForm::WebsocketClientForm(ui::Window* hwnd, string url, WebsocketClient* p) { @@ -8,10 +13,26 @@ WebsocketClientForm::WebsocketClientForm(ui::Window* hwnd, string url, Websocket } mWebsocketClient = p; + mLua = new LuaDelegate; + mLuaFile.open(WEBSOCKET_CLIENT_LUA); + std::string lua_script; + if (mLuaFile.is_open()) { + std::string s; + while (getline(mLuaFile, s)) { + lua_script += s + "\r\n"; + } + mLuaFile.close(); + } + mLuaScript = lua_script; + mLua->DoString(lua_script); + this->mLua->BindFunction("showdata", LuaShowData); + } WebsocketClientForm::~WebsocketClientForm() { + std::cout << "~WebsocketClientForm"<(FindSubControl(L"server_info")); - m_rich_edit_1 = dynamic_cast(FindSubControl(L"uart_recv_eidt")); - m_rich_edit_2 = dynamic_cast(FindSubControl(L"lua_script")); - m_uart_send_edit = dynamic_cast(FindSubControl(L"uart_send_edit")); + m_rich_edit_1 = dynamic_cast(FindSubControl(L"server_recv_edit")); + m_rich_edit_2 = dynamic_cast(FindSubControl(L"lua_script_edit")); + + m_rich_edit_2->SetRich(true); + m_rich_edit_2->SetReturnMsgWantCtrl(true); + + m_rich_edit_2->SetText(string2wstring(mLuaScript)); + + m_websocket_send_edit = dynamic_cast(FindSubControl(L"server_send_edit")); m_btn_send_data = dynamic_cast(FindSubControl(L"btn_send_data")); m_check_box_2 = dynamic_cast(FindSubControl(L"check_time_send")); m_check_box_3 = dynamic_cast(FindSubControl(L"check_hex_send")); @@ -39,6 +66,7 @@ void WebsocketClientForm::Init() string2wstring(mWebsocketClient->Url()) + L"连接失败"); } } + if (nullptr != m_btn_close_form) m_btn_close_form->AttachClick([this](const ui::EventArgs* ev) { wstring* name = new wstring(this->GetName()); @@ -51,14 +79,31 @@ void WebsocketClientForm::Init() if(nullptr != m_btn_save_lua) m_btn_save_lua->AttachClick( [this](ui::EventArgs* ev){ - - return true; + std::cout << "保存lua脚本\r\n"; + std::string lua = wstring2string(this->m_rich_edit_2->GetText()); + if (0 == this->mLua->UpdateScript(lua)) { + this->mLuaScript = lua; + mLuaFileEdit = std::ofstream(WEBSOCKET_CLIENT_LUA, std::ios::out | std::ios::trunc); + mLuaFileEdit.write(lua.c_str(), lua.size()); + mLuaFileEdit.flush(); + mLuaFileEdit.close(); + } + else { + MessageBox(0, L"lua脚本错误", L"", 0); + return true; + } + std::cout << lua.c_str() << "\r\n"; + return true; } ); if (nullptr != m_btn_send_data) { m_btn_send_data->AttachClick([this](const ui::EventArgs* ev) { - + this->mWebsocketClient->SendMsg( + wstring2string(m_websocket_send_edit->GetText()).c_str(), + wstring2string(m_websocket_send_edit->GetText()).size(), + websocketpp::frame::opcode::TEXT); + m_websocket_send_edit->SetText(std::wstring(L"")); return true; }); } @@ -66,7 +111,7 @@ void WebsocketClientForm::Init() LuaDelegate* WebsocketClientForm::LuaVM() { - return nullptr; + return mLua; } void WebsocketClientForm::ShowDataInEdit(const char*src) @@ -96,4 +141,5 @@ void WebsocketClientForm::OnDisConnected(WebsocketClient* ws, WebsocketClient:: void WebsocketClientForm::OnReadHandler(WebsocketClient* ws, std::string message) { std::cout << "recv data from hanlder" << message.c_str(); + this->mLua->CallFuntion("OnWebSocketData", message); } diff --git a/examples/proto_debuger/websocket_client_form.h b/examples/proto_debuger/websocket_client_form.h index 45e5e0df..f0c926f0 100644 --- a/examples/proto_debuger/websocket_client_form.h +++ b/examples/proto_debuger/websocket_client_form.h @@ -15,6 +15,7 @@ #include "udp_libevent.h" #include #include "websocket_client.h" +#include "lua_bind.h" class WebsocketClientForm : public ui::ChildBox, @@ -33,7 +34,7 @@ private: ui::Label* m_label_1; ui::RichEdit* m_rich_edit_1; ui::RichEdit* m_rich_edit_2; - ui::RichEdit* m_uart_send_edit; + ui::RichEdit* m_websocket_send_edit; ui::Button* m_btn_send_data; ui::Button* m_btn_save_lua; ui::Button* m_btn_close_form; @@ -41,8 +42,15 @@ private: ui::CheckBox* m_check_box_2; ui::CheckBox* m_check_box_3; ui::CheckBox* m_check_box_4; + + WebsocketClient* mWebsocketClient; std::fstream mLuaFileRead; std::ofstream mLuaFileEdit; + + + LuaDelegate* mLua; + std::string mLuaScript; + std::ifstream mLuaFile; }; diff --git a/examples/proto_debuger/websocket_server.h b/examples/proto_debuger/websocket_server.h index 16b2f57d..36fb861f 100644 --- a/examples/proto_debuger/websocket_server.h +++ b/examples/proto_debuger/websocket_server.h @@ -1,12 +1,3 @@ -/* - * @Author: your name - * @Date: 2022-02-28 21:03:56 - * @LastEditTime: 2022-02-28 23:24:06 - * @LastEditors: Please set LastEditors - * @Description: 鎵撳紑koroFileHeader鏌ョ湅閰嶇疆 杩涜璁剧疆: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE - * @FilePath: \test\websocket_server.h - */ - #pragma once #include #include