源码如下:
/**
Retrieve the amount of data that can be read from a socket.
@param vio A VIO object.
@param bytes[out] The amount of bytes available.
@retval FALSE Success.
@retval TRUE Failure.
*/
static my_bool socket_peek_read(Vio *vio, uint *bytes)
{
#ifdef __WIN__
int len;
if (ioctlsocket(vio->sd, FIONREAD, &len))//获得读缓冲区大小,还有清除设备准备就绪状态的作用.
return TRUE;
*bytes= len;
return FALSE;
#elif FIONREAD_IN_SYS_IOCTL
int len;
if (ioctl(vio->sd, FIONREAD, &len) < 0)//获得读缓冲区大小,还有清除设备准备就绪状态的作用.
return TRUE;
*bytes= len;
return FALSE;
#else
char buf[1024];
ssize_t res= recv(vio->sd, &buf, sizeof(buf), MSG_PEEK);//MSG_PEEK也就是偷看缓冲区数据长度
if (res < 0)
return TRUE;
*bytes= res;
return FALSE;
#endif
}