no message

This commit is contained in:
zcy 2021-10-12 00:51:48 +08:00
parent d163878aef
commit a6ce4363ec
6 changed files with 69 additions and 22 deletions

View File

@ -137,12 +137,13 @@ void NewMonitorForm::InitWindow()
int port = atoi(wstring2string(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); m_tcp_client = new TcpClientLibevent(wstring2string(m_ip_select->GetText()), port,nullptr);
if (m_tcp_client->Connected()) { if (m_tcp_client->Connected()) {
printf("连接成功\r\n");
TcpClientInfo* p = new TcpClientInfo; TcpClientInfo* p = new TcpClientInfo;
p->ip = m_port_select->GetText(); p->ip = m_port_select->GetText();
p->port = port; p->port = port;
p->socket_fd = m_tcp_client->SocketFd(); p->socket_fd = m_tcp_client->SocketFd();
printf("连接成功 %d \r\n", p->socket_fd);
auto succ = ::PostMessage(m_parent->GetHWND(), auto succ = ::PostMessage(m_parent->GetHWND(),
WM_ADD_TCPCLIENT_MONITOR, (WPARAM)p, (LPARAM)m_tcp_client); WM_ADD_TCPCLIENT_MONITOR, (WPARAM)p, (LPARAM)m_tcp_client);
if (!succ) { if (!succ) {

View File

@ -7,6 +7,9 @@
#include <stdio.h> #include <stdio.h>
#include <cstring> #include <cstring>
#include <string.h> #include <string.h>
#include <chrono>
using namespace std::chrono;
static void conn_writecb(struct bufferevent *, void *); static void conn_writecb(struct bufferevent *, void *);
static void conn_readcb(struct bufferevent *, void *); static void conn_readcb(struct bufferevent *, void *);
@ -26,9 +29,10 @@ int ThreadRun(TcpClientLibevent *p) {
int ret = p->Dispatch(); int ret = p->Dispatch();
if (0 > ret){ if (0 > ret){
} }
while ((p->mStatus != TcpClientLibevent::UNCONNECTED )) while ((p->mStatus != TcpClientLibevent::STOP))
{ {
if (p->mStatus == TcpClientLibevent::FAIL) { //连接失败,如果有设置自动重连就一直重连 if ((p->mStatus == TcpClientLibevent::FAIL) ||
(p->mStatus == TcpClientLibevent::UNCONNECTED)){ //连接失败,如果有设置自动重连就一直重连
p->ConnectServer(); p->ConnectServer();
#ifdef _WIN32 #ifdef _WIN32
Sleep(100); Sleep(100);
@ -39,7 +43,6 @@ int ThreadRun(TcpClientLibevent *p) {
ret = p->Dispatch(); ret = p->Dispatch();
} }
} }
p->mStatus = TcpClientLibevent::UNCONNECTED;
return 0; return 0;
} }
@ -82,11 +85,10 @@ void conn_eventcb(struct bufferevent *bev, short events, void *user_data)
} }
else if (events & BEV_EVENT_CONNECTED) { else if (events & BEV_EVENT_CONNECTED) {
p->mSocketFD = (uint64_t)event_get_fd(&(bev->ev_read)); 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();
p->mStatus = TcpClientLibevent::UNCONNECTED; p->mStatus = TcpClientLibevent::CONNECTED;
return; return;
} }
bufferevent_free(bev); bufferevent_free(bev);
@ -103,8 +105,8 @@ bool TcpClientLibevent::Connected() {
} }
TcpClientLibevent::TcpClientLibevent(std::string addrinfo, int port, TcpClientLibevent::TcpClientObserver *p) : TcpClientLibevent::TcpClientLibevent(std::string addrinfo, int port, TcpClientLibevent::TcpClientObserver *p) :
mStatus(UNCONNECTED), mStatus(UNCONNECTED),
mObserver(nullptr) mObserver(nullptr)
{ {
memset(&mSrv, 0, sizeof(mSrv)); memset(&mSrv, 0, sizeof(mSrv));
#ifdef linux #ifdef linux
@ -127,33 +129,71 @@ TcpClientLibevent::TcpClientLibevent(std::string addrinfo, int port, TcpClientLi
#else #else
evthread_use_pthreads(); evthread_use_pthreads();
#endif #endif
ConnectServer();
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;
ConnectServerSync();
} }
int TcpClientLibevent::ConnectServer() { int TcpClientLibevent::ConnectServer() {
printf("server conecting...\r\n"); printf("server conecting...\r\n");
evthread_make_base_notifiable(mBase); evthread_make_base_notifiable(mBase);
bev = bufferevent_socket_new(mBase, -1, mBev = bufferevent_socket_new(mBase, -1,
BEV_OPT_CLOSE_ON_FREE | BEV_OPT_THREADSAFE); BEV_OPT_CLOSE_ON_FREE | BEV_OPT_THREADSAFE);
if (nullptr == bev) { if (nullptr == mBev) {
this->mStatus = TcpClientLibevent::FAIL; this->mStatus = TcpClientLibevent::FAIL;
return - 1; return - 1;
} }
bufferevent_setcb(bev, conn_readcb, conn_writecb, conn_eventcb, this); bufferevent_setcb(mBev, conn_readcb, conn_writecb, conn_eventcb, this);
int flag = bufferevent_socket_connect(bev, (struct sockaddr *)&mSrv, sizeof(mSrv)); int flag = bufferevent_socket_connect(mBev, (struct sockaddr *)&mSrv, sizeof(mSrv));
bufferevent_enable(bev, EV_READ | EV_WRITE); bufferevent_enable(mBev, EV_READ | EV_WRITE);
if (-1 == flag) { if (-1 == flag) {
this->mStatus = TcpClientLibevent::FAIL; this->mStatus = TcpClientLibevent::FAIL;
bufferevent_free(bev); bufferevent_free(mBev);
bev = nullptr; mBev = nullptr;
printf("Connect failed\n"); printf("Connect failed\n");
return -1; return -1;
} }
this->mStatus = TcpClientLibevent::CONNECTED; this->mStatus = TcpClientLibevent::CONNECTING;
return 0;
}
int TcpClientLibevent::ConnectServerSync()
{
evthread_make_base_notifiable(mBase);
if (nullptr != mBev)
{
delete mBev;
}
mBev = bufferevent_socket_new(mBase, -1,
BEV_OPT_CLOSE_ON_FREE | BEV_OPT_THREADSAFE);
if (nullptr == mBev) {
this->mStatus = TcpClientLibevent::FAIL;
return -1;
}
bufferevent_setcb(mBev, conn_readcb, conn_writecb, conn_eventcb, this);
int flag = bufferevent_socket_connect(mBev, (struct sockaddr*)&mSrv, sizeof(mSrv));
bufferevent_enable(mBev, EV_READ | EV_WRITE);
if (-1 == flag) {
this->mStatus = TcpClientLibevent::FAIL;
bufferevent_free(mBev);
mBev = nullptr;
printf("Connect failed\n");
return -1;
}
this->mStatus = TcpClientLibevent::CONNECTING;
auto start = system_clock::to_time_t(system_clock::now());
while (this->mStatus != TcpClientLibevent::CONNECTED) {
auto end = system_clock::to_time_t(system_clock::now());
if ((end - start) > 2) {
this->mStatus = TcpClientLibevent::FAIL;
break;
}
}
return 0; return 0;
} }

View File

@ -40,8 +40,10 @@ class TcpClientLibevent {
public: public:
typedef enum { typedef enum {
UNCONNECTED, // 未连接 UNCONNECTED, // 未连接
CONNECTING, //已经连接
CONNECTED, //已经连接 CONNECTED, //已经连接
FAIL, // 连接失败 FAIL, // 连接失败
STOP, // 初始状态
}Status; }Status;
class TcpClientObserver { class TcpClientObserver {
@ -61,12 +63,15 @@ public:
friend void conn_eventcb(struct bufferevent*, short, void*); friend void conn_eventcb(struct bufferevent*, short, void*);
int ConnectServer(); int ConnectServer();
int ConnectServerSync();
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(); uint64_t SocketFd();
Status mStatus; Status mStatus;
@ -75,7 +80,7 @@ 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* mBev;
struct sockaddr_in mSrv; struct sockaddr_in mSrv;
std::thread* mThread; std::thread* mThread;
mutex mLock; // 互斥锁 mutex mLock; // 互斥锁

View File

@ -24,6 +24,7 @@ void TcpClientForm::Init()
} }
void TcpClientForm::HandleMessage(ui::EventArgs& msg) void TcpClientForm::HandleMessage(ui::EventArgs& msg)
{ {
} }

View File

@ -36,7 +36,7 @@
<ConfigurationType>StaticLibrary</ConfigurationType> <ConfigurationType>StaticLibrary</ConfigurationType>
<UseOfMfc>false</UseOfMfc> <UseOfMfc>false</UseOfMfc>
<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>StaticLibrary</ConfigurationType> <ConfigurationType>StaticLibrary</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">
@ -242,8 +242,8 @@
<ClCompile Include="windows_manager\window_ex.cpp" /> <ClCompile Include="windows_manager\window_ex.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\third_party\cef_wrapper\libcef_dll_wrapper.vcxproj"> <ProjectReference Include="..\duilib\duilib.vcxproj">
<Project>{a9d6dc71-c0dc-4549-aea0-3b15b44e86a9}</Project> <Project>{e106acd7-4e53-4aee-942b-d0dd426db34e}</Project>
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />