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());
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();
printf("连接成功 %d \r\n", p->socket_fd);
auto succ = ::PostMessage(m_parent->GetHWND(),
WM_ADD_TCPCLIENT_MONITOR, (WPARAM)p, (LPARAM)m_tcp_client);
if (!succ) {

View File

@ -7,6 +7,9 @@
#include <stdio.h>
#include <cstring>
#include <string.h>
#include <chrono>
using namespace std::chrono;
static void conn_writecb(struct bufferevent *, void *);
static void conn_readcb(struct bufferevent *, void *);
@ -26,9 +29,10 @@ int ThreadRun(TcpClientLibevent *p) {
int ret = p->Dispatch();
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();
#ifdef _WIN32
Sleep(100);
@ -39,7 +43,6 @@ int ThreadRun(TcpClientLibevent *p) {
ret = p->Dispatch();
}
}
p->mStatus = TcpClientLibevent::UNCONNECTED;
return 0;
}
@ -82,11 +85,10 @@ void conn_eventcb(struct bufferevent *bev, short events, void *user_data)
}
else if (events & BEV_EVENT_CONNECTED) {
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)
p->mObserver->OnConnected();
p->mStatus = TcpClientLibevent::UNCONNECTED;
p->mStatus = TcpClientLibevent::CONNECTED;
return;
}
bufferevent_free(bev);
@ -103,8 +105,8 @@ bool TcpClientLibevent::Connected() {
}
TcpClientLibevent::TcpClientLibevent(std::string addrinfo, int port, TcpClientLibevent::TcpClientObserver *p) :
mStatus(UNCONNECTED),
mObserver(nullptr)
mStatus(UNCONNECTED),
mObserver(nullptr)
{
memset(&mSrv, 0, sizeof(mSrv));
#ifdef linux
@ -127,33 +129,71 @@ TcpClientLibevent::TcpClientLibevent(std::string addrinfo, int port, TcpClientLi
#else
evthread_use_pthreads();
#endif
ConnectServer();
this->mThread = new thread(ThreadRun,this);
this->mObserver = p;
mByteRecv = 0;
mByteSend = 0;
ConnectServerSync();
}
int TcpClientLibevent::ConnectServer() {
printf("server conecting...\r\n");
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);
if (nullptr == bev) {
if (nullptr == mBev) {
this->mStatus = TcpClientLibevent::FAIL;
return - 1;
}
bufferevent_setcb(bev, conn_readcb, conn_writecb, conn_eventcb, this);
int flag = bufferevent_socket_connect(bev, (struct sockaddr *)&mSrv, sizeof(mSrv));
bufferevent_enable(bev, EV_READ | EV_WRITE);
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(bev);
bev = nullptr;
bufferevent_free(mBev);
mBev = nullptr;
printf("Connect failed\n");
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;
}

View File

@ -40,8 +40,10 @@ class TcpClientLibevent {
public:
typedef enum {
UNCONNECTED, // 未连接
CONNECTING, //已经连接
CONNECTED, //已经连接
FAIL, // 连接失败
STOP, // 初始状态
}Status;
class TcpClientObserver {
@ -61,12 +63,15 @@ public:
friend void conn_eventcb(struct bufferevent*, short, void*);
int ConnectServer();
int ConnectServerSync();
bool Connected();
int Dispatch();
int OnTCPPackage(uint8_t*, uint16_t);
int SetReconnect(bool);
int SetObserver(TcpClientObserver*);
int Close();
uint64_t SocketFd();
Status mStatus;
@ -75,7 +80,7 @@ private:
bool mReConnect = false;
int sendData(void*, size_t);
struct event_base* mBase;
struct bufferevent* bev;
struct bufferevent* mBev;
struct sockaddr_in mSrv;
std::thread* mThread;
mutex mLock; // 互斥锁

View File

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

View File

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

View File

@ -42,7 +42,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v120_xp</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
@ -242,8 +242,8 @@
<ClCompile Include="windows_manager\window_ex.cpp" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\third_party\cef_wrapper\libcef_dll_wrapper.vcxproj">
<Project>{a9d6dc71-c0dc-4549-aea0-3b15b44e86a9}</Project>
<ProjectReference Include="..\duilib\duilib.vcxproj">
<Project>{e106acd7-4e53-4aee-942b-d0dd426db34e}</Project>
</ProjectReference>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />