From 01ce2b3263339d6f34766ca7be28ded6d8f2439f Mon Sep 17 00:00:00 2001 From: bestlqiang Date: Wed, 31 Mar 2021 10:29:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BF=98=E8=AE=B0=E5=B1=8F=E8=94=BD=E5=89=AF?= =?UTF-8?q?=E6=9C=BA,=E4=B8=94=E5=89=AF=E6=9C=BA=E6=B2=A1=E5=BC=80?= =?UTF-8?q?=E6=97=B6.=E4=B8=8D=E4=BC=9A=E5=86=8D=E9=98=BB=E5=A1=9E?= =?UTF-8?q?=E5=8D=A1=E6=AD=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LaiPuLaser/ClientMgr.cpp | 43 ++++++++++++++++++++++++++++++++++++++++ LaiPuLaser/ClientMgr.h | 13 ++++++++++++ 2 files changed, 56 insertions(+) diff --git a/LaiPuLaser/ClientMgr.cpp b/LaiPuLaser/ClientMgr.cpp index 80b3668..2ecc5cc 100644 --- a/LaiPuLaser/ClientMgr.cpp +++ b/LaiPuLaser/ClientMgr.cpp @@ -53,6 +53,7 @@ void CClientMgr::DisConnectServer() if (m_hSocket!=INVALID_SOCKET) { Close(); + m_bOnline = false; } } @@ -322,6 +323,48 @@ void CClientMgr::OnConnect(int nErrorCode) CSocket::OnConnect(nErrorCode); } +BOOL CClientMgr::Connect(LPCTSTR lpszHostAddress, UINT nHostPort) +{ + SetTimeOut(); + auto ret = CSocket::Connect(lpszHostAddress, nHostPort); + KillTimeOut(); + return ret; +} + +BOOL CClientMgr::SetTimeOut(UINT uTimeOut) +{ + LARGE_INTEGER llCnt; + ::QueryPerformanceCounter(&llCnt); + m_llDtStart = llCnt.QuadPart; + m_uTimeOut = uTimeOut; + return TRUE; +} + +BOOL CClientMgr::KillTimeOut() +{ + m_llDtStart = 0;//表明取消计时 + return TRUE; +} + +BOOL CClientMgr::OnMessagePending() +{ + if (m_llDtStart) + { + LARGE_INTEGER lldtEnd; + ::QueryPerformanceCounter(&lldtEnd); + LARGE_INTEGER llFrq; + ::QueryPerformanceFrequency(&llFrq); + double dbDealy = (double)(lldtEnd.QuadPart - m_llDtStart) * 1000 / llFrq.QuadPart; + if (dbDealy > m_uTimeOut) + { + CancelBlockingCall();//退出阻塞的函数 + AfxMessageBox("网络超时!"); + return FALSE; // No need for idle time processing. + } + } + return CSocket::OnMessagePending(); +} + bool CClientMgr::CheckServerDoneCmd() { int waitCnt = 0; diff --git a/LaiPuLaser/ClientMgr.h b/LaiPuLaser/ClientMgr.h index 66fc40a..96c5808 100644 --- a/LaiPuLaser/ClientMgr.h +++ b/LaiPuLaser/ClientMgr.h @@ -61,6 +61,19 @@ private: virtual void OnReceive(int nErrorCode); virtual void OnClose(int nErrorCode); virtual void OnConnect(int nErrorCode); + + //start--------------设定发送超时--------------------- +private: + //virtual int Send(const void* lpBuf, int nBufLen, int nFlags = 0) override; //重写 加入超时 + virtual BOOL Connect(LPCTSTR lpszHostAddress, UINT nHostPort); //重写 加入超时 + BOOL SetTimeOut(UINT uTimeOut = 500);//ms级延时 + BOOL KillTimeOut(); + virtual BOOL OnMessagePending() override; + LONGLONG m_llDtStart; + UINT m_uTimeOut; + + //end--------------设定读写超时相关--------------------- + void SendCmd_TransFile(CString filePath);//给伺服端传文件(复制到共享盘,无通信) void DoEvents(); bool m_bOnline = false;