Commit 886fbb67 by 刘帅

1. 捕获退出消息,正确停止线程,进行清理工作

2. 修改socket属性设置SO_RESUEADDR
parent d0ebe816
......@@ -3,4 +3,4 @@ partner_id=1371
store_id=fm99999
pos_id=1
operator_id=001
business_date=20171016
business_date=20171205
......@@ -7,7 +7,7 @@ typedef struct _STORE_INFO{
QString store_id; //商家门店号
QString pos_id; //商家POS机编号
QString operator_id; //营业员编号
QString business_date; //营业日
QString business_date; //营业日
_STORE_INFO(QString _partner_id,
QString _store_id,
QString _pos_id,
......
......@@ -4,14 +4,36 @@
//不显示控制台
//#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
BOOL WINAPI HandlerRoutine(_In_ DWORD dwCtrlType);
QFmClient* pFm = nullptr;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
SetConsoleCtrlHandler(HandlerRoutine, TRUE);
InitLogger();
QLOG_INFO()<<"start client...";
QFmClient client;
pFm = &client;
client.start();
return a.exec();
}
BOOL WINAPI HandlerRoutine(_In_ DWORD dwCtrlType)
{
switch (dwCtrlType)
{
// Handle the CTRL + C signal.
case CTRL_C_EVENT:
case CTRL_CLOSE_EVENT:
OutputDebugStringA("quit client...\n");
pFm->CloseSock();
pFm->wait();
QLOG_INFO()<<"quit client...\n\n\n\n";
break;
// CTRL-CLOSE: confirm that the user wants to exit.
default:
break;
}
return FALSE;
}
......@@ -40,6 +40,11 @@ QFmClient::QFmClient(QObject *parent) : QThread(parent),store_info("1371","fm999
_endflag = false;
}
QFmClient::~QFmClient()
{
OutputDebugStringA("~QFmClient()");
}
bool QFmClient::Init(QString proxy_IP, unsigned short proxy_port, unsigned short listen_prot, unsigned short long_timeout, unsigned short short_timeout)
{
......@@ -116,6 +121,14 @@ bool QFmClient::InitStoreInfo()
return true;
}
void QFmClient::CloseSock()
{
_endflag = true;
closesocket(_listenSock);
requestInterruption();
OutputDebugStringA("close listen socket");
}
int QFmClient::RecvSockData(int sock, char *buffer, int length)
{
#ifdef WIN32
......@@ -148,7 +161,7 @@ bool QFmClient::WaitForConnectReqFromPos()
QLOG_INFO()<<"waiting for connection from pos...\n";
_acceptedSock = accept(_listenSock, (struct sockaddr *)(&c_add), &sin_size);
if(-1 == _acceptedSock)
{
{
return false;
}
QLOG_INFO() << "accept pos connection ok!\r\n";
......@@ -778,7 +791,7 @@ int QFmClient::ProcessZhProxyRspData(int nType)
QLOG_ERROR() << "error proxy response json data!\r\n " << json_error.error;
return -1;
}
QJsonObject object = doc.object();
QJsonObject object = doc.object();
ConvertResponseJsonFormat(object,nType); //!转换JSON
if(object.contains("status_code") && object.value("status_code").toInt() == 204)
{
......@@ -896,7 +909,7 @@ void QFmClient::run()
int nResult = 0;
if (!WaitForConnectReqFromPos())
{
QLOG_ERROR() << "accept pos connection failed!\r\n";
QLOG_ERROR() << "accept pos connection failed!\r\n";
break;
}
......@@ -971,20 +984,11 @@ void QFmClient::run()
qDebug() << "send to pos return : " << rlt << ";error : " << WSAGetLastError() << ";\r\n";
if(nResult == FREEMUD_PAY)
{
bBackup = true;
break;
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)
......@@ -993,10 +997,12 @@ void QFmClient::run()
QLOG_INFO()<<"backup:\n"<<_sendbuf;
bBackup = false;
}
WaitAndSleep(3000);
}while(!_endflag);
if(_endflag)
WaitAndSleep(3000);
}while(!isInterruptionRequested());
qDebug() << "Exit ZH Client!\r\n";
QLOG_INFO()<< "Exit ZH Client!\r\n";
#ifdef WIN32
WSACleanup();
......
......@@ -23,9 +23,11 @@ class QFmClient : public QThread
Q_OBJECT
public:
QFmClient(QObject *parent = NULL);
~QFmClient();
void run();
bool Init(QString proxy_IP, unsigned short proxy_port, unsigned short listen_prot, unsigned short long_timeout, unsigned short short_timeout);
bool InitStoreInfo();
void CloseSock();
signals:
void Error(QString error);
......
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