AsynchronousByteCharChannel and Timeouts
cowwoc
cowwoc at bbs.darktech.org
Sun Jul 11 21:05:10 PDT 2010
Alan Bateman wrote:
>
> Gili wrote:
>> :
>> Okay. I agree with everything you wrote except for the earlier post about
>> how it should be possible to implement read-with-timeout in terms of
>> read-forever.
> Right, this is where we differ because AsynchronousSocketChannel does
> not require the implementation to have a mechanism to cancel the
> underlying I/O operation.
Alan,
Why doesn't the underlying AsynchronousByteChannel provide the timeout
methods instead of AsynchronousSocketChannel? I think it's a lot easier to
implement read-forever in terms of read-with-timeout than the opposite and
that most operating systems will support read-with-timeout.
I think you can implement read-with-timeout on top of read-forever in the
following way.
First, some definitions:
Internal Buffer: Each channel is associated with an "internal buffer" that
contains any bytes returned by the underlying system that have not (yet)
been requested by the user.
nativeRead(): The channel read() method invokes a native function
"nativeRead()" on the underlying socket, serial port, etc.
available(): Returns the number of bytes that may be requested from
nativeRead() without blocking
I propose the following implementation:
1. If the OS supports available() and the number of available bytes is
greater than the number requested, simply read() without the fear of
blocking.
2. If the OS does not support available() or the number of available bytes
is less than the number requested, issue a nativeRead() for one byte against
the internal buffer. If a timeout occurs before the byte is read, throw
InterruptedByTimeoutException and allow the nativeRead() to continue.
3. If a read() is issued while a nativeRead() is still pending, wait on the
nativeRead() as opposed to issuing a new one.
What do you think?
Gili
--
View this message in context: http://nio-discuss.3093094.n2.nabble.com/AsynchronousByteCharChannel-and-Timeouts-tp3363004p5281550.html
Sent from the nio-discuss mailing list archive at Nabble.com.
More information about the nio-discuss
mailing list