Commit d0ebe816 by 刘帅

1. 优化流程

2. 整理文件和代码
parent e391071b
......@@ -2,4 +2,5 @@
/bin/*
!/bin/*.dll
!/bin/*.ini
......@@ -2,7 +2,7 @@ INCLUDEPATH += $$PWD
#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_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 \
$$PWD/QsLog.cpp \
$$PWD/QsLogDestConsole.cpp \
......
[store]
partner_id=1371
store_id=fm99999
pos_id=1
operator_id=001
business_date=20171016
......@@ -2,6 +2,7 @@
#include <QJsonArray>
#include <QJsonDocument>
#include <QDebug>
#include "QsLog/QsLog.h"
Convert::Convert()
{
......@@ -70,7 +71,7 @@ bool Convert::QueryPay(QJsonObject &object, STORE_INFO store_info)
}
else
{
qDebug()<<"can not find trans_id in json request!";
QLOG_ERROR()<<"can not find trans_id in json request!";
return false;
}
if(object.contains("fm_order_id"))
......
......@@ -30,8 +30,8 @@ static void InitLogger()
QsLogging::MaxOldLogCount(logCount)));
logger.addDestination(fileDestination);
/* 输出到控制台 */
// QsLogging::DestinationPtr consleDest(QsLogging::DestinationFactory::MakeDebugOutputDestination());
// logger.addDestination(consleDest);
QsLogging::DestinationPtr consleDest(QsLogging::DestinationFactory::MakeDebugOutputDestination());
logger.addDestination(consleDest);
}
#endif // LOG
......
......@@ -9,7 +9,7 @@ int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
InitLogger();
QLOG_INFO()<<"start";
QLOG_INFO()<<"start client...";
QFmClient client;
client.start();
return a.exec();
......
......@@ -145,17 +145,13 @@ bool QFmClient::WaitForConnectReqFromPos()
RecvSockData(_acceptedSock, _recvbuf, (MAX_BUF_LEN - 1));
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);
if(-1 == _acceptedSock)
{
qDebug() << "accept socket fail!\r\n";
{
return false;
}
qDebug() << "accept ok, ZF Client get connection from POS!\r\n";
QLOG_INFO() << "accept pos connection ok!\r\n";
return true;
}
......@@ -279,7 +275,7 @@ char * QFmClient::DecodeRecvData(char * buf, int length, int * dLength)
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;
......@@ -307,7 +303,7 @@ int QFmClient::WaitAndReadDataFromPos(int type, int flag)
//recbytes = GBKToUTF8( (unsigned char *)_sendbuf, recbytes, (unsigned char *)_tempbuf, MAX_BUF_LEN - 100);
}
#endif
qDebug() << "received data from pos(socket " << _acceptedSock << ") : \n" << _tempbuf;
QLOG_INFO() << "received data from pos(socket " << _acceptedSock << ") : \n" << _tempbuf;
precvBuf = _tempbuf;
_tempbuf[recbytes] = '\0';
......@@ -321,7 +317,7 @@ int QFmClient::WaitAndReadDataFromPos(int type, int flag)
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;
}
......@@ -336,7 +332,7 @@ int QFmClient::WaitAndReadDataFromPos(int type, int flag)
_rcvLength = curPos;
curPos = 0;
checkCount = 0;
qDebug() << "checkCount = 0";
//qDebug() << "checkCount = 0";
goto finishRecv;
}
......@@ -401,7 +397,7 @@ int QFmClient::ProcessPosReqData(QJsonObject& object)
{
SetStoreInfo(object);
}
qDebug()<<"convert json format";
//qDebug()<<"convert json format";
convert.FreemudPay(object,store_info);
_sockTimeOut = _longTimeOut;
AddReqCount(object);
......@@ -424,11 +420,11 @@ int QFmClient::ProcessPosReqData(QJsonObject& object)
qDebug()<<"unknown command: "<<fm_cmd;
break;
}
qDebug()<<"client request json:\n"<<object;
//QLOG_INFO()<<"client request json:\n"<<object;
}
else
{
qDebug()<<"unknown command(can not find fm_cmd section) ";
QLOG_ERROR()<<"unknown command(can not find fm_cmd section) ";
nResult = -1;
}
return nResult;
......@@ -442,7 +438,7 @@ int QFmClient::ProcessPosReqData(QJsonObject& object)
bool QFmClient::ConvertResponseJsonFormat(QJsonObject &object, int nType)
{
bool bRtValue = true;
qDebug()<<"proxy response json:\n"<<object;
//qDebug()<<"proxy response json:\n"<<object;
switch(nType)
{
case SET_STORE_INFO:
......@@ -529,7 +525,7 @@ void QFmClient::sendSetResponseToPos(bool bFlag)
int nRt = SendSocketData(_acceptedSock,pResponse,st,0);
delete []pResponse;
if(nRt == 0)
qDebug()<<"send response for set store info failed!";
QLOG_ERROR()<<"send response for set store info failed!";
}
int QFmClient::Try2ConnectZhProxy()
......@@ -668,7 +664,7 @@ int QFmClient::SendCtrlInfo2Proxy()
if (sendRlt != MAX_CTRL_INFO_LEN)
{
#ifdef WIN32
qDebug() << "recv() return : " << sendRlt << ";error : " << WSAGetLastError() << ";\r\n";
QLOG_ERROR() << "SendCtrlInfo2Proxy() return : " << sendRlt << ";error : " << WSAGetLastError() << ";\r\n";
#endif
return -1;
}
......@@ -709,7 +705,7 @@ int QFmClient::RecvAndCheckDataFromSock(int sock, int flag, int type) //type =
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)
{
......@@ -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))
{
qDebug() << "checkCount out!\r\n";
QLOG_ERROR() << "checkCount out!\r\n";
return -1;
}
......@@ -728,13 +724,11 @@ int QFmClient::RecvAndCheckDataFromSock(int sock, int flag, int type) //type =
if (0 == checkCount)
{//a complete json data received
_recvbuf[curPos] = '\0';
_rcvLength = curPos;
curPos = 0;
checkCount = 0;
qDebug() << "checkCount = 0";
//qDebug() << "checkCount = 0";
goto finishRecv;
}
......@@ -749,7 +743,7 @@ finishRecv:
return 0;
}
int QFmClient::SendData2ZhProxyAndWaitRspData()
int QFmClient::SendData2ZhProxy()
{
int rlt, length;
......@@ -757,24 +751,43 @@ int QFmClient::SendData2ZhProxyAndWaitRspData()
rlt = SendSocketData(_zhProxySock, _sendbuf, length, _codeFlag);
qDebug()<<"_zhProxySock "<<_zhProxySock<< "data:\n"<<_sendbuf;
QLOG_INFO()<<"send to proxy:\n "<< _sendbuf;
if (rlt != length)
{
#ifdef WIN32
qDebug() << "recv() return : " << rlt << ";error : " << WSAGetLastError() << ";\r\n";
QLOG_ERROR() << "SendData2ZhProxy() return : " << rlt << ";error : " << WSAGetLastError() << ";\r\n";
#endif
qDebug() << "send data to socket failed!\r\n";
QLOG_ERROR() << "send data to socket failed!\r\n";
return -1;
}
return 0;
}
qDebug() << "wait and receive data from ZH Proxy...\r\n";
if (0 != RecvAndCheckDataFromSock(_zhProxySock, _codeFlag,0))
/* 处理proxy 的支付回复数据
* 返回值:
* 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;
}
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;
}
......@@ -807,7 +820,7 @@ int QFmClient::ProcessZhProxyRspDataAndSend2Pos(int nType)
length = strlen(_recvbuf);
//! TODO: 转换JSON
//!转换JSON
QByteArray recvdata(_recvbuf);
......@@ -855,22 +868,25 @@ void QFmClient::BackupPosReq(char * req)
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";
emit Error("Init QFmClient failed");
QLOG_ERROR() << "initialise client failed!";
emit Error("initialise client failed!");
}
InitStoreInfo();
qDebug()<< "Init QFmClient success";
QLOG_INFO()<< "initialise client success\n";
bool bBackup = false; //是否需要备份至 "fmclient.rbk"
do
{
CloseSocket(&_acceptedSock);
CloseSocket(&_listenSock);
CheckRollBackData();
if(!StartListenport())
{
qDebug() << "Start Listen port : " << _clientPort <<" failed";
QLOG_ERROR() << "start listen port : " << _clientPort <<" failed!";
WaitAndSleep(3000);
continue ;
}
......@@ -880,6 +896,7 @@ void QFmClient::run()
int nResult = 0;
if (!WaitForConnectReqFromPos())
{
QLOG_ERROR() << "accept pos connection failed!\r\n";
break;
}
......@@ -890,14 +907,12 @@ void QFmClient::run()
}
QJsonObject object;
char temp_recv[MAX_BUF_LEN] = {0};
//strcpy(temp_recv, _recvbuf);
nResult = ProcessPosReqData(object);
if (0 > nResult)
{
CloseSocket(&_acceptedSock);
qDebug()<<"ProcessPosReqData error";
QLOG_ERROR()<<"ProcessPosReqData() error";
continue;
}
if(nResult == SET_STORE_INFO)
......@@ -908,7 +923,7 @@ void QFmClient::run()
}
if (0 != Try2ConnectZhProxy())
{
qDebug() << "connect to proxy failed";
QLOG_ERROR() << "connect to proxy failed!";
CloseSocket(&_acceptedSock);
continue;
}
......@@ -921,28 +936,63 @@ void QFmClient::run()
memset(_sendbuf, 0, MAX_BUF_LEN);
QString temp = QString(QJsonDocument(object).toJson());
strcpy(_sendbuf, temp.toStdString().c_str());
strcpy(temp_recv, _sendbuf);
rlt = SendData2ZhProxyAndWaitRspData();
//CloseSocket(&_acceptedSock); //if test cerat fmclient.rbk file,uncomment this
if (0 == rlt)
rlt = SendData2ZhProxy();
if(rlt != 0)
{
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
BackupPosReq(temp_recv);
bBackup = true;
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);
}while(1);
if(bBackup)
{
BackupPosReq(_sendbuf);
QLOG_INFO()<<"backup:\n"<<_sendbuf;
bBackup = false;
}
WaitAndSleep(3000);
}while(!_endflag);
......@@ -987,7 +1037,7 @@ bool QFmClient::StartListenport()
s_add.sin_family=AF_INET;
s_add.sin_addr.s_addr=htonl(INADDR_ANY);
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));
if(-1 == bind(_listenSock,(struct sockaddr *)(&s_add), sizeof(struct sockaddr)))
......@@ -1081,13 +1131,18 @@ void QFmClient::CheckRollBackData()
continue;
}
if (0 != SendData2ZhProxyAndWaitRspData())
if (0 != SendData2ZhProxy())
{
qDebug() << "roll back data: send data to proxy fail, will try after 5 seconds...\r\n";
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;
}while(1);
......
......@@ -54,7 +54,8 @@ private:
int CheckReqFromPos(char *pJsonData, int dLength, int chkValue);
int SendSocketData(int sock, char *buf, int length, int flag);
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 ProcessZhProxyRspDataAndSend2Pos(int nType);
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