Skip to content

Replaced "select" with "poll" for non-Windows Platforms - #2

Open
wolgemoth wants to merge 1 commit into
mingodad:masterfrom
wolgemoth:master
Open

Replaced "select" with "poll" for non-Windows Platforms#2
wolgemoth wants to merge 1 commit into
mingodad:masterfrom
wolgemoth:master

Conversation

@wolgemoth

Copy link
Copy Markdown
Contributor

An error can sometimes occur when waiting for data. In my case I am testing on Android, and was able to mitigate the issue by polling the socket instead of running select:

#include <poll.h>
...
// return true if socket has data waiting to be read
bool datawaiting( int sock )
{
#ifdef _WIN32
	fd_set fds;
	FD_ZERO( &fds );
	FD_SET( sock, &fds );

	struct timeval tv;
	tv.tv_sec = 0;
	tv.tv_usec = 0;

	int r = select( sock+1, &fds, NULL, NULL, &tv);
	if (r < 0)
		BailOnSocketError( "select" );

	return FD_ISSET( sock, &fds ) != 0;
#else
	struct pollfd pfd;
	pfd.fd = sock;
	pfd.events = POLLIN;
	pfd.revents = 0;

	int r = poll( &pfd, 1, 0 );
	if (r < 0)
		BailOnSocketError( "poll" );

	return ( pfd.revents & POLLIN ) != 0;
#endif
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant