no message

This commit is contained in:
zcy 2021-10-11 00:00:10 +08:00
parent 249b3c0ac6
commit d6045de076
19 changed files with 295 additions and 76 deletions

View File

@ -143,7 +143,7 @@
<ConfigurationType>StaticLibrary</ConfigurationType> <ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries> <UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>Unicode</CharacterSet> <CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v120_xp</PlatformToolset> <PlatformToolset>v142</PlatformToolset>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType> <ConfigurationType>StaticLibrary</ConfigurationType>

View File

@ -42,7 +42,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries> <UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v120_xp</PlatformToolset> <PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet> <CharacterSet>Unicode</CharacterSet>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
@ -131,6 +131,7 @@
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<DelayLoadDLLs>nim_libcef.dll</DelayLoadDLLs> <DelayLoadDLLs>nim_libcef.dll</DelayLoadDLLs>
<AdditionalDependencies>nim_libcef.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">

View File

@ -31,8 +31,6 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ui_components", "..\ui_comp
EndProject EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "proto_debuger", "proto_debuger\proto_debuger.vcxproj", "{6D78D8B7-1617-4ED4-B155-94369FC3E73C}" Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "proto_debuger", "proto_debuger\proto_debuger.vcxproj", "{6D78D8B7-1617-4ED4-B155-94369FC3E73C}"
EndProject EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "catch_test", "catch_test\catch_test.vcxproj", "{91850193-0E5E-4983-9D11-F9875805D91E}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32 Debug|Win32 = Debug|Win32
@ -135,14 +133,6 @@ Global
{6D78D8B7-1617-4ED4-B155-94369FC3E73C}.Release|Win32.Build.0 = Release|Win32 {6D78D8B7-1617-4ED4-B155-94369FC3E73C}.Release|Win32.Build.0 = Release|Win32
{6D78D8B7-1617-4ED4-B155-94369FC3E73C}.Release|x64.ActiveCfg = Release|x64 {6D78D8B7-1617-4ED4-B155-94369FC3E73C}.Release|x64.ActiveCfg = Release|x64
{6D78D8B7-1617-4ED4-B155-94369FC3E73C}.Release|x64.Build.0 = Release|x64 {6D78D8B7-1617-4ED4-B155-94369FC3E73C}.Release|x64.Build.0 = Release|x64
{91850193-0E5E-4983-9D11-F9875805D91E}.Debug|Win32.ActiveCfg = Debug|Win32
{91850193-0E5E-4983-9D11-F9875805D91E}.Debug|Win32.Build.0 = Debug|Win32
{91850193-0E5E-4983-9D11-F9875805D91E}.Debug|x64.ActiveCfg = Debug|x64
{91850193-0E5E-4983-9D11-F9875805D91E}.Debug|x64.Build.0 = Debug|x64
{91850193-0E5E-4983-9D11-F9875805D91E}.Release|Win32.ActiveCfg = Release|Win32
{91850193-0E5E-4983-9D11-F9875805D91E}.Release|Win32.Build.0 = Release|Win32
{91850193-0E5E-4983-9D11-F9875805D91E}.Release|x64.ActiveCfg = Release|x64
{91850193-0E5E-4983-9D11-F9875805D91E}.Release|x64.Build.0 = Release|x64
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@ -156,7 +146,6 @@ Global
{A9D6DC71-C0DC-4549-AEA0-3B15B44E86A9} = {1DA0A8E2-5832-42FC-83F7-2CDCAD379C90} {A9D6DC71-C0DC-4549-AEA0-3B15B44E86A9} = {1DA0A8E2-5832-42FC-83F7-2CDCAD379C90}
{8BD95440-9000-4745-8011-27DD553EF06F} = {B2087994-3DF6-4A57-B8C6-6F744520D7FA} {8BD95440-9000-4745-8011-27DD553EF06F} = {B2087994-3DF6-4A57-B8C6-6F744520D7FA}
{E35589C6-9509-4116-996F-1D045C2DACAE} = {B2087994-3DF6-4A57-B8C6-6F744520D7FA} {E35589C6-9509-4116-996F-1D045C2DACAE} = {B2087994-3DF6-4A57-B8C6-6F744520D7FA}
{91850193-0E5E-4983-9D11-F9875805D91E} = {B2087994-3DF6-4A57-B8C6-6F744520D7FA}
EndGlobalSection EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {68CA0970-4242-4E4F-94D2-C19760FCA05D} SolutionGuid = {68CA0970-4242-4E4F-94D2-C19760FCA05D}

View File

@ -39,7 +39,7 @@
<CharacterSet>Unicode</CharacterSet> <CharacterSet>Unicode</CharacterSet>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Label="Configuration" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <PropertyGroup Label="Configuration" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<PlatformToolset>v120_xp</PlatformToolset> <PlatformToolset>v142</PlatformToolset>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Label="Configuration" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <PropertyGroup Label="Configuration" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<PlatformToolset>v120_xp</PlatformToolset> <PlatformToolset>v120_xp</PlatformToolset>

View File

@ -1,5 +1,6 @@
#include "base_form.h" #include "base_form.h"
#include "serial_port.h" #include "serial_port.h"
#include "utils.h"
const std::wstring BasicForm::kClassName = L"Basic"; const std::wstring BasicForm::kClassName = L"Basic";
@ -61,9 +62,58 @@ LRESULT BasicForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
//this->Paint(); //this->Paint();
} }
if (uMsg == WM_USER_POS_CHANGED) { if (uMsg == WM_USER_POS_CHANGED) {
} }
if (uMsg == WM_ADD_TCPCLIENT_MONITOR) {
TcpClientInfo* info = (TcpClientInfo*)wParam;
TcpClientLibevent* cli = (TcpClientLibevent*)lParam;
ui::TreeNode* node = new ui::TreeNode;
node->SetText(info->ip);
node->SetClass(L"listitem");
node->SetFixedHeight(20);
node->SetMargin({ 20, 0, 0, 0 });
mMonitor->GetRootNode()->GetChildNode(1)->AddChildNode(node);
if (mTcpClientForm.find(info->socket_fd) == mTcpClientForm.end()) {
auto form = new TcpClientForm(wstring2string(info->ip),info->port, cli);
form->SetChildLayoutXML(L"basic/tcp_form.xml");
form->SetName(info->ip);
form->SetVisible(false);
mTcpClientForm[info->socket_fd] = form;
if (!mRightSide->Add(form))
printf("erroer 1");
}
node->AttachAllEvents([this](ui::EventArgs* ev) {
if (ui::EventType::kEventSelect == ev->Type) {
wprintf(L"%s\r\n", dynamic_cast<ui::TreeNode*> (ev->pSender)->GetText().c_str());
printf("GetCurSel %d\r\n", mRightSide->GetCurSel());
//TcpClientForm* p = mTcpClientForm[dynamic_cast<ui::TreeNode*> (ev->pSender)->GetText()];
/*
if (p != nullptr) {
p->SetAutoDestroy(true);
if (mRightShow != nullptr) {
mRightShow->SetVisible(false);
p->SetVisible(true);
mRightShow = p;
mRightSide->SelectItem(p->GetName());
}
else {
p->SetVisible(true);
mRightSide->SelectItem(p->GetName());
mRightShow = p;
}
}*/
}
return true;
});
}
if (uMsg == WM_ADD_UART_MONITOR) { if (uMsg == WM_ADD_UART_MONITOR) {
printf("add monitor"); printf("add monitor");
UartInfo* p = (UartInfo*)wParam; UartInfo* p = (UartInfo*)wParam;

View File

@ -16,11 +16,13 @@
#include "ui_components/ui_components.h" #include "ui_components/ui_components.h"
#include "new_monitor_form.h" #include "new_monitor_form.h"
#include "uart_process.h" #include "uart_process.h"
#include "tcp_client_form.h"
#include <vector> #include <vector>
#include <map> #include <map>
#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)
class BasicForm : public ui::WindowImplBase class BasicForm : public ui::WindowImplBase
@ -55,5 +57,7 @@ private:
ui::TabBox *mRightSide; ui::TabBox *mRightSide;
ui::TreeView *mMonitor; ui::TreeView *mMonitor;
std::map<std::wstring,UartForm*> mUartForm; std::map<std::wstring,UartForm*> mUartForm;
std::map<uint32_t, TcpClientForm*> mTcpClientForm;
ui::Control* mRightShow; ui::Control* mRightShow;
}; };

View File

@ -16,6 +16,34 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_ LPWSTR lpCmdLine, _In_ LPWSTR lpCmdLine,
_In_ int nCmdShow) _In_ int nCmdShow)
{ {
#ifdef WIN32
WORD wdVersion = MAKEWORD(2, 2);//定义自己需要的网络库版本这里是2.2
WSADATA wdSockMsg;//这是一个结构体
int nRes = WSAStartup(wdVersion, &wdSockMsg);//打开一个套接字
if (0 != nRes) {
switch (nRes) {
case WSASYSNOTREADY:
printf("check your library");
break;
case WSAVERNOTSUPPORTED:
printf("need updated");
break;
case WSAEINPROGRESS:
printf("need reboot");
break;
case WSAEPROCLIM:
printf("sdfsdfsa");
break;
}
}
if (2 != HIBYTE(wdSockMsg.wVersion) || 2 != LOBYTE(wdSockMsg.wVersion)) {
printf("WSACleanup");
WSACleanup();
return 0;
}
#endif
AllocConsole(); AllocConsole();
freopen("CONOUT$", "w", stdout); freopen("CONOUT$", "w", stdout);

View File

@ -8,6 +8,9 @@
#include <winspool.h> #include <winspool.h>
#include "serial_port.h" #include "serial_port.h"
#include "base_form.h" #include "base_form.h"
#include "tcp_client.h"
#include "utils.h"
#include <string>
using namespace std; using namespace std;
@ -60,6 +63,7 @@ vector<wstring> EnumPortsWdm()
NewMonitorForm::NewMonitorForm(ui::Window* parent) NewMonitorForm::NewMonitorForm(ui::Window* parent)
{ {
m_parent = parent; m_parent = parent;
m_tcp_client = nullptr;
} }
NewMonitorForm::~NewMonitorForm() NewMonitorForm::~NewMonitorForm()
@ -128,6 +132,23 @@ void NewMonitorForm::InitWindow()
} }
} }
if (m_combo_type->GetText() == L"tcp client") { if (m_combo_type->GetText() == L"tcp client") {
wprintf(L"%s\r\n",m_ip_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());
m_tcp_client = new TcpClientLibevent(wstring2string(m_ip_select->GetText()), port,nullptr);
if (m_tcp_client->Connected()) {
printf("Á¬½Ó³É¹¦\r\n");
TcpClientInfo* p = new TcpClientInfo;
p->ip = m_port_select->GetText();
p->port = port;
p->socket_fd = m_tcp_client->SocketFd();
auto succ = ::PostMessage(m_parent->GetHWND(),
WM_ADD_TCPCLIENT_MONITOR, (WPARAM)p, (LPARAM)m_tcp_client);
if (!succ) {
printf("postmessage error :%d\r\n", GetLastError());
}
}
} }
if (m_combo_type->GetText() == L"tcp server") { if (m_combo_type->GetText() == L"tcp server") {

View File

@ -9,9 +9,9 @@
#include "base/base.h" #include "base/base.h"
// duilib
#include "duilib/UIlib.h" #include "duilib/UIlib.h"
#include "ui_components/ui_components.h" #include "ui_components/ui_components.h"
#include "tcp_client.h"
class NewMonitorForm : public ui::WindowImplBase class NewMonitorForm : public ui::WindowImplBase
@ -52,6 +52,6 @@ private:
ui::Combo* m_combo_type; ui::Combo* m_combo_type;
ui::Window* m_parent; ui::Window* m_parent;
TcpClientLibevent* m_tcp_client;
}; };

