彻底解决关于CSocket类的Receive超时的问题

网上有一些相关的东西,但经自己测试后,并没有实现功能。OnMessagePending没有监测到WM_TIMER消息。


然后我对类进行了调整,一来使得封装更好,二来外部调用基本不用做任何变化,三来希望能使OnMessagePending能够起作用。


其实修改很简单,就是将SetTimeOut和KillTimeOut都修改为私有函数,并重载CSocket基类的Receive和Send函数,在收发前后启动和关闭定时器。
注意:原文中 if(::PeekMessage(&msg, NULL, WM_TIMER, WM_TIMER, PM_NOREMOVE))语句并没有实现获取WM_TIMER消息的功能,后来,经过网上查找资料
PM_NOREMOVE 修改为PM_REMOVE

头文件 ClientSocket.h
#pragma once
#include "afxwin.h"
#include <afxsock.h>  
// CClientSocket 命令目标
class CTimeOutSocket : public CSocket
{
// Attributes
public:
CTimeOutSocket();
virtual~CTimeOutSocket();
public:
virtual BOOL OnMessagePending();
virtual int Receive(void* lpBuf, int nBufLen, int nFlags =0);
virtual int Send(const void* lpBuf, int nBufLen, int nFlags =0);
virtual int SendTo(const void* lpBuf, int nBufLen,UINT nHostPort, LPCTSTR lpszHostAddress , int nFlags);
virtual int ReceiveFrom(void* lpBuf, int nBufLen,CString& rSocketAddress, UINT& rSocketPort, int nFlags);
int m_nTimerID;
private:
BOOL KillTimeOut();
BOOL SetTimeOut(int nTimeOut);
};




以下为ClientSocket.c文件
#include "stdafx.h"
#include "ClientSocket.h"

CTimeOutSocket::CTimeOutSocket()
{
}
CTimeOutSocket::~CTimeOutSocket()
{
}
BOOL CTimeOutSocket::OnMessagePending() 
{
MSG msg;
if(::PeekMessage(&msg, NULL, WM_TIMER, WM_TIMER, PM_REMOVE))//PM_NOREMOVE
{
if (msg.wParam == (UINT) m_nTimerID)
{
// Remove the message and call CancelBlockingCall.
::PeekMessage(&msg, NULL, WM_TIMER, WM_TIMER, PM_REMOVE);
CancelBlockingCall();
return FALSE;  // No need for idle time processing.
};
};
return CSocket::OnMessagePending();
}
int CTimeOutSocket::Receive(void* lpBuf, int nBufLen, int nFlags) 
{
SetTimeOut(5000);
int nRecv = CSocket::Receive(lpBuf, nBufLen, nFlags);
KillTimeOut();
return nRecv;
}
int CTimeOutSocket::ReceiveFrom(void* lpBuf, int nBufLen,CString& rSocketAddress, UINT& rSocketPort, int nFlags)
{
SetTimeOut(10000);
int nRecv = CSocket::ReceiveFrom(lpBuf,  nBufLen, rSocketAddress, rSocketPort,nFlags);
KillTimeOut();
return nRecv;
}
int CTimeOutSocket::Send(const void* lpBuf, int nBufLen, int nFlags) 
{
SetTimeOut(10000);
int nSend = CSocket::Send(lpBuf, nBufLen, nFlags);
KillTimeOut();
return nSend; 
}
int CTimeOutSocket::SendTo(const void* lpBuf, int nBufLen,UINT nHostPort, LPCTSTR lpszHostAddress, int nFlags )
{
SetTimeOut(10000);
int nSend = CSocket::SendTo(lpBuf, nBufLen, nHostPort,lpszHostAddress,nFlags);
KillTimeOut();
return nSend; 
}
BOOL CTimeOutSocket::SetTimeOut(int nTimeOut)
{
m_nTimerID = SetTimer(NULL,0,nTimeOut,NULL);
return m_nTimerID;
}
BOOL CTimeOutSocket::KillTimeOut()
{
return KillTimer(NULL,m_nTimerID);
}
经过修改的代码,运行后OnMessagePending得到了WM_TIMER消息,正确解决了Receive的阻塞问题,同时我只需将原来的CSocket对象改为CTimeOutSocket对象就可以了,以上代码经测试可直接使用。

猜你喜欢

转载自blog.csdn.net/enlaihe/article/details/72628700