Skip to content

Instantly share code, notes, and snippets.

@methodmissing
Created August 29, 2008 19:24
Show Gist options
  • Save methodmissing/8036 to your computer and use it in GitHub Desktop.
Save methodmissing/8036 to your computer and use it in GitHub Desktop.
/* Return 0 if there is data to be read */
my_bool vio_poll_read(Vio *vio,uint timeout)
{
#ifndef HAVE_POLL
return 0;
#else
struct pollfd fds;
int res;
DBUG_ENTER("vio_poll");
fds.fd=vio->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 ? 0 : 1);
#endif
}
void vio_timeout(Vio *vio, uint which, uint timeout)
{
#if defined(SO_SNDTIMEO) && defined(SO_RCVTIMEO)
int r;
DBUG_ENTER("vio_timeout");
{
#ifdef __WIN__
/* Windows expects time in milliseconds as int */
int wait_timeout= (int) timeout * 1000;
#else
/* POSIX specifies time as struct timeval. */
struct timeval wait_timeout;
wait_timeout.tv_sec= timeout;
wait_timeout.tv_usec= 0;
#endif
r= setsockopt(vio->sd, SOL_SOCKET, which ? SO_SNDTIMEO : SO_RCVTIMEO,
IF_WIN(const char*, const void*)&wait_timeout,
sizeof(wait_timeout));
}
#ifndef DBUG_OFF
if (r != 0)
DBUG_PRINT("error", ("setsockopt failed: %d, errno: %d", r, socket_errno));
#endif
DBUG_VOID_RETURN;
#else
/*
Platforms not suporting setting of socket timeout should either use
thr_alarm or just run without read/write timeout(s)
*/
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment