nim_duilib/examples/proto_debuger/tcp_server_form.cpp

217 lines
6.6 KiB
C++
Raw Permalink Normal View History

2021-12-30 17:27:33 +08:00
#include "tcp_server_form.h"
2021-12-31 15:35:46 +08:00
#include "utils.h"
2022-01-06 00:31:24 +08:00
#include <functional>
2022-01-31 01:00:02 +08:00
#include "lua_bind.h"
2022-02-19 00:53:51 +08:00
#include "msgdef.h"
2022-01-06 00:31:24 +08:00
extern "C" {
#include "event2/bufferevent.h"
#include "event2/buffer.h"
#include "event2/listener.h"
#include "event2/util.h"
#include "event2/event.h"
#include "event2/thread.h"
};
2022-02-03 00:22:01 +08:00
#define TCP_SERVER_SCRIPT "tcp_server.lua"
TcpServerFrom::~TcpServerFrom() {
}
TcpServerFrom::TcpServerFrom(ui::Window* hwnd, string url,
uint32_t port, TcpServerLibevent* p):
mLua(nullptr)
2021-12-30 17:27:33 +08:00
{
2021-12-31 01:12:11 +08:00
m_server = p;
m_url = url;
m_port = port;
2022-01-11 01:07:59 +08:00
mFlagSelectClient = true;
2022-01-27 00:42:33 +08:00
mLua = new LuaDelegate;
2022-02-03 00:22:01 +08:00
mLuaFileRead.open(TCP_SERVER_SCRIPT);
2022-01-27 00:42:33 +08:00
std::string lua_script;
2022-02-03 00:22:01 +08:00
if (mLuaFileRead.is_open()) {
2022-01-27 00:42:33 +08:00
std::string s;
2022-02-03 00:22:01 +08:00
while (getline(mLuaFileRead, s)) {
2022-01-27 00:42:33 +08:00
lua_script += s + "\r\n";
}
2022-02-03 00:22:01 +08:00
mLuaFileRead.close();
2022-01-27 00:42:33 +08:00
}
mLuaScript = lua_script;
mLua->DoString(lua_script);
std::cout << "lua script is " << lua_script << std::endl;
2021-12-31 01:12:11 +08:00
2022-01-08 00:23:09 +08:00
TcpServerLibevent::OnAccept handler = std::bind(&TcpServerFrom::OnNewConnAccept, this,std::placeholders::_1 );
2022-01-06 00:31:24 +08:00
m_server->SetNewConnectionHandle(handler);
TcpServerLibevent::OnDisconnect handler2 = std::bind(&TcpServerFrom::OnDisConnected,
this, std::placeholders::_1);
m_server->SetConnectionLeaveHandle(handler2);
2022-01-31 01:00:02 +08:00
this->mLua->BindFunction("showdata", LuaShowData);
2021-12-30 17:27:33 +08:00
}
TcpServerLibevent* TcpServerFrom::ServerP()
{
return this->m_server;
}
void TcpServerFrom::Init()
{
ui::ChildBox::Init(); // <20>dz<EFBFBD><C7B3><EFBFBD>Ҫ,û<><C3BB><EFBFBD>º<EFBFBD>childbox <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>FindSubControl<6F>޷<EFBFBD><DEB7><EFBFBD>Ч
2022-01-06 00:31:24 +08:00
m_label_1 = dynamic_cast<ui::Label*>(FindSubControl(L"server_info"));
m_rich_edit_1 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"server_recv_edit"));
2022-01-27 00:42:33 +08:00
mEditLua = dynamic_cast<ui::RichEdit*>(FindSubControl(L"lua_script_edit"));
mEditLua->SetRich(true);
mEditLua->SetReturnMsgWantCtrl(true);
mEditLua->SetText(string2wstring(mLuaScript));
2022-01-06 00:31:24 +08:00
m_rich_edit_3 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"server_send_edit"));
m_button_1 = dynamic_cast<ui::Button*>(FindSubControl(L"btn_send_data"));
m_check_box_2 = dynamic_cast<ui::CheckBox*>(FindSubControl(L"check_time_send"));
m_rich_edit_4 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"cycle_eidt"));
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_button_2 = dynamic_cast<ui::Button*>(FindSubControl(L"btn_close_uart"));
m_label_2 = dynamic_cast<ui::Label*>(FindSubControl(L"title"));
m_combo_1 = dynamic_cast<ui::Combo*>(FindSubControl(L"clients"));
m_button_3 = dynamic_cast<ui::Button*>(FindSubControl(L"btn_do_lua"));
2022-01-31 01:00:02 +08:00
m_btn_save_lua = dynamic_cast<ui::Button*>(FindSubControl(L"btn_save_lua"));
2022-02-19 00:53:51 +08:00
if (m_button_2 != nullptr) {
m_button_2->AttachClick([this](ui::EventArgs* ev) {
wstring* name = new wstring(this->GetName());
::PostMessage(this->GetWindow()->GetHWND(),
WM_USER_TCP_SERVER_CLOSE, (WPARAM)name, 0);
return true;
});
}
2022-01-31 01:00:02 +08:00
if (nullptr != m_button_1) {
m_button_1->AttachClick([this](ui::EventArgs* ev) {
auto client = m_combo_1->GetText();
if (client != L"ȫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>") {
int clii = atoi(wstring2string(client).c_str());
if (nullptr != this->mClients[clii]) {
std::wstring send = m_rich_edit_3->GetText();
this->mClients[clii]->WriteData(wstring2string(send).c_str(),
wstring2string(send).size());
}
}
else {
if ((nullptr != m_rich_edit_3)) {
std::wstring send = m_rich_edit_3->GetText();
for (auto itr = this->mClients.begin(); itr != this->mClients.end(); itr++) {
itr->second->WriteData(wstring2string(send).c_str(),
wstring2string(send).size());
}
}
}
return true;
});
}
m_btn_save_lua->AttachClick([this](ui::EventArgs* ev) {
std::cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>lua<EFBFBD>ű<EFBFBD>\r\n";
std::string lua = wstring2string(mEditLua->GetText());
if (0 == this->mLua->UpdateScript(lua)) {
this->mLuaScript = lua;
2022-02-03 00:22:01 +08:00
mLuaFileEdit = std::ofstream(TCP_SERVER_SCRIPT, std::ios::out | std::ios::trunc);
mLuaFileEdit.write(lua.c_str(), lua.size());
mLuaFileEdit.flush();
mLuaFileEdit.close();
2022-01-31 01:00:02 +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;
});
2022-01-11 01:07:59 +08:00
m_button_3->AttachClick([this](ui::EventArgs*ev) {
std::cout <<"m_button_3 " << wstring2string(this->m_combo_1->GetText())<<std::endl;
return true;
});
2021-12-31 15:35:46 +08:00
wchar_t p[200] = L"";
wsprintf(p,L"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ: %s <20>˿<EFBFBD>: %d <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ: %d",string2wstring(m_url).c_str(),
m_port,
m_server->ConnectionCount());
m_label_1->SetText(p);
2022-01-11 01:07:59 +08:00
auto element = new ui::ListContainerElement;
element->SetClass(L"listitem");
element->SetFixedHeight(30);
element->SetBkColor(L"white");
element->SetTextPadding({ 6,0,6,0 });
element->SetText(L"ȫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
m_combo_1->Add(element);
2021-12-30 17:27:33 +08:00
}
2022-01-05 01:41:58 +08:00
2022-01-11 01:07:59 +08:00
void TcpServerFrom::OnNewConnAccept(ConnectionLibevent* p)
2022-01-06 00:31:24 +08:00
{
2022-01-11 01:07:59 +08:00
std::cout << "TcpServerFrom OnNewConnAccept addr " << p->IpAddress() << p->SocketFd() << std::endl;
2022-01-08 00:23:09 +08:00
updateStatus();
2022-01-31 01:00:02 +08:00
int ret = mLua->CallFuntion < std::string, LUA_INTEGER >("OnNewClient",p->IpAddress(),p->SocketFd());
if (ret < 0) {
std::cout << "call funtion error" << ret<<"std::endl";
}
2022-01-09 11:00:38 +08:00
auto element = new ui::ListContainerElement;
element->SetClass(L"listitem");
element->SetFixedHeight(30);
element->SetBkColor(L"white");
element->SetTextPadding({ 6,0,6,0 });
element->SetText(std::to_wstring(p->SocketFd()));
2022-01-11 01:07:59 +08:00
mClients[p->SocketFd()] = p;
2022-01-09 11:00:38 +08:00
m_combo_1->Add(element);
2022-01-13 01:37:24 +08:00
p->SetRecvHandler(
2022-01-31 01:00:02 +08:00
[this,p](char* recv, uint32_t len) {
std::cout << "RecvHandler "<<p->IpAddress()<<p->SocketFd() <<" " << recv;
int ret = this->mLua->CallFuntion <std::string,std::string,LUA_INTEGER >
("OnClientRecvData", std::string(recv),p->IpAddress(), p->SocketFd());
if (ret < 0) {
MessageBox(0, L"lua<EFBFBD>ű<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> OnClientRecvData", L"", 0);
}
2022-01-13 01:37:24 +08:00
}
);
2022-01-08 00:23:09 +08:00
return;
2022-01-06 00:31:24 +08:00
}
void TcpServerFrom::OnDisConnected(ConnectionLibevent* cli)
2022-01-05 01:41:58 +08:00
{
2022-01-06 00:31:24 +08:00
std::cout << "TcpServerFrom Disconnected" << cli->IpAddress() << " fd " << cli->SocketFd() << std::endl;
2022-01-08 00:23:09 +08:00
updateStatus();
2022-01-11 01:07:59 +08:00
mClients.erase(cli->SocketFd());
2022-01-31 01:00:02 +08:00
int ret = this->mLua->CallFuntion <std::string, LUA_INTEGER >
("OnClientDisconnect", cli->IpAddress(), cli->SocketFd());
if (ret < 0) {
MessageBox(0, L"lua<EFBFBD>ű<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> OnClientRecvData", L"", 0);
}
2022-01-08 00:23:09 +08:00
}
2022-01-25 23:45:07 +08:00
LuaDelegate* TcpServerFrom::LuaVM()
{
return mLua;
}
void TcpServerFrom::ShowDataInEdit(const char* value)
{
if (nullptr == value) {
std::cout << "nullp pointer in ShowDataInEdit" << std::endl;
return;
}
2022-01-31 01:00:02 +08:00
m_rich_edit_1->AppendText(string2wstring(std::string(value)));
2022-01-25 23:45:07 +08:00
}
2022-01-08 00:23:09 +08:00
void TcpServerFrom::updateStatus()
{
wchar_t tmp[200] = L"";
wsprintf(tmp, L"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ: %s <20>˿<EFBFBD>: %d <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ: %d", string2wstring(m_url).c_str(),
m_port,
m_server->ConnectionCount());
m_label_1->SetText(tmp);
2022-01-05 01:41:58 +08:00
}