can someone confirm me that select() is broken?
I use it for testing if a socket is ready to send and receive, and it always tell me that the socket is ready for reading and writing also if it is not.
code:
Code: Select all
#define POLLIN 1
#define POLLOUT 2
#define POLLERR 4
#define POLLHUP POLLERR
#define POLLPRI 8
#define POLLNVAL POLLERR
struct pollfd
{
int fd;
unsigned char events; //What events to capture
unsigned char revents; //Events captured
};
int poll(struct pollfd* socklist,int count, int unk)
{
fd_set read_flags,write_flags;
int i=0,ret;
struct timeval waitd;
int maxfd=0;
waitd.tv_sec = 0;
waitd.tv_usec = 5;
FD_ZERO(&read_flags);
FD_ZERO(&write_flags);
for (i=0;i<count;i++)
{
if((socklist[i]->fd)<0)
continue;
if(socklist[i]->events & POLLIN)
FD_SET(socklist[i]->fd, &read_flags);
if(socklist[i]->events & POLLOUT)
FD_SET(socklist[i]->fd, &write_flags);
if ((socklist[i]->fd)>maxfd)
{
maxfd=socklist[i]->fd;
}
}
ret=select((maxfd)+1, &read_flags,&write_flags,(fd_set*)0,&waitv);
if(ret<0)
{
//Error handling
}
for (i=0;i<count;i++)
{
if((socklist[i]->fd)<0)
continue;
if(socklist[i]->events & POLLIN)
if(FD_ISSET(socklist[i]->fd,&read_flags))
{
socklist[i]->revents |=POLLIN;
}
if(socklist[i]->events & POLLOUT)
if(FD_ISSET(socklist[i]->fd,&write_flags))
{
socklist[i]->revents |=POLLOUT;
}
}
return 1;
}