Commit d0ebe816 by 刘帅

1. 优化流程

2. 整理文件和代码
parent e391071b
...@@ -2,4 +2,5 @@ ...@@ -2,4 +2,5 @@
/bin/* /bin/*
!/bin/*.dll !/bin/*.dll
!/bin/*.ini
...@@ -2,7 +2,7 @@ INCLUDEPATH += $$PWD ...@@ -2,7 +2,7 @@ INCLUDEPATH += $$PWD
#DEFINES += QS_LOG_LINE_NUMBERS # automatically writes the file and line for each log message #DEFINES += QS_LOG_LINE_NUMBERS # automatically writes the file and line for each log message
#DEFINES += QS_LOG_DISABLE # logging code is replaced with a no-op #DEFINES += QS_LOG_DISABLE # logging code is replaced with a no-op
#DEFINES += QS_LOG_SEPARATE_THREAD # messages are queued and written from a separate thread #DEFINES += QS_LOG_SEPARATE_THREAD # messages are queued and written from a separate thread
#DEFINES += QS_LOG_WIN_PRINTF_CONSOLE # Use fprintf instead of OutputDebugString on Windows DEFINES += QS_LOG_WIN_PRINTF_CONSOLE # Use fprintf instead of OutputDebugString on Windows
SOURCES += $$PWD/QsLogDest.cpp \ SOURCES += $$PWD/QsLogDest.cpp \
$$PWD/QsLog.cpp \ $$PWD/QsLog.cpp \
$$PWD/QsLogDestConsole.cpp \ $$PWD/QsLogDestConsole.cpp \
......
[store]
partner_id=1371
store_id=fm99999
pos_id=1
operator_id=001
business_date=20171016
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include <QJsonArray> #include <QJsonArray>
#include <QJsonDocument> #include <QJsonDocument>
#include <QDebug> #include <QDebug>
#include "QsLog/QsLog.h"
Convert::Convert() Convert::Convert()
{ {
...@@ -70,7 +71,7 @@ bool Convert::QueryPay(QJsonObject &object, STORE_INFO store_info) ...@@ -70,7 +71,7 @@ bool Convert::QueryPay(QJsonObject &object, STORE_INFO store_info)
} }
else else
{ {
qDebug()<<"can not find trans_id in json request!"; QLOG_ERROR()<<"can not find trans_id in json request!";
return false; return false;
} }
if(object.contains("fm_order_id")) if(object.contains("fm_order_id"))
......
...@@ -30,8 +30,8 @@ static void InitLogger() ...@@ -30,8 +30,8 @@ static void InitLogger()
QsLogging::MaxOldLogCount(logCount))); QsLogging::MaxOldLogCount(logCount)));
logger.addDestination(fileDestination); logger.addDestination(fileDestination);
/* 输出到控制台 */ /* 输出到控制台 */
// QsLogging::DestinationPtr consleDest(QsLogging::DestinationFactory::MakeDebugOutputDestination()); QsLogging::DestinationPtr consleDest(QsLogging::DestinationFactory::MakeDebugOutputDestination());
// logger.addDestination(consleDest); logger.addDestination(consleDest);
} }
#endif // LOG #endif // LOG
......
...@@ -9,7 +9,7 @@ int main(int argc, char *argv[]) ...@@ -9,7 +9,7 @@ int main(int argc, char *argv[])
{ {
QCoreApplication a(argc, argv); QCoreApplication a(argc, argv);
InitLogger(); InitLogger();
QLOG_INFO()<<"start"; QLOG_INFO()<<"start client...";
QFmClient client; QFmClient client;
client.start(); client.start();
return a.exec(); return a.exec();
......
...@@ -145,17 +145,13 @@ bool QFmClient::WaitForConnectReqFromPos() ...@@ -145,17 +145,13 @@ bool QFmClient::WaitForConnectReqFromPos()
RecvSockData(_acceptedSock, _recvbuf, (MAX_BUF_LEN - 1)); RecvSockData(_acceptedSock, _recvbuf, (MAX_BUF_LEN - 1));
CloseSocket(&_acceptedSock); CloseSocket(&_acceptedSock);
} }
qDebug()<<"wait for connection from pos"; QLOG_INFO()<<"waiting for connection from pos...\n";
_acceptedSock = accept(_listenSock, (struct sockaddr *)(&c_add), &sin_size); _acceptedSock = accept(_listenSock, (struct sockaddr *)(&c_add), &sin_size);
if(-1 == _acceptedSock) if(-1 == _acceptedSock)
{ {
qDebug() << "accept socket fail!\r\n";
return false; return false;
} }
QLOG_INFO() << "accept pos connection ok!\r\n";
qDebug() << "accept ok, ZF Client get connection from POS!\r\n";
return true; return true;
} }
...@@ -279,7 +275,7 @@ char * QFmClient::DecodeRecvData(char * buf, int length, int * dLength) ...@@ -279,7 +275,7 @@ char * QFmClient::DecodeRecvData(char * buf, int length, int * dLength)
int QFmClient::WaitAndReadDataFromPos(int type, int flag) int QFmClient::WaitAndReadDataFromPos(int type, int flag)
{ {
qDebug() << "wait to receive data from POS...\r\n"; QLOG_INFO() << "waiting to receive data from pos...\r\n";
_rcvLength = 0; _rcvLength = 0;
...@@ -307,7 +303,7 @@ int QFmClient::WaitAndReadDataFromPos(int type, int flag) ...@@ -307,7 +303,7 @@ int QFmClient::WaitAndReadDataFromPos(int type, int flag)
//recbytes = GBKToUTF8( (unsigned char *)_sendbuf, recbytes, (unsigned char *)_tempbuf, MAX_BUF_LEN - 100); //recbytes = GBKToUTF8( (unsigned char *)_sendbuf, recbytes, (unsigned char *)_tempbuf, MAX_BUF_LEN - 100);
} }
#endif #endif
qDebug() << "received data from pos(socket " << _acceptedSock << ") : \n" << _tempbuf; QLOG_INFO() << "received data from pos(socket " << _acceptedSock << ") : \n" << _tempbuf;
precvBuf = _tempbuf; precvBuf = _tempbuf;
_tempbuf[recbytes] = '\0'; _tempbuf[recbytes] = '\0';
...@@ -321,7 +317,7 @@ int QFmClient::WaitAndReadDataFromPos(int type, int flag) ...@@ -321,7 +317,7 @@ int QFmClient::WaitAndReadDataFromPos(int type, int flag)
if (((0 == checkCount) && ( '}' != precvBuf[ prosLen + finLen - 1])) || ((MAX_BUF_LEN - 1) < curPos + finLen)) if (((0 == checkCount) && ( '}' != precvBuf[ prosLen + finLen - 1])) || ((MAX_BUF_LEN - 1) < curPos + finLen))
{ {
qDebug() << "checkCount out!\r\n"; QLOG_ERROR() << "checkCount out!\r\n";
return -1; return -1;
} }
...@@ -336,7 +332,7 @@ int QFmClient::WaitAndReadDataFromPos(int type, int flag) ...@@ -336,7 +332,7 @@ int QFmClient::WaitAndReadDataFromPos(int type, int flag)
_rcvLength = curPos; _rcvLength = curPos;
curPos = 0; curPos = 0;
checkCount = 0; checkCount = 0;
qDebug() << "checkCount = 0"; //qDebug() << "checkCount = 0";
goto finishRecv; goto finishRecv;
} }
...@@ -401,7 +397,7 @@ int QFmClient::ProcessPosReqData(QJsonObject& object) ...@@ -401,7 +397,7 @@ int QFmClient::ProcessPosReqData(QJsonObject& object)
{ {
SetStoreInfo(object); SetStoreInfo(object);
} }
qDebug()<<"convert json format"; //qDebug()<<"convert json format";
convert.FreemudPay(object,store_info); convert.FreemudPay(object,store_info);
_sockTimeOut = _longTimeOut; _sockTimeOut = _longTimeOut;
AddReqCount(object); AddReqCount(object);
...@@ -424,11 +420,11 @@ int QFmClient::ProcessPosReqData(QJsonObject& object) ...@@ -424,11 +420,11 @@ int QFmClient::ProcessPosReqData(QJsonObject& object)
qDebug()<<"unknown command: "<<fm_cmd; qDebug()<<"unknown command: "<<fm_cmd;
break; break;
} }
qDebug()<<"client request json:\n"<<object; //QLOG_INFO()<<"client request json:\n"<<object;
} }
else else
{ {
qDebug()<<"unknown command(can not find fm_cmd section) "; QLOG_ERROR()<<"unknown command(can not find fm_cmd section) ";
nResult = -1; nResult = -1;
} }
return nResult; return nResult;
...@@ -442,7 +438,7 @@ int QFmClient::ProcessPosReqData(QJsonObject& object) ...@@ -442,7 +438,7 @@ int QFmClient::ProcessPosReqData(QJsonObject& object)
bool QFmClient::ConvertResponseJsonFormat(QJsonObject &object, int nType) bool QFmClient::ConvertResponseJsonFormat(QJsonObject &object, int nType)
{ {
bool bRtValue = true; bool bRtValue = true;
qDebug()<<"proxy response json:\n"<<object; //qDebug()<<"proxy response json:\n"<<object;
switch(nType) switch(nType)
{ {
case SET_STORE_INFO: case SET_STORE_INFO:
...@@ -529,7 +525,7 @@ void QFmClient::sendSetResponseToPos(bool bFlag) ...@@ -529,7 +525,7 @@ void QFmClient::sendSetResponseToPos(bool bFlag)
int nRt = SendSocketData(_acceptedSock,pResponse,st,0); int nRt = SendSocketData(_acceptedSock,pResponse,st,0);
delete []pResponse; delete []pResponse;
if(nRt == 0) if(nRt == 0)
qDebug()<<"send response for set store info failed!"; QLOG_ERROR()<<"send response for set store info failed!";
} }
int QFmClient::Try2ConnectZhProxy() int QFmClient::Try2ConnectZhProxy()
...@@ -668,7 +664,7 @@ int QFmClient::SendCtrlInfo2Proxy() ...@@ -668,7 +664,7 @@ int QFmClient::SendCtrlInfo2Proxy()
if (sendRlt != MAX_CTRL_INFO_LEN) if (sendRlt != MAX_CTRL_INFO_LEN)
{ {
#ifdef WIN32 #ifdef WIN32
qDebug() << "recv() return : " << sendRlt << ";error : " << WSAGetLastError() << ";\r\n"; QLOG_ERROR() << "SendCtrlInfo2Proxy() return : " << sendRlt << ";error : " << WSAGetLastError() << ";\r\n";
#endif #endif
return -1; return -1;
} }
...@@ -709,7 +705,7 @@ int QFmClient::RecvAndCheckDataFromSock(int sock, int flag, int type) //type = ...@@ -709,7 +705,7 @@ int QFmClient::RecvAndCheckDataFromSock(int sock, int flag, int type) //type =
precvBuf = DecodeRecvData(_tempbuf, recbytes, &recbytes); precvBuf = DecodeRecvData(_tempbuf, recbytes, &recbytes);
} }
qDebug() << "received data from proxy " << sock << " :\n " << precvBuf; QLOG_INFO() << "received data from proxy " << sock << " :\n " << precvBuf<< "\n";
while(0 < recbytes) while(0 < recbytes)
{ {
...@@ -717,7 +713,7 @@ int QFmClient::RecvAndCheckDataFromSock(int sock, int flag, int type) //type = ...@@ -717,7 +713,7 @@ int QFmClient::RecvAndCheckDataFromSock(int sock, int flag, int type) //type =
if (((0 == checkCount) && ( '}' != precvBuf[ prosLen + finLen - 1])) || ((MAX_BUF_LEN - 1) < curPos + finLen)) if (((0 == checkCount) && ( '}' != precvBuf[ prosLen + finLen - 1])) || ((MAX_BUF_LEN - 1) < curPos + finLen))
{ {
qDebug() << "checkCount out!\r\n"; QLOG_ERROR() << "checkCount out!\r\n";
return -1; return -1;
} }
...@@ -728,13 +724,11 @@ int QFmClient::RecvAndCheckDataFromSock(int sock, int flag, int type) //type = ...@@ -728,13 +724,11 @@ int QFmClient::RecvAndCheckDataFromSock(int sock, int flag, int type) //type =
if (0 == checkCount) if (0 == checkCount)
{//a complete json data received {//a complete json data received
_recvbuf[curPos] = '\0'; _recvbuf[curPos] = '\0';
_rcvLength = curPos; _rcvLength = curPos;
curPos = 0; curPos = 0;
checkCount = 0; checkCount = 0;
qDebug() << "checkCount = 0"; //qDebug() << "checkCount = 0";
goto finishRecv; goto finishRecv;
} }
...@@ -749,7 +743,7 @@ finishRecv: ...@@ -749,7 +743,7 @@ finishRecv:
return 0; return 0;
} }
int QFmClient::SendData2ZhProxyAndWaitRspData() int QFmClient::SendData2ZhProxy()
{ {
int rlt, length; int rlt, length;
...@@ -757,24 +751,43 @@ int QFmClient::SendData2ZhProxyAndWaitRspData() ...@@ -757,24 +751,43 @@ int QFmClient::SendData2ZhProxyAndWaitRspData()
rlt = SendSocketData(_zhProxySock, _sendbuf, length, _codeFlag); rlt = SendSocketData(_zhProxySock, _sendbuf, length, _codeFlag);
qDebug()<<"_zhProxySock "<<_zhProxySock<< "data:\n"<<_sendbuf; QLOG_INFO()<<"send to proxy:\n "<< _sendbuf;
if (rlt != length) if (rlt != length)
{ {
#ifdef WIN32 #ifdef WIN32
qDebug() << "recv() return : " << rlt << ";error : " << WSAGetLastError() << ";\r\n"; QLOG_ERROR() << "SendData2ZhProxy() return : " << rlt << ";error : " << WSAGetLastError() << ";\r\n";
#endif #endif
qDebug() << "send data to socket failed!\r\n"; QLOG_ERROR() << "send data to socket failed!\r\n";
return -1; return -1;
} }
return 0;
}
qDebug() << "wait and receive data from ZH Proxy...\r\n"; /* 处理proxy 的支付回复数据
* 返回值:
if (0 != RecvAndCheckDataFromSock(_zhProxySock, _codeFlag,0)) * 0 成功
* 204 交易超时或者支付平台异常 进行冲正处理
*/
int QFmClient::ProcessZhProxyRspData(int nType)
{
QByteArray recvdata(_recvbuf);
QJsonParseError json_error;
QJsonDocument doc = QJsonDocument::fromJson(recvdata, &json_error);
if(json_error.error != QJsonParseError::NoError || !doc.isObject())
{ {
qDebug() << "receive data or check data failed\r\n"; QLOG_ERROR() << "error proxy response json data!\r\n " << json_error.error;
return -1; return -1;
} }
QJsonObject object = doc.object();
ConvertResponseJsonFormat(object,nType); //!转换JSON
if(object.contains("status_code") && object.value("status_code").toInt() == 204)
{
return 204;
}
QString str = QString(QJsonDocument(object).toJson(QJsonDocument::Compact));
QByteArray byteArray = str.toLocal8Bit();
const char* temp = byteArray.data();
strcpy(_recvbuf,temp);
return 0; return 0;
} }
...@@ -807,7 +820,7 @@ int QFmClient::ProcessZhProxyRspDataAndSend2Pos(int nType) ...@@ -807,7 +820,7 @@ int QFmClient::ProcessZhProxyRspDataAndSend2Pos(int nType)
length = strlen(_recvbuf); length = strlen(_recvbuf);
//! TODO: 转换JSON //!转换JSON
QByteArray recvdata(_recvbuf); QByteArray recvdata(_recvbuf);
...@@ -855,22 +868,25 @@ void QFmClient::BackupPosReq(char * req) ...@@ -855,22 +868,25 @@ void QFmClient::BackupPosReq(char * req)
void QFmClient::run() void QFmClient::run()
{ {
if(!Init("115.159.63.201", 29534, 34952, 5, 30)) if(!Init("115.159.63.201", 29534, 34952, 30, 5))
{ {
qDebug() << "Init QFmClient failed"; QLOG_ERROR() << "initialise client failed!";
emit Error("Init QFmClient failed"); emit Error("initialise client failed!");
} }
InitStoreInfo(); InitStoreInfo();
qDebug()<< "Init QFmClient success"; QLOG_INFO()<< "initialise client success\n";
bool bBackup = false; //是否需要备份至 "fmclient.rbk"
do do
{ {
CloseSocket(&_acceptedSock); CloseSocket(&_acceptedSock);
CloseSocket(&_listenSock); CloseSocket(&_listenSock);
CheckRollBackData(); CheckRollBackData();
if(!StartListenport()) if(!StartListenport())
{ {
qDebug() << "Start Listen port : " << _clientPort <<" failed";
QLOG_ERROR() << "start listen port : " << _clientPort <<" failed!";
WaitAndSleep(3000); WaitAndSleep(3000);
continue ; continue ;
} }
...@@ -880,6 +896,7 @@ void QFmClient::run() ...@@ -880,6 +896,7 @@ void QFmClient::run()
int nResult = 0; int nResult = 0;
if (!WaitForConnectReqFromPos()) if (!WaitForConnectReqFromPos())
{ {
QLOG_ERROR() << "accept pos connection failed!\r\n";
break; break;
} }
...@@ -890,14 +907,12 @@ void QFmClient::run() ...@@ -890,14 +907,12 @@ void QFmClient::run()
} }
QJsonObject object; QJsonObject object;
char temp_recv[MAX_BUF_LEN] = {0};
//strcpy(temp_recv, _recvbuf);
nResult = ProcessPosReqData(object); nResult = ProcessPosReqData(object);
if (0 > nResult) if (0 > nResult)
{ {
CloseSocket(&_acceptedSock); CloseSocket(&_acceptedSock);
qDebug()<<"ProcessPosReqData error"; QLOG_ERROR()<<"ProcessPosReqData() error";
continue; continue;
} }
if(nResult == SET_STORE_INFO) if(nResult == SET_STORE_INFO)
...@@ -908,7 +923,7 @@ void QFmClient::run() ...@@ -908,7 +923,7 @@ void QFmClient::run()
} }
if (0 != Try2ConnectZhProxy()) if (0 != Try2ConnectZhProxy())
{ {
qDebug() << "connect to proxy failed"; QLOG_ERROR() << "connect to proxy failed!";
CloseSocket(&_acceptedSock); CloseSocket(&_acceptedSock);
continue; continue;
} }
...@@ -921,28 +936,63 @@ void QFmClient::run() ...@@ -921,28 +936,63 @@ void QFmClient::run()
memset(_sendbuf, 0, MAX_BUF_LEN); memset(_sendbuf, 0, MAX_BUF_LEN);
QString temp = QString(QJsonDocument(object).toJson()); QString temp = QString(QJsonDocument(object).toJson());
strcpy(_sendbuf, temp.toStdString().c_str()); strcpy(_sendbuf, temp.toStdString().c_str());
strcpy(temp_recv, _sendbuf);
rlt = SendData2ZhProxyAndWaitRspData(); rlt = SendData2ZhProxy();
//CloseSocket(&_acceptedSock); //if test cerat fmclient.rbk file,uncomment this if(rlt != 0)
if (0 == rlt)
{ {
rlt = ProcessZhProxyRspDataAndSend2Pos(nResult); QLOG_ERROR()<<"send to proxy failed!";
continue;
} }
CloseSocket(&_zhProxySock); QLOG_INFO() << "waiting and receive data from ZH Proxy...\r\n";
if (0 != RecvAndCheckDataFromSock(_zhProxySock, _codeFlag,0)) //进行冲正处理
{
QLOG_ERROR() << "receive data from proxy or check data failed!\r\n";
CloseSocket(&_acceptedSock);
continue;
}
if (0 != rlt) //CloseSocket(&_acceptedSock); //if test cerat fmclient.rbk file,uncomment this
rlt = ProcessZhProxyRspData(nResult);
if(rlt != 0)
{ {
if (nResult == FREEMUD_PAY) QLOG_ERROR()<<"process proxy response data failed!";
if(nResult == FREEMUD_PAY)
{ {
//is write off data, need to check roll back bBackup = true;
BackupPosReq(temp_recv);
break; break;
} }
} }
//rlt = ProcessZhProxyRspDataAndSend2Pos(nResult);
int length = strlen(_sendbuf);
rlt = SendSocketData(_acceptedSock, _recvbuf, length, 0);
if (rlt != length)
{
qDebug() << "send to pos return : " << rlt << ";error : " << WSAGetLastError() << ";\r\n";
if(nResult == FREEMUD_PAY)
{
bBackup = true;
break;
}
}
CloseSocket(&_zhProxySock);
// if (0 != rlt)
// {
// if (nResult == FREEMUD_PAY)
// {
// //is write off data, need to check roll back
// BackupPosReq(temp_recv);
// break;
// }
// }
CloseSocket(&_acceptedSock); CloseSocket(&_acceptedSock);
}while(1); }while(1);
if(bBackup)
{
BackupPosReq(_sendbuf);
QLOG_INFO()<<"backup:\n"<<_sendbuf;
bBackup = false;
}
WaitAndSleep(3000); WaitAndSleep(3000);
}while(!_endflag); }while(!_endflag);
...@@ -987,7 +1037,7 @@ bool QFmClient::StartListenport() ...@@ -987,7 +1037,7 @@ bool QFmClient::StartListenport()
s_add.sin_family=AF_INET; s_add.sin_family=AF_INET;
s_add.sin_addr.s_addr=htonl(INADDR_ANY); s_add.sin_addr.s_addr=htonl(INADDR_ANY);
s_add.sin_port=htons(_clientPort); s_add.sin_port=htons(_clientPort);
qDebug()<<inet_ntoa(s_add.sin_addr)<<" : "<<_clientPort; QLOG_INFO()<<"listen: "<<inet_ntoa(s_add.sin_addr)<<":"<<_clientPort;
setsockopt(_listenSock, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)); setsockopt(_listenSock, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse));
if(-1 == bind(_listenSock,(struct sockaddr *)(&s_add), sizeof(struct sockaddr))) if(-1 == bind(_listenSock,(struct sockaddr *)(&s_add), sizeof(struct sockaddr)))
...@@ -1081,13 +1131,18 @@ void QFmClient::CheckRollBackData() ...@@ -1081,13 +1131,18 @@ void QFmClient::CheckRollBackData()
continue; continue;
} }
if (0 != SendData2ZhProxyAndWaitRspData()) if (0 != SendData2ZhProxy())
{ {
qDebug() << "roll back data: send data to proxy fail, will try after 5 seconds...\r\n"; qDebug() << "roll back data: send data to proxy fail, will try after 5 seconds...\r\n";
continue; continue;
} }
qDebug() << "wait and receive data from ZH Proxy...\r\n";
if (0 != RecvAndCheckDataFromSock(_zhProxySock, _codeFlag,0))
{
qDebug() << "receive data or check data failed\r\n";
continue;
}
break; break;
}while(1); }while(1);
......
...@@ -54,7 +54,8 @@ private: ...@@ -54,7 +54,8 @@ private:
int CheckReqFromPos(char *pJsonData, int dLength, int chkValue); int CheckReqFromPos(char *pJsonData, int dLength, int chkValue);
int SendSocketData(int sock, char *buf, int length, int flag); int SendSocketData(int sock, char *buf, int length, int flag);
char *EncodeSendData(char *buf, int length, int *eLength); char *EncodeSendData(char *buf, int length, int *eLength);
int SendData2ZhProxyAndWaitRspData(); int SendData2ZhProxy();
int ProcessZhProxyRspData(int nType);
int RecvAndCheckDataFromSock(int sock, int flag, int type); int RecvAndCheckDataFromSock(int sock, int flag, int type);
int ProcessZhProxyRspDataAndSend2Pos(int nType); int ProcessZhProxyRspDataAndSend2Pos(int nType);
int CheckRecvedData(); int CheckRecvedData();
......
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