Browse Source

解决由于memset数组m_szReceiveBuff时,用错大小(1024变成了BUFFSIZE)导致m_IoRead被动重置为0的问题。

Jeff 3 years ago
parent
commit
2f57e59151
2 changed files with 47 additions and 15 deletions
  1. 43 11
      Source/OGCAssist/OGCAssist/PipeClient.cpp
  2. 4 4
      Source/OGCAssist/OGCAssist/PipeClient.h

+ 43 - 11
Source/OGCAssist/OGCAssist/PipeClient.cpp

@@ -2,6 +2,9 @@
 #include "PipeClient.h"
 #include "Utility.h"
 
+PER_IO_CONTEXT CPipeClient::m_IoRead;
+PER_IO_CONTEXT CPipeClient::m_IoWrite;
+
 CPipeClient::CPipeClient(LPCTSTR lpPipeName, DWORD dwMode)
 {
     m_hReadInst = INVALID_HANDLE_VALUE;
@@ -126,25 +129,26 @@ DWORD CPipeClient::ReadMsgThread(LPVOID lpParam)
             continue;
         }
 
-        do 
+        //do 
         { 
             bSuccess = ReadFile( 
                 pInstance->m_hReadInst,						// pipe handle 
-                pInstance->m_IoRead.szBuffer,				// buffer to receive reply 
+                m_IoRead.szBuffer,				// buffer to receive reply 
                 BUFSIZE,									// size of buffer 
-                &cbRead,									// number of bytes read 
+                &m_IoRead.dwBufferSize,			// number of bytes read 
                 (OVERLAPPED*)&pInstance->m_IoRead);			// not overlapped 
 
-			if ( !bSuccess && (dwError = GetLastError()) != ERROR_MORE_DATA )
-				break; 
 
+			dwError = GetLastError();
+			//if ( !bSuccess && (dwError = GetLastError()) != ERROR_MORE_DATA )
+			//	break; 
 
-			if ( WaitFinish(pInstance->m_hReadInst, &pInstance->m_IoRead) )
+			if ( WaitFinish(pInstance->m_hReadInst, &m_IoRead) )
 			{
 
 			}
             
-            Utility::dprintf(_T("读取数据%d:%ld, %ld, %s"), i++, dwError, pInstance->m_IoRead.m_Overlapped.InternalHigh, pInstance->m_IoRead.szBuffer);
+            Utility::dprintf(_T("读取数据:Error=%ld, Len=%ld, Data=%s\n"), dwError, m_IoRead.m_Overlapped.InternalHigh, m_IoRead.szBuffer);
             //Utility::dprintf(_T("读取数据:%ld, %ld"), dwError, cbRead);
 #if 0
             TCHAR szMsg[8912] = {0};
@@ -154,11 +158,12 @@ DWORD CPipeClient::ReadMsgThread(LPVOID lpParam)
             // 追回数据;
             memcpy(pInstance->m_szReceiveBuff + dwDataIndex, chBuf, cbRead);
             dwDataIndex += cbRead;
-        } while ( !bSuccess );  // repeat loop if ERROR_MORE_DATA 
+        } //while ( !bSuccess );  // repeat loop if ERROR_MORE_DATA 
 
         // 清空缓存数据;
         dwDataIndex = 0;
         memset(chBuf, 0, BUFSIZE*sizeof(TCHAR));
