Tanaka said:
I'm not sure why people want a such method.
What is a situation you need the method?
I suppose because it's part of the only way I've found to get around the
lack of
a someSocket.readallavailable - something that will give me *all* the data
that can be read from the socket at the time I call it. (I could use recv with
a number larger than anything I should ever have waiting for an argument,
but that's crude. Admittedly, the workaround is also crude.)
Sometimes the only way I have of being certain that I've gotten all of an
incoming line is that I haven't been sent anything more. So, in place of
a readallavailable function, I have this procedure (which I have yet to code,
but I think should work):
1. Get a chunk of data with recv. At this point, I don't care if it blocks.
2. Add received data to a buffer.
3. Check to see if there's any data waiting.
4. If there is, go back to step 1. If there isn't, go on.
5. Now being sure that the buffer contains all data currently
available, the program can process it.
I'd also love to see a method that would tell me how many bytes are
waiting to be read at the moment. But I wouldn't be surprised if that's
not feasible for some reason or another.
If it returns false, your program cannot have data from the socket.
So your program cannot do about the data. If your program has
something to do in such case, there should be some work which doesn't
depend the data.
If the work is done by other threads, the blocking behavior is
appropriate because the other threads is run at the blocking time.
If the program use a event driven framework, the readability test
should be done by the main event loop in the framework. So the
program don't need the method.
I tried doing something like this (or at least what I think you're
referring to) in the program I'm working on now. (Using Fox toolkit
via FXRuby.) However, I couldn't get it to work right on my own, and
my message here asking for help got no replies...
In any case, the method called by the framework to handle the
event will still have to deal with the "do I have all the data?" issue.
Threads, though powerful, are rather annoying to deal with sometimes.
In the program I'm working on now, they're probably the best way
of handling things anyway, but I'd like to have a way of working
with sockets that doesn't force me to use them when they don't
suit my needs.
So I guess your program has another situation I don't imagine.
However it is possible to implement the method by IO.select.
It can do the job, yes. But it also seems like overkill for someone
who's only interested in one object. And it'd be a lot easier to read
something that simply calls a method on the socket of concern.
-Morgan