2022-02-09 01:29:05 +08:00
|
|
|
|
#include "udp_form.h"
|
2022-02-11 11:31:24 +08:00
|
|
|
|
#include <functional>
|
|
|
|
|
#include "utils.h"
|
|
|
|
|
#include "lua_bind.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define UDP_LUA_SCRIPT "udp_script.lua"
|
2022-02-09 01:29:05 +08:00
|
|
|
|
|
2022-02-09 23:31:01 +08:00
|
|
|
|
UdpForm::UdpForm(ui::Window* hwnd, string url, uint32_t port, UdpDataGramLibevent* p)
|
|
|
|
|
{
|
|
|
|
|
this->mUdpPeer = p;
|
|
|
|
|
this->m_url = url;
|
|
|
|
|
this->m_port = port;
|
2022-02-11 11:31:24 +08:00
|
|
|
|
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);
|
|
|
|
|
|
2022-02-11 14:26:25 +08:00
|
|
|
|
UdpDataGramLibevent::OnReadDataHandle onRecvCallBack
|
|
|
|
|
= [this](const char* dat, int len, struct sockaddr_in sendarr){
|
|
|
|
|
std::cout << "receive udp datagram len" << len << dat << "\r\n";
|
|
|
|
|
|
2022-02-11 11:31:24 +08:00
|
|
|
|
this->mLua->CallFuntion<std::string,std::string, LUA_INTEGER>("OnUdpData", std::string(dat),
|
|
|
|
|
inet_ntoa(sendarr.sin_addr),sendarr.sin_port);
|
2022-02-11 14:26:25 +08:00
|
|
|
|
|
|
|
|
|
};
|
2022-02-11 11:31:24 +08:00
|
|
|
|
if (mUdpPeer != nullptr) {
|
2022-02-11 14:26:25 +08:00
|
|
|
|
std::cout << "\r\n"<< &onRecvCallBack <<"\r\n";
|
|
|
|
|
mUdpPeer->SetOnReadHandle(onRecvCallBack);
|
2022-02-11 11:31:24 +08:00
|
|
|
|
}
|
|
|
|
|
this->mLua->BindFunction("showdata", LuaShowData);
|
|
|
|
|
std::cout << "lua script is " << lua_script << std::endl;
|
2022-02-09 23:31:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UdpForm::~UdpForm()
|
|
|
|
|
{
|
2022-02-11 11:31:24 +08:00
|
|
|
|
|
2022-02-09 23:31:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UdpForm::Init()
|
|
|
|
|
{
|
|
|
|
|
ui::ChildBox::Init();
|
|
|
|
|
ui::Label* m_label_1 = dynamic_cast<ui::Label*>(FindSubControl(L"uart_info_label"));
|
2022-02-16 23:54:45 +08:00
|
|
|
|
m_rich_edit_1 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"uart_recv_eidt"));
|
|
|
|
|
m_rich_edit_2 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"lua_script"));
|
2022-02-09 23:31:01 +08:00
|
|
|
|
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"));
|
|
|
|
|
|
2022-02-11 11:31:24 +08:00
|
|
|
|
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));
|
|
|
|
|
}
|
2022-02-09 23:31:01 +08:00
|
|
|
|
|
2022-02-11 11:31:24 +08:00
|
|
|
|
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 << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>lua<EFBFBD>ű<EFBFBD>\r\n";
|
2022-02-13 15:32:47 +08:00
|
|
|
|
std::string lua = wstring2string(this->m_rich_edit_2->GetText());
|
2022-02-11 11:31:24 +08:00
|
|
|
|
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<EFBFBD>ű<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>", 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;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-02-09 23:31:01 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LuaDelegate* UdpForm::LuaVM()
|
|
|
|
|
{
|
|
|
|
|
return mLua;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-11 14:26:25 +08:00
|
|
|
|
void UdpForm::ShowDataInEdit(const char*src)
|
2022-02-09 01:29:05 +08:00
|
|
|
|
{
|
2022-02-11 14:26:25 +08:00
|
|
|
|
std::cout << src << " from ShowDataInEdit";
|
|
|
|
|
this->m_rich_edit_1->AppendText(string2wstring(std::string(src)));
|
2022-02-09 01:29:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UdpForm::HandleMessage(ui::EventArgs& msg)
|
|
|
|
|
{
|
2022-02-13 15:29:57 +08:00
|
|
|
|
|
2022-02-09 01:29:05 +08:00
|
|
|
|
}
|