/**
Indicate whether there is data to read on a given socket.
@note An exceptional condition event and/or errors are
interpreted as if there is data to read.
@param sd A connected socket.
@param timeout Maximum time in seconds to poll.
@retval FALSE There is data to read.
@retval TRUE There is no data to read.
*/
static my_bool socket_poll_read(my_socket sd, uint timeout)
{
#ifdef __WIN__
int res;
my_socket fd= sd;
fd_set readfds, errorfds;
struct timeval tm;
DBUG_ENTER("socket_poll_read");
tm.tv_sec= timeout;
tm.tv_usec= 0;
FD_ZERO(&readfds);
FD_ZERO(&errorfds);
FD_SET(fd, &readfds);
FD_SET(fd, &errorfds);
/* The first argument is ignored on Windows, so a conversion to int is OK */
if ((res= select((int) fd, &readfds, NULL, &errorfds, &tm) <= 0))//等待可读或错误事件
{
DBUG_RETURN(res < 0 ? 0 : 1);
}
res= FD_ISSET(fd, &readfds) || FD_ISSET(fd, &errorfds);//是否有可读或错误事件
DBUG_RETURN(!res);
#elif defined(HAVE_POLL)
struct pollfd fds;
int res;
DBUG_ENTER("socket_poll_read");
fds.fd=sd;
fds.events=POLLIN;//可读事件
fds.revents=0;
if ((res=poll(&fds,1,(int) timeout*1000)) <= 0)//等待可读事件
{
DBUG_RETURN(res < 0 ? 0 : 1); /* Don't return 1 on errors */
}
DBUG_RETURN(fds.revents & (POLLIN | POLLERR | POLLHUP) ? 0 : 1);
#else
return 0;
#endif
}
都说使用select具有可移植性,但是在该函数中应该并没有使用其可移植的功能,而是对WINDOWS和LINUX分别做了处理的,其原因应该就是在LINUX下,select函数的效率是没有poll效率高。