📄 asyncsocketexlayer.cpp
字号:
if (lphost != NULL)
sockAddr.sin_addr.s_addr = ((LPIN_ADDR)lphost->h_addr)->s_addr;
else
{
WSASetLastError(WSAEINVAL);
res = FALSE;
}
}
sockAddr.sin_port = htons((u_short)nHostPort);
res = ( SOCKET_ERROR!=connect(m_pOwnerSocket->GetSocketHandle(), (SOCKADDR*)&sockAddr, sizeof(sockAddr)) );
}
if (res || WSAGetLastError()==WSAEWOULDBLOCK)
{
SetLayerState(connecting);
}
return res;
}
BOOL CAsyncSocketExLayer::ConnectNext( const SOCKADDR* lpSockAddr, int nSockAddrLen )
{
ASSERT(GetLayerState()==unconnected);
ASSERT(m_pOwnerSocket);
BOOL res;
if (m_pNextLayer)
res=m_pNextLayer->Connect(lpSockAddr, nSockAddrLen);
else
res = (SOCKET_ERROR!=connect(m_pOwnerSocket->GetSocketHandle(), lpSockAddr, nSockAddrLen));
if (res || WSAGetLastError()==WSAEWOULDBLOCK)
SetLayerState(connecting);
return res;
}
//Gets the address of the peer socket to which the socket is connected
#ifdef _AFX
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;
}
}
void CAsyncSocketExLayer::Init(CAsyncSocketExLayer *pPrevLayer, CAsyncSocketEx *pOwnerSocket)
{
ASSERT(pOwnerSocket);
m_pPrevLayer=pPrevLayer;
m_pOwnerSocket=pOwnerSocket;
m_pNextLayer=0;
}
int CAsyncSocketExLayer::GetLayerState()
{
return m_nLayerState;
}
void CAsyncSocketExLayer::CallEvent(int nEvent, int nErrorCode)
{
if (m_nCriticalError)
return;
m_nCriticalError=nErrorCode;
switch (nEvent)
{
case FD_READ:
case FD_FORCEREAD:
if (GetLayerState()==connected)
{
if (nErrorCode)
SetLayerState(aborted);
OnReceive(nErrorCode);
}
break;
case FD_WRITE:
if (GetLayerState()==connected)
{
if (nErrorCode)
SetLayerState(aborted);
OnSend(nErrorCode);
}
break;
case FD_CONNECT:
if (GetLayerState()==connecting)
{
if (!nErrorCode)
SetLayerState(connected);
else
SetLayerState(aborted);
OnConnect(nErrorCode);
}
break;
case FD_ACCEPT:
if (GetLayerState()==listening)
{
if (!nErrorCode)
SetLayerState(connected);
else
SetLayerState(aborted);
OnAccept(nErrorCode);
}
break;
case FD_CLOSE:
if (GetLayerState()==connected)
{
if (nErrorCode)
SetLayerState(aborted);
else
SetLayerState(closed);
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;
}
void CAsyncSocketExLayer::SetLayerState(int nLayerState)
{
ASSERT(m_pOwnerSocket);
int nOldLayerState=GetLayerState();
m_nLayerState=nLayerState;
if (nOldLayerState!=nLayerState)
DoLayerCallback(LAYERCALLBACK_STATECHANGE, GetLayerState(), nOldLayerState);
}
int CAsyncSocketExLayer::DoLayerCallback(int nType, int nParam1, int nParam2)
{
ASSERT(m_pOwnerSocket);
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)
{
SetLayerState(listening);
}
return res;
}
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);
}
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 + -