|
@@ -69,6 +69,7 @@ DWORD WINAPI CIOCPPipe::_WorkerThread(LPVOID lpParam)
|
|
|
if ( EXIT_CODE==(DWORD)pPipeContext )
|
|
|
{
|
|
|
//break;
|
|
|
+ dprintf(_T("IOCP: 退出消息"));
|
|
|
continue;
|
|
|
}
|
|
|
|
|
@@ -91,6 +92,7 @@ DWORD WINAPI CIOCPPipe::_WorkerThread(LPVOID lpParam)
|
|
|
// 读取传入的参数
|
|
|
PER_IO_CONTEXT* pIoContext = CONTAINING_RECORD(pOverlapped, PER_IO_CONTEXT, m_Overlapped);
|
|
|
|
|
|
+ dprintf(_T("IOCP: 读取传入的参数=%ld"), pIoContext->m_OpType);
|
|
|
// 判断是否有客户端断开了
|
|
|
if((0 == dwBytesTransfered) && ( OP_RECV==pIoContext->m_OpType || OP_SEND==pIoContext->m_OpType))
|
|
|
{
|
|
@@ -109,9 +111,10 @@ DWORD WINAPI CIOCPPipe::_WorkerThread(LPVOID lpParam)
|
|
|
switch( pIoContext->m_OpType )
|
|
|
{
|
|
|
case OP_ACCEPT:
|
|
|
- {
|
|
|
+ {
|
|
|
+ dprintf(_T("客户端连接成功"));
|
|
|
pIOCPModel->DoAccpet( pPipeContext, pIoContext );
|
|
|
- dprintf(_T("客户端连接成功"));
|
|
|
+
|
|
|
// 发送第一个请求:回调处理;
|
|
|
if ( lpOnConnectCallback )
|
|
|
{
|
|
@@ -121,14 +124,19 @@ DWORD WINAPI CIOCPPipe::_WorkerThread(LPVOID lpParam)
|
|
|
break;
|
|
|
case OP_RECV:
|
|
|
{
|
|
|
+ dprintf(_T("客户端消息到达"));
|
|
|
pIOCPModel->DoRecv( pPipeContext, pIoContext );
|
|
|
}
|
|
|
break;
|
|
|
case OP_SEND:
|
|
|
+ {
|
|
|
+ // 发送成功后,此处有消息到达;
|
|
|
+ dprintf(_T("消息送达客户端"));
|
|
|
+ }
|
|
|
break;
|
|
|
default:
|
|
|
// 不应该执行到这里
|
|
|
- TRACE(_T("_WorkThread中的 pIoContext->m_OpType 参数异常.\n"));
|
|
|
+ dprintf(_T("_WorkThread中的 pIoContext->m_OpType 参数异常.\n"));
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
@@ -320,6 +328,8 @@ bool CIOCPPipe::PostAccept()
|
|
|
|
|
|
return fPendingIO;
|
|
|
#else
|
|
|
+
|
|
|
+ dprintf(_T("Post Accept <%lp> 成功!"), &pNewIoContext->m_Overlapped);
|
|
|
AddToContextList(pPipeContext);
|
|
|
|
|
|
return true;
|
|
@@ -352,7 +362,7 @@ bool CIOCPPipe::PostRecv( PER_IO_CONTEXT* pIoContext )
|
|
|
|
|
|
dprintf(_T("等待客户端消息"));
|
|
|
// 初始化完成后,投递WSARecv请求
|
|
|
- int nBytesRecv = ReadFile( pIoContext->m_PipeAccept, pIoContext->chRequest, BUFSIZE, &dwBytes, p_ol );
|
|
|
+ int nBytesRecv = ReadFile( pIoContext->m_PipeAccept, pIoContext->szBuffer, BUFSIZE, &dwBytes, p_ol );
|
|
|
|
|
|
// 如果返回值错误,并且错误的代码并非是Pending的话,那就说明这个重叠请求失败了
|
|
|
if ((-1 == nBytesRecv) && (ERROR_IO_PENDING != GetLastError()))
|
|
@@ -457,8 +467,8 @@ void CIOCPPipe::RecvProcess(PER_PIPE_CONTEXT* pPipeContext, PER_IO_CONTEXT* pIoC
|
|
|
*/
|
|
|
DATAHEADER header;
|
|
|
MSG_INFO msg_info;
|
|
|
- memcpy(&header, pIoContext->chRequest, sizeof(DATAHEADER));
|
|
|
- memcpy(&msg_info, pIoContext->chRequest+sizeof(DATAHEADER), sizeof(MSG_INFO));
|
|
|
+ memcpy(&header, pIoContext->szBuffer, sizeof(DATAHEADER));
|
|
|
+ memcpy(&msg_info, pIoContext->szBuffer+sizeof(DATAHEADER), sizeof(MSG_INFO));
|
|
|
|
|
|
dprintf(_T("接收到客户端消息:Result=%d, Tpye=%d, Proctocl=%d, Len=%ld, Name=%s, Id=%ld, Data=%s"),
|
|
|
msg_info.byResult,
|
|
@@ -473,12 +483,27 @@ void CIOCPPipe::RecvProcess(PER_PIPE_CONTEXT* pPipeContext, PER_IO_CONTEXT* pIoC
|
|
|
DWORD lpNumberOfBytesWritten = 0;
|
|
|
TCHAR szMsg[MAX_PATH] = {0};
|
|
|
_stprintf(szMsg, _T("%ld:%ld"), GetCurrentThreadId(), GetTickCount());
|
|
|
+#if 0
|
|
|
// WriteFile时,不需要异步处理,所以重叠IO参数NUULL;
|
|
|
- BOOL bRet = WriteFile(pIoContext->m_PipeAccept, pIoContext->chRequest, pIoContext->m_Overlapped.InternalHigh, &lpNumberOfBytesWritten, NULL);
|
|
|
+ BOOL bRet = WriteFile(pIoContext->m_PipeAccept, pIoContext->szBuffer, pIoContext->m_Overlapped.InternalHigh, &lpNumberOfBytesWritten, NULL);
|
|
|
if ( !bRet )
|
|
|
{
|
|
|
- dprintf(_T("发送消息失败:%s, %ld, %ld"), pIoContext->chRequest, GetLastError(), GetCurrentThreadId());
|
|
|
+ dprintf(_T("发送消息失败:%s, %ld, %ld"), pIoContext->szBuffer, GetLastError(), GetCurrentThreadId());
|
|
|
}
|
|
|
+#else
|
|
|
+ if ( pIoContext->pWriteIoContext == NULL )
|
|
|
+ {
|
|
|
+ pIoContext->pWriteIoContext = new PER_IO_CONTEXT();
|
|
|
+ pIoContext->pWriteIoContext->m_OpType = OP_SEND;
|
|
|
+ pIoContext->pWriteIoContext->m_PipeAccept = pIoContext->m_PipeAccept;
|
|
|
+ }
|
|
|
+ memcpy(pIoContext->pWriteIoContext->szBuffer, pIoContext->szBuffer, pIoContext->m_Overlapped.InternalHigh);
|
|
|
+ BOOL bRet = WriteFile(pIoContext->m_PipeAccept, pIoContext->pWriteIoContext->szBuffer, pIoContext->m_Overlapped.InternalHigh, &lpNumberOfBytesWritten, &pIoContext->pWriteIoContext->m_Overlapped);
|
|
|
+ if ( !bRet )
|
|
|
+ {
|
|
|
+ dprintf(_T("发送消息失败:%s, %ld, %ld"), pIoContext->szBuffer, GetLastError(), GetCurrentThreadId());
|
|
|
+ }
|
|
|
+#endif
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -492,6 +517,7 @@ bool CIOCPPipe::HandleError( PER_PIPE_CONTEXT *pPipeContext, const DWORD& dwErr
|
|
|
// 如果是超时了,就再继续等吧
|
|
|
if( WAIT_TIMEOUT == dwErr )
|
|
|
{
|
|
|
+ dprintf(_T("HandleError: 超时"));
|
|
|
return true;
|
|
|
}
|
|
|
// 可能是客户端异常退出了
|
|
@@ -499,10 +525,11 @@ bool CIOCPPipe::HandleError( PER_PIPE_CONTEXT *pPipeContext, const DWORD& dwErr
|
|
|
{
|
|
|
RemoveContext(pPipeContext);
|
|
|
//ShowMessage( _T("检测到客户端异常退出!") );
|
|
|
- dprintf(_T("检测到客户端异常退出"));
|
|
|
+ dprintf(_T("检测到客户端异常退出\n\n"));
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ dprintf(_T("完成端口操作出现错误,线程退出。错误代码:%d"),dwErr);
|
|
|
ShowMessage( _T("完成端口操作出现错误,线程退出。错误代码:%d"),dwErr );
|
|
|
|
|
|
return false;
|