Cancelling CFNetwork operations from another thread?
Cancelling CFNetwork operations from another thread?
- Subject: Cancelling CFNetwork operations from another thread?
- From: Rich Siegel <email@hidden>
- Date: Mon, 7 Nov 2005 19:05:18 -0500
I'm using synchronous CFSocketStream pairs for my networking; the
implementation was simple and it's working pretty well.
One problem I'm having is with exception handling; when something goes
wrong with the network, my client effectively gets stuck in a loop,
chewing up CPU and never returning. This happens on both read and write.
My write loop looks like this:
//
// we may block for flow control or other reasons, so write in
// a loop until we're all done.
//
const UInt8 *p = reinterpret_cast<const UInt8*>(data);
SInt32 bytesRemaining = length;
while (bytesRemaining > 0)
{
if (CFWriteStreamCanAcceptBytes(fWriteStream))
{
enum { kMaxXferSize = 32768L };
SInt32 actCount = 0;
actCount = CFWriteStreamWrite(fWriteStream, p, pin(bytesRemaining, kMaxXferSize));
FailTestWithWriteStreamError(actCount >= 0, fWriteStream);
bytesRemaining -= actCount;
p += actCount;
if (NULL != bytesSentForStatusDisplay)
*bytesSentForStatusDisplay += actCount;
FailOSStatus(Tickle(kNWSendCmd));
}
}
Now, I can insert code into the loop such that if I'm unable to send
data for some reasonable amount of time (say, 15 seconds) because
CFWriteStreamCanAcceptBytes() continually returns false, I can bail;
something similar can be done for reads.
However, there's no good way that I can see to interrupt a
CFReadStreamRead() or CFWriteStreamWrite() that gets stuck and never
returns.
All I've got to go on is this comment from CFStream.h, regarding
CFReadStreamClose() and CFWriteStreamClose():
/* Terminates the flow of bytes; releases any system resources required by the
stream. The stream may not fail to close. You may call CFStreamClose() to
effectively abort a stream. */
So, this makes me wonder whether it would be OK to call
CFReadStreamClose() or CFWriteStreamClose() from another thread in order
to abort a "stuck" CFReadStreamRead() or CFWriteStreamWrite() call.
Intuition tells me that this is a bad idea, but it can't hurt to ask. Is
it, in fact, possible to do this?
Thanks for any advice,
R.
--
Rich Siegel Bare Bones Software, Inc.
<email@hidden> <http://www.barebones.com/>
Someday I'll look back on all this and laugh... until they sedate me.
_______________________________________________
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