148 lines
4.5 KiB
C++
148 lines
4.5 KiB
C++
#include "tcp_client_form.h"
|
||
#include "utils.h"
|
||
#include "lua_bind.h"
|
||
|
||
|
||
#define TCP_CLIENT_SCRIPT "tcp_client.lua"
|
||
TcpClientForm::~TcpClientForm() {
|
||
delete mClient;
|
||
std::cout << "45678\r\n";
|
||
}
|
||
|
||
TcpClientForm::TcpClientForm(ui::Window* hwnd,std::string url, uint32_t port, TcpClientLibevent* p):
|
||
m_connected(false),
|
||
mLua(nullptr) {
|
||
|
||
mClient = p;
|
||
mClient->SetObserver(this);
|
||
if (nullptr != hwnd) {
|
||
this->SetWindow(hwnd, nullptr, false);
|
||
}
|
||
|
||
mLua = new LuaDelegate;
|
||
mLuaFileRead.open(TCP_CLIENT_SCRIPT);
|
||
std::string lua_script;
|
||
if (mLuaFileRead.is_open()) {
|
||
std::string s;
|
||
while (getline(mLuaFileRead, s)) {
|
||
lua_script += s + "\r\n";
|
||
}
|
||
mLuaFileRead.close();
|
||
}
|
||
mLuaScript = lua_script;
|
||
mLua->DoString(lua_script);
|
||
|
||
std::cout << "lua script is " << lua_script << std::endl;
|
||
m_url = url;
|
||
m_port = port;
|
||
this->mLua->BindFunction("showdata", LuaShowData);
|
||
}
|
||
|
||
void TcpClientForm::ShowDataInEdit(const char*p) {
|
||
if (nullptr == p) {
|
||
std::cout << "ShowDataInEdit " << std::endl;
|
||
}
|
||
m_rich_edit_1->AppendText(string2wstring(p), false);
|
||
}
|
||
|
||
void TcpClientForm::Init() {
|
||
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"));
|
||
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"));
|
||
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"));
|
||
m_btn_save_lua = dynamic_cast<ui::Button*>(FindSubControl(L"btn_save_lua"));
|
||
m_btn_close_form = dynamic_cast<ui::Button*>(FindSubControl(L"btn_close_uart"));
|
||
|
||
m_rich_edit_2->SetRich(true);
|
||
m_rich_edit_2->SetReturnMsgWantCtrl(true);
|
||
m_rich_edit_2->SetText(string2wstring(mLuaScript));
|
||
|
||
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));
|
||
|
||
if (nullptr != m_btn_close_form) {
|
||
m_btn_close_form->AttachClick([this](ui::EventArgs*) {
|
||
wstring* name = new wstring(this->GetName());
|
||
::PostMessage(this->GetWindow()->GetHWND(),
|
||
WM_USER_TCP_CLIENT_CLOSE, (WPARAM)name, 0);
|
||
return true;
|
||
});
|
||
}
|
||
|
||
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());
|
||
data += std::string("\r\n");
|
||
this->ClientEvent()->SendDataAsync(data.c_str(), data.size());
|
||
}
|
||
return true;
|
||
});
|
||
|
||
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;
|
||
mLuaFileEdit = std::ofstream(TCP_CLIENT_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;
|
||
});
|
||
}
|
||
}
|
||
|
||
TcpClientLibevent* TcpClientForm::ClientEvent() {
|
||
return mClient;
|
||
}
|
||
|
||
LuaDelegate* TcpClientForm::LuaVM() {
|
||
return mLua;
|
||
}
|
||
|
||
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) {
|
||
//std::cout << (char*)dat << "len is " << len << std::endl;
|
||
//m_rich_edit_1->AppendText(string2wstring(std::string((char*)dat)), false);
|
||
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);
|
||
}
|
||
}
|
||
|
||
void TcpClientForm::OnClose() {
|
||
m_rich_edit_1->AppendText(string2wstring("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><EFBFBD>Ͽ<EFBFBD>\r\n"), false);
|
||
}
|
||
|
||
|
||
void TcpClientForm::HandleMessage(ui::EventArgs& msg) {
|
||
}
|