Select() broken?

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
roby65
Posts: 52
Joined: Sun Jun 01, 2008 9:12 pm
Location: Mid Italy
Contact:

Select() broken?

Post by roby65 »

Hi guys,
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 &#40;i=0;i<count;i++&#41;
	&#123;
		if&#40;&#40;socklist&#91;i&#93;->fd&#41;<0&#41;
			continue;
		if&#40;socklist&#91;i&#93;->events & POLLIN&#41;
			FD_SET&#40;socklist&#91;i&#93;->fd, &read_flags&#41;;
		if&#40;socklist&#91;i&#93;->events & POLLOUT&#41;
			FD_SET&#40;socklist&#91;i&#93;->fd, &write_flags&#41;;
			
		if &#40;&#40;socklist&#91;i&#93;->fd&#41;>maxfd&#41;
		&#123;
			maxfd=socklist&#91;i&#93;->fd;
		&#125;
			
	&#125;
	ret=select&#40;&#40;maxfd&#41;+1, &read_flags,&write_flags,&#40;fd_set*&#41;0,&waitv&#41;;
	if&#40;ret<0&#41;
	&#123;
		//Error handling
	&#125;
	
	for &#40;i=0;i<count;i++&#41;
	&#123;
		if&#40;&#40;socklist&#91;i&#93;->fd&#41;<0&#41;
			continue;
		if&#40;socklist&#91;i&#93;->events & POLLIN&#41;
			if&#40;FD_ISSET&#40;socklist&#91;i&#93;->fd,&read_flags&#41;&#41;
			&#123;
				socklist&#91;i&#93;->revents |=POLLIN;
			&#125;
		if&#40;socklist&#91;i&#93;->events & POLLOUT&#41;
			if&#40;FD_ISSET&#40;socklist&#91;i&#93;->fd,&write_flags&#41;&#41;
			&#123;
				socklist&#91;i&#93;->revents |=POLLOUT;
			&#125;
	&#125;	
	return 1;
	
&#125;
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Post by jimparis »

What's ret? If it's zero due to timeout (yours is extremely short), maybe the fd_sets aren't being cleared. You can try also try drilling down into newlib's libc/sys/psp/select.c to see where they're getting set.
roby65
Posts: 52
Joined: Sun Jun 01, 2008 9:12 pm
Location: Mid Italy
Contact:

Post by roby65 »

jimparis wrote:What's ret? If it's zero due to timeout (yours is extremely short), maybe the fd_sets aren't being cleared. You can try also try drilling down into newlib's libc/sys/psp/select.c to see where they're getting set.
That's the problem, i must check if ret is 0 due to timeout, thanks! :)
Post Reply