I have some questions about when it is needed to store the data in the wait buffer (waiting for the FD_WRITE event).
This is my send function (fixed):
bool MyClass::DataSend(char *buf, int len)
{
if (len <=0 || m_Socket == INVALID_SOCKET) return false;
if (m_SendBufferLen > 0)
{
if ((m_SendBufferLen + len) < MAX_BUFF_SIZE)
{
memcpy((m_SendBuffer + m_SendBufferLen), buf, len);
m_SendBufferLen += len;
return true;
}
else
{
// close the connection and log insuficient wait buffer size
return false;
}
}
int iResult;
int nPosition = 0;
int nLeft = len;
while (true)
{
iResult = send(m_Socket, (char*)(buf + nPosition), nLeft, 0);
if (iResult != SOCKET_ERROR)
{
if (iResult > 0)
{
nPosition += iResult;
nLeft -= iResult;
}
else
{
// log 0 bytes sent
break;
}
}
else
{
if (WSAGetLastError() == WSAEWOULDBLOCK)
{
if ((m_SendBufferLen + nLeft) < MAX_BUFF_SIZE)
{
// log data copied to the wait buffer
memcpy((m_SendBuffer + m_SendBufferLen), (buf + nPosition), nLeft);
m_SendBufferLen += nLeft;
return true;
}
else
{
// close the connection and log insuficient wait buffer size
return false;
}
}
else
{
// close the connection and log winsock error
return false;
}
}
if (nLeft <= 0) break;
}
return true;
}
My send (FD_WRITE event) function (fixed):
bool MyClass::DataSendEvent()
{
if (m_SendBufferLen < 1) return true;
int iResult;
int nPosition = 0;
int nLeft = m_SendBufferLen;
while (true)
{
iResult = send(m_Socket, (char*)(m_SendBuffer + nPosition), nLeft, 0);
if (iResult != SOCKET_ERROR)
{
if (iResult > 0)
{
nPosition += iResult;
nLeft -= iResult;
}
else
{
// log 0 bytes sent
break;
}
}
else
{
if (WSAGetLastError() == WSAEWOULDBLOCK)
{
if (nPosition > 0)
{
memmove(m_SendBuffer, (m_SendBuffer + nPosition), (m_SendBufferLen - nPosition));
m_SendBufferLen -= nPosition;
}
break;
}
else
{
// close the connection and log winsock error
return false;
}
}
if (nLeft <= 0)
{
if (m_SendBufferLen == nPosition)
{
m_SendBufferLen = 0;
break;
}
else
{
memmove(m_SendBuffer, (m_SendBuffer + nPosition), (m_SendBufferLen - nPosition));
m_SendBufferLen -= nPosition;
nPosition = 0;
nLeft = m_SendBufferLen;
}
}
}
return true;
}
Do I really need the if (nPosition > 0) or not? How do I simulate this scenario? Is there possible send() in non-blocking mode send less bytes than the requested? If not, why using the while() looping?
- This is the final code (thanks to @user315052)
At the top of your while loop, you are already decrementing
nLeft, so you don’t need to subtractnPositionfrom it.In your second function, when you are shifting the unsent bytes to the beginning of the array, you should use
memmove, since you have overlapped regions (you are copying a region ofm_SendBufferinto itself). The overlap is illustrated below, where some of theA‘s would get copied onto itself.I am a little confused about why
DataSend()is implemented to allow the caller to keep calling it with success even when theWSAEWOULDBLOCKis encountered. I would suggest the interface be modified to return a result that lets the caller know that it should stop sending, and to wait for an indication to resume sending.You don’t need the
nPosition > 0check.You can force the case to occur by having the receiver of the data not read anything.
It is definitely possible for
sendin non-blocking mode to send fewer bytes than requested.