no message

This commit is contained in:
zcy 2021-12-30 16:56:16 +08:00
parent 701c00d8ce
commit bfb52281e8
11 changed files with 99 additions and 38 deletions

View File

@ -30,7 +30,7 @@ std::wstring BasicForm::GetWindowClassName() const
{ {
return kClassName; return kClassName;
} }
int gTcpServerCnt = 0;
int gTcpClientCnt = 0; int gTcpClientCnt = 0;
LRESULT BasicForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) LRESULT BasicForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{ {
@ -122,38 +122,37 @@ LRESULT BasicForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
if (uMsg == WM_ADD_TCPSERVER_MONITOR) { if (uMsg == WM_ADD_TCPSERVER_MONITOR) {
TcpServerInfo* info = (TcpServerInfo*)wParam; TcpServerInfo* info = (TcpServerInfo*)wParam;
TcpServerLibevent* cli = (TcpServerLibevent*)lParam; TcpServerLibevent* server = (TcpServerLibevent*)lParam;
ui::TreeNode* node = new ui::TreeNode; ui::TreeNode* node = new ui::TreeNode;
auto key = info->ip + L":" + std::to_wstring(gTcpClientCnt); auto key = info->ip + L":" + std::to_wstring(gTcpServerCnt);
node->SetText(key); node->SetText(key);
node->SetClass(L"listitem"); node->SetClass(L"listitem");
node->SetFixedHeight(20); node->SetFixedHeight(20);
node->SetMargin({ 20, 0, 0, 0 }); node->SetMargin({ 20, 0, 0, 0 });
mMonitor->GetRootNode()->GetChildNode(1)->AddChildNode(node); mMonitor->GetRootNode()->GetChildNode(2)->AddChildNode(node);
gTcpClientCnt++; gTcpServerCnt++;
printf("WM_ADD_TCPSERVER_MONITOR\r\n"); printf("WM_ADD_TCPSERVER_MONITOR\r\n");
if (mTcpClientForm.find(info->ip + L":" if (mTcpServerForm.find(info->ip + L":"
+ std::to_wstring(gTcpClientCnt)) == mTcpClientForm.end()) + std::to_wstring(gTcpServerCnt)) == mTcpServerForm.end())
{ {
/* auto form = new TcpServerFrom(this, wstring2string(info->ip), info->port, server);
auto form = new TcpClientForm(this, wstring2string(info->ip), info->port, cli); form->SetChildLayoutXML(L"basic/tcp_server_form.xml");
form->SetChildLayoutXML(L"basic/tcp_form.xml");
form->SetName(key); form->SetName(key);
form->SetVisible(false); form->SetVisible(false);
mTcpClientForm[key] = form; mTcpServerForm[key] = form;
if (!mRightSide->Add(form)) if (!mRightSide->Add(form))
printf("erroer 1"); printf("error 1");
*/
} }
/*
node->AttachAllEvents( node->AttachAllEvents(
[this](ui::EventArgs* ev) { [this](ui::EventArgs* ev) {
if (ui::EventType::kEventSelect == ev->Type) { if (ui::EventType::kEventSelect == ev->Type) {
wprintf(L"%s\r\n", dynamic_cast<ui::TreeNode*> (ev->pSender)->GetText().c_str()); wprintf(L"%s\r\n", dynamic_cast<ui::TreeNode*> (ev->pSender)->GetText().c_str());
printf("GetCurSel %d\r\n", mRightSide->GetCurSel()); printf("GetCurSel %d\r\n", mRightSide->GetCurSel());
TcpClientForm* p = mTcpClientForm[dynamic_cast<ui::TreeNode*> (ev->pSender)->GetText()]; TcpServerFrom* p = mTcpServerForm[dynamic_cast<ui::TreeNode*> (ev->pSender)->GetText()];
if (p != nullptr) { if (p != nullptr) {
printf("GetCurSel %d\r\n", mRightSide->GetCurSel()); printf("GetCurSel %d\r\n", mRightSide->GetCurSel());
@ -177,7 +176,7 @@ LRESULT BasicForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
} }
); );
*/
} }

View File

@ -17,6 +17,7 @@
#include "new_monitor_form.h" #include "new_monitor_form.h"
#include "uart_process.h" #include "uart_process.h"
#include "tcp_client_form.h" #include "tcp_client_form.h"
#include "tcp_server_form.h"
#include <vector> #include <vector>
#include <map> #include <map>
@ -55,6 +56,7 @@ private:
ui::TreeView *mMonitor; ui::TreeView *mMonitor;
std::map<std::wstring,UartForm*> mUartForm; std::map<std::wstring,UartForm*> mUartForm;
std::map<std::wstring, TcpClientForm*> mTcpClientForm; std::map<std::wstring, TcpClientForm*> mTcpClientForm;
std::map<std::wstring, TcpServerFrom*> mTcpServerForm;
ui::Control* mRightShow; ui::Control* mRightShow;
}; };

