Re: Using kqueue to time out reading a socket
Re: Using kqueue to time out reading a socket
- Subject: Re: Using kqueue to time out reading a socket
- From: Scott Ribe <email@hidden>
- Date: Tue, 12 Jul 2005 13:56:16 -0600
- Resent-date: Wed, 13 Jul 2005 15:49:20 -0600
- Resent-from: Scott Ribe <email@hidden>
- Resent-message-id: <BEF97C80.2B091%email@hidden>
- Resent-to: <email@hidden>
OK, let's try again. I'm ignoring SIGPIPE, but forgot to check for EINTR
where appropriate, and had also just reordered the if/else clauses and
neglected to check for some conditions. Sorry... How about comments on
*this* version of the function ;-)
OSStatus ReadBlock( int sock, void * buf, long len, bool waitforever )
{
OSStatus err = 0;
long togo = len;
char * cur = (char *) buf;
int kq = kqueue();
if( kq == -1 )
{
err = errno;
LogErrorG( "error (", errno, ") creating kqueue." );
}
while( togo > 0 && !err )
{
long chunk = togo <= 1024 ? togo : 1024;
struct kevent kev;
EV_SET( &kev, sock, EVFILT_READ, EV_ADD | EV_ENABLE, NOTE_LOWAT,
chunk, 0 );
timespec tmout = { 15, 0 };
int evtcnt = kevent( kq, &kev, 1, &kev, 1, waitforever ? NULL :
&tmout );
if( evtcnt == -1 )
{
if( err != EINTR )
{
err = errno;
LogErrorG( "error (", err, ") waiting for input." );
}
}
else if( evtcnt == 1 && (kev.flags & EV_ERROR) )
{
err = kev.data;
LogErrorG( "error (", err, ") waiting for input." );
}
else if( evtcnt == 1 && kev.data > 0 )
{
chunk = recv( sock, cur, togo, 0 );
if( chunk == -1 )
{
if( errno != EINTR )
err = errno;
}
else
{
togo -= chunk;
cur += chunk;
}
}
else if( evtcnt == 0 )
{
LogErrorG( "timed out reading data" );
err = ETIMEDOUT;
}
else if( kev.data == 0 )
err = ENOTCONN;
}
if( kq != -1 )
close( kq );
return err;
}
--
Scott Ribe
email@hidden
http://www.killerbytes.com/
(303) 665-7007 voice
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden