2021-10-11 00:00:10 +08:00
|
|
|
|
#include "tcp_client_form.h"
|
2021-11-21 13:47:19 +08:00
|
|
|
|
#include "utils.h"
|
2022-02-01 00:19:32 +08:00
|
|
|
|
#include "lua_bind.h"
|
2021-10-11 00:00:10 +08:00
|
|
|
|
|
2022-02-03 00:22:01 +08:00
|
|
|
|
|
|
|
|
|
#define TCP_CLIENT_SCRIPT "tcp_client.lua"
|
|
|
|
|
TcpClientForm::~TcpClientForm() {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-22 23:53:21 +08:00
|
|
|
|
TcpClientForm::TcpClientForm(ui::Window* hwnd,std::string url, uint32_t port, TcpClientLibevent* p):
|
2022-01-23 01:02:13 +08:00
|
|
|
|
m_connected(false),
|
|
|
|
|
mLua(nullptr)
|
2021-10-11 00:00:10 +08:00
|
|
|
|
{
|
|
|
|
|
mClient = p;
|
2021-11-22 23:53:21 +08:00
|
|
|
|
mClient->SetObserver(this);
|
2021-10-11 00:54:04 +08:00
|
|
|
|
if (nullptr != hwnd) {
|
|
|
|
|
this->SetWindow(hwnd, nullptr, false);
|
|
|
|
|
}
|
2022-01-31 01:00:02 +08:00
|
|
|
|
|
|
|
|
|
mLua = new LuaDelegate;
|
2022-02-03 00:22:01 +08:00
|
|
|
|
mLuaFileRead.open(TCP_CLIENT_SCRIPT);
|
2022-01-31 01:00:02 +08:00
|
|
|
|
std::string lua_script;
|
2022-02-03 00:22:01 +08:00
|
|
|
|
if (mLuaFileRead.is_open()) {
|
2022-01-31 01:00:02 +08:00
|
|
|
|
std::string s;
|
2022-02-03 00:22:01 +08:00
|
|
|
|
while (getline(mLuaFileRead, s)) {
|
2022-01-31 01:00:02 +08:00
|
|
|
|
lua_script += s + "\r\n";
|
|
|
|
|
}
|
2022-02-03 00:22:01 +08:00
|
|
|
|
mLuaFileRead.close();
|
2022-01-31 01:00:02 +08:00
|
|
|
|
}
|
|
|
|
|
mLuaScript = lua_script;
|
|
|
|
|
mLua->DoString(lua_script);
|
|
|
|
|
|
|
|
|
|
std::cout << "lua script is " << lua_script << std::endl;
|
2021-11-21 13:47:19 +08:00
|
|
|
|
m_url = url;
|
|
|
|
|
m_port = port;
|
2022-02-01 00:19:32 +08:00
|
|
|
|
this->mLua->BindFunction("showdata", LuaShowData);
|
2021-10-11 00:00:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-25 23:45:07 +08:00
|
|
|
|
void TcpClientForm::ShowDataInEdit(const char*p) {
|
|
|
|
|
if (nullptr == p) {
|
|
|
|
|
std::cout << "ShowDataInEdit " << std::endl;
|
|
|
|
|
}
|
|
|
|
|
m_rich_edit_1->AppendText(string2wstring(p), false);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-11 00:00:10 +08:00
|
|
|
|
void TcpClientForm::Init()
|
|
|
|
|
{
|
2021-10-11 00:54:04 +08:00
|
|
|
|
ui::ChildBox::Init();
|
|
|
|
|
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"));
|
|
|
|
|
m_rich_edit_2 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"lua_script"));
|
2021-11-22 23:53:21 +08:00
|
|
|
|
m_uart_send_edit = dynamic_cast<ui::RichEdit*>(FindSubControl(L"uart_send_edit"));
|
|
|
|
|
m_btn_send_data = dynamic_cast<ui::Button*>(FindSubControl(L"btn_send_data"));
|
2021-10-11 00:54:04 +08:00
|
|
|
|
m_check_box_2 = dynamic_cast<ui::CheckBox*>(FindSubControl(L"check_time_send"));
|
|
|
|
|
m_check_box_3 = dynamic_cast<ui::CheckBox*>(FindSubControl(L"check_hex_send"));
|
|
|
|
|
m_check_box_4 = dynamic_cast<ui::CheckBox*>(FindSubControl(L"check_hex_recv"));
|
2022-02-01 00:19:32 +08:00
|
|
|
|
m_btn_save_lua = dynamic_cast<ui::Button*>(FindSubControl(L"btn_save_lua"));
|
|
|
|
|
|
2022-01-31 01:00:02 +08:00
|
|
|
|
m_rich_edit_2->SetRich(true);
|
|
|
|
|
m_rich_edit_2->SetReturnMsgWantCtrl(true);
|
|
|
|
|
m_rich_edit_2->SetText(string2wstring(mLuaScript));
|
|
|
|
|
|
2021-11-21 13:47:19 +08:00
|
|
|
|
wchar_t p[100] = { 0 };
|
2021-11-22 23:53:21 +08:00
|
|
|
|
wsprintf(p, L"<EFBFBD><EFBFBD>ַ%s,<2C>˿ں<CBBF>%d δ<><CEB4><EFBFBD><EFBFBD>", string2wstring(m_url).c_str(),m_port);
|
2021-11-21 13:47:19 +08:00
|
|
|
|
m_label_1->SetText(std::wstring(p));
|
|
|
|
|
|
2021-11-22 23:53:21 +08:00
|
|
|
|
m_btn_send_data->AttachAllEvents([this](ui::EventArgs* ev) {
|
|
|
|
|
if (ev->Type == ui::EventType::kEventClick) {
|
|
|
|
|
std::wcout << m_uart_send_edit->GetText() << std::endl;
|
|
|
|
|
auto data = wstring2string(m_uart_send_edit->GetText());
|
2022-02-06 23:29:47 +08:00
|
|
|
|
data += std::string("\r\n");
|
2021-11-22 23:53:21 +08:00
|
|
|
|
this->ClientEvent()->SendDataAsync(data.c_str(), data.size());
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2022-02-06 23:29:47 +08:00
|
|
|
|
|
2022-02-01 00:19:32 +08:00
|
|
|
|
if (nullptr != m_btn_save_lua) {
|
|
|
|
|
m_btn_save_lua->AttachClick([this](ui::EventArgs*) {
|
|
|
|
|
std::cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>lua<EFBFBD>ű<EFBFBD>\r\n";
|
|
|
|
|
std::string lua = wstring2string(m_rich_edit_2->GetText());
|
|
|
|
|
if (0 == this->mLua->UpdateScript(lua)) {
|
|
|
|
|
this->mLuaScript = lua;
|
2022-02-03 00:22:01 +08:00
|
|
|
|
mLuaFileEdit = std::ofstream(TCP_CLIENT_SCRIPT, std::ios::out | std::ios::trunc);
|
|
|
|
|
mLuaFileEdit.write(lua.c_str(), lua.size());
|
|
|
|
|
mLuaFileEdit.flush();
|
|
|
|
|
mLuaFileEdit.close();
|
2022-02-01 00:19:32 +08:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
MessageBox(0, L"lua<EFBFBD>ű<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>", L"", 0);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
std::cout << lua.c_str() << "\r\n";
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-11-22 23:53:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TcpClientLibevent* TcpClientForm::ClientEvent()
|
|
|
|
|
{
|
|
|
|
|
return mClient;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-25 23:45:07 +08:00
|
|
|
|
LuaDelegate* TcpClientForm::LuaVM()
|
|
|
|
|
{
|
|
|
|
|
return mLua;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-22 23:53:21 +08:00
|
|
|
|
void TcpClientForm::OnConnected()
|
|
|
|
|
{
|
|
|
|
|
std::cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳɹ<EFBFBD>\r\n"<<__FILE__<<" " << __LINE__ << std::endl;
|
|
|
|
|
wchar_t p[100] = { 0 };
|
|
|
|
|
wsprintf(p, L"<EFBFBD><EFBFBD>ַ%s,<2C>˿ں<CBBF>%d <20><><EFBFBD>ӳɹ<D3B3>", string2wstring(m_url).c_str(), m_port);
|
|
|
|
|
m_label_1->SetText(std::wstring(p));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TcpClientForm::OnDisConnected(std::string reason)
|
|
|
|
|
{
|
|
|
|
|
wchar_t p[100] = { 0 };
|
|
|
|
|
wsprintf(p, L"<EFBFBD><EFBFBD>ַ%s,<2C>˿ں<CBBF>%d δ<><CEB4><EFBFBD><EFBFBD> " , string2wstring(m_url).c_str(), m_port);
|
|
|
|
|
m_label_1->SetText(std::wstring(p) + L" " + string2wstring(reason));
|
|
|
|
|
m_connected = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TcpClientForm::OnData(uint8_t* dat, uint64_t len)
|
|
|
|
|
{
|
2022-01-31 01:00:02 +08:00
|
|
|
|
//std::cout << (char*)dat << "len is " << len << std::endl;
|
|
|
|
|
//m_rich_edit_1->AppendText(string2wstring(std::string((char*)dat)), false);
|
2022-02-01 00:19:32 +08:00
|
|
|
|
int ret = this->mLua->CallFuntion <std::string>
|
|
|
|
|
("OnClientRecvData", std::string((char*)dat));
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
MessageBox(0, L"lua<EFBFBD>ű<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> OnClientRecvData", L"", 0);
|
|
|
|
|
}
|
2021-11-22 23:53:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TcpClientForm::OnClose()
|
|
|
|
|
{
|
2022-01-25 23:45:07 +08:00
|
|
|
|
m_rich_edit_1->AppendText(string2wstring("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><EFBFBD>Ͽ<EFBFBD>\r\n"), false);
|
2021-10-11 00:00:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 00:51:48 +08:00
|
|
|
|
|
2021-10-11 00:00:10 +08:00
|
|
|
|
void TcpClientForm::HandleMessage(ui::EventArgs& msg)
|
|
|
|
|
{
|
|
|
|
|
}
|