View File

@ -3,7 +3,7 @@
#define WM_USER_POS_CHANGED (WM_USER + 2) #define WM_USER_POS_CHANGED (WM_USER + 2)
#define WM_ADD_UART_MONITOR (WM_USER + 3) #define WM_ADD_UART_MONITOR (WM_USER + 3)
#define WM_ADD_TCPCLIENT_MONITOR (WM_USER + 4) #define WM_ADD_TCPCLIENT_MONITOR (WM_USER + 4)
#define WM_ADD_TCPSERVER_MONITOR (WM_USER + 4)
#define WM_ADD_UART_RECVDATA (WM_USER + 5) #define WM_ADD_UART_RECVDATA (WM_USER + 5)
#define WM_ADD_UART_CLOSE (WM_USER + 6) #define WM_ADD_UART_CLOSE (WM_USER + 6)
#define WM_ADD_TCPSERVER_MONITOR (WM_USER + 7)

View File

@ -161,6 +161,7 @@ void NewMonitorForm::InitWindow()
wprintf(L"%s\r\n", m_port_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()); int port = atoi(wstring2string(m_port_select->GetText()).c_str());
m_tcp_server = new TcpServerLibevent(port, wstring2string(m_ip_select->GetText().c_str())); m_tcp_server = new TcpServerLibevent(port, wstring2string(m_ip_select->GetText().c_str()));
m_tcp_server->StartServerAsync();
TcpServerInfo* p = new TcpServerInfo; TcpServerInfo* p = new TcpServerInfo;
p->ip = m_ip_select->GetText(); p->ip = m_ip_select->GetText();
p->port = port; p->port = port;

View File

@ -168,6 +168,7 @@
<ClCompile Include="main.cpp" /> <ClCompile Include="main.cpp" />
<ClCompile Include="new_monitor_form.cpp" /> <ClCompile Include="new_monitor_form.cpp" />
<ClCompile Include="serial_port.cpp" /> <ClCompile Include="serial_port.cpp" />
<ClCompile Include="tcp_server_form.cpp" />
<ClCompile Include="tcp_client.cpp" /> <ClCompile Include="tcp_client.cpp" />
<ClCompile Include="tcp_client_form.cpp" /> <ClCompile Include="tcp_client_form.cpp" />
<ClCompile Include="tcp_server_libevent.cpp" /> <ClCompile Include="tcp_server_libevent.cpp" />
@ -182,6 +183,7 @@
<ClInclude Include="new_monitor_form.h" /> <ClInclude Include="new_monitor_form.h" />
<ClInclude Include="resource1.h" /> <ClInclude Include="resource1.h" />
<ClInclude Include="serial_port.h" /> <ClInclude Include="serial_port.h" />
<ClInclude Include="tcp_server_form.h" />
<ClInclude Include="tcp_client_form.h" /> <ClInclude Include="tcp_client_form.h" />
<ClInclude Include="uart_process.h" /> <ClInclude Include="uart_process.h" />
<ClInclude Include="utils.h" /> <ClInclude Include="utils.h" />
@ -192,6 +194,7 @@
<Xml Include="..\Debug\resources\themes\default\basic\uart_form.xml" /> <Xml Include="..\Debug\resources\themes\default\basic\uart_form.xml" />
<Xml Include="..\Debug\resources\themes\default\global.xml" /> <Xml Include="..\Debug\resources\themes\default\global.xml" />
<Xml Include="..\x64\Debug\resources\themes\default\basic\tcp_form.xml" /> <Xml Include="..\x64\Debug\resources\themes\default\basic\tcp_form.xml" />
<Xml Include="..\x64\Debug\resources\themes\default\basic\tcp_server_form.xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ResourceCompile Include="proto_debuger.rc" /> <ResourceCompile Include="proto_debuger.rc" />

View File

@ -48,6 +48,9 @@
<ClCompile Include="loger.cpp"> <ClCompile Include="loger.cpp">
<Filter>源文件</Filter> <Filter>源文件</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="tcp_server_form.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="main.h"> <ClInclude Include="main.h">
@ -80,6 +83,9 @@
<ClInclude Include="lua_wraper.h"> <ClInclude Include="lua_wraper.h">
<Filter>头文件</Filter> <Filter>头文件</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="tcp_server_form.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Xml Include="..\Debug\resources\themes\default\global.xml"> <Xml Include="..\Debug\resources\themes\default\global.xml">
@ -97,6 +103,9 @@
<Xml Include="..\x64\Debug\resources\themes\default\basic\tcp_form.xml"> <Xml Include="..\x64\Debug\resources\themes\default\basic\tcp_form.xml">
<Filter>资源文件</Filter> <Filter>资源文件</Filter>
</Xml> </Xml>
<Xml Include="..\x64\Debug\resources\themes\default\basic\tcp_server_form.xml">
<Filter>资源文件</Filter>
</Xml>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ResourceCompile Include="proto_debuger.rc"> <ResourceCompile Include="proto_debuger.rc">

View File

@ -109,31 +109,31 @@ TcpClientLibevent::TcpClientLibevent(std::string addrinfo, int port, TcpClientLi
mStatus(UNCONNECTED), mStatus(UNCONNECTED),
mObserver(nullptr) mObserver(nullptr)
{ {
memset(&mSrv, 0, sizeof(mSrv)); memset(&mSrv, 0, sizeof(mSrv));
#ifdef linux #ifdef linux
mSrv.sin_addr.s_addr = inet_addr(addrinfo.c_str()); mSrv.sin_addr.s_addr = inet_addr(addrinfo.c_str());
mSrv.sin_family = AF_INET; mSrv.sin_family = AF_INET;
#endif #endif
#ifdef _WIN32 #ifdef _WIN32
mSrv.sin_addr.S_un.S_addr = inet_addr(addrinfo.c_str()); mSrv.sin_addr.S_un.S_addr = inet_addr(addrinfo.c_str());
mSrv.sin_family = AF_INET; mSrv.sin_family = AF_INET;
#endif #endif
mSrv.sin_port = htons(port); mSrv.sin_port = htons(port);
mBase = event_base_new(); mBase = event_base_new();
if (!mBase) if (!mBase)
{ {
printf("Could not initialize libevent\n"); printf("Could not initialize libevent\n");
} }
#ifdef WIN32 #ifdef WIN32
evthread_use_windows_threads(); evthread_use_windows_threads();
#else #else
evthread_use_pthreads(); evthread_use_pthreads();
#endif #endif
this->mThread = new thread(ThreadRun,this); this->mThread = new thread(ThreadRun, this);
this->mObserver = p; this->mObserver = p;
mByteRecv = 0; mByteRecv = 0;
mByteSend = 0; mByteSend = 0;
} }

View File

@ -31,8 +31,6 @@ protected:
virtual void OnData(uint8_t* dat, uint64_t len) ; virtual void OnData(uint8_t* dat, uint64_t len) ;
virtual void OnClose(); virtual void OnClose();
private: private:
ui::Label* m_label_1; ui::Label* m_label_1;
ui::RichEdit* m_rich_edit_1; ui::RichEdit* m_rich_edit_1;

View File

@ -127,7 +127,8 @@ void event_cb(struct bufferevent* bev, short events, void* arg)
bufferevent_free(bev); bufferevent_free(bev);
server->RemoveConnection(conn->SocketFd()); server->RemoveConnection(conn->SocketFd());
} }
delete conn;
//delete conn;
} }
void ServerCallbacks::cb_listener(struct evconnlistener* listener, evutil_socket_t fd, struct sockaddr* addr, int len, void* ptr) void ServerCallbacks::cb_listener(struct evconnlistener* listener, evutil_socket_t fd, struct sockaddr* addr, int len, void* ptr)
@ -198,8 +199,10 @@ void ServerCallbacks::server_run(TcpServerLibevent* p) {
if (nullptr != p) { if (nullptr != p) {
if (p->m_status == TcpServerLibevent::STOP) { if (p->m_status == TcpServerLibevent::STOP) {
p->m_status = TcpServerLibevent::RUNNING; p->m_status = TcpServerLibevent::RUNNING;
event_base_dispatch(p->m_event_base); event_base_dispatch(p->m_event_base);
evconnlistener_free(p->m_event_listener); if(!p->m_event_listener)
evconnlistener_free(p->m_event_listener);
event_base_free(p->m_event_base); event_base_free(p->m_event_base);
} }
} }
@ -275,6 +278,7 @@ TcpServerLibevent::TcpServerLibevent(int port, string bindip) :
m_backlog, m_backlog,
(struct sockaddr*)&m_server_addr, (struct sockaddr*)&m_server_addr,
sizeof(m_server_addr)); sizeof(m_server_addr));
if (NULL == m_event_listener) if (NULL == m_event_listener)
{ {
m_status = FAIL; m_status = FAIL;
@ -323,4 +327,10 @@ TcpServerLibevent::~TcpServerLibevent() {
event_base_loopbreak(m_event_base); event_base_loopbreak(m_event_base);
this->m_status = STOP; this->m_status = STOP;
} }
} }
uint64_t TcpServerLibevent::SocketFd()
{
return mSocketFD;
}

View File

@ -70,6 +70,7 @@ public:
int RemoveConnection(uint32_t); int RemoveConnection(uint32_t);
int SetNewConnectionHandle(OnAccept); int SetNewConnectionHandle(OnAccept);
int AddConnection(uint32_t fd, ConnectionLibevent* p); int AddConnection(uint32_t fd, ConnectionLibevent* p);
uint64_t SocketFd();
friend struct ServerCallbacks; friend struct ServerCallbacks;
private: private:
uint32_t m_port; // 监听端口号 uint32_t m_port; // 监听端口号
@ -83,6 +84,8 @@ private:
thread* m_thread; thread* m_thread;
map<uint32_t, ConnectionLibevent*> m_map_client; map<uint32_t, ConnectionLibevent*> m_map_client;
OnAccept m_handle_accept; OnAccept m_handle_accept;
intptr_t mSocketFD; // 操作系统原生socket
}; };
#endif #endif

View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<ChildBox name="tcp_form" width="stretch" height="stretch">
<VBox width="stretch" height="stretch">
<Label name="uart_info_label" text="当前串口: COM" margin="0,10,0,0" height="25" />
<HBox height="25" width="stretch">
<Label text="数据接收:" margin="0,10,0,0" height="25" width="stretch"/>
<Label text="lua脚本:" margin="0,10,0,0" height="25" width="stretch"/>
<Control width="stretch"/>
<Control width="stretch"/>
</HBox>
<HBox>
<RichEdit class="simple input" width="stretch" name="uart_recv_eidt" height="stretch" margin="5,5,5,5" padding="6,6,6" multiline="true" vscrollbar="true" hscrollbar="true" autovscroll="true" normaltextcolor="darkcolor" wantreturnmsg="true" rich="true" />
<RichEdit class="simple input" width="stretch" name="lua_script" height="stretch" margin="5,5,5,5" padding="6,6,6" multiline="true" vscrollbar="true" hscrollbar="true" autovscroll="true" normaltextcolor="darkcolor" wantreturnmsg="true" rich="true" />
</HBox>
<Label text="发送数据" height="25" />
<RichEdit class="simple input" width="stretch" name="uart_send_edit" height="30" margin="5,5,5,5" padding="6,6,6" />
<HBox height="40">
<Button class="btn_global_blue_80x30" name="btn_send_data" width="100" text="发送" bkcolor="lightcolor" margin="25,3,5,3" />
<CheckBox class="checkbox_font12" name="check_new_line" text="发送新行" margin="10,12,0,0" selected="true"/>
<CheckBox class="checkbox_font12" name="check_time_send" text="定时发送" margin="10,12,0,0" selected="true"/>
<RichEdit class="simple input" name="cycle_eidt" width="50" margin="5,5,5,5" padding="6,6,6" />
<Label text="ms" margin="5,5,5,5" width="50" />
<CheckBox class="checkbox_font12" name="check_hex_send" text="hex显示" margin="10,12,0,0" selected="true"/>
<CheckBox class="checkbox_font12" name="check_hex_recv" text="hex接受" margin="10,12,0,0" selected="true"/>
<Button class="btn_global_blue_80x30" name="btn_close_uart" width="100" text="关闭" bkcolor="lightcolor" margin="25,3,5,3" />
<Label name="title" text="客户端:" width="60" valign="bottom" margin="0,0,0,0" height="30" font="arial_14"/>
<Combo class="list" name="clients" width="100" height="30" bkimage="file='../public/combo/normal.png' corner='5,5,30,5'"/>
</HBox>
<HBox height="40">
<Button class="btn_global_blue_80x30" name="btn_do_lua" width="100" text="执行" bkcolor="lightcolor" margin="25,3,5,3" />
<RichEdit class="simple input" width="stretch" name="lua_do" height="30" margin="5,5,5,5" multiline="true" vscrollbar="true" hscrollbar="true" autovscroll="true" normaltextcolor="darkcolor" wantreturnmsg="true" rich="true" />
</HBox>
</VBox>
</ChildBox>