no message
This commit is contained in:
parent
fdb5f15fef
commit
f2e52e1fbd
@ -173,7 +173,6 @@ LRESULT BasicForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
|
|||||||
);
|
);
|
||||||
mMonitorNewSelect->Close();
|
mMonitorNewSelect->Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (uMsg == WM_ADD_UDP_MONITOR) {
|
if (uMsg == WM_ADD_UDP_MONITOR) {
|
||||||
printf("add udp monitor");
|
printf("add udp monitor");
|
||||||
wchar_t pdata[100] = L"\0";
|
wchar_t pdata[100] = L"\0";
|
||||||
@ -226,6 +225,58 @@ LRESULT BasicForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
if (uMsg == WM_ADD_UDP_GROUP_MONITOR) {
|
||||||
|
printf("add udp monitor");
|
||||||
|
wchar_t pdata[100] = L"\0";
|
||||||
|
TcpServerInfo* p = (TcpServerInfo*)wParam;
|
||||||
|
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(4)->AddChildNode(node);
|
||||||
|
|
||||||
|
if (mUdpGroupForm.find(std::wstring(pdata)) == mUdpGroupForm.end())
|
||||||
|
{
|
||||||
|
auto form = new UdpGroupForm(this, wstring2string(p->ip), p->port, udp);
|
||||||
|
form->SetChildLayoutXML(L"basic/udp_form.xml");
|
||||||
|
form->SetName(pdata);
|
||||||
|
form->SetVisible(false);
|
||||||
|
mUdpGroupForm[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());
|
||||||
|
UdpGroupForm* p = mUdpGroupForm[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) {
|
if (uMsg == WM_ADD_UART_MONITOR) {
|
||||||
printf("add uart monitor");
|
printf("add uart monitor");
|
||||||
UartInfo* p = (UartInfo*)wParam;
|
UartInfo* p = (UartInfo*)wParam;
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
#include "tcp_client_form.h"
|
#include "tcp_client_form.h"
|
||||||
#include "tcp_server_form.h"
|
#include "tcp_server_form.h"
|
||||||
#include "udp_form.h"
|
#include "udp_form.h"
|
||||||
|
#include "udp_group_form.h"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
|
||||||
@ -59,6 +60,7 @@ private:
|
|||||||
std::map<std::wstring, TcpClientForm*> mTcpClientForm;
|
std::map<std::wstring, TcpClientForm*> mTcpClientForm;
|
||||||
std::map<std::wstring, TcpServerFrom*> mTcpServerForm;
|
std::map<std::wstring, TcpServerFrom*> mTcpServerForm;
|
||||||
std::map<std::wstring, UdpForm*> mUdpForm;
|
std::map<std::wstring, UdpForm*> mUdpForm;
|
||||||
|
std::map<std::wstring, UdpGroupForm*> mUdpGroupForm;
|
||||||
|
|
||||||
ui::Control* mRightShow;
|
ui::Control* mRightShow;
|
||||||
};
|
};
|
||||||
|
@ -106,6 +106,7 @@ void NewMonitorForm::InitWindow()
|
|||||||
ui::Button* btn_add_new = dynamic_cast<ui::Button*>(FindControl(L"open_button"));
|
ui::Button* btn_add_new = dynamic_cast<ui::Button*>(FindControl(L"open_button"));
|
||||||
if (nullptr != btn_add_new) {
|
if (nullptr != btn_add_new) {
|
||||||
btn_add_new->AttachClick([this](ui::EventArgs*) {
|
btn_add_new->AttachClick([this](ui::EventArgs*) {
|
||||||
|
|
||||||
if(m_combo_type->GetText() == L"uart"){
|
if(m_combo_type->GetText() == L"uart"){
|
||||||
UINT PortNum = 0;
|
UINT PortNum = 0;
|
||||||
for (int i = 3; m_uart_select_combo->GetText()[i] != '\0'; i++) //ת»»ÎªÊý×Ö
|
for (int i = 3; m_uart_select_combo->GetText()[i] != '\0'; i++) //ת»»ÎªÊý×Ö
|
||||||
@ -134,8 +135,6 @@ void NewMonitorForm::InitWindow()
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (m_combo_type->GetText() == L"tcp client") {
|
if (m_combo_type->GetText() == L"tcp client") {
|
||||||
wprintf(L"%s\r\n", m_ip_select->GetText().c_str());
|
wprintf(L"%s\r\n", m_ip_select->GetText().c_str());
|
||||||
wprintf(L"%s\r\n", m_port_select->GetText().c_str());
|
wprintf(L"%s\r\n", m_port_select->GetText().c_str());
|
||||||
@ -189,8 +188,27 @@ void NewMonitorForm::InitWindow()
|
|||||||
if (!succ) {
|
if (!succ) {
|
||||||
printf("postmessage error :%d\r\n", GetLastError());
|
printf("postmessage error :%d\r\n", GetLastError());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (m_combo_type->GetText() == L"udp group") {
|
||||||
|
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());
|
||||||
|
UdpDataGramLibevent* udp = new UdpDataGramLibevent("0.0.0.0",
|
||||||
|
port, wstring2string(m_ip_select->GetText().c_str()));
|
||||||
|
TcpServerInfo* p = new TcpServerInfo;
|
||||||
|
p->ip = m_ip_select->GetText();
|
||||||
|
p->port = port;
|
||||||
|
p->socket_fd = udp->SocketFD();
|
||||||
|
printf("¿ªÆô·þÎñ¶Ë %d \r\n", p->socket_fd);
|
||||||
|
|
||||||
|
auto succ = ::PostMessage(m_parent->GetHWND(),
|
||||||
|
WM_ADD_UDP_GROUP_MONITOR, (WPARAM)p, (LPARAM)udp);
|
||||||
|
if (!succ) {
|
||||||
|
printf("postmessage error :%d\r\n", GetLastError());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -174,8 +174,8 @@
|
|||||||
<ClCompile Include="tcp_client_form.cpp" />
|
<ClCompile Include="tcp_client_form.cpp" />
|
||||||
<ClCompile Include="tcp_server_libevent.cpp" />
|
<ClCompile Include="tcp_server_libevent.cpp" />
|
||||||
<ClCompile Include="uart_form.cpp" />
|
<ClCompile Include="uart_form.cpp" />
|
||||||
<ClCompile Include="udp_group_libevent.cpp" />
|
|
||||||
<ClCompile Include="udp_form.cpp" />
|
<ClCompile Include="udp_form.cpp" />
|
||||||
|
<ClCompile Include="udp_group_form.cpp" />
|
||||||
<ClCompile Include="udp_libevent.cpp" />
|
<ClCompile Include="udp_libevent.cpp" />
|
||||||
<ClCompile Include="utils.cpp" />
|
<ClCompile Include="utils.cpp" />
|
||||||
<ClCompile Include="lua_bind.cpp" />
|
<ClCompile Include="lua_bind.cpp" />
|
||||||
@ -194,8 +194,8 @@
|
|||||||
<ClInclude Include="tcp_client_form.h" />
|
<ClInclude Include="tcp_client_form.h" />
|
||||||
<ClInclude Include="tcp_server_libevent.h" />
|
<ClInclude Include="tcp_server_libevent.h" />
|
||||||
<ClInclude Include="uart_process.h" />
|
<ClInclude Include="uart_process.h" />
|
||||||
<ClInclude Include="udp_group_libevent.h" />
|
|
||||||
<ClInclude Include="udp_form.h" />
|
<ClInclude Include="udp_form.h" />
|
||||||
|
<ClInclude Include="udp_group_form.h" />
|
||||||
<ClInclude Include="udp_libevent.h" />
|
<ClInclude Include="udp_libevent.h" />
|
||||||
<ClInclude Include="utils.h" />
|
<ClInclude Include="utils.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
@ -63,6 +63,9 @@
|
|||||||
<ClCompile Include="udp_form.cpp">
|
<ClCompile Include="udp_form.cpp">
|
||||||
<Filter>源文件</Filter>
|
<Filter>源文件</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="udp_group_form.cpp">
|
||||||
|
<Filter>源文件</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="main.h">
|
<ClInclude Include="main.h">
|
||||||
@ -113,6 +116,9 @@
|
|||||||
<ClInclude Include="udp_form.h">
|
<ClInclude Include="udp_form.h">
|
||||||
<Filter>头文件</Filter>
|
<Filter>头文件</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="udp_group_form.h">
|
||||||
|
<Filter>头文件</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Xml Include="..\Debug\resources\themes\default\basic\basic.xml">
|
<Xml Include="..\Debug\resources\themes\default\basic\basic.xml">
|
||||||
|
152
examples/proto_debuger/udp_group_form.cpp
Normal file
152
examples/proto_debuger/udp_group_form.cpp
Normal file
@ -0,0 +1,152 @@
|
|||||||
|
#include "udp_group_form.h"
|
||||||
|
#include <functional>
|
||||||
|
#include "utils.h"
|
||||||
|
#include "lua_bind.h"
|
||||||
|
|
||||||
|
|
||||||
|
#define UDP_LUA_SCRIPT "udp_script.lua"
|
||||||
|
|
||||||
|
UdpGroupForm::UdpGroupForm(ui::Window* hwnd, string url, uint32_t port, UdpDataGramLibevent* p)
|
||||||
|
{
|
||||||
|
this->mUdpPeer = p;
|
||||||
|
this->m_url = url;
|
||||||
|
this->m_port = port;
|
||||||
|
mLua = new LuaDelegate;
|
||||||
|
|
||||||
|
mLuaFile.open(UDP_LUA_SCRIPT);
|
||||||
|
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);
|
||||||
|
|
||||||
|
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<std::string, std::string, LUA_INTEGER>("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;
|
||||||
|
}
|
||||||
|
|
||||||
|
UdpGroupForm::~UdpGroupForm()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void UdpGroupForm::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"));
|
||||||
|
|
||||||
|
m_label_1 = dynamic_cast<ui::Label*>(FindSubControl(L"uart_info_label"));
|
||||||
|
m_rich_edit_1 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"uart_recv_eidt"));
|
||||||
|
if (nullptr != m_rich_edit_1) {
|
||||||
|
m_rich_edit_1->SetReadOnly(true);
|
||||||
|
m_rich_edit_1->SetRich(false);
|
||||||
|
m_rich_edit_1->SetAttribute(L"autovscroll", L"true");
|
||||||
|
m_rich_edit_1->SetAttribute(L"multiline", L"true");
|
||||||
|
m_rich_edit_1->SetReturnMsgWantCtrl(true);
|
||||||
|
m_rich_edit_1->SetNeedReturnMsg(true);
|
||||||
|
m_rich_edit_1->SetWordWrap(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
m_rich_edit_2 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"lua_script"));
|
||||||
|
if (nullptr != m_rich_edit_2) {
|
||||||
|
m_rich_edit_2->SetRich(true);
|
||||||
|
m_rich_edit_2->SetReturnMsgWantCtrl(true);
|
||||||
|
m_rich_edit_2->SetText(string2wstring(mLuaScript));
|
||||||
|
}
|
||||||
|
|
||||||
|
m_rich_edit_3 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"uart_send_edit"));
|
||||||
|
m_button_1 = dynamic_cast<ui::Button*>(FindSubControl(L"btn_send_data"));
|
||||||
|
m_button_2 = dynamic_cast<ui::Button*>(FindSubControl(L"btn_close_uart"));
|
||||||
|
m_button_3 = dynamic_cast<ui::Button*>(FindSubControl(L"btn_save_lua"));
|
||||||
|
m_button_4 = dynamic_cast<ui::Button*>(FindSubControl(L"btn_do_lua"));
|
||||||
|
|
||||||
|
if (nullptr != m_label_1) {
|
||||||
|
m_label_1->SetText(wstring(L"udp:") + string2wstring(m_url)
|
||||||
|
+ L":" + std::to_wstring(m_port));
|
||||||
|
}
|
||||||
|
if (nullptr != m_button_1) {
|
||||||
|
m_button_1->AttachClick(
|
||||||
|
[this](ui::EventArgs*) {
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (nullptr != m_button_2) {
|
||||||
|
m_button_2->AttachClick(
|
||||||
|
[this](ui::EventArgs*) {
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (nullptr != m_button_3) {
|
||||||
|
m_button_3->AttachClick(
|
||||||
|
[this](ui::EventArgs*) {
|
||||||
|
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(UDP_LUA_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);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
std::cout << lua.c_str() << "\r\n";
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (nullptr != m_button_4) {
|
||||||
|
m_button_4->AttachClick(
|
||||||
|
[this](ui::EventArgs* ev) {
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
LuaDelegate* UdpGroupForm::LuaVM()
|
||||||
|
{
|
||||||
|
return mLua;
|
||||||
|
}
|
||||||
|
|
||||||
|
void UdpGroupForm::ShowDataInEdit(const char* src)
|
||||||
|
{
|
||||||
|
std::cout << src << " from ShowDataInEdit";
|
||||||
|
this->m_rich_edit_1->AppendText(string2wstring(std::string(src)));
|
||||||
|
}
|
||||||
|
|
||||||
|
void UdpGroupForm::HandleMessage(ui::EventArgs& msg)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
60
examples/proto_debuger/udp_group_form.h
Normal file
60
examples/proto_debuger/udp_group_form.h
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include "msgdef.h"
|
||||||
|
|
||||||
|
// C runtime header
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <malloc.h>
|
||||||
|
#include <memory.h>
|
||||||
|
#include <tchar.h>
|
||||||
|
|
||||||
|
// base header
|
||||||
|
#include "base/base.h"
|
||||||
|
#include "serial_port.h"
|
||||||
|
// duilib
|
||||||
|
#include "duilib/UIlib.h"
|
||||||
|
#include "tcp_client.h"
|
||||||
|
|
||||||
|
#include "lua_wraper.h"
|
||||||
|
#include "global.h"
|
||||||
|
#include <fstream>
|
||||||
|
#include "udp_libevent.h"
|
||||||
|
#include <istream>
|
||||||
|
|
||||||
|
class UdpGroupForm :
|
||||||
|
public ui::ChildBox,
|
||||||
|
LuaBindInterface
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
UdpGroupForm(ui::Window* hwnd, string url, uint32_t port, UdpDataGramLibevent* p);
|
||||||
|
~UdpGroupForm();
|
||||||
|
virtual void Init() override;
|
||||||
|
LuaDelegate* LuaVM();
|
||||||
|
ui::RichEdit* m_rich_edit_2;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void ShowDataInEdit(const char*);
|
||||||
|
|
||||||
|
private:
|
||||||
|
ui::Label* m_label_1;
|
||||||
|
ui::RichEdit* m_rich_edit_1;
|
||||||
|
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;
|
||||||
|
|
||||||
|
|
||||||
|
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;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
@ -1,133 +0,0 @@
|
|||||||
#include "udp_group_libevent.h"
|
|
||||||
|
|
||||||
#include "udp_group_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
|
|
||||||
|
|
||||||
static void read_cb(evutil_socket_t fd, short event, void* arg) {
|
|
||||||
UdpGroupLibevent* parent = (UdpGroupLibevent*)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) {
|
|
||||||
UdpGroupLibevent::OnReadDataHandle p = parent->OnReadHandle();
|
|
||||||
if (p)
|
|
||||||
p(buf, len, cli_addr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int UdpGroupLibevent::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;
|
|
||||||
}
|
|
||||||
|
|
||||||
UdpGroupLibevent::OnReadDataHandle UdpGroupLibevent::OnReadHandle() {
|
|
||||||
return this->mOnRead;
|
|
||||||
}
|
|
||||||
|
|
||||||
void UdpGroupLibevent::SetOnReadHandle(UdpGroupLibevent::OnReadDataHandle p) {
|
|
||||||
this->mOnRead = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
void UdpGroupLibevent::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 UdpGroupLibevent::SocketFD() {
|
|
||||||
return this->mSocketFD;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
UdpGroupLibevent::
|
|
||||||
UdpGroupLibevent(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;
|
|
||||||
}
|
|
@ -1,53 +0,0 @@
|
|||||||
#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 UdpGroupLibevent {
|
|
||||||
public:
|
|
||||||
typedef std::function<void(const char* dat, int len, struct sockaddr_in)> OnReadDataHandle;
|
|
||||||
UdpGroupLibevent(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;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
@ -4,6 +4,8 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#include <winsock2.h>
|
||||||
|
#include <ws2tcpip.h>
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
@ -15,7 +17,7 @@ extern "C" {
|
|||||||
#endif
|
#endif
|
||||||
#define MSG_LEN 1024
|
#define MSG_LEN 1024
|
||||||
|
|
||||||
static void read_cb(evutil_socket_t fd, short event, void* arg) {
|
void read_cb(intptr_t fd, short event, void* arg) {
|
||||||
UdpDataGramLibevent* parent = (UdpDataGramLibevent*)arg;
|
UdpDataGramLibevent* parent = (UdpDataGramLibevent*)arg;
|
||||||
if (parent == nullptr)
|
if (parent == nullptr)
|
||||||
return;
|
return;
|
||||||
@ -40,7 +42,7 @@ static void read_cb(evutil_socket_t fd, short event, void* arg) {
|
|||||||
// sendto(fd, buf, len, 0, (struct sockaddr*)&cli_addr, addr_len);
|
// sendto(fd, buf, len, 0, (struct sockaddr*)&cli_addr, addr_len);
|
||||||
if (parent->OnReadHandle() != nullptr) {
|
if (parent->OnReadHandle() != nullptr) {
|
||||||
UdpDataGramLibevent::OnReadDataHandle p = (parent->OnReadHandle());
|
UdpDataGramLibevent::OnReadDataHandle p = (parent->OnReadHandle());
|
||||||
if(p)
|
if (p)
|
||||||
p(buf, len, cli_addr);
|
p(buf, len, cli_addr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -76,6 +78,17 @@ int UdpDataGramLibevent::bind_socket(struct event* ev, const char* ip, uint16_t
|
|||||||
else {
|
else {
|
||||||
printf("bind success, port[%d]\n", port);
|
printf("bind success, port[%d]\n", port);
|
||||||
}
|
}
|
||||||
|
// ¼ÓÈë×é²¥
|
||||||
|
ip_mreq multiCast;
|
||||||
|
multiCast.imr_interface.S_un.S_addr = INADDR_ANY;
|
||||||
|
// multiCast.imr_interface.S_un.S_addr = inet_addr("192.168.0.104");
|
||||||
|
multiCast.imr_multiaddr.S_un.S_addr = inet_addr("239.2.2.2");
|
||||||
|
int iRet = setsockopt(sock_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&multiCast, sizeof(multiCast));
|
||||||
|
if (iRet != 0) {
|
||||||
|
printf("setsockopt fail:%d", WSAGetLastError());
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
event_set(ev, sock_fd, EV_READ | EV_PERSIST, &read_cb, (void*)this);
|
event_set(ev, sock_fd, EV_READ | EV_PERSIST, &read_cb, (void*)this);
|
||||||
if (event_add(ev, NULL) == -1) {
|
if (event_add(ev, NULL) == -1) {
|
||||||
perror("event_set");
|
perror("event_set");
|
||||||
@ -84,6 +97,57 @@ int UdpDataGramLibevent::bind_socket(struct event* ev, const char* ip, uint16_t
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int UdpDataGramLibevent::bind_socket_with_group(struct event* ev, const char* ip, uint16_t port, const char* group_addr) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
// ¼ÓÈë×é²¥
|
||||||
|
ip_mreq multiCast;
|
||||||
|
multiCast.imr_interface.S_un.S_addr = INADDR_ANY;
|
||||||
|
// multiCast.imr_interface.S_un.S_addr = inet_addr("192.168.0.104");
|
||||||
|
multiCast.imr_multiaddr.S_un.S_addr = inet_addr(group_addr);
|
||||||
|
int iRet = setsockopt(sock_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&multiCast, sizeof(multiCast));
|
||||||
|
if (iRet != 0) {
|
||||||
|
printf("setsockopt fail:%d", WSAGetLastError());
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
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() {
|
UdpDataGramLibevent::OnReadDataHandle UdpDataGramLibevent::OnReadHandle() {
|
||||||
return this->mOnRead;
|
return this->mOnRead;
|
||||||
}
|
}
|
||||||
@ -129,3 +193,26 @@ UdpDataGramLibevent(std::string ip, uint32_t port)
|
|||||||
);
|
);
|
||||||
m_status = RUNNING;
|
m_status = RUNNING;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UdpDataGramLibevent::UdpDataGramLibevent(std::string ip, uint32_t port, std::string group_addr)
|
||||||
|
{
|
||||||
|
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_with_group(m_event, ip.c_str(), port, group_addr.c_str())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_thread = new std::thread([this]() {
|
||||||
|
event_dispatch();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
m_status = RUNNING;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -19,12 +19,15 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
|
||||||
class UdpDataGramLibevent {
|
class UdpDataGramLibevent {
|
||||||
public:
|
public:
|
||||||
typedef std::function<void(const char* dat, int len, struct sockaddr_in)> OnReadDataHandle;
|
typedef std::function<void(const char* dat, int len, struct sockaddr_in)> OnReadDataHandle;
|
||||||
UdpDataGramLibevent(std::string ip, uint32_t port);
|
UdpDataGramLibevent(std::string ip, uint32_t port);
|
||||||
|
UdpDataGramLibevent(std::string ip, uint32_t port, std::string group_addr);
|
||||||
|
|
||||||
OnReadDataHandle OnReadHandle();
|
OnReadDataHandle OnReadHandle();
|
||||||
typedef enum {
|
typedef enum {
|
||||||
RUNNING,
|
RUNNING,
|
||||||
@ -37,15 +40,17 @@ public:
|
|||||||
int SocketFD();
|
int SocketFD();
|
||||||
private:
|
private:
|
||||||
int bind_socket(struct event* ev, const char* ip, uint16_t port);
|
int bind_socket(struct event* ev, const char* ip, uint16_t port);
|
||||||
uint32_t m_port; // 监听端口号
|
int bind_socket_with_group(struct event* ev, const char* ip, uint16_t port, const char* udp_group);
|
||||||
std::string m_bind_ip; // 绑定端口号
|
|
||||||
|
uint32_t m_port; //
|
||||||
|
std::string m_bind_ip; //
|
||||||
uint16_t m_backlog;
|
uint16_t m_backlog;
|
||||||
int m_sock_fd;
|
int m_sock_fd;
|
||||||
struct sockaddr_in m_bind_addr; // 绑定地址
|
struct sockaddr_in m_bind_addr; //
|
||||||
struct event* m_event;
|
struct event* m_event;
|
||||||
STATUS m_status; //
|
STATUS m_status; //
|
||||||
std::thread* m_thread; // 当前线程
|
std::thread* m_thread; //
|
||||||
intptr_t mSocketFD; // 操作系统原生socket
|
intptr_t mSocketFD; // socket
|
||||||
OnReadDataHandle mOnRead;
|
OnReadDataHandle mOnRead;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user