Home > Software engineering >  WSAAsyncSelect model receiving array package problem
WSAAsyncSelect model receiving array package problem

Time:09-16

Below is the WSAAsyncSelect processing code, but always feel uncomfortable,
1. If there is a boring man connect up and don't send data doesn't close the connection there will never be FD_CLOSE lead to store the information of lp_client_read_info will always be kept in the list,
2. Use the list for group packet buffer, this way or not appropriate, how to save a general WSAAsyncSelect model SOCKET set of packet buffer?

 void DSocketServer: : OnMessage (SOCKET sock, LPARAM LPARAM, HWND HWND) 
{
If (WSAGETSELECTERROR (lParam)) {
Closesocket (sock);
return;
}
The switch (WSAGETSELECTEVENT (lParam))
{
Case FD_ACCEPT:
{
Sockaddr_in clientAddr;
Int addr_size=sizeof (sockaddr);
The SOCKET clientSock=accept (m_socket_server, (sockaddr *) & amp; ClientAddr, & amp; Addr_size);
If (INVALID_SOCKET==clientSock)
{
GlobalUserLog. Error (TEXT (" the client sets according to the word is invalid! Error code: % ld "), WSAGetLastError ());
return;
}
Char * clientIp=inet_ntoa (clientAddr sin_addr);

//read the data structure, for the first time in first need further information, describe the size of the packet information
Lp_client_read_info lpReadInfo=new client_read_info {0}.
//save the client's IP
_tcscpy_s (lpReadInfo - & gt; Client_ip, 88, clientIp);
LpReadInfo - & gt; Client_sock=clientSock;
//should receive data length, h_info header structure
LpReadInfo - & gt; Data_len=sizeof (h_info);
//receiving data buffer
LpReadInfo - & gt; Data=https://bbs.csdn.net/topics/new TCHAR [lpReadInfo -> data_len] {0}.
//to save structure to a list, it is a class attribute
Push_client_info (lpReadInfo);
//bind the READ and CLOSE message
WSAAsyncSelect (clientSock, hWnd WM_ASYNC_SOCKET, FD_READ | FD_CLOSE);
break;
}
Case FD_READ:
{
WSAAsyncSelect (sock, hWnd, 0, 0);
//based on SOCKET out corresponding lp_client_read_info structure
Lp_client_read_info lpReadInfo=get_client_info (sock);
If (the sock!=lpReadInfo - & gt; Client_sock)
{
return;
}

//calculate the rest need to read the data
UINT readBufLen=lpReadInfo - & gt; Data_len lpReadInfo - & gt; Readed_len;
//up to a read MAX_READ_BUF bytes
If (readBufLen & gt; MAX_READ_BUF)
{
ReadBufLen=MAX_READ_BUF;
}

TCHAR tmpBuf [MAX_READ_BUF]={0};
Int recvLen=recv (sock, tmpBuf readBufLen, 0).
If (SOCKET_ERROR==recvLen)
{
Int wsaErrorNo=WSAGetLastError ();
If (wsaErrorNo!=WSAEWOULDBLOCK & amp; & WsaErrorNo!=WSAEINTR)
{
The switch (wsaErrorNo)
{
Case WSAETIMEDOUT:
{
GlobalUserLog. Error (TEXT (" read data timeout, IP: % s "), lpReadInfo - & gt; Client_ip);
}
break;
Case WSAECONNRESET:
{
GlobalUserLog. Error (TEXT (" remote host mandatory disconnected, IP: % s "), lpReadInfo - & gt; Client_ip);
}
break;
Default:
{
GlobalUserLog. Error (TEXT (" unknown network Error condition occurred while reading data, Error code: % d, IP: % s "), wsaErrorNo, lpReadInfo - & gt; Client_ip);
}
break;
}
WSAAsyncSelect (sock, hWnd WM_ASYNC_SOCKET, FD_CLOSE);
}
The else
{
WSAAsyncSelect (sock, hWnd WM_ASYNC_SOCKET, FD_READ);
}
}
Else if (recvLen & gt; 0 & & RecvLen + lpReadInfo - & gt; Readed_len & gt; LpReadInfo - & gt; Data_len)
{
GlobalUserLog. Error (TEXT (" data may have been tampered with, IP: % s "), lpReadInfo - & gt; Client_ip);
WSAAsyncSelect (sock, hWnd WM_ASYNC_SOCKET, FD_CLOSE);
}
The else
{
//read data saved to the buffer after
If (recvLen & gt; 0)
{
Memcpy_s (lpReadInfo - & gt; The data + lpReadInfo - & gt; Readed_len lpReadInfo - & gt; Data_len lpReadInfo - & gt; Readed_len tmpBuf, recvLen);
LpReadInfo - & gt; Readed_len +=recvLen;
}
//if the data is read complete start processing data
If (lpReadInfo - & gt; Data_len==lpReadInfo - & gt; Readed_len)
{

//judge set package whether the data packet, if is the first packet, continue to read the actual data to be used
if (! LpReadInfo - & gt; Readed_header)
{
H_info header_info={0};
Memcpy_s (& amp; Header_info, sizeof (h_info), lpReadInfo - & gt; The data, sizeof (h_info));
The delete [] lpReadInfo - & gt; The data;
LpReadInfo - & gt; Data=https://bbs.csdn.net/topics/nullptr;

//logo for the first packet has finished reading
LpReadInfo - & gt; Readed_header=TRUE;
LpReadInfo - & gt; Data_len=ntohl (header_info. Data_len);
LpReadInfo - & gt; Origin_data_len=ntohl (header_info. Origin_data_len);
LpReadInfo - & gt; Readed_len=0;
Try
{
LpReadInfo - & gt; Data=https://bbs.csdn.net/topics/new TCHAR [lpReadInfo -> data_len] {0}.
If (NULL==lpReadInfo - & gt; Data)
{
WSAAsyncSelect (sock, hWnd WM_ASYNC_SOCKET, FD_CLOSE);
GlobalUserLog. Error (TEXT (" distribution of memory allocation failure, size: % ld "), lpReadInfo - & gt; Data_len);
}
The else
{
WSAAsyncSelect (sock, hWnd WM_ASYNC_SOCKET, FD_READ);
}
}
The catch (const STD: : bad_alloc & amp; MemEx)
{
WSAAsyncSelect (sock, hWnd WM_ASYNC_SOCKET, FD_CLOSE);
GlobalUserLog. Error (TEXT (" the extraordinary memory allocation, the allocation size: % ld "), lpReadInfo - & gt; Data_len);
}
}
The else
{
//the last processing data
The EnterCriticalSection (& amp; M_cs);
UINT ret=m_dataHandler. Handler (lpReadInfo - & gt; The data, lpReadInfo - & gt; Data_len lpReadInfo - & gt; Origin_data_len);
LeaveCriticalSection (& amp; M_cs);
Ret=htonl (ret);
Int sendRet=send (the sock, (const char *) & amp; Ret, sizeof (UINT), 0);
If (SOCKET_ERROR==sendRet)
{
GlobalUserLog. Error (TEXT (" there was an unknown network Error, Error code: % d, IP: % s "), WSAGetLastError (), lpReadInfo - & gt; Client_ip);
}
WSAAsyncSelect (sock, hWnd WM_ASYNC_SOCKET, FD_CLOSE);
}
}
The else
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related