📄 asyncsocketexlayer.cpp
字号:
BOOL CAsyncSocketExLayer::GetPeerName( CString& rPeerAddress, UINT& rPeerPort )
{
return GetPeerNameNext(rPeerAddress, rPeerPort);
}
BOOL CAsyncSocketExLayer::GetPeerNameNext( CString& rPeerAddress, UINT& rPeerPort )
{
if (m_pNextLayer)
return m_pNextLayer->GetPeerName(rPeerAddress, rPeerPort);
else
{
ASSERT(m_pOwnerSocket);
SOCKADDR_IN sockAddr;
memset(&sockAddr, 0, sizeof(sockAddr));
int nSockAddrLen = sizeof(sockAddr);
if (!getpeername(m_pOwnerSocket->GetSocketHandle(), (SOCKADDR*)&sockAddr, &nSockAddrLen))
{
rPeerPort = ntohs(sockAddr.sin_port);
rPeerAddress = inet_ntoa(sockAddr.sin_addr);
return TRUE;
}
else
return FALSE;
}
}
#endif //_AFX
BOOL CAsyncSocketExLayer::GetPeerName( SOCKADDR* lpSockAddr, int* lpSockAddrLen )
{
return GetPeerNameNext(lpSockAddr, lpSockAddrLen);
}
BOOL CAsyncSocketExLayer::GetPeerNameNext( SOCKADDR* lpSockAddr, int* lpSockAddrLen )
{
if (m_pNextLayer)
return m_pNextLayer->GetPeerName(lpSockAddr, lpSockAddrLen);
else
{
ASSERT(m_pOwnerSocket);
if ( !getpeername(m_pOwnerSocket->GetSocketHandle(), lpSockAddr, lpSockAddrLen) )
return TRUE;
else
return FALSE;
}
}
//Gets the address of the sock socket to which the socket is connected
#ifdef _AFX
BOOL CAsyncSocketExLayer::GetSockName( CString& rSockAddress, UINT& rSockPort )
{
return GetSockNameNext(rSockAddress, rSockPort);
}
BOOL CAsyncSocketExLayer::GetSockNameNext( CString& rSockAddress, UINT& rSockPort )
{
if (m_pNextLayer)
return m_pNextLayer->GetSockName(rSockAddress, rSockPort);
else
{
ASSERT(m_pOwnerSocket);
SOCKADDR_IN sockAddr;
memset(&sockAddr, 0, sizeof(sockAddr));
int nSockAddrLen = sizeof(sockAddr);
if (!getsockname(m_pOwnerSocket->GetSocketHandle(), (SOCKADDR*)&sockAddr, &nSockAddrLen))
{
rSockPort = ntohs(sockAddr.sin_port);
rSockAddress = inet_ntoa(sockAddr.sin_addr);
return TRUE;
}
else
return FALSE;
}
}
#endif //_AFX
BOOL CAsyncSocketExLayer::GetSockName( SOCKADDR* lpSockAddr, int* lpSockAddrLen )
{
return GetSockNameNext(lpSockAddr, lpSockAddrLen);
}
BOOL CAsyncSocketExLayer::GetSockNameNext( SOCKADDR* lpSockAddr, int* lpSockAddrLen )
{
if (m_pNextLayer)
return m_pNextLayer->GetSockName(lpSockAddr, lpSockAddrLen);
else
{
ASSERT(m_pOwnerSocket);
if ( !getsockname(m_pOwnerSocket->GetSocketHandle(), lpSockAddr, lpSockAddrLen) )
return TRUE;
else
return FALSE;
}
}
void CAsyncSocketExLayer::Init(CAsyncSocketExLayer *pPrevLayer, CAsyncSocketEx *pOwnerSocket)
{
ASSERT(pOwnerSocket);
m_pPrevLayer=pPrevLayer;
m_pOwnerSocket=pOwnerSocket;
m_pNextLayer=0;
#ifndef NOSOCKETSTATES
SetLayerState(pOwnerSocket->GetState());
#endif //NOSOCKETSTATES
}
int CAsyncSocketExLayer::GetLayerState()
{
return m_nLayerState;
}
void CAsyncSocketExLayer::SetLayerState(int nLayerState)
{
ASSERT(m_pOwnerSocket);
int nOldLayerState=GetLayerState();
m_nLayerState=nLayerState;
if (nOldLayerState!=nLayerState)
DoLayerCallback(LAYERCALLBACK_STATECHANGE, GetLayerState(), nOldLayerState);
}
void CAsyncSocketExLayer::CallEvent(int nEvent, int nErrorCode)
{
if (m_nCriticalError)
return;
m_nCriticalError=nErrorCode;
switch (nEvent)
{
case FD_READ:
case FD_FORCEREAD:
if (GetLayerState()==connecting && !nErrorCode)
{
m_nPendingEvents |= nEvent;
break;
}
else if (GetLayerState()==attached)
SetLayerState(connected);
if (nEvent & FD_READ)
m_nPendingEvents &= ~FD_READ;
else
m_nPendingEvents &= ~FD_FORCEREAD;
if (GetLayerState()==connected || nErrorCode)
{
if (nErrorCode)
SetLayerState(aborted);
OnReceive(nErrorCode);
}
break;
case FD_WRITE:
if (GetLayerState()==connecting && !nErrorCode)
{
m_nPendingEvents |= nEvent;
break;
}
else if (GetLayerState()==attached)
SetLayerState(connected);
m_nPendingEvents &= ~FD_WRITE;
if (GetLayerState()==connected || nErrorCode)
{
if (nErrorCode)
SetLayerState(aborted);
OnSend(nErrorCode);
}
break;
case FD_CONNECT:
if (GetLayerState()==connecting || GetLayerState() == attached)
{
if (!nErrorCode)
SetLayerState(connected);
else
SetLayerState(aborted);
m_nPendingEvents &= ~FD_CONNECT;
OnConnect(nErrorCode);
if (!nErrorCode)
{
if ((m_nPendingEvents & FD_READ) && GetLayerState()==connected)
OnReceive(0);
if ((m_nPendingEvents & FD_FORCEREAD) && GetLayerState()==connected)
OnReceive(0);
if ((m_nPendingEvents & FD_WRITE) && GetLayerState()==connected)
OnSend(0);
}
m_nPendingEvents = 0;
}
break;
case FD_ACCEPT:
if (GetLayerState()==listening)
{
if (nErrorCode)
SetLayerState(aborted);
m_nPendingEvents &= ~FD_ACCEPT;
OnAccept(nErrorCode);
}
break;
case FD_CLOSE:
if (GetLayerState()==connected || GetLayerState()==attached)
{
if (nErrorCode)
SetLayerState(aborted);
else
SetLayerState(closed);
m_nPendingEvents &= ~FD_CLOSE;
OnClose(nErrorCode);
}
break;
}
}
//Creates a socket
BOOL CAsyncSocketExLayer::Create(UINT nSocketPort, int nSocketType,
long lEvent, LPCTSTR lpszSocketAddress)
{
return CreateNext(nSocketPort, nSocketType, lEvent, lpszSocketAddress);
}
BOOL CAsyncSocketExLayer::CreateNext(UINT nSocketPort, int nSocketType, long lEvent, LPCTSTR lpszSocketAddress)
{
ASSERT(GetLayerState()==notsock);
BOOL res=FALSE;
if (m_pNextLayer)
res=m_pNextLayer->Create(nSocketPort, nSocketType, lEvent, lpszSocketAddress);
else
{
SOCKET hSocket=socket(AF_INET, nSocketType, 0);
if (hSocket==INVALID_SOCKET)
res=FALSE;
m_pOwnerSocket->m_SocketData.hSocket=hSocket;
m_pOwnerSocket->AttachHandle(hSocket);
if (!m_pOwnerSocket->AsyncSelect(lEvent))
{
m_pOwnerSocket->Close();
res=FALSE;
}
if (m_pOwnerSocket->m_pFirstLayer)
{
if (WSAAsyncSelect(m_pOwnerSocket->m_SocketData.hSocket, m_pOwnerSocket->GetHelperWindowHandle(), m_pOwnerSocket->m_SocketData.nSocketIndex+WM_SOCKETEX_NOTIFY, FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE) )
{
m_pOwnerSocket->Close();
res=FALSE;
}
}
if (!m_pOwnerSocket->Bind(nSocketPort, lpszSocketAddress))
{
m_pOwnerSocket->Close();
res=FALSE;
}
res=TRUE;
}
if (res)
SetLayerState(unconnected);
return res;
}
int CAsyncSocketExLayer::DoLayerCallback(int nType, int nParam1, int nParam2)
{
if (!m_pOwnerSocket)
return 0;
int nError=WSAGetLastError();
int res = m_pOwnerSocket->OnLayerCallback(this, nType, nParam1, nParam2);
WSASetLastError(nError);
return res;
}
BOOL CAsyncSocketExLayer::Listen( int nConnectionBacklog)
{
return ListenNext( nConnectionBacklog);
}
BOOL CAsyncSocketExLayer::ListenNext( int nConnectionBacklog)
{
ASSERT(GetLayerState()==unconnected);
BOOL res;
if (m_pNextLayer)
res=m_pNextLayer->Listen(nConnectionBacklog);
else
res=listen(m_pOwnerSocket->GetSocketHandle(), nConnectionBacklog);
if (res!=SOCKET_ERROR)
{
SetLayerState(listening);
}
return res!=SOCKET_ERROR;
}
BOOL CAsyncSocketExLayer::Accept( CAsyncSocketEx& rConnectedSocket, SOCKADDR* lpSockAddr /*=NULL*/, int* lpSockAddrLen /*=NULL*/ )
{
return AcceptNext(rConnectedSocket, lpSockAddr, lpSockAddrLen);
}
BOOL CAsyncSocketExLayer::AcceptNext( CAsyncSocketEx& rConnectedSocket, SOCKADDR* lpSockAddr /*=NULL*/, int* lpSockAddrLen /*=NULL*/ )
{
ASSERT(GetLayerState()==listening);
BOOL res;
if (m_pNextLayer)
res=m_pNextLayer->Accept(rConnectedSocket, lpSockAddr, lpSockAddrLen);
else
{
SOCKET hTemp = accept(m_pOwnerSocket->m_SocketData.hSocket, lpSockAddr, lpSockAddrLen);
if (hTemp == INVALID_SOCKET)
return FALSE;
VERIFY(rConnectedSocket.InitAsyncSocketExInstance());
rConnectedSocket.m_SocketData.hSocket=hTemp;
rConnectedSocket.AttachHandle(hTemp);
#ifndef NOSOCKETSTATES
rConnectedSocket.SetState(connected);
#endif //NOSOCKETSTATES
}
return TRUE;
}
BOOL CAsyncSocketExLayer::ShutDown(int nHow /*=sends*/)
{
return ShutDownNext(nHow);
}
BOOL CAsyncSocketExLayer::ShutDownNext(int nHow /*=sends*/)
{
if (m_nCriticalError)
{
WSASetLastError(m_nCriticalError);
return FALSE;
}
else if (GetLayerState()==notsock)
{
WSASetLastError(WSAENOTSOCK);
return FALSE;
}
else if (GetLayerState()==unconnected || GetLayerState()==connecting || GetLayerState()==listening)
{
WSASetLastError(WSAENOTCONN);
return FALSE;
}
if (!m_pNextLayer)
{
ASSERT(m_pOwnerSocket);
return shutdown(m_pOwnerSocket->GetSocketHandle(), nHow);
}
else
return m_pNextLayer->ShutDownNext(nHow);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -