资源简介
自己封装的etcd c++ client, 有线上项目使用先例。接口参考头文件,附带测试代码,有需要的话可以随意修改,写的不合适的代码还请见谅。
代码片段和文件信息
#include “EtcdClientV3.h“
#include
#include
int ECSTDCALL CreateECClient(IEtcdClient** client PECParam param)
{
int ret = EC_SUCCESS;
do
{
CEtcdClient* pClient = new(std::nothrow) CEtcdClient();
if (pClient == NULL)
{
ret = EC_SYSTEM_ERROR;
break;
}
if (!pClient->Init(param))
{
delete pClient;
ret = EC_SYSTEM_ERROR;
break;
}
*client = pClient;
} while (false);
return ret;
}
void ECSTDCALL ReleaseClient(IEtcdClient* client)
{
CEtcdClient* pClient = static_cast(client);
pClient->UnInit();
}
CEtcdClient::CEtcdClient()
{
m_bInit = false;
m_bStop = false;
m_bAlreadyWatch = false;
m_kvClient = NULL;
m_watchClient = NULL;
m_leaseClient = NULL;
m_leaseID = 0;
m_pThread = NULL;
m_ttl = 60;
m_nCurrentHostIndex = 0;
}
CEtcdClient::~CEtcdClient()
{
UnInit();
}
bool CEtcdClient::SplitHostInfo(std::string hostAddr)
{
size_t pos = hostAddr.find_first_of(‘;‘);
while (hostAddr.length() > 0)
{
std::string str = hostAddr.substr(0 pos);
m_hostArray.push_back(str);
if (pos == std::string::npos)
break;
hostAddr = hostAddr.substr(pos + 1);
pos = hostAddr.find_first_of(‘;‘);
}
return m_hostArray.size() > 0;
}
std::string CEtcdClient::GetHost()
{
if (m_hostArray.size() > 0)
{
int nIndex = m_nCurrentHostIndex;
m_nCurrentHostIndex = (m_nCurrentHostIndex + 1) % m_hostArray.size();
return m_hostArray[nIndex];
}
return ““;
}
bool CEtcdClient::Init(PECParam param)
{
if (m_bInit)
return true;
do
{
m_logManager.CreateLog();
param->server_addr[sizeof(param->server_addr) - 1] = ‘\0‘;
if (!SplitHostInfo(param->server_addr))
{
LOG(“info“ LOG_LEVEL_ERROR “split addr failed addr: %s“ param->server_addr);
break;
}
m_timer.SetTimer(3 * 1000);
/*m_ttl = param->ttl;
if (m_ttl > 180 || m_ttl == 0)
m_ttl = 180;*/
int loop_count = 0;
while (loop_count++ < m_hostArray.size())
{
string host = GetHost();
m_channel = grpc::CreateChannel(host grpc::InsecureChannelCredentials());
std::chrono::system_clock::time_point deadline =
std::chrono::system_clock::now() + std::chrono::seconds(3);
if (m_channel->WaitForConnected(deadline))
{
LOG(“info“ LOG_LEVEL_INFO “connect to host: %s successful“ host.c_str());
break;
}
else
{
m_channel.reset();
LOG(“info“ LOG_LEVEL_ERROR “connect to host: %s failed“ host.c_str());
}
}
m_kvClient = new(std::nothrow) KVClient(m_channel);
if (m_kvClient == NULL)
break;
m_watchClient = new(std::nothrow) WatchClient(m_channel this);
if (m_watchClient == NULL)
break;
m_leaseClient = new(std::nothrow) LeaseClient(m_channel);
if (m_leaseClient == NULL)
break;
//m_leaseID = m_leaseClient->GetLeaseID(m_ttl);
//if (m_leaseID <= 0)
// break;
m_pThread = new(std::
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-01-29 10:35 EtcdClientV3\
文件 7206 2017-08-28 20:30 EtcdClientV3\EtcdClientV3.cpp
文件 1716 2017-08-15 10:03 EtcdClientV3\EtcdClientV3.h
文件 10096 2017-07-06 12:13 EtcdClientV3\EtcdClientV3.vcxproj
文件 3389 2017-07-03 14:41 EtcdClientV3\EtcdClientV3.vcxproj.filters
文件 4940 2017-07-03 15:03 EtcdClientV3\LogManager.cpp
文件 1859 2017-07-03 14:27 EtcdClientV3\LogManager.h
文件 1711 2017-07-06 10:48 EtcdClientV3\Makefile
文件 12899 2017-08-15 11:02 EtcdClientV3\RpcClient.cpp
文件 2464 2017-08-14 19:06 EtcdClientV3\RpcClient.h
文件 2026 2017-07-03 13:28 EtcdClientV3\util.h
文件 2097 2017-07-03 09:46 EtcdClientV3.sln
目录 0 2018-01-29 10:35 Release\
文件 1741312 2017-08-28 20:32 Release\EtcdClientV3.dll
文件 855 2017-08-28 20:31 Release\EtcdClientV3.exp
文件 15519785 2017-08-28 20:32 Release\EtcdClientV3.iobj
文件 5691696 2017-08-28 20:32 Release\EtcdClientV3.ipdb
文件 2088 2017-08-28 20:31 Release\EtcdClientV3.lib
文件 15749120 2017-08-28 20:32 Release\EtcdClientV3.pdb
文件 1262080 2017-06-12 11:05 Release\libeay32.dll
文件 294400 2017-06-12 11:05 Release\ssleay32.dll
文件 391680 2017-06-12 16:40 Release\zlib.dll
目录 0 2018-01-29 10:35 TestEtcdClientV3\
文件 7821 2017-07-03 09:52 TestEtcdClientV3\TestEtcdClientV3.vcxproj
文件 957 2017-07-03 09:46 TestEtcdClientV3\TestEtcdClientV3.vcxproj.filters
文件 5070 2017-12-19 11:11 TestEtcdClientV3\TestEtctClientV3.cpp
文件 14 2015-09-20 17:20 TestEtcdClientV3\test.h
目录 0 2018-01-29 10:35 debug\
文件 8614400 2017-08-28 20:31 debug\EtcdClientV3_d.dll
文件 857 2017-08-28 20:31 debug\EtcdClientV3_d.exp
文件 20595428 2017-08-28 20:31 debug\EtcdClientV3_d.ilk
............此处省略392个文件信息
- 上一篇:MFC学生成绩管理系统功能全面版
- 下一篇:高质量嵌入式Linux-C编程
相关资源
- 《基于LINUX的C++》教程课件,有详细的
- c++test软件测试工具附带破解文件
- Visual C++开发实战1200例(第2卷).(配
- 数据结构与算法分析(C++语言描述)
- 深度探索C++对象模型PDF 2012 中文 高清
- 计算机图形学MFC-双缓冲二维图形几何
- C++ Primer 第5版(中文) 超清晰,彩色
- C++高级编程 第3版 PDF电子书 带书签目
- C++高级编程第3版_带书签_高清完整版
- Visual C++网络编程郑阿奇源代码+PPT
- 基于QT的打地鼠游戏
- C++ QT 俄罗斯方块源码
- 数据结构(C语言版)
- vc++6.0下载199209
- CEF 下C++与JS交互
- C++primer plus_第六版_高清_中文版
- C++ winsock2编写的demo包含服务器和客户
- C++播放音频198655
- VC++MFC封装的圆形按钮类
- C++程序设计谭浩强.pdf
- tensorflow180 C++ API CPU
- C++ Templates The Complete Guide (2nd Editio
- MFC编程自己的简单记事本教学视频和
- codeblocks-16.01mingw-setup.exe
- Devc++5.11
- C++ Primer 第5版 中文完整高清版
- C++Primer第五版 课后习题集完整版
- 数字图像处理与机器视觉 Visual C++与
- Visual C++音频视频处理技术及工程实践
- Linux多线程服务端编程 使用muduo C++网
评论
共有 条评论