|
@@ -6,7 +6,7 @@
|
|
|
|
|
|
CPipeClient::CPipeClient(LPCTSTR lpPipeName, DWORD dwMode)
|
|
CPipeClient::CPipeClient(LPCTSTR lpPipeName, DWORD dwMode)
|
|
{
|
|
{
|
|
- m_hPipeInst = INVALID_HANDLE_VALUE;
|
|
|
|
|
|
+ m_hReadInst = INVALID_HANDLE_VALUE;
|
|
m_bClientStop = FALSE;
|
|
m_bClientStop = FALSE;
|
|
m_dwMode = dwMode;
|
|
m_dwMode = dwMode;
|
|
memset(m_szPipeName, 0, MAX_PATH*sizeof(TCHAR));
|
|
memset(m_szPipeName, 0, MAX_PATH*sizeof(TCHAR));
|
|
@@ -19,8 +19,8 @@ CPipeClient::CPipeClient(LPCTSTR lpPipeName, DWORD dwMode)
|
|
CPipeClient::~CPipeClient(void)
|
|
CPipeClient::~CPipeClient(void)
|
|
{
|
|
{
|
|
StopWork();
|
|
StopWork();
|
|
- if ( m_hPipeInst != INVALID_HANDLE_VALUE )
|
|
|
|
- CloseHandle(m_hPipeInst);
|
|
|
|
|
|
+ if ( m_hReadInst != INVALID_HANDLE_VALUE )
|
|
|
|
+ CloseHandle(m_hReadInst);
|
|
}
|
|
}
|
|
|
|
|
|
BOOL CPipeClient::StartWork()
|
|
BOOL CPipeClient::StartWork()
|
|
@@ -54,10 +54,10 @@ DWORD CPipeClient::ConnectThread(LPVOID lpParam)
|
|
|
|
|
|
while(!pInstance->m_bClientStop)
|
|
while(!pInstance->m_bClientStop)
|
|
{
|
|
{
|
|
- if ( pInstance->m_hPipeInst != INVALID_HANDLE_VALUE ) {
|
|
|
|
|
|
+ if ( pInstance->m_hReadInst != INVALID_HANDLE_VALUE ) {
|
|
// 1分钟检测;
|
|
// 1分钟检测;
|
|
Sleep(60000);
|
|
Sleep(60000);
|
|
- Utility::dprintf(_T("m_hPipeInst 已存在\n"));
|
|
|
|
|
|
+ Utility::dprintf(_T("m_hReadInst 已存在\n"));
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -69,7 +69,7 @@ DWORD CPipeClient::ConnectThread(LPVOID lpParam)
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
|
|
- pInstance->m_hPipeInst = CreateFile(
|
|
|
|
|
|
+ pInstance->m_hReadInst = CreateFile(
|
|
pInstance->m_szPipeName, // pipe name
|
|
pInstance->m_szPipeName, // pipe name
|
|
GENERIC_READ | GENERIC_WRITE, // read and write access
|
|
GENERIC_READ | GENERIC_WRITE, // read and write access
|
|
0, // no sharing
|
|
0, // no sharing
|
|
@@ -79,19 +79,29 @@ DWORD CPipeClient::ConnectThread(LPVOID lpParam)
|
|
NULL); // no template file
|
|
NULL); // no template file
|
|
|
|
|
|
// 创建成功,退出;
|
|
// 创建成功,退出;
|
|
- if ( pInstance->m_hPipeInst != INVALID_HANDLE_VALUE )
|
|
|
|
|
|
+ if ( pInstance->m_hReadInst != INVALID_HANDLE_VALUE )
|
|
{
|
|
{
|
|
// 管道连接成功,修改管道通信模式:message-read mode.
|
|
// 管道连接成功,修改管道通信模式:message-read mode.
|
|
BOOL fSuccess = SetNamedPipeHandleState(
|
|
BOOL fSuccess = SetNamedPipeHandleState(
|
|
- pInstance->m_hPipeInst, // pipe handle
|
|
|
|
|
|
+ pInstance->m_hReadInst, // pipe handle
|
|
&pInstance->m_dwMode, // new pipe mode
|
|
&pInstance->m_dwMode, // new pipe mode
|
|
NULL, // don't set maximum bytes
|
|
NULL, // don't set maximum bytes
|
|
NULL); // don't set maximum time
|
|
NULL); // don't set maximum time
|
|
|
|
|
|
if (!fSuccess) {
|
|
if (!fSuccess) {
|
|
Utility::dprintf(_T("SetNamedPipeHandleState failed. GLE=%d\n"), GetLastError() );
|
|
Utility::dprintf(_T("SetNamedPipeHandleState failed. GLE=%d\n"), GetLastError() );
|
|
- CloseHandle(pInstance->m_hPipeInst);
|
|
|
|
|
|
+ CloseHandle(pInstance->m_hReadInst);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ // 初始化写管道;
|
|
|
|
+ pInstance->m_hWriteInst = CreateFile(
|
|
|
|
+ pInstance->m_szPipeName, // pipe name
|
|
|
|
+ GENERIC_READ | GENERIC_WRITE, // read and write access
|
|
|
|
+ 0, // no sharing
|
|
|
|
+ NULL, // default security attributes
|
|
|
|
+ OPEN_EXISTING, // opens existing pipe
|
|
|
|
+ 0, // default attributes
|
|
|
|
+ NULL); // no template file
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
@@ -122,7 +132,7 @@ DWORD CPipeClient::ReadMsgThread(LPVOID lpParam)
|
|
|
|
|
|
while(!pInstance->m_bClientStop)
|
|
while(!pInstance->m_bClientStop)
|
|
{
|
|
{
|
|
- if ( pInstance->m_hPipeInst == INVALID_HANDLE_VALUE ) {
|
|
|
|
|
|
+ if ( pInstance->m_hReadInst == INVALID_HANDLE_VALUE ) {
|
|
Sleep(2000);
|
|
Sleep(2000);
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
@@ -130,7 +140,7 @@ DWORD CPipeClient::ReadMsgThread(LPVOID lpParam)
|
|
do
|
|
do
|
|
{
|
|
{
|
|
bSuccess = ReadFile(
|
|
bSuccess = ReadFile(
|
|
- pInstance->m_hPipeInst, // pipe handle
|
|
|
|
|
|
+ pInstance->m_hReadInst, // pipe handle
|
|
chBuf, // buffer to receive reply
|
|
chBuf, // buffer to receive reply
|
|
BUFSIZE*sizeof(TCHAR), // size of buffer
|
|
BUFSIZE*sizeof(TCHAR), // size of buffer
|
|
&cbRead, // number of bytes read
|
|
&cbRead, // number of bytes read
|
|
@@ -170,8 +180,8 @@ DWORD CPipeClient::ReadMsgThread(LPVOID lpParam)
|
|
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)
|
|
if ( dwError == ERROR_PIPE_NOT_CONNECTED || dwError == ERROR_BROKEN_PIPE)
|
|
{
|
|
{
|
|
- CloseHandle(pInstance->m_hPipeInst);
|
|
|
|
- pInstance->m_hPipeInst = INVALID_HANDLE_VALUE;
|
|
|
|
|
|
+ CloseHandle(pInstance->m_hReadInst);
|
|
|
|
+ pInstance->m_hReadInst = INVALID_HANDLE_VALUE;
|
|
}
|
|
}
|
|
|
|
|
|
#ifdef _DEBUG
|
|
#ifdef _DEBUG
|
|
@@ -185,4 +195,14 @@ DWORD CPipeClient::ReadMsgThread(LPVOID lpParam)
|
|
Utility::dprintf(_T("<%ld> ReadMsgThread 退出\n"),Utility::g_WndInfo.dwProcessId);
|
|
Utility::dprintf(_T("<%ld> ReadMsgThread 退出\n"),Utility::g_WndInfo.dwProcessId);
|
|
|
|
|
|
return 0;
|
|
return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+BOOL CPipeClient::SendMessage(PACKAGE &pak)
|
|
|
|
+{
|
|
|
|
+ // 是否连接了服务端;
|
|
|
|
+ if ( m_hReadInst == INVALID_HANDLE_VALUE )
|
|
|
|
+ return FALSE;
|
|
|
|
+
|
|
|
|
+ // 是否初始化了句柄;
|
|
|
|
+
|
|
}
|
|
}
|