📄 asyncsocketex.cpp
字号:
}
break;
}
pPrev=pList;
pList=pList->pNext;
ASSERT(pList);
}
}
catch(...){
TRACE("Unknown exception in CAsyncSocketEx::FreeAsyncSocketExInstance()\n");
ASSERT(0);
}
m_sGlobalCriticalSection.Unlock();
}
int CAsyncSocketEx::Receive(void* lpBuf, int nBufLen, int nFlags /*=0*/)
{
#ifndef NOLAYERS
if (m_pFirstLayer)
return m_pFirstLayer->Receive(lpBuf, nBufLen, nFlags);
else
#endif //NOLAYERS
return recv(m_SocketData.hSocket, (LPSTR)lpBuf, nBufLen, nFlags);
}
int CAsyncSocketEx::Send(const void* lpBuf, int nBufLen, int nFlags /*=0*/)
{
#ifndef NOLAYERS
if (m_pFirstLayer)
return m_pFirstLayer->Send(lpBuf, nBufLen, nFlags);
else
#endif //NOLAYERS
return send(m_SocketData.hSocket, (LPSTR)lpBuf, nBufLen, nFlags);
}
BOOL CAsyncSocketEx::Connect(LPCTSTR lpszHostAddress, UINT nHostPort)
{
#ifndef NOLAYERS
if (m_pFirstLayer)
return m_pFirstLayer->Connect(lpszHostAddress, nHostPort);
else
#endif //NOLAYERS
{
USES_CONVERSION;
ASSERT(lpszHostAddress != NULL);
SOCKADDR_IN sockAddr;
memset(&sockAddr,0,sizeof(sockAddr));
LPSTR lpszAscii = T2A((LPTSTR)lpszHostAddress);
sockAddr.sin_family = AF_INET;
sockAddr.sin_addr.s_addr = inet_addr(lpszAscii);
if (sockAddr.sin_addr.s_addr == INADDR_NONE)
{
m_pAsyncGetHostByNameBuffer=new char[MAXGETHOSTSTRUCT];
m_nAsyncGetHostByNamePort=nHostPort;
m_hAsyncGetHostByNameHandle=WSAAsyncGetHostByName(GetHelperWindowHandle(), WM_SOCKETEX_GETHOST, lpszAscii, m_pAsyncGetHostByNameBuffer, MAXGETHOSTSTRUCT);
if (!m_hAsyncGetHostByNameHandle)
return FALSE;
WSASetLastError(WSAEWOULDBLOCK);
return TRUE;
}
sockAddr.sin_port = htons((u_short)nHostPort);
return CAsyncSocketEx::Connect((SOCKADDR*)&sockAddr, sizeof(sockAddr));
}
}
BOOL CAsyncSocketEx::Connect( const SOCKADDR* lpSockAddr, int nSockAddrLen )
{
#ifndef NOLAYERS
if (m_pFirstLayer)
return m_pFirstLayer->Connect(lpSockAddr, nSockAddrLen);
else
#endif //NOLAYERS
return SOCKET_ERROR!=connect(m_SocketData.hSocket, lpSockAddr, nSockAddrLen);
}
#ifdef _AFX
BOOL CAsyncSocketEx::GetPeerName( CString& rPeerAddress, UINT& rPeerPort )
{
#ifndef NOLAYERS
if (m_pFirstLayer)
return m_pFirstLayer->GetPeerName(rPeerAddress, rPeerPort);
#endif NOLAYERS
SOCKADDR_IN sockAddr;
memset(&sockAddr, 0, sizeof(sockAddr));
int nSockAddrLen = sizeof(sockAddr);
BOOL bResult = GetPeerName((SOCKADDR*)&sockAddr, &nSockAddrLen);
if (bResult)
{
rPeerPort = ntohs(sockAddr.sin_port);
rPeerAddress = inet_ntoa(sockAddr.sin_addr);
}
return bResult;
}
#endif //AFX
BOOL CAsyncSocketEx::GetPeerName( SOCKADDR* lpSockAddr, int* lpSockAddrLen )
{
#ifndef NOLAYERS
if (m_pFirstLayer)
return m_pFirstLayer->GetPeerName(lpSockAddr, lpSockAddrLen);
#endif //NOLAYERS
if ( !getpeername(m_SocketData.hSocket, lpSockAddr, lpSockAddrLen) )
return TRUE;
else
return FALSE;
}
#ifdef _AFX
BOOL CAsyncSocketEx::GetSockName(CString& rSocketAddress, UINT& rSocketPort)
{
SOCKADDR_IN sockAddr;
memset(&sockAddr, 0, sizeof(sockAddr));
int nSockAddrLen = sizeof(sockAddr);
BOOL bResult = GetSockName((SOCKADDR*)&sockAddr, &nSockAddrLen);
if (bResult)
{
rSocketPort = ntohs(sockAddr.sin_port);
rSocketAddress = inet_ntoa(sockAddr.sin_addr);
}
return bResult;
}
#endif
BOOL CAsyncSocketEx::GetSockName( SOCKADDR* lpSockAddr, int* lpSockAddrLen )
{
if ( !getsockname(m_SocketData.hSocket, lpSockAddr, lpSockAddrLen) )
return TRUE;
else
return FALSE;
}
BOOL CAsyncSocketEx::ShutDown( int nHow /*=sends*/ )
{
#ifndef NOLAYERS
if (m_pFirstLayer)
{
return m_pFirstLayer->ShutDown();
}
else
#endif
{
if ( !shutdown(m_SocketData.hSocket, nHow) )
return TRUE;
else
return FALSE;
}
}
SOCKET CAsyncSocketEx::Detach( )
{
SOCKET socket=m_SocketData.hSocket;
DetachHandle(socket);
m_SocketData.hSocket=INVALID_SOCKET;
return socket;
}
BOOL CAsyncSocketEx::Attach( SOCKET hSocket, long lEvent /*= FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE*/ )
{
if (hSocket==INVALID_SOCKET || !hSocket)
return FALSE;
VERIFY(InitAsyncSocketExInstance());
m_SocketData.hSocket=hSocket;
AttachHandle(hSocket);
return AsyncSelect(lEvent);
}
BOOL CAsyncSocketEx::AsyncSelect( long lEvent /*= FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE*/ )
{
ASSERT(m_pLocalAsyncSocketExThreadData);
m_lEvent=lEvent;
#ifndef NOLAYERS
if (!m_pFirstLayer)
#endif //NOLAYERS
{
if ( !WSAAsyncSelect(m_SocketData.hSocket, GetHelperWindowHandle(), m_SocketData.nSocketIndex+WM_SOCKETEX_NOTIFY, lEvent) )
return TRUE;
else
return FALSE;
}
return TRUE;
}
BOOL CAsyncSocketEx::Listen( int nConnectionBacklog /*=5*/ )
{
#ifndef NOLAYERS
if (m_pFirstLayer)
return m_pFirstLayer->Listen(nConnectionBacklog);
#endif //NOLAYERS
if (!listen(m_SocketData.hSocket, nConnectionBacklog))
return TRUE;
else
return FALSE;
}
BOOL CAsyncSocketEx::Accept( CAsyncSocketEx& rConnectedSocket, SOCKADDR* lpSockAddr /*=NULL*/, int* lpSockAddrLen /*=NULL*/ )
{
ASSERT(rConnectedSocket.m_SocketData.hSocket == INVALID_SOCKET);
#ifndef NOLAYERS
if (m_pFirstLayer)
{
return m_pFirstLayer->Accept(rConnectedSocket, lpSockAddr, lpSockAddrLen);
}
else
#endif //NOLAYERS
{
SOCKET hTemp = accept(m_SocketData.hSocket, lpSockAddr, lpSockAddrLen);
if (hTemp == INVALID_SOCKET)
return FALSE;
VERIFY(rConnectedSocket.InitAsyncSocketExInstance());
rConnectedSocket.m_SocketData.hSocket=hTemp;
rConnectedSocket.AttachHandle(hTemp);
}
return TRUE;
}
BOOL CAsyncSocketEx::IOCtl( long lCommand, DWORD* lpArgument )
{
return ioctlsocket(m_SocketData.hSocket, lCommand, lpArgument) != SOCKET_ERROR;
}
int CAsyncSocketEx::GetLastError()
{
return WSAGetLastError();
}
BOOL CAsyncSocketEx::TriggerEvent(long lEvent)
{
if (m_SocketData.hSocket==INVALID_SOCKET)
return FALSE;
ASSERT(m_pLocalAsyncSocketExThreadData);
ASSERT(m_pLocalAsyncSocketExThreadData->m_pHelperWindow);
ASSERT(m_SocketData.nSocketIndex!=-1);
#ifndef NOLAYERS
if (m_pFirstLayer)
{
CAsyncSocketExLayer::t_LayerNotifyMsg *pMsg=new CAsyncSocketExLayer::t_LayerNotifyMsg;
pMsg->lEvent=lEvent%0xFFFF;
pMsg->pLayer=0;
BOOL res=PostMessage(GetHelperWindowHandle(), WM_SOCKETEX_TRIGGER, (WPARAM)this, (LPARAM)pMsg);
if (!res)
delete pMsg;
return res;
}
else
#endif //NOLAYERS
return PostMessage(GetHelperWindowHandle(), m_SocketData.nSocketIndex+WM_SOCKETEX_NOTIFY, m_SocketData.hSocket, lEvent%0xFFFF);
}
SOCKET CAsyncSocketEx::GetSocketHandle()
{
return m_SocketData.hSocket;
}
HWND CAsyncSocketEx::GetHelperWindowHandle()
{
if (!m_pLocalAsyncSocketExThreadData)
return 0;
if (!m_pLocalAsyncSocketExThreadData->m_pHelperWindow)
return 0;
return m_pLocalAsyncSocketExThreadData->m_pHelperWindow->GetHwnd();
}
#ifndef NOLAYERS
BOOL CAsyncSocketEx::AddLayer(CAsyncSocketExLayer *pLayer)
{
ASSERT(pLayer);
if (m_SocketData.hSocket!=INVALID_SOCKET)
return FALSE;
if (m_pFirstLayer)
{
ASSERT(m_pLastLayer);
m_pLastLayer=m_pLastLayer->AddLayer(pLayer, this);
return m_pLastLayer?TRUE:FALSE;
}
else
{
ASSERT(!m_pLastLayer);
pLayer->Init(0, this);
m_pFirstLayer=pLayer;
m_pLastLayer=m_pFirstLayer;
}
return TRUE;
}
void CAsyncSocketEx::RemoveAllLayers()
{
m_pFirstLayer=0;
m_pLastLayer=0;
}
int CAsyncSocketEx::OnLayerCallback(const CAsyncSocketExLayer *pLayer, int nType, int nParam1, int nParam2)
{
ASSERT(pLayer);
return 1;
}
#endif //NOLAYERS
BOOL CAsyncSocketEx::GetSockOpt(int nOptionName, void* lpOptionValue, int* lpOptionLen, int nLevel /*=SOL_SOCKET*/)
{
return (SOCKET_ERROR != getsockopt(m_SocketData.hSocket, nLevel, nOptionName, (LPSTR)lpOptionValue, lpOptionLen));
}
BOOL CAsyncSocketEx::SetSockOpt(int nOptionName, const void* lpOptionValue, int nOptionLen, int nLevel /*=SOL_SOCKET*/)
{
return (SOCKET_ERROR != setsockopt(m_SocketData.hSocket, nLevel, nOptionName, (LPSTR)lpOptionValue, nOptionLen));
}
#ifdef _DEBUG
void CAsyncSocketEx::AssertValid() const
{
CObject::AssertValid();
(void)m_SocketData;
(void)m_lEvent;
(void)m_pAsyncGetHostByNameBuffer;
(void)m_hAsyncGetHostByNameHandle;
(void)m_nAsyncGetHostByNamePort;
//Pointer to the data of the local thread
// struct t_AsyncSocketExThreadData
// {
// CAsyncSocketExHelperWindow *m_pHelperWindow;
// int nInstanceCount;
// DWORD nThreadId;
// } *m_pLocalAsyncSocketExThreadData;
//List of the data structures for all threads
// static struct t_AsyncSocketExThreadDataList
// {
// t_AsyncSocketExThreadDataList *pNext;
// t_AsyncSocketExThreadData *pThreadData;
// } *m_spAsyncSocketExThreadDataList;
#ifndef NOLAYERS
CHECK_PTR(m_pFirstLayer);
CHECK_PTR(m_pLastLayer);
#endif //NOLAYERS
}
#endif
#ifdef _DEBUG
void CAsyncSocketEx::Dump(CDumpContext& dc) const
{
CObject::Dump(dc);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -