no message

This commit is contained in:
zcy 2022-02-09 23:31:01 +08:00
parent d448eb3a26
commit 5f70c4333f
8 changed files with 280 additions and 22 deletions

View File

@ -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<ui::TreeNode*> (ev->pSender)->GetText().c_str());
printf("GetCurSel %d\r\n", mRightSide->GetCurSel());
UdpForm* p = mUdpForm[dynamic_cast<ui::TreeNode*> (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;

View File

@ -17,6 +17,7 @@
#include "uart_process.h"
#include "tcp_client_form.h"
#include "tcp_server_form.h"
#include "udp_form.h"
#include <vector>
#include <map>
@ -57,6 +58,7 @@ private:
std::map<std::wstring,UartForm*> mUartForm;
std::map<std::wstring, TcpClientForm*> mTcpClientForm;
std::map<std::wstring, TcpServerFrom*> mTcpServerForm;
std::map<std::wstring, UdpForm*> mUdpForm;
ui::Control* mRightShow;
};

View File

@ -174,6 +174,7 @@
<ClCompile Include="tcp_client_form.cpp" />
<ClCompile Include="tcp_server_libevent.cpp" />
<ClCompile Include="uart_form.cpp" />
<ClCompile Include="udp_form.cpp" />
<ClCompile Include="udp_libevent.cpp" />
<ClCompile Include="utils.cpp" />
<ClCompile Include="lua_bind.cpp" />
@ -192,6 +193,7 @@
<ClInclude Include="tcp_client_form.h" />
<ClInclude Include="tcp_server_libevent.h" />
<ClInclude Include="uart_process.h" />
<ClInclude Include="udp_form.h" />
<ClInclude Include="udp_libevent.h" />
<ClInclude Include="utils.h" />
</ItemGroup>
@ -201,7 +203,7 @@
<Xml Include="..\Debug\resources\themes\default\basic\uart_form.xml" />
<Xml Include="..\x64\Debug\resources\themes\default\basic\tcp_form.xml" />
<Xml Include="..\x64\Debug\resources\themes\default\basic\tcp_server_form.xml" />
<Xml Include="Debug\themes\default\global.xml" />
<Xml Include="..\x64\Debug\resources\themes\default\basic\udp_form.xml" />
<Xml Include="x64\Debug\themes\default\global.xml" />
</ItemGroup>
<ItemGroup>

View File

@ -60,6 +60,9 @@
<ClCompile Include="udp_libevent.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="udp_form.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="main.h">
@ -107,6 +110,9 @@
<ClInclude Include="udp_libevent.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="udp_form.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Xml Include="..\Debug\resources\themes\default\basic\basic.xml">
@ -127,7 +133,7 @@
<Xml Include="x64\Debug\themes\default\global.xml">
<Filter>资源文件</Filter>
</Xml>
<Xml Include="Debug\themes\default\global.xml">
<Xml Include="..\x64\Debug\resources\themes\default\basic\udp_form.xml">
<Filter>资源文件</Filter>
</Xml>
</ItemGroup>

View File

@ -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<ui::Label*>(FindSubControl(L"uart_info_label"));
ui::RichEdit* m_rich_edit_1 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"uart_recv_eidt"));
ui::RichEdit* m_rich_edit_2 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"lua_script"));
ui::RichEdit* m_rich_edit_3 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"uart_send_edit"));
ui::Button* m_button_1 = dynamic_cast<ui::Button*>(FindSubControl(L"btn_send_data"));
ui::Button* m_button_2 = dynamic_cast<ui::Button*>(FindSubControl(L"btn_close_uart"));
ui::Button* m_button_3 = dynamic_cast<ui::Button*>(FindSubControl(L"btn_save_lua"));
ui::Button* m_button_4 = dynamic_cast<ui::Button*>(FindSubControl(L"btn_do_lua"));
}
LuaDelegate* UdpForm::LuaVM()
{
return mLua;
}
void UdpForm::ShowDataInEdit(const char*)
{
}

View File

@ -19,19 +19,17 @@
#include "lua_wraper.h"
#include "global.h"
#include <fstream>
#include "udp_libevent.h"
#include <istream>
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;
};

View File

@ -0,0 +1,129 @@
#include "udp_libevent.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#ifdef __cplusplus
extern "C" {
#endif
#include <event.h>
#include <event2/listener.h>
#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;
}

View File

@ -0,0 +1,53 @@
#pragma once
#include <string>
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0500
#include <WinSock2.h>
#endif
#ifdef linux
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#define EVENT__HAVE_PTHREADS
#endif
#include <stdint.h>
#include <iostream>
#include <mutex>
#include <thread>
#include <map>
#include <functional>
#include <functional>
class UdpDataGramLibevent {
public:
typedef std::function<void(const char* dat, int len, struct sockaddr_in)> 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;
};