pengweibo(马甸尼)
我好讨厌匈牙利命名法啊!(纯粹个人意见,莫见怪.^_^。)
//////////////////////////////////////////////////////////////////////////////
// Function Name : sendAll
// Description : try to send all the outbuf
// Input Parameters : outbuf -- the data which want to be send
// want -- how many bytes you wan to send
// Return Value : If no error occurs, recv returns the number of bytes received.
// If the connection has been gracefully closed, the return value is zero.
/// Otherwise, a value of SOCKET_ERROR is returned
//////////////////////////////////////////////////////////////////////////////
int AbstractSocket::sendAll(const char *outbuf, int want) const
{
assert(m_socket!=INVALID_SOCKET);
assert(outbuf);
assert(want>0);
int left;
int idx;
int ret;
left = want;
idx = 0;
while(left > 0)
{
ret = ::send(m_socket, &outbuf[idx], left, 0);
// send error, or has been close by peer
if(ret == 0 || ret == SOCKET_ERROR)
break;
left -= ret;
idx += ret;
}
// left == 0 means we do the job successfully
return left == 0 ? idx : ret;
}
//////////////////////////////////////////////////////////////////////////////
// Function Name : recvAll
// Description : try to recv all data we want
// Input Parameters : inbuf -- where to store data
// want -- how much data we want
// Return Value : If no error occurs, recv returns the number of bytes received.
// If the connection has been gracefully closed, the return value is zero.
/// Otherwise, a value of SOCKET_ERROR is returned
//////////////////////////////////////////////////////////////////////////////
int AbstractSocket::recvAll(char *inbuf, int want) const
{
assert(m_socket!=INVALID_SOCKET);
assert(inbuf);
assert(want>0);
int ret;
int read = 0;
while(read < want)
{
ret = recv(&inbuf[read], want-read);
// send error, or has been close by peer
if(ret == 0 || ret == SOCKET_ERROR)
break;
read += ret;
}
// read == want means we do the job successfully
return read == want ? read : ret;
}