From 5f70c4333fd4b394b7aa885c4eb252db84797b11 Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq.com> Date: Wed, 9 Feb 2022 23:31:01 +0800 Subject: [PATCH] no message --- examples/proto_debuger/base_form.cpp | 46 ++++++- examples/proto_debuger/base_form.h | 2 + examples/proto_debuger/proto_debuger.vcxproj | 4 +- .../proto_debuger.vcxproj.filters | 8 +- examples/proto_debuger/udp_form.cpp | 32 +++++ examples/proto_debuger/udp_form.h | 28 ++-- examples/proto_debuger/udp_libevent.cpp | 129 ++++++++++++++++++ examples/proto_debuger/udp_libevent.h | 53 +++++++ 8 files changed, 280 insertions(+), 22 deletions(-) create mode 100644 examples/proto_debuger/udp_libevent.cpp create mode 100644 examples/proto_debuger/udp_libevent.h diff --git a/examples/proto_debuger/base_form.cpp b/examples/proto_debuger/base_form.cpp index b0f6ac49..789f13c7 100644 --- a/examples/proto_debuger/base_form.cpp +++ b/examples/proto_debuger/base_form.cpp @@ -1,5 +1,6 @@ #include "base_form.h" #include "serial_port.h" +#include "udp_form.h" #include "utils.h" #include "global.h" @@ -177,15 +178,54 @@ LRESULT BasicForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) printf("add udp monitor"); wchar_t pdata[100] = L"\0"; TcpServerInfo* p = (TcpServerInfo*)wParam; - wsprintf(pdata,L"%s:%d", p->ip.c_str(),p->port); + UdpDataGramLibevent* udp = (UdpDataGramLibevent*)lParam; + + wsprintf(pdata, L"%s:%d", p->ip.c_str(), p->port); ui::TreeNode* node = new ui::TreeNode; node->SetText(pdata); node->SetClass(L"listitem"); mMonitor->GetRootNode()->GetChildNode(3)->AddChildNode(node); - return WindowImplBase::HandleMessage(uMsg, wParam, lParam); - } + if (mUdpForm.find(std::wstring(pdata)) == mUdpForm.end()) + { + auto form = new UdpForm(this, wstring2string(p->ip), p->port, udp); + form->SetChildLayoutXML(L"basic/udp_form.xml"); + form->SetName(pdata); + form->SetVisible(false); + mUdpForm[pdata] = 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()); + UdpForm* p = mUdpForm[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_UART_MONITOR) { printf("add uart monitor"); UartInfo* p = (UartInfo*)wParam; diff --git a/examples/proto_debuger/base_form.h b/examples/proto_debuger/base_form.h index c7b19984..f9402dea 100644 --- a/examples/proto_debuger/base_form.h +++ b/examples/proto_debuger/base_form.h @@ -17,6 +17,7 @@ #include "uart_process.h" #include "tcp_client_form.h" #include "tcp_server_form.h" +#include "udp_form.h" #include #include @@ -57,6 +58,7 @@ private: std::map mUartForm; std::map mTcpClientForm; std::map mTcpServerForm; + std::map mUdpForm; ui::Control* mRightShow; }; diff --git a/examples/proto_debuger/proto_debuger.vcxproj b/examples/proto_debuger/proto_debuger.vcxproj index 3b6a335f..8bb7ee8f 100644 --- a/examples/proto_debuger/proto_debuger.vcxproj +++ b/examples/proto_debuger/proto_debuger.vcxproj @@ -174,6 +174,7 @@ + @@ -192,6 +193,7 @@ + @@ -201,7 +203,7 @@ - + diff --git a/examples/proto_debuger/proto_debuger.vcxproj.filters b/examples/proto_debuger/proto_debuger.vcxproj.filters index ea0a722e..44279c15 100644 --- a/examples/proto_debuger/proto_debuger.vcxproj.filters +++ b/examples/proto_debuger/proto_debuger.vcxproj.filters @@ -60,6 +60,9 @@ 源文件 + + 源文件 + @@ -107,6 +110,9 @@ 头文件 + + 头文件 + @@ -127,7 +133,7 @@ 资源文件 - + 资源文件 diff --git a/examples/proto_debuger/udp_form.cpp b/examples/proto_debuger/udp_form.cpp index 90c859ba..47c2df3f 100644 --- a/examples/proto_debuger/udp_form.cpp +++ b/examples/proto_debuger/udp_form.cpp @@ -1,5 +1,37 @@ #include "udp_form.h" +UdpForm::UdpForm(ui::Window* hwnd, string url, uint32_t port, UdpDataGramLibevent* p) +{ + this->mUdpPeer = p; + this->m_url = url; + this->m_port = port; +} + +UdpForm::~UdpForm() +{ +} + +void UdpForm::Init() +{ + ui::ChildBox::Init(); + + + ui::Label* m_label_1 = dynamic_cast(FindSubControl(L"uart_info_label")); + ui::RichEdit* m_rich_edit_1 = dynamic_cast(FindSubControl(L"uart_recv_eidt")); + ui::RichEdit* m_rich_edit_2 = dynamic_cast(FindSubControl(L"lua_script")); + ui::RichEdit* m_rich_edit_3 = dynamic_cast(FindSubControl(L"uart_send_edit")); + ui::Button* m_button_1 = dynamic_cast(FindSubControl(L"btn_send_data")); + ui::Button* m_button_2 = dynamic_cast(FindSubControl(L"btn_close_uart")); + ui::Button* m_button_3 = dynamic_cast(FindSubControl(L"btn_save_lua")); + ui::Button* m_button_4 = dynamic_cast(FindSubControl(L"btn_do_lua")); + +} + +LuaDelegate* UdpForm::LuaVM() +{ + return mLua; +} + void UdpForm::ShowDataInEdit(const char*) { } diff --git a/examples/proto_debuger/udp_form.h b/examples/proto_debuger/udp_form.h index e3bc21a2..ce0b3c29 100644 --- a/examples/proto_debuger/udp_form.h +++ b/examples/proto_debuger/udp_form.h @@ -19,19 +19,17 @@ #include "lua_wraper.h" #include "global.h" #include - +#include "udp_libevent.h" #include class UdpForm : - public ui::ChildBox, - public TcpClientLibevent::TcpClientObserver + public ui::ChildBox { public: - UdpForm(ui::Window* hwnd, string url, uint32_t port, TcpClientLibevent* p); + UdpForm(ui::Window* hwnd, string url, uint32_t port, UdpDataGramLibevent* p); ~UdpForm(); virtual void Init() override; - TcpClientLibevent* ClientEvent(); LuaDelegate* LuaVM(); protected: @@ -41,23 +39,19 @@ 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::Button* m_btn_send_data; - ui::Button* m_btn_save_lua; - ui::CheckBox* m_check_box_1; - ui::CheckBox* m_check_box_2; - ui::CheckBox* m_check_box_3; - ui::CheckBox* m_check_box_4; - TcpClientLibevent* mClient; - std::fstream mLuaFileRead; - std::ofstream mLuaFileEdit; + ui::RichEdit* m_rich_edit_3; + ui::Button* m_button_1; + ui::Button* m_button_2; + ui::Button* m_button_3; + ui::Button* m_button_4; + virtual void HandleMessage(ui::EventArgs& msg); std::string mLuaScript; std::string m_url; int m_port; - bool m_connected; LuaDelegate* mLua; - + UdpDataGramLibevent* mUdpPeer; }; + diff --git a/examples/proto_debuger/udp_libevent.cpp b/examples/proto_debuger/udp_libevent.cpp new file mode 100644 index 00000000..ded555b3 --- /dev/null +++ b/examples/proto_debuger/udp_libevent.cpp @@ -0,0 +1,129 @@ +#include "udp_libevent.h" +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif +#include +#include + +#ifdef __cplusplus +} +#endif +#define MSG_LEN 1024 + +void read_cb(evutil_socket_t fd, short event, void* arg) { + UdpDataGramLibevent* parent = (UdpDataGramLibevent*)arg; + if (parent == nullptr) + return; + + char buf[MSG_LEN]; + int len; + int addr_len = sizeof(struct sockaddr); + struct sockaddr_in cli_addr; + + memset(buf, 0, sizeof(buf)); + len = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr*)&cli_addr, &addr_len); + + if (len == -1) { + perror("recvfrom"); + } + else if (len == 0) { + printf("Connection Closed\n"); + } + else { + buf[len] = '\0'; + printf("recv[%s:%d]\n", buf, len); + // sendto(fd, buf, len, 0, (struct sockaddr*)&cli_addr, addr_len); + if (parent->OnReadHandle() != nullptr) { + (*parent->OnReadHandle())(buf, len, cli_addr); + } + } +} + +int UdpDataGramLibevent::bind_socket(struct event* ev, const char* ip, uint16_t port) { + int sock_fd; + int flag = 1; + struct sockaddr_in local_addr; + + if ((nullptr == ip) || (nullptr == ev)) { + m_status = FAIL; + return -1; + } + + if ((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { + perror("socket"); + return -1; + } + + if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&flag, sizeof(flag)) < 0) { + perror("setsockopt"); + return 1; + } + memset(&local_addr, 0, sizeof(local_addr)); + local_addr.sin_family = AF_INET; + local_addr.sin_port = htons(port); + local_addr.sin_addr.s_addr = inet_addr(ip); + if (bind(sock_fd, (struct sockaddr*)&local_addr, sizeof(struct sockaddr)) < 0) { + perror("bind"); + return -1; + } + else { + printf("bind success, port[%d]\n", port); + } + event_set(ev, sock_fd, EV_READ | EV_PERSIST, &read_cb, (void*)this); + if (event_add(ev, NULL) == -1) { + perror("event_set"); + } + m_sock_fd = sock_fd; + return 0; +} + +UdpDataGramLibevent::OnReadDataHandle* UdpDataGramLibevent::OnReadHandle() { + return this->mOnRead; +} + +void UdpDataGramLibevent::SetOnReadHandle(UdpDataGramLibevent::OnReadDataHandle* p) { + this->mOnRead = p; +} + +void UdpDataGramLibevent::SendTo(const char* dat, uint32_t len, std::string ip, int port) { + struct sockaddr_in dest_addr; + memset(&dest_addr, 0, sizeof(dest_addr)); + dest_addr.sin_family = AF_INET; + dest_addr.sin_port = htons(port); + dest_addr.sin_addr.s_addr = inet_addr(ip.c_str()); + int addr_len = sizeof(struct sockaddr); + if (m_sock_fd > 0) { + sendto(m_sock_fd, (const char*)dat, len, 0, (struct sockaddr*)&dest_addr, addr_len); + } +} + +int UdpDataGramLibevent::SocketFD() { + return this->mSocketFD; +} + + +UdpDataGramLibevent:: +UdpDataGramLibevent(std::string ip, uint32_t port) +{ + m_status = STOP; + m_bind_ip = ip; + m_port = port; + if (event_init() == NULL) { + printf("event_init() failed\n"); + } + m_event = new struct event; + if (0 > bind_socket(m_event, ip.c_str(), port)) { + return; + } + + m_thread = new std::thread([this]() { + event_dispatch(); + } + ); + m_status = RUNNING; +} diff --git a/examples/proto_debuger/udp_libevent.h b/examples/proto_debuger/udp_libevent.h new file mode 100644 index 00000000..a0e3d382 --- /dev/null +++ b/examples/proto_debuger/udp_libevent.h @@ -0,0 +1,53 @@ +#pragma once +#include + +#ifndef _WIN32_WINNT +#define _WIN32_WINNT 0x0500 +#include +#endif + +#ifdef linux +#include +#include +#include +#define EVENT__HAVE_PTHREADS +#endif + +#include +#include +#include +#include +#include +#include +#include + + +class UdpDataGramLibevent { +public: + typedef std::function OnReadDataHandle; + UdpDataGramLibevent(std::string ip, uint32_t port); + OnReadDataHandle* OnReadHandle(); + typedef enum { + RUNNING, + STOP, + FAIL + }STATUS; + friend void read_cb(int, short, void*); + void SetOnReadHandle(OnReadDataHandle*); + void SendTo(const char* dat, uint32_t len, std::string ip, int port); + int SocketFD(); +private: + int bind_socket(struct event* ev, const char* ip, uint16_t port); + uint32_t m_port; // ˿ں + std::string m_bind_ip; // 󶨶˿ں + uint16_t m_backlog; + int m_sock_fd; + struct sockaddr_in m_bind_addr; // 󶨵ַ + struct event* m_event; + STATUS m_status; // + std::thread* m_thread; // ǰ߳ + intptr_t mSocketFD; // ϵͳԭsocket + OnReadDataHandle* mOnRead; +}; + +