Commit e0484b4f by guanghui.cui

长连接添加心跳

parent e9687bbc
[SYS]
port = 24446
port=24446
[ODS]
ip = "115.234.23.176"
pushPort = 8996
recvPort = 8997
ip=115.234.23.176
pushPort=8996
recvPort=8997
socketTimeout=60
[POS]
ip = "127.0.0.1"
port = 24445
ip=127.0.0.1
port=24445
......@@ -476,6 +476,22 @@ bool JsonModule::isInitData(const std::string &data)
return document["fm_cmd"].GetInt() == 1000;
}
bool JsonModule::isHeartbeatData(IN const char* data)
{
rapidjson::Document document; // 定义一个Document对象
document.Parse(data); // 解析,Parse()无返回值,也不会抛异常
if (document.HasParseError()) // 通过HasParseError()来判断解析是否成功
{
LOG(ERROR) << "JSON parse error:" << document.GetParseError() << ":" << document.GetErrorOffset();
return false;
}
if(document.HasMember("fm_cmd")){
if(document["fm_cmd"].GetInt() == 3)
return true;
}
return false;
}
bool JsonModule::checkInitData(const std::string &data, int &posListenPort)
{
rapidjson::Document document; // 定义一个Document对象
......
......@@ -21,6 +21,13 @@ public:
* 返回:...
* */
bool isInitData(IN const std::string& data);
/* 功能:判断是否是心跳包
* 参数:[1]待判断数据
* 返回:...
* */
bool isHeartbeatData(IN const char* data);
/* 功能:检查初始化数据是否可用
* 参数:[1]待检查数据[2]POS监听端口
* 返回:是否正确格式
......
......@@ -138,6 +138,14 @@ bool TCPClient::doConnect(unsigned short port, const char *ip)
return false;
}
void TCPClient::setSocketTimeout(int timeout)
{
struct timeval vtime;
vtime.tv_sec = timeout;
vtime.tv_usec = 0;
setsockopt(m_sockfd, SOL_SOCKET, SO_RCVTIMEO, &vtime, sizeof(struct timeval));
}
//send
bool TCPClient::send(const std::string& message)
{
......
......@@ -54,6 +54,7 @@ public:
bool write(const char *msg);
bool receive(std::string& message);
bool send(const std::string& message);
void setSocketTimeout(int timeout);
bool isValid(){ return m_bValid; }
private:
......
......@@ -148,9 +148,11 @@ int main()
std::string strIniPath(strBinPath.data());
strIniPath.append("config.ini");
LOG(INFO) << "strIniPath: " << strIniPath.data();
ods_ip = ZIni::readString("ODS","ip", "", strIniPath.c_str());
ods_push_port = ZIni::readInt("ODS", "pushPort", 0, strIniPath.c_str());
ods_recv_port = ZIni::readInt("ODS", "recvPort", 0, strIniPath.c_str());
int ods_socket_timeout = ZIni::readInt("ODS", "socketTimeout", 0, strIniPath.c_str());
pos_ip = ZIni::readString("POS","ip", "", strIniPath.c_str());
pos_listen_port = ZIni::readInt("POS","port", 0, strIniPath.c_str());
......@@ -159,7 +161,8 @@ int main()
LOG(INFO) << "[ODS]ip: " << ods_ip.data()
<< "-push port: " << ods_push_port
<< "-recv port: " << ods_recv_port;
<< "-recv port: " << ods_recv_port
<< "-socket timeout: " << ods_socket_timeout;
LOG(INFO) << "[POS]ip: " << pos_ip
<< "-listen port: " << pos_listen_port;
LOG(INFO) << "[Local]listen port: " << client_listen_port;
......@@ -187,11 +190,11 @@ int main()
if( ods.doConnect(ods_push_port, ods_ip.c_str()) )
{
LOG(INFO) << "connect ODS successful";
ods.setSocketTimeout(ods_socket_timeout); //设置超时时间10s
// 注册socket信息
while(true)
{
//g_init_data = "{\"fm_cmd\": 1000,\"store_id\": \"208888\",\"pos_id\": \"0001\",\"operator_id\": \"00001\",\"business_date\": \"20171225\",\"is_master\": true,\"listen_port\": 3289}";
g_init_data = "{\"fm_cmd\": 1000,\"store_id\": \"208888\",\"pos_id\": \"0001\",\"operator_id\": \"00001\",\"business_date\": \"20171225\",\"is_master\": true,\"listen_port\": 3289}";
if(!g_init_data.empty())
{
......@@ -219,6 +222,7 @@ int main()
{
LOG(INFO) << "----------------recved ODS data------------------";
LOG(INFO) << odsPushData;
if(!jsonTool.isHeartbeatData(odsPushData.data())){
if( jsonTool.convertDataOds2Pos(odsPushData, pushPosData) )
{
LOG(INFO) << "********convert data to pos************";
......@@ -254,9 +258,12 @@ int main()
// TODO待加入重试机制
ods.send(responseData);
}
}else
{
LOG(INFO) << "recv ODS pushDate failed";
ods.close();
LOG(INFO) << "recv ODS pushDate failed or timeout";
}
}
......
......@@ -15,6 +15,7 @@
#include <sstream>
#include <cctype>
#include <cstdlib>
#include <iostream>
bool ZIni::writeString(std::string strSectName,
std::string strKeyName,
......@@ -217,7 +218,7 @@ double ZIni::readDouble(std::string strSectName,
ssDefault.str(), strFileName).c_str());
}
//消除字串两段的空格,字串中间的保留
//消除字串两段的空格,字串中间的保留
std::string ZIni::trim(std::string &strInput)
{
std::string str(strInput);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment