DataCommlnt.h
// 本文件用于上位机下位机通信定义。
#pragma once
#include "ZMQSender.h"
#include "ZMQReceiver.h"
#include "ThriftRequestClient.h"
#include "ThriftResponseClient.h"
#include "ThriftServer.h"
#include "ThriftStructCodec.h"
class CDataCommInt
{
public:
// 析构函数
~CDataCommInt();
public:
static CDataCommInt * GetInstance();
public:
// 初始化
int iInitialize();
// 发送告警信息
int iSendWarrning(const MsgRequest& msgRequire);
// 发送心跳消息
int iBeatHeart(const MsgRequest& msgRequire);
// 发送状态信息
int iSendStatus(const MsgRequest& msgRequire);
// 响应消息
int iResponseMsg(const MsgResponse& msgReponse);
// 发送请求消息
int iSendRequireMsg(ReturnMsg & returnMsg, const MsgRequest & msgRequest);
// 发送亮度数据
int iSendLumData(unsigned char * ucData, int iDataLen);
// 取接收数据
int iFetchRecvData(string & sRecvData);
// 判断是否有接收数据
bool bHadRecvData();
// 等待线程退出
void wait();
// 退出线程
void ExitThread();
// 编码
template<class T>
int iEncode(string &sBinary, const T &tructInfo)
{
return m_structCodec.iEncode(sBinary, tructInfo);
}
// 解码
template<class T>
int iDecode(T & tructInfo, const string & sBinary)
{
return m_structCodec.iDecode(tructInfo, sBinary);
}
private:
// ZMQ发送器
CZMQSender *m_pZMQSender;
// ZMQ接收器
CZMQReceiver * m_pZMQReceiver;
// Thrfit发送消息客户端
CThriftRequestClient * m_pRequestClient;
// Thrift接收消息客户端
CThriftResponseClient * m_pResponseClient;
// Thrift请求消息服务端
CThriftServer * m_pRequestServer;
// Thrift响应消息服务端
CThriftServer * m_pResponseServer;
// Thrift结构体编解码
CThriftStructCodec m_structCodec;
private:
// 构造函数
CDataCommInt();
static CDataCommInt * m_pInst;
};
DataCommlnt.cpp
#include "DataCommInt.h"
#include "ThriftRequestClientFactory.h"
#include "ThriftResponseClientFactory.h"
#include "ThriftServerFactory.h"
#include "ZMQReceiverFactory.h"
#include "ZMQSenderFactory.h"
#include "SysConfigMng.h"
CDataCommInt * CDataCommInt::m_pInst = nullptr;
// 构造函数
CDataCommInt::CDataCommInt()
{
m_pZMQSender = nullptr;
m_pZMQReceiver = nullptr;
m_pRequestClient = nullptr;
m_pResponseClient = nullptr;
m_pRequestServer = nullptr;
m_pResponseServer = nullptr;
return;
}
// 析构函数
CDataCommInt::~CDataCommInt()
{
if (nullptr != m_pZMQSender)
{
delete m_pZMQSender;
m_pZMQSender = nullptr;
}
if (nullptr != m_pZMQReceiver)
{
delete m_pZMQReceiver;
m_pZMQReceiver = nullptr;
}
if (nullptr != m_pRequestClient)
{
delete m_pRequestClient;
m_pRequestClient = nullptr;
}
if (nullptr != m_pResponseClient)
{
delete m_pResponseClient;
m_pResponseClient = nullptr;
}
if (nullptr != m_pRequestServer)
{
delete m_pRequestServer;
m_pRequestServer = nullptr;
}
if (nullptr != m_pResponseServer)
{
delete m_pResponseServer;
m_pResponseServer = nullptr;
}
if (nullptr == CThriftRequestClientFactory::GetInstance())
{
delete CThriftRequestClientFactory::GetInstance();
}
if (nullptr == CThriftResponseClientFactory::GetInstance())
{
delete CThriftResponseClientFactory::GetInstance();
}
if (nullptr == CThriftServerFactory::GetInstance())
{
delete CThriftServerFactory::GetInstance();
}
if (nullptr == CZMQReceiverFactory::GetInstance())
{
delete CZMQReceiverFactory::GetInstance();
}
if (nullptr == CZMQSenderFactory::GetInstance())
{
delete CZMQSenderFactory::GetInstance();
}
return;
}
CDataCommInt * CDataCommInt::GetInstance()
{
if (nullptr == m_pInst)
{
m_pInst = new CDataCommInt();
}
return m_pInst;
}
// 初始化
int CDataCommInt::iInitialize()
{
ostringstream os;
os << "CDataCommInt Initialize begin" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Info);
stDataCommIntConfig dataCommIntConfig = CSysConfigMng::GetInstance()->GetDataCommIntConfig();
// 初始化ZMQSender
m_pZMQSender = CZMQSenderFactory::GetInstance()->CreateZMQSender(dataCommIntConfig.m_sendLumDataCommIntConfig.m_sServerIP \
, dataCommIntConfig.m_sendLumDataCommIntConfig.m_iListenPort);
if (nullptr == m_pZMQSender)
{
os.str("");
os << "failed to malloc memory for ZMQSender" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
return EXIT_FAILURE;
}
// 初始化ThriftResponseClient
m_pResponseClient = CThriftResponseClientFactory::GetInstance()->CreateThriftResponseClient(\
dataCommIntConfig.m_sendCmdCommIntConfig.m_iProtocolType\
, dataCommIntConfig.m_sendCmdCommIntConfig.m_commCommonConfig.m_sServerIP\
, dataCommIntConfig.m_sendCmdCommIntConfig.m_commCommonConfig.m_iListenPort\
, dataCommIntConfig.m_sendCmdCommIntConfig.m_sslCertificateConfig.m_sSSLPubCertificate\
, dataCommIntConfig.m_sendCmdCommIntConfig.m_sslCertificateConfig.m_sSSLPriCertificate\
, dataCommIntConfig.m_sendCmdCommIntConfig.m_sslCertificateConfig.m_sSSLTrustedCertificate);
if (nullptr == m_pResponseClient)
{
os.str("");
os << "failed to malloc memory for ThriftResponseClient" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
return EXIT_FAILURE;
}
// 初始化RequestServer
m_pRequestServer = CThriftServerFactory::GetInstance()->CreateThriftServer(0\
, dataCommIntConfig.m_recvCmdCommIntConfig.m_iProtocolType\
, dataCommIntConfig.m_recvCmdCommIntConfig.m_commCommonConfig.m_iListenPort\
, dataCommIntConfig.m_recvCmdCommIntConfig.m_sslCertificateConfig.m_sSSLPubCertificate\
, dataCommIntConfig.m_recvCmdCommIntConfig.m_sslCertificateConfig.m_sSSLPriCertificate);
if (nullptr == m_pRequestServer)
{
os.str("");
os << "failed to malloc memory for RequestServer" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
return EXIT_FAILURE;
}
os.str("");
os << "CDataCommInt Initialize end" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Info);
return EXIT_SUCCESS;
}
// 发送告警信息
int CDataCommInt::iSendWarrning(const MsgRequest& msgRequire)
{
if (nullptr != m_pResponseClient)
{
return m_pResponseClient->iSendWarnning(msgRequire);
}
else
{
ostringstream os;
os << "ResponseClientPtr can't be empty" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
return EXIT_FAILURE;
}
}
// 发送心跳消息
int CDataCommInt::iBeatHeart(const MsgRequest& msgRequire)
{
if (nullptr != m_pResponseClient)
{
return m_pResponseClient->iBeatHeart(msgRequire);
}
else
{
ostringstream os;
os << "ResponseClientPtr can't be empty" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
return EXIT_FAILURE;
}
}
// 发送状态信息
int CDataCommInt::iSendStatus(const MsgRequest& msgRequire)
{
if (nullptr != m_pResponseClient)
{
return m_pResponseClient->iSendStatus(msgRequire);
}
else
{
ostringstream os;
os << "ResponseClientPtr can't be empty" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
return EXIT_FAILURE;
}
}
// 响应消息
int CDataCommInt::iResponseMsg(const MsgResponse& msgReponse)
{
if (nullptr != m_pResponseClient)
{
return m_pResponseClient->iResponseMsg(msgReponse);
}
else
{
ostringstream os;
os << "ResponseClientPtr can't be empty" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
return EXIT_FAILURE;
}
}
// 发送请求消息
int CDataCommInt::iSendRequireMsg(ReturnMsg & returnMsg, const MsgRequest & msgRequest)
{
if (nullptr != m_pRequestClient)
{
return m_pRequestClient->iRequireMsg(returnMsg, msgRequest);
}
else
{
ostringstream os;
os << "RequestClientPtr can't be empty" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
return EXIT_FAILURE;
}
}
// 发送亮度数据
int CDataCommInt::iSendLumData(unsigned char * ucData, int iDataLen)
{
if (nullptr != m_pZMQSender)
{
return m_pZMQSender->iSendData(ucData, iDataLen);
}
else
{
ostringstream os;
os << "RequestClientPtr can't be empty" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
return EXIT_FAILURE;
}
}
// 取接收数据
int CDataCommInt::iFetchRecvData(string & sRecvData)
{
if (nullptr != m_pZMQReceiver)
{
return m_pZMQReceiver->iFetchRecvData(sRecvData);
}
else
{
ostringstream os;
os << "RequestClientPtr can't be empty" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
return EXIT_FAILURE;
}
}
// 判断是否有接收数据
bool CDataCommInt::bHadRecvData()
{
if (nullptr != m_pZMQReceiver)
{
return m_pZMQReceiver->bHadRecvData();
}
else
{
ostringstream os;
os << "RequestClientPtr can't be empty" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
return EXIT_FAILURE;
}
}
// 等待线程退出
void CDataCommInt::wait()
{
if (nullptr != m_pZMQReceiver)
{
m_pZMQReceiver->wait();
}
if (nullptr != m_pRequestServer)
{
m_pRequestServer->wait();
}
if (nullptr != m_pResponseServer)
{
m_pResponseServer->wait();
}
return;
}
// 退出线程
void CDataCommInt::ExitThread()
{
if (nullptr != m_pZMQReceiver)
{
m_pZMQReceiver->SetExitFlag(true);
}
if (nullptr != m_pRequestServer)
{
m_pRequestServer->SetExitFlag(true);
}
if (nullptr != m_pResponseServer)
{
m_pResponseServer->SetExitFlag(true);
}
return;
}
ThriftClient.h
#pragma once
#include <string>
#include <mutex>
using namespace std;
// 链接超时时间,3s
const int cniConnectTimeout = 3000;
// 发送超时时间
const int cniSendTimeout = 5000;
class CThriftClient
{
public:
// 构造函数
CThriftClient(const string & sHost, int iPort);
// 析构函数
virtual ~CThriftClient();
public:
// 设置主机地址
inline void SetHost(const string & sHost)
{
m_sHost = sHost;
return;
}
// 获取主机地址
inline string sGetHost() const
{
return m_sHost;
}
// 设置端口号
inline void SetPort(int iPort)
{
m_iPort = iPort;
return;
}
// 获取端口号
inline int iGetPort() const
{
return m_iPort;
}
// 设置最后连接时间
inline void SetLastTime(time_t tLastTime)
{
m_tLastTime = tLastTime;
return;
}
// 获取最后连接时间
inline time_t tGetLastTime() const
{
return m_tLastTime;
}
protected:
// 客户端锁
mutex m_mtxClient;
protected:
// 初始化客户端
virtual void InitClient() = 0;
// 关闭客户端
virtual void CloseClient() = 0;
// 客户端重连
void Reconnect();
private:
// 服务端IP地址
string m_sHost;
// 服务端监听地址
int m_iPort;
// 客户端最后连接时间
time_t m_tLastTime;
};
ThriftClient.cpp
#include <time.h>
#include "ThriftClient.h"
// 异常重连间隔时间
const int cniReconnectInterval = 3;
// 构造函数
CThriftClient::CThriftClient(const string & sHost, int iPort)
{
m_sHost = sHost;
m_iPort = iPort;
m_tLastTime = time(NULL);
return;
}
// 析构函数
CThriftClient::~CThriftClient()
{
return;
}
// 客户端重连
void CThriftClient::Reconnect()
{
{
lock_guard<mutex>guard(m_mtxClient);
time_t tTimeInterval = time(NULL) - tGetLastTime();
if (cniReconnectInterval < tTimeInterval)
{
CloseClient();
InitClient();
}
}
return;
}
ThriftComlntlmp.h
#pragma once
#include "CommInt/gen-cpp/ThriftComInt.h"
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;
using namespace ::thrift::commint;
class CThriftComIntImp : virtual public ThriftComIntIf
{
public:
// 构造函数
CThriftComIntImp()
{
return;
}
// 析构函数
~CThriftComIntImp()
{
return;
}
// 请求消息
void RequireMsg(ReturnMsg& _return, const MsgRequest& msgRequest);
};
ThriftComlntlmp.cpp
#include "ThriftComIntImp.h"
#include "CommandRecv.h"
// 请求消息
void CThriftComIntImp::RequireMsg(ReturnMsg& _return, const MsgRequest& msgRequest)
{
int iRet = CCommandRecv::GetInstance()->iVerifyCommand(msgRequest, _return.sErr);
if (EXIT_SUCCESS == iRet)
{
CCommandRecv::GetInstance()->Push(msgRequest);
}
_return.iRet = iRet;
return;
}
ThriftComReponseIntImp.h
Description: ThriftComIntImp响应命令接口定义
Others: 无
*************************************************/
#pragma once
#include "CommInt/gen-cpp/ThriftComReponseInt.h"
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;
using namespace ::thrift::commint;
class CThriftComReponseIntImp : virtual public ThriftComReponseIntIf
{
public:
// 构造函数
CThriftComReponseIntImp();
// 析构函数
~CThriftComReponseIntImp();
// 响应消息
void ResponseMsg(const MsgResponse& msgReponse);
// 发送状态
void SendStatus(const MsgRequest& msgRequire);
// 发送心跳
void BeatHeart(const MsgRequest& msgRequire);
// 发送告警
void SendWarnning(const MsgRequest& msgRequire);
};
ThriftComReponseIntImp.cpp
#include "ThriftComReponseIntImp.h"
CThriftComReponseIntImp::CThriftComReponseIntImp()
{
return;
}
CThriftComReponseIntImp::~CThriftComReponseIntImp()
{
return;
}
void CThriftComReponseIntImp::ResponseMsg(const MsgResponse& msgReponse)
{
return;
}
void CThriftComReponseIntImp::SendStatus(const MsgRequest& msgRequire)
{
return;
}
void CThriftComReponseIntImp::BeatHeart(const MsgRequest& msgRequire)
{
return;
}
void CThriftComReponseIntImp::SendWarnning(const MsgRequest& msgRequire)
{
return;
}
ThriftRequestClient.h
Description: Thrift请求客户端基类
Others: 无
*************************************************/
#pragma once
#include "ThriftClient.h"
#include "CommInt/gen-cpp/ThriftComInt.h"
using namespace thrift::commint;
class CThriftRequestClient:public CThriftClient
{
public:
// 构造函数
CThriftRequestClient(const string & sHost, int iPort);
// 析构函数
~CThriftRequestClient();
public:
virtual int iRequireMsg(ReturnMsg & returnMsg, const MsgRequest & msgRequest) = 0;
};
ThriftRequestClient.cpp
#include "ThriftRequestClient.h"
// 构造函数
CThriftRequestClient::CThriftRequestClient(const string & sHost, int iPort)
:CThriftClient(sHost, iPort)
{
return;
}
// 析构函数
CThriftRequestClient::~CThriftRequestClient()
{
return;
}
ThriftRequestClientFactory.h
Description: Thrift请求客户端工厂类定义
Others: 无
*************************************************/
#pragma once
#include "ThriftRequestClient.h"
class CThriftRequestClientFactory
{
public:
// 析构函数
~CThriftRequestClientFactory();
static CThriftRequestClientFactory * GetInstance();
// 创建请求客户端对象
CThriftRequestClient * CreateThriftRequestClient(int iProtocolType, const string &sHost, int iPort, const string & sCertificate = "" \
, const string & sPrivateKey = "", const string & sTrustedCertificates = "");
private:
// 构造函数
CThriftRequestClientFactory();
private:
static CThriftRequestClientFactory * m_pInst;
};
ThriftRequestClientFactory.cpp
#include "Log.h"
#include "ThriftRequestClientFactory.h"
#include "ThriftTCPClient.h"
#include "ThriftSSLClient.h"
CThriftRequestClientFactory * CThriftRequestClientFactory::m_pInst = nullptr;
// 构造函数
CThriftRequestClientFactory::CThriftRequestClientFactory()
{
return;
}
// 析构函数
CThriftRequestClientFactory::~CThriftRequestClientFactory()
{
return;
}
CThriftRequestClientFactory * CThriftRequestClientFactory::GetInstance()
{
if (nullptr == m_pInst)
{
m_pInst = new CThriftRequestClientFactory();
}
return m_pInst;
}
// 创建请求客户端对象
CThriftRequestClient * CThriftRequestClientFactory::CreateThriftRequestClient(int iProtocolType, const string &sHost, int iPort, const string & sCertificate \
, const string & sPrivateKey, const string & sTrustedCertificates)
{
CThriftRequestClient * pThriftRequestClient = nullptr;
if (0 == iProtocolType)
{
pThriftRequestClient = new CThriftTCPClient(sHost, iPort);
}
else if (1 == iProtocolType)
{
pThriftRequestClient = new CThriftSSLClient(sHost, iPort, sCertificate, sPrivateKey, sTrustedCertificates);
}
else
{
ostringstream os;
os << "ThriftRequestClient not support unkown protocolType[" << iProtocolType << "]" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
return pThriftRequestClient;
}
ThriftResponseClient.h
Description: Thrift响应客户端基类
Others: 无
*************************************************/
#pragma once
#include "ThriftClient.h"
#include "CommInt/gen-cpp/ThriftComReponseInt.h"
using namespace thrift::commint;
class CThriftResponseClient : public CThriftClient
{
public:
// 构造函数
CThriftResponseClient(const string & sHost, int iPort);
// 析构函数
~CThriftResponseClient();
public:
// 响应消息
virtual int iResponseMsg(const MsgResponse& msgReponse) = 0;
// 发送状态
virtual int iSendStatus(const MsgRequest& msgRequire) = 0;
// 发送心跳信息
virtual int iBeatHeart(const MsgRequest& msgRequire) = 0;
// 发送告警信息
virtual int iSendWarnning(const MsgRequest& msgRequire) = 0;
};
ThriftResponseClient.cpp
#include "ThriftResponseClient.h"
// 构造函数
CThriftResponseClient::CThriftResponseClient(const string & sHost, int iPort) :
CThriftClient(sHost, iPort)
{
return;
}
// 析构函数
CThriftResponseClient::~CThriftResponseClient()
{
return;
}
ThriftResponseClientFactory.h
Description: Thrift响应客户端工厂类定义
Others: 无
*************************************************/
#pragma once
#include "ThriftResponseClient.h"
class CThriftResponseClientFactory
{
public:
// 析构函数
~CThriftResponseClientFactory();
static CThriftResponseClientFactory * GetInstance();
// 创建请求客户端对象
CThriftResponseClient * CreateThriftResponseClient(int iProtocolType, const string &sHost, int iPort, const string & sCertificate = "" \
, const string & sPrivateKey = "", const string & sTrustedCertificates = "");
private:
// 构造函数
CThriftResponseClientFactory();
private:
static CThriftResponseClientFactory * m_pInst;
};
ThriftResponseClientFactory.cpp
Description: Thrift响应客户端工厂类实现
Others: 无
*************************************************/
#include "Log.h"
#include "ThriftResponseClientFactory.h"
#include "ThriftTCPClientForReponse.h"
#include "ThriftSSLClientForReponse.h"
CThriftResponseClientFactory * CThriftResponseClientFactory::m_pInst = nullptr;
// 构造函数
CThriftResponseClientFactory::CThriftResponseClientFactory()
{
return;
}
// 析构函数
CThriftResponseClientFactory::~CThriftResponseClientFactory()
{
return;
}
CThriftResponseClientFactory * CThriftResponseClientFactory::GetInstance()
{
if (nullptr == m_pInst)
{
m_pInst = new CThriftResponseClientFactory();
}
return m_pInst;
}
// 创建请求客户端对象
CThriftResponseClient * CThriftResponseClientFactory::CreateThriftResponseClient(int iProtocolType, const string &sHost, int iPort, const string & sCertificate \
, const string & sPrivateKey, const string & sTrustedCertificates)
{
CThriftResponseClient * pThriftResponseClient = nullptr;
if (0 == iProtocolType)
{
pThriftResponseClient = new CThriftTCPClientForReponse(sHost, iPort);
}
else if (1 == iProtocolType)
{
pThriftResponseClient = new CThriftSSLClientForReponse(sHost, iPort, sCertificate, sPrivateKey, sTrustedCertificates);
}
else
{
ostringstream os;
os << "ThriftRequestClient not support unkown protocolType[" << iProtocolType << "]" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
return pThriftResponseClient;
}
ThriftServer.h
Description: Thrift服务端基类定义
Others: 无
*************************************************/
#pragma once
#include "ThreadProc.h"
// service处理默认线程数
const int cniServerThreadNum = 10;
// 默认处理任务数,不受限制
const int cniServerTaskNum = 0;
class CThriftServer:public CThreadProc
{
public:
// 构造函数
CThriftServer(int iPort);
// 析构函数
~CThriftServer();
public:
// 停止工作
virtual void StopWork();
// 设置端口号
inline void SetPort(int iPort)
{
m_iPort = iPort;
return;
}
// 获取端口号
int iGetPort() const
{
return m_iPort;
}
// 线程调用
virtual int src(int id) override;
protected:
// 开始工作
virtual void StartWork() = 0;
// 初始化服务端
virtual void InitServer() = 0;
// 接收线程
void RecvThread();
private:
// 端口号
int m_iPort;
};
ThriftServer.cpp
#include <stdlib.h>
#include "ThriftServer.h"
// 构造函数
CThriftServer::CThriftServer(int iPort):
CThreadProc(1)
{
m_iPort = iPort;
return;
}
// 析构函数
CThriftServer::~CThriftServer()
{
StopWork();
return;
}
// 停止工作
void CThriftServer::StopWork()
{
return;
}
// 线程调用
int CThriftServer::src(int id)
{
if (0 == id)
{
RecvThread();
}
return EXIT_SUCCESS;
}
// 接收线程
void CThriftServer::RecvThread()
{
bool bExitFlag = bGetExitFlag();
while (!bExitFlag)
{
StartWork();
bExitFlag = bGetExitFlag();
if (!bExitFlag)
{
StopWork();
}
}
return;
}
ThriftServerFactory.h
Description: Thrift服务端工厂类定义
Others: 无
*************************************************/
#pragma once
#include "ThriftServer.h"
class CThriftServerFactory
{
public:
// 析构函数
~CThriftServerFactory();
static CThriftServerFactory * GetInstance();
// 创建Thrift服务端对象
CThriftServer * CreateThriftServer(int iIntType, int iProtocolType, int iPort, const string & sCertificate \
, const string & sPrivateKey);
private:
// 构造函数
CThriftServerFactory();
private:
static CThriftServerFactory * m_pInst;
};
TThriftServerFactory.cpp
#include "Log.h"
#include "ThriftServerFactory.h"
#include "ThriftTCPServer.h"
#include "ThriftSSLServer.h"
#include "ThriftTCPServerForReponse.h"
#include "ThriftSSLServerForReponse.h"
#include "ThriftSSLServerForReponse.h"
CThriftServerFactory * CThriftServerFactory::m_pInst = nullptr;
// 构造函数
CThriftServerFactory::CThriftServerFactory()
{
return;
}
// 析构函数
CThriftServerFactory::~CThriftServerFactory()
{
return;
}
CThriftServerFactory * CThriftServerFactory::GetInstance()
{
if (nullptr == m_pInst)
{
m_pInst = new CThriftServerFactory();
}
return m_pInst;
}
// 创建Thrift服务端对象
CThriftServer * CThriftServerFactory::CreateThriftServer(int iIntType, int iProtocolType, int iPort, const string & sCertificate \
, const string & sPrivateKey)
{
CThriftServer * pThriftServer = nullptr;
if (0 == iIntType)
{
if (0 == iProtocolType)
{
pThriftServer = new CThriftTCPServer(iPort);
}
else if (1 == iProtocolType)
{
pThriftServer = new CThriftSSLServer(iPort, sCertificate, sPrivateKey);
}
else
{
ostringstream os;
os << "ThriftRequestServer not support unkown protocolType[" << iProtocolType << "]" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
}
else if (1 == iIntType)
{
if (0 == iProtocolType)
{
pThriftServer = new CThriftTCPServerForReponse(iPort);
}
else if (1 == iProtocolType)
{
pThriftServer = new CThriftSSLServerForReponse(iPort, sCertificate, sPrivateKey);
}
else
{
ostringstream os;
os << "ThriftRequestServer not support unkown protocolType[" << iProtocolType << "]" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
}
else
{
ostringstream os;
os << "ThriftRequestServer not support unkown InterfaceType[" << iIntType << "]" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
return pThriftServer;
}
ThriftSSLClient.h
Description: ThriftSSL请求客户端定义
Others: 无
*************************************************/
#pragma once
#include <iostream>
#include <string>
#include <thrift/transport/TTransportUtils.h>
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TSSLSocket.h>
#include <thrift/transport/TSSLServerSocket.h>
#include <thrift/protocol/TBinaryProtocol.h>
#include <boost/filesystem.hpp>
#include <boost/format.hpp>
#include "CommInt/gen-cpp/ThriftComInt.h"
#include "ThriftRequestClient.h"
using namespace std;
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using apache::thrift::transport::TSSLSocketFactory;
using apache::thrift::transport::TSSLSocket;
class CThriftSSLClient : public CThriftRequestClient
{
public:
// 构造函数
CThriftSSLClient(const string & sHost, int iPort, const string & sCertificate \
, const string & sPrivateKey, const string & sTrustedCertificates);
// 析构函数
~CThriftSSLClient();
public:
virtual int iRequireMsg(ReturnMsg & returnMsg, const MsgRequest & msgRequest);
protected:
// 初始化客户端
virtual void InitClient();
// 关闭客户端
virtual void CloseClient();
private:
// 获取TSSLSocketFactory指针
std::shared_ptr<TSSLSocketFactory> pCreateClientSocketFactory();
private:
string m_sCertificate; // 公钥证书
string m_sPrivateKey; // 私钥证书
string m_sTrustedCertificates; // CA证书
::std::shared_ptr<TSSLSocketFactory> m_clientSocketFactory; // SSL socket工厂对象
::std::shared_ptr<TSSLSocket> m_sslSocket; // SSL Socket
::std::shared_ptr<TTransport> m_transport; // 传输指针
::std::shared_ptr<TProtocol> m_protocol; // 传输协议指针
::std::shared_ptr<ThriftComIntClient> m_client; // 同步传输客户端
};
ThriftSSLClient.cpp
#include "Log.h"
#ifndef BOOST_ERROR_CODE_HEADER_ONLY
#define BOOST_ERROR_CODE_HEADER_ONLY
#endif
#include "ThriftSSLClient.h"
#include "ZMQConfigParam.h"
// 构造函数
CThriftSSLClient::CThriftSSLClient(const string & sHost, int iPort, const string & sCertificate \
, const string & sPrivateKey, const string & sTrustedCertificates)
:CThriftRequestClient(sHost, iPort)
{
m_sCertificate = sCertificate;
m_sPrivateKey = sPrivateKey;
m_sTrustedCertificates = sTrustedCertificates;
InitClient();
return;
}
// 析构函数
CThriftSSLClient::~CThriftSSLClient()
{
CloseClient();
return;
}
int CThriftSSLClient::iRequireMsg(ReturnMsg & returnMsg, const MsgRequest & msgRequest)
{
try
{
bool bEmptyClient = false;
{
lock_guard<mutex>guard(m_mtxClient);
if (m_client)
{
m_client->RequireMsg(returnMsg, msgRequest);
}
else
{
bEmptyClient = true;
}
}
if (bEmptyClient)
{
ostringstream os;
os << "Error: client can't be empty" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
}
catch (apache::thrift::transport::TSSLException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
}
catch (apache::thrift::transport::TTransportException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
}
catch (exception &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
catch (...)
{
ostringstream os;
os << "Occurred exception" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
// 初始化客户端
void CThriftSSLClient::InitClient()
{
try
{
#ifdef _WIN32
TWinsockSingleton::create();
#endif
string sHost = sGetHost();
int iPort = iGetPort();
SetLastTime(time(NULL));
m_clientSocketFactory = pCreateClientSocketFactory();
m_sslSocket = m_clientSocketFactory->createSocket(sHost, iPort);
m_sslSocket->setConnTimeout(cniConnectTimeout);
//m_sslSocket->setKeepAlive(true);
m_sslSocket->setKeepAliveParam(cniKeepAliveEnable, cniKeepAliveCnt, cniKeepAliveIdle, cniKeepAliveIntvl);
m_sslSocket->setSendTimeout(cniSendTimeout);
m_transport = std::make_shared<TBufferedTransport>(m_sslSocket);
m_protocol = std::make_shared<TBinaryProtocol>(m_transport);
m_client = std::make_shared<ThriftComIntClient>(m_protocol);
m_transport->open();
}
catch (apache::thrift::transport::TSSLException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (apache::thrift::transport::TTransportException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (exception &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (...)
{
ostringstream os;
os << "Occurred exception" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
return;
}
// 关闭客户端
void CThriftSSLClient::CloseClient()
{
try
{
if (m_transport)
{
m_transport->close();
}
}
catch (apache::thrift::transport::TSSLException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (apache::thrift::transport::TTransportException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (exception &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (...)
{
ostringstream os;
os << "Occurred exception" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
return;
}
// 获取TSSLSocketFactory指针
std::shared_ptr<TSSLSocketFactory> CThriftSSLClient::pCreateClientSocketFactory()
{
std::shared_ptr<TSSLSocketFactory> pClientSocketFactory;
pClientSocketFactory.reset(new TSSLSocketFactory());
pClientSocketFactory->authenticate(true);
pClientSocketFactory->loadCertificate(m_sCertificate.c_str());
pClientSocketFactory->loadPrivateKey(m_sPrivateKey.c_str());
pClientSocketFactory->loadTrustedCertificates(m_sTrustedCertificates.c_str());
return pClientSocketFactory;
}
ThriftSSLClientForReponse.h
Description: ThriftSSL响应客户端定义
Others: 无
*************************************************/
#pragma once
#include <iostream>
#include <string>
#include <thrift/transport/TTransportUtils.h>
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TSSLSocket.h>
#include <thrift/transport/TSSLServerSocket.h>
#include <thrift/protocol/TBinaryProtocol.h>
#include <boost/filesystem.hpp>
#include <boost/format.hpp>
#include "CommInt/gen-cpp/ThriftComInt.h"
#include "ThriftResponseClient.h"
using namespace std;
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using apache::thrift::transport::TSSLSocketFactory;
using apache::thrift::transport::TSSLSocket;
class CThriftSSLClientForReponse : public CThriftResponseClient
{
public:
// 构造函数
CThriftSSLClientForReponse(const string & sHost, int iPort, const string & sCertificate \
, const string & sPrivateKey, const string & sTrustedCertificates);
// 析构函数
~CThriftSSLClientForReponse();
public:
// 发送响应消息
virtual int iResponseMsg(const MsgResponse& msgReponse);
// 发送状态消息
virtual int iSendStatus(const MsgRequest& msgRequire);
// 发送心跳消息
virtual int iBeatHeart(const MsgRequest& msgRequire);
// 发送告警消息
virtual int iSendWarnning(const MsgRequest& msgRequire);
protected:
// 初始化客户端
virtual void InitClient();
// 关闭客户端
virtual void CloseClient();
private:
// 获取TSSLSocketFactory指针
std::shared_ptr<TSSLSocketFactory> pCreateClientSocketFactory();
private:
string m_sCertificate; // 公钥证书
string m_sPrivateKey; // 私钥证书
string m_sTrustedCertificates; // CA证书
::std::shared_ptr<TSSLSocketFactory> m_clientSocketFactory; // SSL socket工厂对象
::std::shared_ptr<TSSLSocket> m_sslSocket; // SSL Socket
::std::shared_ptr<TTransport> m_transport; // 传输指针
::std::shared_ptr<TProtocol> m_protocol; // 传输协议指针
::std::shared_ptr<ThriftComReponseIntClient> m_client; // 同步传输客户端
};
ThriftSSLClientForReponse.cpp
#include "Log.h"
#include "ThriftSSLClientForReponse.h"
#include "ZMQConfigParam.h"
// 构造函数
CThriftSSLClientForReponse::CThriftSSLClientForReponse(const string & sHost, int iPort, const string & sCertificate \
, const string & sPrivateKey, const string & sTrustedCertificates)
:CThriftResponseClient(sHost, iPort)
{
m_sCertificate = sCertificate;
m_sPrivateKey = sPrivateKey;
m_sTrustedCertificates = sTrustedCertificates;
InitClient();
return;
}
// 析构函数
CThriftSSLClientForReponse::~CThriftSSLClientForReponse()
{
CloseClient();
return;
}
// 发送响应消息
int CThriftSSLClientForReponse::iResponseMsg(const MsgResponse& msgReponse)
{
try
{
bool bEmptyClient = false;
{
lock_guard<mutex>guard(m_mtxClient);
if (m_client)
{
m_client->ResponseMsg(msgReponse);
}
else
{
bEmptyClient = true;
}
}
if (bEmptyClient)
{
ostringstream os;
os << "Error: client can't be empty" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
}
catch (apache::thrift::transport::TSSLException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
catch (apache::thrift::transport::TTransportException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
catch (exception &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
catch (...)
{
ostringstream os;
os << "Occurred exception" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
// 发送状态消息
int CThriftSSLClientForReponse::iSendStatus(const MsgRequest& msgRequire)
{
try
{
bool bEmptyClient = false;
{
lock_guard<mutex>guard(m_mtxClient);
if (m_client)
{
m_client->SendStatus(msgRequire);
}
else
{
bEmptyClient = true;
}
}
if (bEmptyClient)
{
ostringstream os;
os << "Error: client can't be empty" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
}
catch (apache::thrift::transport::TSSLException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
catch (apache::thrift::transport::TTransportException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
catch (exception &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
catch (...)
{
ostringstream os;
os << "Occurred exception" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
// 发送心跳消息
int CThriftSSLClientForReponse::iBeatHeart(const MsgRequest& msgRequire)
{
try
{
bool bEmptyClient = false;
{
lock_guard<mutex>guard(m_mtxClient);
if (m_client)
{
m_client->BeatHeart(msgRequire);
}
else
{
bEmptyClient = true;
}
}
if (bEmptyClient)
{
ostringstream os;
os << "Error: client can't be empty" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
}
catch (apache::thrift::transport::TSSLException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
catch (apache::thrift::transport::TTransportException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
catch (exception &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
catch (...)
{
ostringstream os;
os << "Occurred exception" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
// 发送告警消息
int CThriftSSLClientForReponse::iSendWarnning(const MsgRequest& msgRequire)
{
try
{
bool bEmptyClient = false;
{
lock_guard<mutex>guard(m_mtxClient);
if (m_client)
{
m_client->SendWarnning(msgRequire);
}
else
{
bEmptyClient = true;
}
}
if (bEmptyClient)
{
ostringstream os;
os << "Error: client can't be empty" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
}
catch (apache::thrift::transport::TSSLException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
catch (apache::thrift::transport::TTransportException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
catch (exception &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
catch (...)
{
ostringstream os;
os << "Occurred exception" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
// 初始化客户端
void CThriftSSLClientForReponse::InitClient()
{
try
{
#ifdef _WIN32
TWinsockSingleton::create();
#endif
string sHost = sGetHost();
int iPort = iGetPort();
SetLastTime(time(NULL));
m_clientSocketFactory = pCreateClientSocketFactory();
m_sslSocket = m_clientSocketFactory->createSocket(sHost, iPort);
m_sslSocket->setConnTimeout(cniConnectTimeout);
//m_sslSocket->setKeepAlive(true);
m_sslSocket->setKeepAliveParam(cniKeepAliveEnable, cniKeepAliveCnt, cniKeepAliveIdle, cniKeepAliveIntvl);
m_sslSocket->setSendTimeout(cniSendTimeout);
m_transport = std::make_shared<TBufferedTransport>(m_sslSocket);
m_protocol = std::make_shared<TBinaryProtocol>(m_transport);
m_client = std::make_shared<ThriftComReponseIntClient>(m_protocol);
m_transport->open();
}
catch (apache::thrift::transport::TSSLException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (apache::thrift::transport::TTransportException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (exception &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (...)
{
ostringstream os;
os << "Occurred exception" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
return;
}
// 关闭客户端
void CThriftSSLClientForReponse::CloseClient()
{
try
{
if (m_transport)
{
m_transport->close();
}
}
catch (apache::thrift::transport::TSSLException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (apache::thrift::transport::TTransportException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (exception &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (...)
{
ostringstream os;
os << "Occurred exception" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
return;
}
// 获取TSSLSocketFactory指针
std::shared_ptr<TSSLSocketFactory> CThriftSSLClientForReponse::pCreateClientSocketFactory()
{
std::shared_ptr<TSSLSocketFactory> pClientSocketFactory;
pClientSocketFactory.reset(new TSSLSocketFactory());
pClientSocketFactory->authenticate(true);
pClientSocketFactory->loadCertificate(m_sCertificate.c_str());
pClientSocketFactory->loadPrivateKey(m_sPrivateKey.c_str());
pClientSocketFactory->loadTrustedCertificates(m_sTrustedCertificates.c_str());
return pClientSocketFactory;
}
ThriftSSLServer.h
Description: ThriftSSL请求服务端定义
Others: 无
*************************************************/
#pragma once
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/concurrency/ThreadFactory.h>
#include <thrift/concurrency/ThreadManager.h>
#include <thrift/server/TThreadPoolServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/transport/TSSLSocket.h>
#include <thrift/transport/TSSLServerSocket.h>
#include "ThriftServer.h"
#include "CommInt/gen-cpp/ThriftComInt.h"
#include "ThriftComIntImp.h"
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::concurrency;
using namespace ::apache::thrift::server;
using namespace ::thrift::commint;
using apache::thrift::transport::TSSLSocketFactory;
using apache::thrift::transport::TSSLSocket;
class CThriftSSLServer : public CThriftServer
{
public:
// 构造函数
CThriftSSLServer(int iPort, const string & sCertificate \
, const string & sPrivateKey);
// 析构函数
~CThriftSSLServer();
public:
// 停止工作
virtual void StopWork();
protected:
// 开始工作
virtual void StartWork();
// 初始化服务端
virtual void InitServer();
// 创建服务端ssl工厂对象
std::shared_ptr<TSSLSocketFactory> pCreateServerSocketFactory();
private:
string m_sCertificate; // 公钥证书
string m_sPrivateKey; // 私钥证书
::std::shared_ptr<TSSLSocketFactory> m_pServerSocketFactory; // Server Socket工厂对象
::std::shared_ptr<TServerSocket> m_sslSocket; // ssl socket
::std::shared_ptr<CThriftComIntImp> m_handler; // 同步接口实现
::std::shared_ptr<TProcessor> m_processor; // 接口处理器
::std::shared_ptr<TServerTransport> m_serverTransport; // 服务端传输
::std::shared_ptr<TTransportFactory> m_transportFactory; // 传输工厂对象
::std::shared_ptr<TProtocolFactory> m_protocolFactory; // 协议工厂对象
::std::shared_ptr <TThreadPoolServer> m_server; // 同步传输服务
::std::shared_ptr<ThreadManager> m_threadManager; // 线程管理
};
ThriftSSLServer.cpp
#include "Log.h"
#include "ThriftSSLServer.h"
#include "ZMQConfigParam.h"
// 构造函数
CThriftSSLServer::CThriftSSLServer(int iPort, const string & sCertificate \
, const string & sPrivateKey)
:CThriftServer(iPort)
{
m_sCertificate = sCertificate;
m_sPrivateKey = sPrivateKey;
InitServer();
open();
return;
}
// 析构函数
CThriftSSLServer::~CThriftSSLServer()
{
return;
}
// 停止工作
void CThriftSSLServer::StopWork()
{
try
{
if (m_server)
{
m_server->stop();
}
else
{
ostringstream os;
os << "Error: AsysnClient can't be empty" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
}
catch (apache::thrift::transport::TSSLException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (apache::thrift::transport::TTransportException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (exception &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (...)
{
ostringstream os;
os << "Occurred exception" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
return;
}
// 开始工作
void CThriftSSLServer::StartWork()
{
try
{
if (m_server)
{
m_server->serve();
}
else
{
ostringstream os;
os << "Error: AsysnClient can't be empty" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
}
catch (apache::thrift::transport::TSSLException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (apache::thrift::transport::TTransportException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (exception &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (...)
{
ostringstream os;
os << "Occurred exception" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
return;
}
// 初始化服务端
void CThriftSSLServer::InitServer()
{
try
{
#ifdef _WIN32
TWinsockSingleton::create();
#endif
int iPort = iGetPort();
m_pServerSocketFactory = pCreateServerSocketFactory();
m_sslSocket = std::make_shared<TSSLServerSocket>(iPort, m_pServerSocketFactory);
//m_sslSocket->setKeepAlive(true);
m_sslSocket->setKeepAliveParam(cniKeepAliveEnable, cniKeepAliveCnt, cniKeepAliveIdle, cniKeepAliveIntvl);
m_handler = std::make_shared<CThriftComIntImp>();
m_processor = std::make_shared<ThriftComIntProcessor>(m_handler);
m_transportFactory = std::make_shared<TBufferedTransportFactory>();
m_protocolFactory = std::make_shared<TBinaryProtocolFactory>();
m_threadManager = ThreadManager::newSimpleThreadManager(cniServerThreadNum, cniServerTaskNum);
m_threadManager->threadFactory(std::make_shared<ThreadFactory>());
m_threadManager->start();
m_server = std::make_shared<TThreadPoolServer>(m_processor, m_sslSocket, m_transportFactory, m_protocolFactory, m_threadManager);
}
catch (apache::thrift::transport::TSSLException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (apache::thrift::transport::TTransportException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (exception &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (...)
{
ostringstream os;
os << "Occurred exception" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
return;
}
// 创建服务端ssl工厂对象
std::shared_ptr<TSSLSocketFactory> CThriftSSLServer::pCreateServerSocketFactory()
{
std::shared_ptr<TSSLSocketFactory> pServerSocketFactory;
pServerSocketFactory.reset(new TSSLSocketFactory());
pServerSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
pServerSocketFactory->loadCertificate(m_sCertificate.c_str());
pServerSocketFactory->loadPrivateKey(m_sPrivateKey.c_str());
pServerSocketFactory->server(true);
return pServerSocketFactory;
}
ThriftSSLServerForReponse.h
Description: ThriftSSL响应服务端定义
Others: 无
*************************************************/
#pragma once
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/concurrency/ThreadFactory.h>
#include <thrift/concurrency/ThreadManager.h>
#include <thrift/server/TThreadPoolServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/transport/TSSLSocket.h>
#include <thrift/transport/TSSLServerSocket.h>
#include "ThriftServer.h"
#include "CommInt/gen-cpp/ThriftComReponseInt.h"
#include "ThriftComReponseIntImp.h"
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::concurrency;
using namespace ::apache::thrift::server;
using namespace ::thrift::commint;
using apache::thrift::transport::TSSLSocketFactory;
using apache::thrift::transport::TSSLSocket;
class CThriftSSLServerForReponse : public CThriftServer
{
public:
// 构造函数
CThriftSSLServerForReponse(int iPort, const string & sCertificate \
, const string & sPrivateKey);
// 析构函数
~CThriftSSLServerForReponse();
public:
// 停止工作
virtual void StopWork();
protected:
// 开始工作
virtual void StartWork();
// 初始化服务端
virtual void InitServer();
// 创建服务端ssl工厂对象
std::shared_ptr<TSSLSocketFactory> pCreateServerSocketFactory();
private:
string m_sCertificate; // 公钥证书
string m_sPrivateKey; // 私钥证书
::std::shared_ptr<TSSLSocketFactory> m_pServerSocketFactory; // Server Socket工厂对象
::std::shared_ptr<TServerSocket> m_sslSocket; // ssl socket
::std::shared_ptr<CThriftComReponseIntImp> m_handler; // 同步接口实现
::std::shared_ptr<TProcessor> m_processor; // 接口处理器
::std::shared_ptr<TServerTransport> m_serverTransport; // 服务端传输
::std::shared_ptr<TTransportFactory> m_transportFactory; // 传输工厂对象
::std::shared_ptr<TProtocolFactory> m_protocolFactory; // 协议工厂对象
::std::shared_ptr <TThreadPoolServer> m_server; // 同步传输服务
::std::shared_ptr<ThreadManager> m_threadManager; // 线程管理
};
ThriftSSLServerForReponse.cpp
#include "Log.h"
#include "ThriftSSLServerForReponse.h"
#include "ZMQConfigParam.h"
// 构造函数
CThriftSSLServerForReponse::CThriftSSLServerForReponse(int iPort, const string & sCertificate \
, const string & sPrivateKey)
:CThriftServer(iPort)
{
m_sCertificate = sCertificate;
m_sPrivateKey = sPrivateKey;
InitServer();
open();
return;
}
// 析构函数
CThriftSSLServerForReponse::~CThriftSSLServerForReponse()
{
return;
}
// 停止工作
void CThriftSSLServerForReponse::StopWork()
{
try
{
if (m_server)
{
m_server->stop();
}
else
{
ostringstream os;
os << "Error: AsysnClient can't be empty" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
}
catch (apache::thrift::transport::TSSLException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (apache::thrift::transport::TTransportException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (exception &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (...)
{
ostringstream os;
os << "Occurred exception" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
return;
}
// 开始工作
void CThriftSSLServerForReponse::StartWork()
{
try
{
if (m_server)
{
m_server->serve();
}
else
{
ostringstream os;
os << "Error: AsysnClient can't be empty" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
}
catch (apache::thrift::transport::TSSLException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (apache::thrift::transport::TTransportException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (exception &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (...)
{
ostringstream os;
os << "Occurred exception" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
return;
}
// 初始化服务端
void CThriftSSLServerForReponse::InitServer()
{
try
{
#ifdef _WIN32
TWinsockSingleton::create();
#endif
int iPort = iGetPort();
m_pServerSocketFactory = pCreateServerSocketFactory();
m_sslSocket = std::make_shared<TSSLServerSocket>(iPort, m_pServerSocketFactory);
//m_sslSocket->setKeepAlive(true);
m_sslSocket->setKeepAliveParam(cniKeepAliveEnable, cniKeepAliveCnt, cniKeepAliveIdle, cniKeepAliveIntvl);
m_handler = std::make_shared<CThriftComReponseIntImp>();
m_processor = std::make_shared<ThriftComReponseIntProcessor>(m_handler);
m_transportFactory = std::make_shared<TBufferedTransportFactory>();
m_protocolFactory = std::make_shared<TBinaryProtocolFactory>();
m_threadManager = ThreadManager::newSimpleThreadManager(cniServerThreadNum, cniServerTaskNum);
m_threadManager->threadFactory(std::make_shared<ThreadFactory>());
m_threadManager->start();
m_server = std::make_shared<TThreadPoolServer>(m_processor, m_sslSocket, m_transportFactory, m_protocolFactory, m_threadManager);
}
catch (apache::thrift::transport::TSSLException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (apache::thrift::transport::TTransportException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (exception &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (...)
{
ostringstream os;
os << "Occurred exception" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
return;
}
// 创建服务端ssl工厂对象
std::shared_ptr<TSSLSocketFactory> CThriftSSLServerForReponse::pCreateServerSocketFactory()
{
std::shared_ptr<TSSLSocketFactory> pServerSocketFactory;
pServerSocketFactory.reset(new TSSLSocketFactory());
pServerSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
pServerSocketFactory->loadCertificate(m_sCertificate.c_str());
pServerSocketFactory->loadPrivateKey(m_sPrivateKey.c_str());
pServerSocketFactory->server(true);
return pServerSocketFactory;
}
ThriftStructCodec.h
// 本文件用于编解码器定义。
// 其它 : 无
///
#pragma once
#include <iostream>
#include <sstream>
#include <ostream>
#include <string>
#include <thrift/transport/TTransportUtils.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/protocol/TBinaryProtocol.h>
#include "CommInt/gen-cpp/CommInt_types.h"
#include "Log.h"
using namespace std;
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace ::thrift::commint;
class CThriftStructCodec
{
public:
// 构造函数
CThriftStructCodec();
// 析构函数
~CThriftStructCodec();
public:
// 编码
template<class T>
int iEncode(string &sBinary, const T &tructInfo)
{
shared_ptr<TMemoryBuffer> tmb = make_shared<TMemoryBuffer>();
shared_ptr<TBinaryProtocol> oprot = make_shared<TBinaryProtocol>(tmb);
try
{
tructInfo.write(oprot.get());
}
catch (TException e)
{
ostringstream os;
os << "Exception Ocurred:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
return EXIT_FAILURE;
}
catch (...)
{
ostringstream os;
os << "Exception Ocurred:" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
return EXIT_FAILURE;
}
sBinary = tmb->getBufferAsString();
return EXIT_SUCCESS;
}
// 解码
template<typename T>
int iDecode(T & tructInfo, const string & sBinary)
{
shared_ptr<TMemoryBuffer> tmb1 = make_shared<TMemoryBuffer>((uint8_t*)sBinary.c_str(), (uint32_t)sBinary.size());
shared_ptr<TBinaryProtocol> oprot1 = make_shared<TBinaryProtocol>(tmb1);
MsgRequest request1;
try
{
tructInfo.read(oprot1.get());
}
catch (TException e)
{
ostringstream os;
os << "Exception Ocurred:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
return EXIT_FAILURE;
}
catch (...)
{
ostringstream os;
os << "Exception Ocurred:" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
};
ThriftStructCodec.cpp
#include "ThriftStructCodec.h"
// 构造函数
CThriftStructCodec::CThriftStructCodec()
{
return;
}
// 析构函数
CThriftStructCodec::~CThriftStructCodec()
{
return;
}
ThriftTCPClient.h
Description: ThriftTCP请求客户端类
Others: 无
*************************************************/
#include <iostream>
#include <string>
#include <thrift/transport/TTransportUtils.h>
#include <thrift/transport/TSocket.h>
#include <thrift/protocol/TBinaryProtocol.h>
#include "CommInt/gen-cpp/ThriftComInt.h"
#include "ThriftRequestClient.h"
using namespace std;
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
class CThriftTCPClient : public CThriftRequestClient
{
public:
// 构造函数
CThriftTCPClient(const string & sHost, int iPort);
// 析构函数
~CThriftTCPClient();
public:
virtual int iRequireMsg(ReturnMsg & returnMsg, const MsgRequest & msgRequest);
protected:
// 初始化客户端
virtual void InitClient();
// 关闭客户端
virtual void CloseClient();
private:
::std::shared_ptr<TSocket> m_socket; // socket指针
::std::shared_ptr<TTransport> m_transport; // 传输指针
::std::shared_ptr<TProtocol> m_protocol; // 传输协议指针
::std::shared_ptr<ThriftComIntClient> m_client; // 同步传输客户端
};
ThriftTCPClient.cpp
#include "Log.h"
#include "ThriftTCPClient.h"
#include "ZMQConfigParam.h"
// 构造函数
CThriftTCPClient::CThriftTCPClient(const string & sHost, int iPort)
:CThriftRequestClient(sHost, iPort)
{
InitClient();
return;
}
// 析构函数
CThriftTCPClient::~CThriftTCPClient()
{
CloseClient();
return;
}
// 请求消息发送
int CThriftTCPClient::iRequireMsg(ReturnMsg & returnMsg, const MsgRequest & msgRequest)
{
try
{
bool bEmptyClient = false;
{
lock_guard<mutex>guard(m_mtxClient);
if (m_client)
{
m_client->RequireMsg(returnMsg, msgRequest);
}
else
{
bEmptyClient = true;
}
}
if (bEmptyClient)
{
ostringstream os;
os << "Error: client can't be empty" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
}
catch (exception &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
catch (...)
{
ostringstream os;
os << "Occurred exception" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
// 初始化客户端
void CThriftTCPClient::InitClient()
{
try
{
#ifdef _WIN32
TWinsockSingleton::create();
#endif
string sHost = sGetHost();
int iPort = iGetPort();
SetLastTime(time(NULL));
m_socket = std::make_shared<TSocket>(sHost, iPort);
m_socket->setConnTimeout(cniConnectTimeout);
//m_socket->setKeepAlive(true);
m_socket->setKeepAliveParam(cniKeepAliveEnable, cniKeepAliveCnt, cniKeepAliveIdle, cniKeepAliveIntvl);
m_socket->setSendTimeout(cniSendTimeout);
m_transport = std::make_shared<TBufferedTransport>(m_socket);
m_protocol = std::make_shared<TBinaryProtocol>(m_transport);
m_client = std::make_shared<ThriftComIntClient>(m_protocol);
m_transport->open();
}
catch (apache::thrift::transport::TTransportException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (exception &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (...)
{
ostringstream os;
os << "Occurred exception" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
return;
}
// 关闭客户端
void CThriftTCPClient::CloseClient()
{
try
{
if (m_transport)
{
m_transport->close();
}
}
catch (apache::thrift::transport::TTransportException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (exception &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (...)
{
ostringstream os;
os << "Occurred exception" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
return;
}
ThriftTCPClientForReponse.h
#pragma once
#include <iostream>
#include <string>
#include <thrift/transport/TTransportUtils.h>
#include <thrift/transport/TSocket.h>
#include <thrift/protocol/TBinaryProtocol.h>
#include "CommInt/gen-cpp/ThriftComInt.h"
#include "ThriftResponseClient.h"
using namespace std;
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
class CThriftTCPClientForReponse : public CThriftResponseClient
{
public:
// 构造函数
CThriftTCPClientForReponse(const string & sHost, int iPort);
// 析构函数
~CThriftTCPClientForReponse();
public:
// 发送响应消息
virtual int iResponseMsg(const MsgResponse& msgReponse);
// 发送状态消息
virtual int iSendStatus(const MsgRequest& msgRequire);
// 发送心跳消息
virtual int iBeatHeart(const MsgRequest& msgRequire);
// 发送告警消息
virtual int iSendWarnning(const MsgRequest& msgRequire);
protected:
// 初始化客户端
virtual void InitClient();
// 关闭客户端
virtual void CloseClient();
private:
::std::shared_ptr<TSocket> m_socket; // socket指针
::std::shared_ptr<TTransport> m_transport; // 传输指针
::std::shared_ptr<TProtocol> m_protocol; // 传输协议指针
::std::shared_ptr<ThriftComReponseIntClient> m_client; // 同步传输客户端
};
ThriftTCPClientForReponse.cpp
#include "Log.h"
#include "ThriftTCPClientForReponse.h"
#include "ZMQConfigParam.h"
// 构造函数
CThriftTCPClientForReponse::CThriftTCPClientForReponse(const string & sHost, int iPort)
:CThriftResponseClient(sHost, iPort)
{
InitClient();
return;
}
// 析构函数
CThriftTCPClientForReponse::~CThriftTCPClientForReponse()
{
CloseClient();
return;
}
// 发送响应消息
int CThriftTCPClientForReponse::iResponseMsg(const MsgResponse& msgReponse)
{
try
{
bool bEmptyClient = false;
{
lock_guard<mutex>guard(m_mtxClient);
if (m_client)
{
m_client->ResponseMsg(msgReponse);
}
else
{
bEmptyClient = true;
}
}
if (bEmptyClient)
{
ostringstream os;
os << "Error: client can't be empty" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
}
catch (apache::thrift::transport::TTransportException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
catch (exception &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
catch (...)
{
ostringstream os;
os << "Occurred exception" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
// 发送状态消息
int CThriftTCPClientForReponse::iSendStatus(const MsgRequest& msgRequire)
{
try
{
bool bEmptyClient = false;
{
lock_guard<mutex>guard(m_mtxClient);
if (m_client)
{
m_client->SendStatus(msgRequire);
}
else
{
bEmptyClient = true;
}
}
if (bEmptyClient)
{
ostringstream os;
os << "Error: client can't be empty" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
}
catch (apache::thrift::transport::TTransportException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
catch (exception &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
catch (...)
{
ostringstream os;
os << "Occurred exception" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
// 发送心跳消息
int CThriftTCPClientForReponse::iBeatHeart(const MsgRequest& msgRequire)
{
try
{
bool bEmptyClient = false;
{
lock_guard<mutex>guard(m_mtxClient);
if (m_client)
{
m_client->BeatHeart(msgRequire);
}
else
{
bEmptyClient = true;
}
}
if (bEmptyClient)
{
ostringstream os;
os << "Error: client can't be empty" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
}
catch (apache::thrift::transport::TTransportException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
catch (exception &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
catch (...)
{
ostringstream os;
os << "Occurred exception" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
// 发送告警消息
int CThriftTCPClientForReponse::iSendWarnning(const MsgRequest& msgRequire)
{
try
{
bool bEmptyClient = false;
{
lock_guard<mutex>guard(m_mtxClient);
if (m_client)
{
m_client->SendWarnning(msgRequire);
}
else
{
bEmptyClient = true;
}
}
if (bEmptyClient)
{
ostringstream os;
os << "Error: client can't be empty" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
}
catch (apache::thrift::transport::TTransportException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
catch (exception &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
catch (...)
{
ostringstream os;
os << "Occurred exception" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
Reconnect();
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
// 初始化客户端
void CThriftTCPClientForReponse::InitClient()
{
try
{
#ifdef _WIN32
TWinsockSingleton::create();
#endif
string sHost = sGetHost();
int iPort = iGetPort();
SetLastTime(time(NULL));
m_socket = std::make_shared<TSocket>(sHost, iPort);
m_socket->setConnTimeout(cniConnectTimeout);
//m_socket->setKeepAlive(true);
m_socket->setKeepAliveParam(cniKeepAliveEnable, cniKeepAliveCnt, cniKeepAliveIdle, cniKeepAliveIntvl);
m_socket->setSendTimeout(cniSendTimeout);
m_transport = std::make_shared<TBufferedTransport>(m_socket);
m_protocol = std::make_shared<TBinaryProtocol>(m_transport);
m_client = std::make_shared<ThriftComReponseIntClient>(m_protocol);
m_transport->open();
}
catch (apache::thrift::transport::TTransportException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (exception &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (...)
{
ostringstream os;
os << "Occurred exception" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
return;
}
// 关闭客户端
void CThriftTCPClientForReponse::CThriftTCPClientForReponse::CloseClient()
{
try
{
if (m_transport)
{
m_transport->close();
}
}
catch (apache::thrift::transport::TTransportException &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (exception &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (...)
{
ostringstream os;
os << "Occurred exception" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
return;
}
ThriftTCPServer.h
Description: ThriftTCP请求服务端定义
Others: 无
*************************************************/
#pragma once
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/concurrency/ThreadFactory.h>
#include <thrift/concurrency/ThreadManager.h>
#include <thrift/server/TThreadPoolServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>
#include "ThriftServer.h"
#include "CommInt/gen-cpp/ThriftComInt.h"
#include "ThriftComIntImp.h"
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::concurrency;
using namespace ::apache::thrift::server;
using namespace ::thrift::commint;
class CThriftTCPServer : public CThriftServer
{
public:
// 构造函数
CThriftTCPServer(int iPort);
// 析构函数
~CThriftTCPServer();
public:
public:
// 停止工作
virtual void StopWork();
protected:
// 开始工作
virtual void StartWork();
// 初始化服务端
virtual void InitServer();
private:
::std::shared_ptr<CThriftComIntImp> m_handler; // 同步接口实现
::std::shared_ptr<TProcessor> m_processor; // 接口处理器
::std::shared_ptr<TServerTransport> m_serverTransport; // 服务端传输
::std::shared_ptr<TTransportFactory> m_transportFactory; // 传输工厂对象
::std::shared_ptr<TProtocolFactory> m_protocolFactory; // 协议工厂对象
::std::shared_ptr <TThreadPoolServer> m_server; // 同步传输服务
::std::shared_ptr<ThreadManager> m_threadManager; // 线程管理
};
ThriftTCPServer.cpp
#include "Log.h"
#include "ThriftTCPServer.h"
#include "ZMQConfigParam.h"
// 构造函数
CThriftTCPServer::CThriftTCPServer(int iPort)
:CThriftServer(iPort)
{
InitServer();
open();
return;
}
// 析构函数
CThriftTCPServer::~CThriftTCPServer()
{
return;
}
// 停止工作
void CThriftTCPServer::StopWork()
{
try
{
if (m_server)
{
m_server->stop();
}
else
{
ostringstream os;
os << "Error: AsysnClient can't be empty" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
}
catch (exception &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (...)
{
ostringstream os;
os << "Occurred exception" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
return;
}
// 开始工作
void CThriftTCPServer::StartWork()
{
try
{
if (m_server)
{
m_server->serve();
}
else
{
ostringstream os;
os << "Error: AsysnClient can't be empty" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
}
catch (exception &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (...)
{
ostringstream os;
os << "Occurred exception" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
return;
}
// 初始化服务端
void CThriftTCPServer::InitServer()
{
try
{
#ifdef _WIN32
TWinsockSingleton::create();
#endif
int iPort = iGetPort();
m_handler = std::make_shared<CThriftComIntImp>();
m_processor = std::make_shared<ThriftComIntProcessor>(m_handler);
std::shared_ptr<TServerSocket> serverSocket = std::make_shared<TServerSocket>(iPort);
//serverSocket->setKeepAlive(true);
serverSocket->setKeepAliveParam(cniKeepAliveEnable, cniKeepAliveCnt, cniKeepAliveIdle, cniKeepAliveIntvl);
m_serverTransport = serverSocket;
m_transportFactory = std::make_shared<TBufferedTransportFactory>();
m_protocolFactory = std::make_shared<TBinaryProtocolFactory>();
m_threadManager = ThreadManager::newSimpleThreadManager(cniServerThreadNum, cniServerTaskNum);
m_threadManager->threadFactory(std::make_shared<ThreadFactory>());
m_threadManager->start();
m_server = std::make_shared<TThreadPoolServer>(m_processor, m_serverTransport, m_transportFactory, m_protocolFactory, m_threadManager);
}
catch (exception &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (...)
{
ostringstream os;
os << "Occurred exception" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
return;
}
ThriftTCPServerForReponse.h
Description: ThriftTCP响应服务端定义
Others: 无
*************************************************/
#pragma once
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/concurrency/ThreadFactory.h>
#include <thrift/concurrency/ThreadManager.h>
#include <thrift/server/TThreadPoolServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>
#include "ThriftServer.h"
#include "CommInt/gen-cpp/ThriftComReponseInt.h"
#include "ThriftComReponseIntImp.h"
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::concurrency;
using namespace ::apache::thrift::server;
using namespace ::thrift::commint;
class CThriftTCPServerForReponse : public CThriftServer
{
public:
// 构造函数
CThriftTCPServerForReponse(int iPort);
// 析构函数
~CThriftTCPServerForReponse();
public:
// 停止工作
virtual void StopWork();
protected:
// 开始工作
virtual void StartWork();
// 初始化服务端
virtual void InitServer();
private:
::std::shared_ptr<CThriftComReponseIntImp> m_handler; // 同步接口实现
::std::shared_ptr<TProcessor> m_processor; // 接口处理器
::std::shared_ptr<TServerTransport> m_serverTransport; // 服务端传输
::std::shared_ptr<TTransportFactory> m_transportFactory; // 传输工厂对象
::std::shared_ptr<TProtocolFactory> m_protocolFactory; // 协议工厂对象
::std::shared_ptr <TThreadPoolServer> m_server; // 同步传输服务
::std::shared_ptr<ThreadManager> m_threadManager; // 线程管理
};
ThriftTCPServerForReponse.cpp
#include "Log.h"
#include "ThriftTCPServerForReponse.h"
#include "ZMQConfigParam.h"
// 构造函数
CThriftTCPServerForReponse::CThriftTCPServerForReponse(int iPort)
:CThriftServer(iPort)
{
InitServer();
open();
return;
}
// 析构函数
CThriftTCPServerForReponse::~CThriftTCPServerForReponse()
{
return;
}
// 停止工作
void CThriftTCPServerForReponse::StopWork()
{
try
{
if (m_server)
{
m_server->stop();
}
else
{
ostringstream os;
os << "Error: AsysnClient can't be empty" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
}
catch (exception &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (...)
{
ostringstream os;
os << "Occurred exception" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
return;
}
// 开始工作
void CThriftTCPServerForReponse::StartWork()
{
try
{
if (m_server)
{
m_server->serve();
}
else
{
ostringstream os;
os << "Error: AsysnClient can't be empty" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
}
catch (exception &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (...)
{
ostringstream os;
os << "Occurred exception" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
return;
}
// 初始化服务端
void CThriftTCPServerForReponse::InitServer()
{
try
{
#ifdef _WIN32
TWinsockSingleton::create();
#endif
int iPort = iGetPort();
m_handler = std::make_shared<CThriftComReponseIntImp>();
m_processor = std::make_shared<ThriftComReponseIntProcessor>(m_handler);
std::shared_ptr<TServerSocket> serverSocket = std::make_shared<TServerSocket>(iPort);
//serverSocket->setKeepAlive(true);
serverSocket->setKeepAliveParam(cniKeepAliveEnable, cniKeepAliveCnt, cniKeepAliveIdle, cniKeepAliveIntvl);
m_serverTransport = serverSocket;
m_transportFactory = std::make_shared<TBufferedTransportFactory>();
m_protocolFactory = std::make_shared<TBinaryProtocolFactory>();
m_threadManager = ThreadManager::newSimpleThreadManager(cniServerThreadNum, cniServerTaskNum);
m_threadManager->threadFactory(std::make_shared<ThreadFactory>());
m_threadManager->start();
m_server = std::make_shared<TThreadPoolServer>(m_processor, m_serverTransport, m_transportFactory, m_protocolFactory, m_threadManager);
}
catch (exception &e)
{
ostringstream os;
os << "Occurred exception:" << e.what() << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
catch (...)
{
ostringstream os;
os << "Occurred exception" << endl;
CLog::GetInstance()->WriteLog(__FUNCTION__, __LINE__, os.str(), Log_Error);
}
return;
}
ZMQ.h
ZMQ.cpp