|
@@ -7,12 +7,10 @@ PER_IO_CONTEXT CPipeClient::m_IoWrite;
|
|
|
|
|
|
CPipeClient::CPipeClient(LPCTSTR lpPipeName, DWORD dwMode)
|
|
|
{
|
|
|
- m_hReadInst = INVALID_HANDLE_VALUE;
|
|
|
+ m_hPipeInst = INVALID_HANDLE_VALUE;
|
|
|
m_bClientStop = FALSE;
|
|
|
m_dwMode = dwMode;
|
|
|
memset(m_szPipeName, 0, MAX_PATH*sizeof(TCHAR));
|
|
|
- memset(m_szWriteBuff, 0, 1024*sizeof(TCHAR));
|
|
|
- memset(m_szReceiveBuff, 0, 1024*sizeof(TCHAR));
|
|
|
if ( lpPipeName )
|
|
|
_stprintf_s(m_szPipeName, _T("%s"), lpPipeName);
|
|
|
}
|
|
@@ -20,8 +18,8 @@ CPipeClient::CPipeClient(LPCTSTR lpPipeName, DWORD dwMode)
|
|
|
CPipeClient::~CPipeClient(void)
|
|
|
{
|
|
|
StopWork();
|
|
|
- if ( m_hReadInst != INVALID_HANDLE_VALUE )
|
|
|
- CloseHandle(m_hReadInst);
|
|
|
+ if ( m_hPipeInst != INVALID_HANDLE_VALUE )
|
|
|
+ CloseHandle(m_hPipeInst);
|
|
|
}
|
|
|
|
|
|
BOOL CPipeClient::StartWork()
|
|
@@ -32,7 +30,7 @@ BOOL CPipeClient::StartWork()
|
|
|
HANDLE hConnect = CreateThread(NULL, 0, ConnectThread, this, 0, NULL);
|
|
|
HANDLE hReadMsg = CreateThread(NULL, 0, ReadMsgThread, this, 0, NULL);
|
|
|
|
|
|
- if ( hConnect == NULL /*|| hReadMsg == NULL */)
|
|
|
+ if ( hConnect == NULL || hReadMsg == NULL )
|
|
|
bRet = FALSE;
|
|
|
|
|
|
if ( hConnect )
|
|
@@ -55,10 +53,10 @@ DWORD CPipeClient::ConnectThread(LPVOID lpParam)
|
|
|
|
|
|
while(!pInstance->m_bClientStop)
|
|
|
{
|
|
|
- if ( pInstance->m_hReadInst != INVALID_HANDLE_VALUE ) {
|
|
|
+ if ( pInstance->m_hPipeInst != INVALID_HANDLE_VALUE ) {
|
|
|
// 1分钟检测;
|
|
|
Sleep(60000);
|
|
|
- Utility::dprintf(_T("m_hReadInst 已存在\n"));
|
|
|
+ Utility::dprintf(_T("m_hPipeInst 已存在\n"));
|
|
|
continue;
|
|
|
}
|
|
|
|
|
@@ -70,7 +68,7 @@ DWORD CPipeClient::ConnectThread(LPVOID lpParam)
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
- pInstance->m_hReadInst = CreateFile(
|
|
|
+ pInstance->m_hPipeInst = CreateFile(
|
|
|
pInstance->m_szPipeName, // pipe name
|
|
|
GENERIC_READ | GENERIC_WRITE, // read and write access
|
|
|
0, // no sharing
|
|
@@ -80,18 +78,19 @@ DWORD CPipeClient::ConnectThread(LPVOID lpParam)
|
|
|
NULL); // no template file
|
|
|
|
|
|
// 创建成功,退出;
|
|
|
- if ( pInstance->m_hReadInst != INVALID_HANDLE_VALUE )
|
|
|
+ if ( pInstance->m_hPipeInst != INVALID_HANDLE_VALUE )
|
|
|
{
|
|
|
// 管道连接成功,修改管道通信模式:message-read mode.
|
|
|
BOOL fSuccess = SetNamedPipeHandleState(
|
|
|
- pInstance->m_hReadInst, // pipe handle
|
|
|
+ pInstance->m_hPipeInst, // pipe handle
|
|
|
&pInstance->m_dwMode, // new pipe mode
|
|
|
NULL, // don't set maximum bytes
|
|
|
NULL); // don't set maximum time
|
|
|
|
|
|
if (!fSuccess) {
|
|
|
Utility::dprintf(_T("SetNamedPipeHandleState failed. GLE=%d\n"), GetLastError() );
|
|
|
- CloseHandle(pInstance->m_hReadInst);
|
|
|
+ CloseHandle(pInstance->m_hPipeInst);
|
|
|
+ pInstance->m_hPipeInst = INVALID_HANDLE_VALUE;
|
|
|
}
|
|
|
}
|
|
|
else
|
|
@@ -124,41 +123,73 @@ DWORD CPipeClient::ReadMsgThread(LPVOID lpParam)
|
|
|
int i = 0;
|
|
|
while(!pInstance->m_bClientStop)
|
|
|
{
|
|
|
- if ( pInstance->m_hReadInst == INVALID_HANDLE_VALUE ) {
|
|
|
- Sleep(2000);
|
|
|
+ if ( pInstance->m_hPipeInst == INVALID_HANDLE_VALUE ) {
|
|
|
+ Sleep(5000);
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
- //do
|
|
|
- {
|
|
|
- bSuccess = ReadFile(
|
|
|
- pInstance->m_hReadInst, // pipe handle
|
|
|
- m_IoRead.szBuffer, // buffer to receive reply
|
|
|
- BUFSIZE, // size of buffer
|
|
|
- &m_IoRead.dwBufferSize, // number of bytes read
|
|
|
- (OVERLAPPED*)&pInstance->m_IoRead); // not overlapped
|
|
|
+#if 0 // 分配足够大的缓冲,保证C/S两端通信内容不超过该大小;
|
|
|
+ bSuccess = ReadFile(pInstance->m_hPipeInst, m_IoRead.szBuffer, BUFSIZE, &m_IoRead.dwBufferSize, (OVERLAPPED*)&pInstance->m_IoRead);
|
|
|
+ if ( !bSuccess )
|
|
|
+ {
|
|
|
+ // 打印结果;
|
|
|
+ Utility::dprintf(_T("读取数据:Error=%ld, Len=%ld, Data=%s\n"), dwError, m_IoRead.m_Overlapped.InternalHigh, m_IoRead.szBuffer);
|
|
|
+ // 处理结果;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ // 等待完成;
|
|
|
+ if ( !WaitFinish(pInstance->m_hPipeInst, &m_IoRead) )
|
|
|
+ {
|
|
|
+ // 出现错误;
|
|
|
+ Utility::dprintf("CloseHandle\n");
|
|
|
+ CloseHandle(pInstance->m_hPipeInst);
|
|
|
+ pInstance->m_hPipeInst = INVALID_HANDLE_VALUE;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ // 打印结果;
|
|
|
+ Utility::dprintf(_T("读取数据:Error=%ld, Len=%ld, Data=%s\n"), dwError, m_IoRead.m_Overlapped.InternalHigh, m_IoRead.szBuffer);
|
|
|
+ // 处理结果;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ // 重置Buffer;
|
|
|
+ memset(m_IoRead.szBuffer, 0, BUFSIZE);
|
|
|
+#else // 分配的缓冲区,不足以一次性存储C/S两端的通信内容时;
|
|
|
+ do
|
|
|
+ {
|
|
|
+ bSuccess = ReadFile(pInstance->m_hPipeInst, m_IoRead.szBuffer, BUFSIZE, &m_IoRead.dwBufferSize, (OVERLAPPED*)&pInstance->m_IoRead);
|
|
|
+ if ( bSuccess )
|
|
|
+ {
|
|
|
+ // 打印结果;
|
|
|
+ Utility::dprintf(_T("读取数据:Error=%ld, Len=%ld, Data=%s\n"), dwError, m_IoRead.m_Overlapped.InternalHigh, m_IoRead.szBuffer);
|
|
|
+ // 处理结果;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ dwError = GetLastError();
|
|
|
+ if ( )
|
|
|
+ }
|
|
|
+ // 由于缓冲区不够大,没能一次性读取完;
|
|
|
+ if ( !bSuccess && )
|
|
|
+ if ( !bSuccess && (dwError = GetLastError()) != ERROR_MORE_DATA )
|
|
|
|
|
|
dwError = GetLastError();
|
|
|
//if ( !bSuccess && (dwError = GetLastError()) != ERROR_MORE_DATA )
|
|
|
// break;
|
|
|
|
|
|
- if ( WaitFinish(pInstance->m_hReadInst, &m_IoRead) )
|
|
|
+ if ( WaitFinish(pInstance->m_hPipeInst, &m_IoRead) )
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
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};
|
|
|
- _stprintf_s(szMsg, _T("读取数据:%d, %ld, %ld, %s\n"), (int)bSuccess, dwError, cbRead, chBuf);
|
|
|
- OutputDebugString(szMsg);
|
|
|
-#endif
|
|
|
// 追回数据;
|
|
|
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;
|
|
@@ -180,16 +211,13 @@ DWORD CPipeClient::ReadMsgThread(LPVOID lpParam)
|
|
|
if ( dwError == ERROR_PIPE_NOT_CONNECTED || dwError == ERROR_BROKEN_PIPE)
|
|
|
{
|
|
|
Utility::dprintf("CloseHandle\n");
|
|
|
- CloseHandle(pInstance->m_hReadInst);
|
|
|
- pInstance->m_hReadInst = INVALID_HANDLE_VALUE;
|
|
|
+ CloseHandle(pInstance->m_hPipeInst);
|
|
|
+ pInstance->m_hPipeInst = INVALID_HANDLE_VALUE;
|
|
|
}
|
|
|
-
|
|
|
-#ifdef _DEBUG
|
|
|
- //Sleep(10000);
|
|
|
-#endif
|
|
|
}
|
|
|
|
|
|
- memset(pInstance->m_szReceiveBuff, 0, BUFSIZE*sizeof(TCHAR));
|
|
|
+ memset(pInstance->m_szReceiveBuff, 0, BUFSIZE*sizeof(TCHAR));
|
|
|
+#endif
|
|
|
}
|
|
|
|
|
|
Utility::dprintf(_T("<%ld> ReadMsgThread 退出\n"),Utility::g_WndInfo.dwProcessId);
|
|
@@ -207,6 +235,8 @@ BOOL CPipeClient::WaitFinish(HANDLE hPipe, PER_IO_CONTEXT *pIoContext)
|
|
|
case ERROR_IO_PENDING:
|
|
|
bPendingIO = true;
|
|
|
break;
|
|
|
+ case ERROR_MORE_DATA:
|
|
|
+ break;
|
|
|
// 已经连接;
|
|
|
case ERROR_PIPE_CONNECTED:
|
|
|
SetEvent(pIoContext->m_Overlapped.hEvent);
|
|
@@ -227,7 +257,7 @@ BOOL CPipeClient::WaitFinish(HANDLE hPipe, PER_IO_CONTEXT *pIoContext)
|
|
|
if( GetOverlappedResult(hPipe, &pIoContext->m_Overlapped, &dwTransBytes, TRUE) == FALSE)
|
|
|
{
|
|
|
printf("ConnectNamedPipe failed %d\n",GetLastError());
|
|
|
- return -1;
|
|
|
+ return FALSE;
|
|
|
}
|
|
|
}
|
|
|
break;
|
|
@@ -236,7 +266,7 @@ BOOL CPipeClient::WaitFinish(HANDLE hPipe, PER_IO_CONTEXT *pIoContext)
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
- return 0;
|
|
|
+ return TRUE;
|
|
|
#else
|
|
|
DWORD dwError=::GetLastError();
|
|
|
DWORD dwWait = -1;
|
|
@@ -266,7 +296,7 @@ BOOL CPipeClient::WaitFinish(HANDLE hPipe, PER_IO_CONTEXT *pIoContext)
|
|
|
BOOL CPipeClient::SendMessage(PACKAGE &pak)
|
|
|
{
|
|
|
// 是否连接了服务端;
|
|
|
- if ( m_hReadInst == INVALID_HANDLE_VALUE )
|
|
|
+ if ( m_hPipeInst == INVALID_HANDLE_VALUE )
|
|
|
return FALSE;
|
|
|
|
|
|
// 是否初始化了句柄;
|
|
@@ -276,15 +306,15 @@ BOOL CPipeClient::SendMessage(PACKAGE &pak)
|
|
|
|
|
|
BOOL CPipeClient::SendData(const TCHAR *lpszMsg, DWORD dwDataLen)
|
|
|
{
|
|
|
- if ( m_hReadInst == INVALID_HANDLE_VALUE )
|
|
|
+ if ( m_hPipeInst == INVALID_HANDLE_VALUE )
|
|
|
return FALSE;
|
|
|
|
|
|
static int i = 0;
|
|
|
DWORD dwNumberOfBytesWritten = 0;
|
|
|
char szMsg[255] = {0};//"你好----001";
|
|
|
sprintf(szMsg, "发送内容:%d,%d", ::GetCurrentProcessId(), i++);
|
|
|
- BOOL fWrite = WriteFile(m_hReadInst,szMsg,strlen(szMsg),&dwNumberOfBytesWritten, NULL);//;&m_IoWrite.m_Overlapped);
|
|
|
- //WaitFinish(m_hReadInst, &m_IoWrite);
|
|
|
+ BOOL fWrite = WriteFile(m_hPipeInst,szMsg,strlen(szMsg),&dwNumberOfBytesWritten, NULL);//;&m_IoWrite.m_Overlapped);
|
|
|
+ //WaitFinish(m_hPipeInst, &m_IoWrite);
|
|
|
if ( fWrite )
|
|
|
{
|
|
|
Utility::dprintf(_T("SendData:%s\n"),lpszMsg);
|