PipeClient.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #include "StdAfx.h"
  2. #include "PipeClient.h"
  3. #include "Utility.h"
  4. #define BUFSIZE 512
  5. CPipeClient::CPipeClient(LPCTSTR lpPipeName, DWORD dwMode)
  6. {
  7. m_hPipeInst = INVALID_HANDLE_VALUE;
  8. m_bClientStop = FALSE;
  9. m_dwMode = dwMode;
  10. memset(m_szPipeName, 0, MAX_PATH*sizeof(TCHAR));
  11. memset(m_szWriteBuff, 0, 1024*sizeof(TCHAR));
  12. memset(m_szReceiveBuff, 0, 1024*sizeof(TCHAR));
  13. if ( lpPipeName )
  14. _stprintf_s(m_szPipeName, _T("%s"), lpPipeName);
  15. }
  16. CPipeClient::~CPipeClient(void)
  17. {
  18. StopWork();
  19. if ( m_hPipeInst != INVALID_HANDLE_VALUE )
  20. CloseHandle(m_hPipeInst);
  21. }
  22. BOOL CPipeClient::StartWork()
  23. {
  24. if ( !m_bClientStop )
  25. {
  26. BOOL bRet = TRUE;
  27. HANDLE hConnect = CreateThread(NULL, 0, ConnectThread, this, 0, NULL);
  28. HANDLE hReadMsg = CreateThread(NULL, 0, ReadMsgThread, this, 0, NULL);
  29. if ( hConnect == NULL || hReadMsg == NULL )
  30. bRet = FALSE;
  31. if ( hConnect )
  32. CloseHandle(hConnect);
  33. if ( hReadMsg )
  34. CloseHandle(hReadMsg);
  35. return bRet;
  36. }
  37. return TRUE;
  38. }
  39. DWORD CPipeClient::ConnectThread(LPVOID lpParam)
  40. {
  41. CPipeClient *pInstance = (CPipeClient*)lpParam;
  42. if ( !pInstance )
  43. return 0L;
  44. while(!pInstance->m_bClientStop)
  45. {
  46. if ( pInstance->m_hPipeInst != INVALID_HANDLE_VALUE ) {
  47. // 1分钟检测;
  48. Sleep(60000);
  49. Utility::dprintf(_T("m_hPipeInst 已存在\n"));
  50. continue;
  51. }
  52. // 等待10秒;
  53. if ( !WaitNamedPipe(pInstance->m_szPipeName, 10000) )
  54. {// 如果管道不存在,会立即返回而不考虑超时值,所以此处仍要Sleep;
  55. Utility::dprintf(_T("<%ld> WaitNamedPipe 失败\n"), Utility::g_WndInfo.dwProcessId);
  56. Sleep(10000);
  57. continue;
  58. }
  59. pInstance->m_hPipeInst = CreateFile(
  60. pInstance->m_szPipeName, // pipe name
  61. GENERIC_READ | GENERIC_WRITE, // read and write access
  62. 0, // no sharing
  63. NULL, // default security attributes
  64. OPEN_EXISTING, // opens existing pipe
  65. 0, // default attributes
  66. NULL); // no template file
  67. // 创建成功,退出;
  68. if ( pInstance->m_hPipeInst != INVALID_HANDLE_VALUE )
  69. {
  70. // 管道连接成功,修改管道通信模式:message-read mode.
  71. BOOL fSuccess = SetNamedPipeHandleState(
  72. pInstance->m_hPipeInst, // pipe handle
  73. &pInstance->m_dwMode, // new pipe mode
  74. NULL, // don't set maximum bytes
  75. NULL); // don't set maximum time
  76. if (!fSuccess) {
  77. Utility::dprintf(_T("SetNamedPipeHandleState failed. GLE=%d\n"), GetLastError() );
  78. CloseHandle(pInstance->m_hPipeInst);
  79. }
  80. }
  81. else
  82. {
  83. // Exit if an error other than ERROR_PIPE_BUSY occurs.
  84. if ( GetLastError() != ERROR_PIPE_BUSY )
  85. {
  86. Utility::dprintf(_T("Could not open pipe. GLE=%d\n"), GetLastError() );
  87. }
  88. }
  89. }
  90. Utility::dprintf(_T("<%ld> ConnectThread 退出\n"), Utility::g_WndInfo.dwProcessId);
  91. return 0;
  92. }
  93. DWORD CPipeClient::ReadMsgThread(LPVOID lpParam)
  94. {
  95. DWORD cbRead = 0;
  96. BOOL bSuccess = FALSE;
  97. TCHAR chBuf[BUFSIZE];
  98. DWORD dwDataIndex = 0;
  99. DWORD dwError = 0;
  100. CPipeClient *pInstance = (CPipeClient*)lpParam;
  101. if ( !pInstance )
  102. return 0L;
  103. while(!pInstance->m_bClientStop)
  104. {
  105. if ( pInstance->m_hPipeInst == INVALID_HANDLE_VALUE ) {
  106. Sleep(2000);
  107. continue;
  108. }
  109. do
  110. {
  111. bSuccess = ReadFile(
  112. pInstance->m_hPipeInst, // pipe handle
  113. chBuf, // buffer to receive reply
  114. BUFSIZE*sizeof(TCHAR), // size of buffer
  115. &cbRead, // number of bytes read
  116. NULL); // not overlapped
  117. if ( !bSuccess && (dwError = GetLastError()) != ERROR_MORE_DATA )
  118. break;
  119. Utility::dprintf(_T("读取数据:%ld, %ld, %s"), dwError, cbRead, chBuf);
  120. //Utility::dprintf(_T("读取数据:%ld, %ld"), dwError, cbRead);
  121. #if 0
  122. TCHAR szMsg[8912] = {0};
  123. _stprintf_s(szMsg, _T("读取数据:%d, %ld, %ld, %s\n"), (int)bSuccess, dwError, cbRead, chBuf);
  124. OutputDebugString(szMsg);
  125. #endif
  126. // 追回数据;
  127. memcpy(pInstance->m_szReceiveBuff + dwDataIndex, chBuf, cbRead);
  128. dwDataIndex += cbRead;
  129. Sleep(50);
  130. } while ( !bSuccess ); // repeat loop if ERROR_MORE_DATA
  131. // 清空缓存数据;
  132. dwDataIndex = 0;
  133. memset(chBuf, 0, BUFSIZE*sizeof(TCHAR));
  134. if ( bSuccess )
  135. {
  136. Utility::dprintf(_T("读取到的消息=%d"), sizeof(pInstance->m_szReceiveBuff));
  137. Sleep(3000);
  138. Utility::dprintf(_T("读取到的消息=%s"), pInstance->m_szReceiveBuff);
  139. // 消息处理;
  140. // ...
  141. }
  142. else
  143. {
  144. Utility::dprintf(_T("ReadFile from pipe failed. GLE=%d\n"), dwError );
  145. if ( dwError == ERROR_PIPE_NOT_CONNECTED || dwError == ERROR_BROKEN_PIPE)
  146. {
  147. CloseHandle(pInstance->m_hPipeInst);
  148. pInstance->m_hPipeInst = INVALID_HANDLE_VALUE;
  149. }
  150. #ifdef _DEBUG
  151. Sleep(10000);
  152. #endif
  153. }
  154. memset(pInstance->m_szReceiveBuff, 0, BUFSIZE*sizeof(TCHAR));
  155. }
  156. Utility::dprintf(_T("<%ld> ReadMsgThread 退出\n"),Utility::g_WndInfo.dwProcessId);
  157. return 0;
  158. }