+		memset(m_IoRead.szBuffer, 0, BUFSIZE);
        
         if ( bSuccess )
         {
@@ -171,9 +176,10 @@ DWORD CPipeClient::ReadMsgThread(LPVOID lpParam)
         }
         else
         {
-            Utility::dprintf(_T("ReadFile from pipe failed. GLE=%d\n"), dwError );
+            //Utility::dprintf(_T("ReadFile from pipe failed. GLE=%d\n"), dwError );
             if ( dwError == ERROR_PIPE_NOT_CONNECTED || dwError == ERROR_BROKEN_PIPE)
             {
+				Utility::dprintf("CloseHandle\n");
                 CloseHandle(pInstance->m_hReadInst);
                 pInstance->m_hReadInst = INVALID_HANDLE_VALUE;
             }
@@ -193,6 +199,7 @@ DWORD CPipeClient::ReadMsgThread(LPVOID lpParam)
 
 BOOL CPipeClient::WaitFinish(HANDLE hPipe, PER_IO_CONTEXT *pIoContext)
 {
+#if 1
 	bool bPendingIO = false;
 	switch(GetLastError())
 	{
@@ -217,7 +224,7 @@ BOOL CPipeClient::WaitFinish(HANDLE hPipe, PER_IO_CONTEXT *pIoContext)
 		if (bPendingIO)
 		{
 			// 获取Overlapped结果;
-			if( GetOverlappedResult(hPipe, &pIoContext->m_Overlapped, &dwTransBytes, FALSE) == FALSE)
+			if( GetOverlappedResult(hPipe, &pIoContext->m_Overlapped, &dwTransBytes, TRUE) == FALSE)
 			{
 				printf("ConnectNamedPipe  failed   %d\n",GetLastError());
 				return -1;
@@ -230,6 +237,30 @@ BOOL CPipeClient::WaitFinish(HANDLE hPipe, PER_IO_CONTEXT *pIoContext)
 	}
 
 	return 0;
+#else
+	DWORD dwError=::GetLastError();
+	DWORD dwWait = -1;
+	DWORD dwTransBytes = -1;
+	WaitForSingleObject(pIoContext->m_Overlapped.hEvent, INFINITE); // 等待异步操作完成;
+	if (dwError == ERROR_IO_PENDING) {
+		while (!::GetOverlappedResult(hPipe, &pIoContext->m_Overlapped, &dwTransBytes, false)) {
+			dwError = ::GetLastError();
+			if ( dwError == ERROR_IO_PENDING ) {
+				Sleep(50);
+				//Utility::dprintf("读等待:%ld", dwError);
+			} else if ( dwError == ERROR_SUCCESS || dwError == ERROR_IO_INCOMPLETE ) {
+				//Utility::dprintf("读完成:%ld,%ld,%ld", dwError, dwTransBytes, pIoContext->m_Overlapped.InternalHigh);
+				break;
+			} else {
+				//Utility::dprintf("读错误:%ld", dwError);
+				dwTransBytes = 0;
+				return 0;
+			}
+		}
+	}
+
+	return 1;
+#endif
 }
 
 BOOL CPipeClient::SendMessage(PACKAGE &pak)
@@ -252,7 +283,8 @@ BOOL CPipeClient::SendData(const TCHAR *lpszMsg, DWORD dwDataLen)
 	DWORD dwNumberOfBytesWritten = 0;
 	char szMsg[255] = {0};//"你好----001";
 	sprintf(szMsg, "发送内容:%d,%d", ::GetCurrentProcessId(), i++);
-	BOOL fWrite = WriteFile(m_hReadInst,szMsg,strlen(szMsg),&dwNumberOfBytesWritten,NULL);
+	BOOL fWrite = WriteFile(m_hReadInst,szMsg,strlen(szMsg),&dwNumberOfBytesWritten, NULL);//;&m_IoWrite.m_Overlapped);
+	//WaitFinish(m_hReadInst, &m_IoWrite);
 	if ( fWrite )
 	{
 		Utility::dprintf(_T("SendData:%s\n"),lpszMsg);

+ 4 - 4
Source/OGCAssist/OGCAssist/PipeClient.h

@@ -45,11 +45,11 @@ private:
     // ¹ÜµÀͨÐÅģʽ;
     DWORD m_dwMode;
     // »º´æÇø;
-    TCHAR m_szWriteBuff[1024];
-    TCHAR m_szReceiveBuff[1024];
+    TCHAR m_szWriteBuff[BUFSIZE];
+    TCHAR m_szReceiveBuff[BUFSIZE];
 
-	PER_IO_CONTEXT m_IoRead;
-	PER_IO_CONTEXT m_IoWrite;
+	static PER_IO_CONTEXT m_IoRead;
+	static PER_IO_CONTEXT m_IoWrite;
 public:
     BOOL StartWork();
     void StopWork() { m_bClientStop = TRUE;Sleep(100000);}