View File

@ -81,7 +81,7 @@
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental> <LinkIncremental>true</LinkIncremental>
<IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);..\..\;$(ProjectDir)third\include\libevent\include</IncludePath> <IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);..\..\;$(ProjectDir)third\include\libevent\include;$(ProjectDir)</IncludePath>
<ReferencePath>$(ReferencePath)</ReferencePath> <ReferencePath>$(ReferencePath)</ReferencePath>
<LibraryPath>$(VC_LibraryPath_x64);$(WindowsSDK_LibraryPath_x64);$(ProjectDir)third\lib</LibraryPath> <LibraryPath>$(VC_LibraryPath_x64);$(WindowsSDK_LibraryPath_x64);$(ProjectDir)third\lib</LibraryPath>
</PropertyGroup> </PropertyGroup>
@ -167,7 +167,9 @@
<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_client.cpp" /> <ClCompile Include="tcp_client.cpp" />
<ClCompile Include="tcp_client_form.cpp" />
<ClCompile Include="uart_process.cpp" /> <ClCompile Include="uart_process.cpp" />
<ClCompile Include="utils.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="base_form.h" /> <ClInclude Include="base_form.h" />
@ -175,13 +177,16 @@
<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_client_form.h" />
<ClInclude Include="uart_process.h" /> <ClInclude Include="uart_process.h" />
<ClInclude Include="utils.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Xml Include="..\Debug\resources\themes\default\basic\basic.xml" /> <Xml Include="..\Debug\resources\themes\default\basic\basic.xml" />
<Xml Include="..\Debug\resources\themes\default\basic\newmonitor.xml" /> <Xml Include="..\Debug\resources\themes\default\basic\newmonitor.xml" />
<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" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ResourceCompile Include="proto_debuger.rc" /> <ResourceCompile Include="proto_debuger.rc" />

View File

@ -30,6 +30,15 @@
<ClCompile Include="serial_port.cpp"> <ClCompile Include="serial_port.cpp">
<Filter>源文件</Filter> <Filter>源文件</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="tcp_client.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="utils.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="tcp_client_form.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="main.h"> <ClInclude Include="main.h">
@ -50,6 +59,12 @@
<ClInclude Include="serial_port.h"> <ClInclude Include="serial_port.h">
<Filter>头文件</Filter> <Filter>头文件</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="utils.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="tcp_client_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">
@ -64,6 +79,9 @@
<Xml Include="..\Debug\resources\themes\default\basic\uart_form.xml"> <Xml Include="..\Debug\resources\themes\default\basic\uart_form.xml">
<Filter>资源文件</Filter> <Filter>资源文件</Filter>
</Xml> </Xml>
<Xml Include="..\x64\Debug\resources\themes\default\basic\tcp_form.xml">
<Filter>资源文件</Filter>
</Xml>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ResourceCompile Include="proto_debuger.rc"> <ResourceCompile Include="proto_debuger.rc">

View File

@ -17,6 +17,13 @@ typedef struct{
uint8_t flow_control; uint8_t flow_control;
}UartInfo; }UartInfo;
typedef struct {
wstring ip;
uint32_t port;
uint32_t socket_fd;
}TcpClientInfo;
class SerialPort class SerialPort
{ {

View File

@ -15,8 +15,6 @@ static void conn_eventcb(struct bufferevent *, short, void *);
void delay(int ms); void delay(int ms);
int ThreadRun(TcpClientLibevent *p); int ThreadRun(TcpClientLibevent *p);
void conn_writecb(struct bufferevent *bev, void *user_data) void conn_writecb(struct bufferevent *bev, void *user_data)
{ {
@ -25,7 +23,6 @@ void conn_writecb(struct bufferevent *bev, void *user_data)
// 运行线程 // 运行线程
int ThreadRun(TcpClientLibevent *p) { int ThreadRun(TcpClientLibevent *p) {
if (nullptr != p) { if (nullptr != p) {
p->mStatus = TcpClientLibevent::UNCONNECTED;
int ret = p->Dispatch(); int ret = p->Dispatch();
if (0 > ret){ if (0 > ret){
} }
@ -67,6 +64,9 @@ void conn_eventcb(struct bufferevent *bev, short events, void *user_data)
{ {
TcpClientLibevent *p; TcpClientLibevent *p;
p = (TcpClientLibevent *)user_data; p = (TcpClientLibevent *)user_data;
if (p == nullptr) {
return;
}
if (events & BEV_EVENT_EOF) { if (events & BEV_EVENT_EOF) {
if (nullptr != p->mObserver) if (nullptr != p->mObserver)
p->mObserver->OnDisConnected(); p->mObserver->OnDisConnected();
@ -79,13 +79,13 @@ void conn_eventcb(struct bufferevent *bev, short events, void *user_data)
if (nullptr != p->mObserver) if (nullptr != p->mObserver)
p->mObserver->OnDisConnected(); p->mObserver->OnDisConnected();
p->mStatus = TcpClientLibevent::FAIL; p->mStatus = TcpClientLibevent::FAIL;
} }
else if (events & BEV_EVENT_CONNECTED) { else if (events & BEV_EVENT_CONNECTED) {
printf("Connect succeed\n"); p->mSocketFD = (uint64_t)event_get_fd(&(bev->ev_read));
printf("Connect succeed %d %d \n", p->mSocketFD, event_get_fd(&(bev->ev_write)));
//客户端链接成功后,给服务器发送第一条消息 //客户端链接成功后,给服务器发送第一条消息
if (nullptr != p->mObserver) if (nullptr != p->mObserver)
p->mObserver->OnConnected(); p->mObserver->OnConnected();
if (p != nullptr)
p->mStatus = TcpClientLibevent::UNCONNECTED; p->mStatus = TcpClientLibevent::UNCONNECTED;
return; return;
} }
@ -116,7 +116,6 @@ TcpClientLibevent::TcpClientLibevent(std::string addrinfo, int port, TcpClientLi
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)
{ {
@ -131,10 +130,12 @@ TcpClientLibevent::TcpClientLibevent(std::string addrinfo, int port, TcpClientLi
ConnectServer(); ConnectServer();
this->mThread = new thread(ThreadRun,this); this->mThread = new thread(ThreadRun,this);
this->mObserver = p; this->mObserver = p;
mByteRecv = 0;
mByteSend = 0;
} }
int TcpClientLibevent::ConnectServer() { int TcpClientLibevent::ConnectServer() {
printf("connect server\r\n"); printf("server conecting...\r\n");
evthread_make_base_notifiable(mBase); evthread_make_base_notifiable(mBase);
bev = bufferevent_socket_new(mBase, -1, bev = bufferevent_socket_new(mBase, -1,
BEV_OPT_CLOSE_ON_FREE | BEV_OPT_THREADSAFE); BEV_OPT_CLOSE_ON_FREE | BEV_OPT_THREADSAFE);
@ -174,3 +175,8 @@ int TcpClientLibevent::Close() {
event_base_free(mBase); event_base_free(mBase);
return 0; return 0;
} }
uint64_t TcpClientLibevent::SocketFd()
{
return mSocketFD;
}

View File

@ -1,13 +1,13 @@
// //
// Created by 29019 on 2020/4/18. // Created by 29019 on 2020/4/18.
// //
#pragma once
#ifndef GENERAL_TCPCLIENT_H
#define GENERAL_TCPCLIENT_H
#ifndef _WIN32_WINNT #ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0500 #define _WIN32_WINNT 0x0500
#endif #endif
#ifdef linux #ifdef linux
#include<sys/types.h> #include<sys/types.h>
#include<sys/socket.h> #include<sys/socket.h>
@ -17,15 +17,21 @@
extern "C"{ extern "C"{
#include "event2/bufferevent.h" #include "event2/bufferevent.h"
#include "event2/bufferevent_struct.h"
#include "event2/buffer.h" #include "event2/buffer.h"
#include "event2/listener.h" #include "event2/listener.h"
#include "event2/util.h" #include "event2/util.h"
#include "event2/event.h" #include "event2/event.h"
#include "event2/thread.h" #include "event2/thread.h"
/* For int types. */
#include <event2/util.h>
/* For struct event */
#include <event2/event_struct.h>
}; };
#include<string.h> #include<string.h>
#include <iostream> #include <iostream>
// #include "PackageReceiver.h"
#include <mutex> #include <mutex>
#include <thread> #include <thread>
using namespace std; using namespace std;
@ -33,40 +39,50 @@ using namespace std;
class TcpClientLibevent { class TcpClientLibevent {
public: public:
typedef enum { typedef enum {
UNCONNECTED, // 未连接 UNCONNECTED, // 未连接
CONNECTED, //已经连接 CONNECTED, //已经连接
FAIL, // 连接失败 FAIL, // 连接失败
}Status; }Status;
class TcpClientObserver{
class TcpClientObserver {
public: public:
virtual ~TcpClientObserver(){return;} virtual ~TcpClientObserver() { return; }
mutex mMux; mutex mMux;
virtual void OnConnected() { return; }; virtual void OnConnected() { return; };
virtual void OnDisConnected() { return; }; virtual void OnDisConnected() { return; };
virtual void OnData(uint8_t *dat,uint64_t len){return;}; virtual void OnData(uint8_t* dat, uint64_t len) { return; };
virtual void OnClose(){return;}; virtual void OnClose() { return; };
}; };
TcpClientLibevent(std::string addrinfo,int port, TcpClientObserver *p); TcpClientLibevent(std::string addrinfo, int port, TcpClientObserver* p);
~TcpClientLibevent(){ ~TcpClientLibevent() {
event_base_free(mBase); event_base_free(mBase);
}; };
friend void conn_eventcb(struct bufferevent*, short, void*);
int ConnectServer(); int ConnectServer();
bool Connected(); bool Connected();
int Dispatch(); int Dispatch();
int OnTCPPackage(uint8_t *, uint16_t); int OnTCPPackage(uint8_t*, uint16_t);
int SetReconnect(bool); int SetReconnect(bool);
int SetObserver(TcpClientObserver*); int SetObserver(TcpClientObserver*);
int Close(); int Close();
uint64_t SocketFd();
Status mStatus; Status mStatus;
TcpClientObserver *mObserver; TcpClientObserver* mObserver;
private: private:
bool mReConnect = false; bool mReConnect = false;
int sendData(void*,size_t); int sendData(void*, size_t);
struct event_base *mBase; struct event_base* mBase;
struct bufferevent* bev; struct bufferevent* bev;
struct sockaddr_in mSrv; struct sockaddr_in mSrv;
std::thread *mThread; std::thread* mThread;
mutex mLock; mutex mLock; // 互斥锁
uint64_t mByteSend; // 发送字节数
uint64_t mByteRecv; // 接收字节数
evutil_socket_t mSocketFD; // 操作系统原生socket
}; };
#endif //GENERAL_TCPCLIENT_H

View File

@ -0,0 +1,24 @@
#include "tcp_client_form.h"
TcpClientForm::TcpClientForm(std::string url, uint32_t port, TcpClientLibevent* p)
{
mClient = p;
}
void TcpClientForm::Init()
{
ui::Label* m_label_1 = dynamic_cast<ui::Label*>(FindSubControl(L"uart_info_label"));
ui::RichEdit* m_rich_edit_1 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"uart_recv_eidt"));
ui::RichEdit* m_rich_edit_2 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"lua_script"));
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::CheckBox* m_check_box_1 = dynamic_cast<ui::CheckBox*>(FindSubControl(L"check_new_line"));
ui::CheckBox* m_check_box_2 = dynamic_cast<ui::CheckBox*>(FindSubControl(L"check_time_send"));
ui::CheckBox* m_check_box_3 = dynamic_cast<ui::CheckBox*>(FindSubControl(L"check_hex_send"));
ui::CheckBox* m_check_box_4 = dynamic_cast<ui::CheckBox*>(FindSubControl(L"check_hex_recv"));
}
void TcpClientForm::HandleMessage(ui::EventArgs& msg)
{
}

View File

@ -0,0 +1,38 @@
#include <string>
// C runtime header
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
// base header
#include "base/base.h"
#include "serial_port.h"
// duilib
#include "duilib/UIlib.h"
#include "tcp_client.h"
using namespace std;
class TcpClientForm : public ui::ChildBox {
#pragma once
public:
TcpClientForm(string url, uint32_t port, TcpClientLibevent* p);
virtual void Init() override;
private:
TcpClientLibevent* mClient;
ui::Label* m_label_1;
ui::RichEdit* m_rich_edit_1;
ui::RichEdit* m_rich_edit_2;
ui::RichEdit* m_rich_edit_3;
ui::Button* m_button_1;
ui::CheckBox* m_check_box_1;
ui::CheckBox* m_check_box_2;
ui::CheckBox* m_check_box_3;
ui::CheckBox* m_check_box_4;
virtual void HandleMessage(ui::EventArgs& msg);
};

View File

@ -1,34 +1,5 @@
#include "uart_process.h" #include "uart_process.h"
#include "utils.h"
wstring string2wstring(string str)
{
wstring result;
//获取缓冲区大小,并申请空间,缓冲区大小按字符计算
int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), NULL, 0);
TCHAR* buffer = new TCHAR[len + 1];
//多字节编码转换成宽字节编码
MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), buffer, len);
buffer[len] = '\0'; //添加字符串结尾
//删除缓冲区并返回值
result.append(buffer);
delete[] buffer;
return result;
}
std::string wstring2string(std::wstring wstr)
{
std::string result;
//获取缓冲区大小,并申请空间,缓冲区大小事按字节计算的
int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);
char* buffer = new char[len + 1];
//宽字节编码转换成多字节编码
WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL);
buffer[len] = '\0';
//删除缓冲区并返回值
result.append(buffer);
delete[] buffer;
return result;
}
UartForm::UartForm(ui::Window* hwnd,std::wstring name, UartForm::UartForm(ui::Window* hwnd,std::wstring name,
uint32_t baurate, uint32_t baurate,

View File

@ -0,0 +1,32 @@
#include "utils.h"
#include<Windows.h>
std::wstring string2wstring(std::string str)
{
std::wstring result;
//获取缓冲区大小,并申请空间,缓冲区大小按字符计算
int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), NULL, 0);
TCHAR* buffer = new TCHAR[len + 1];
//多字节编码转换成宽字节编码
MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), buffer, len);
buffer[len] = '\0'; //添加字符串结尾
//删除缓冲区并返回值
result.append(buffer);
delete[] buffer;
return result;
}
std::string wstring2string(std::wstring wstr)
{
std::string result;
//获取缓冲区大小,并申请空间,缓冲区大小事按字节计算的
int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);
char* buffer = new char[len + 1];
//宽字节编码转换成多字节编码
WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL);
buffer[len] = '\0';
//删除缓冲区并返回值
result.append(buffer);
delete[] buffer;
return result;
}

View File

@ -0,0 +1,9 @@
#pragma once
#include <tchar.h>
#include <iostream>
std::wstring string2wstring(std::string str);
std::string wstring2string(std::wstring wstr);