You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by re...@apache.org on 2007/05/01 01:36:11 UTC
svn commit: r533881 -
/tomcat/tc6.0.x/trunk/webapps/examples/WEB-INF/classes/chat/ChatServlet.java
Author: remm
Date: Mon Apr 30 16:36:10 2007
New Revision: 533881
URL: http://svn.apache.org/viewvc?view=rev&rev=533881
Log:
- New read loop (much more conventional using the more accurate InputStream.available() method).
Modified:
tomcat/tc6.0.x/trunk/webapps/examples/WEB-INF/classes/chat/ChatServlet.java
Modified: tomcat/tc6.0.x/trunk/webapps/examples/WEB-INF/classes/chat/ChatServlet.java
URL: http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/webapps/examples/WEB-INF/classes/chat/ChatServlet.java?view=diff&rev=533881&r1=533880&r2=533881
==============================================================================
--- tomcat/tc6.0.x/trunk/webapps/examples/WEB-INF/classes/chat/ChatServlet.java (original)
+++ tomcat/tc6.0.x/trunk/webapps/examples/WEB-INF/classes/chat/ChatServlet.java Mon Apr 30 16:36:10 2007
@@ -149,16 +149,18 @@
throws IOException, ServletException {
InputStream is = request.getInputStream();
byte[] buf = new byte[512];
- do {
+ while (is.available() > 0) {
+ log("Available: " + is.available());
int n = is.read(buf);
if (n > 0) {
log("Read " + n + " bytes: " + new String(buf, 0, n)
+ " for session: " + request.getSession(true).getId());
} else if (n < 0) {
- error(event, request, response);
+ log("End of file: " + n);
+ end(event, request, response);
return;
}
- } while (is.available() > 0);
+ }
}
protected void service(HttpServletRequest request, HttpServletResponse response)
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org
Re: svn commit: r533881 -
/tomcat/tc6.0.x/trunk/webapps/examples/WEB-INF/classes/chat/ChatServlet.java
Posted by Filip Hanik - Dev Lists <de...@hanik.com>.
Remy Maucherat wrote:
> Filip Hanik - Dev Lists wrote:
>> correct, but what does available() return on a client disconnect,
>> since in NIO it's detected as a read. Are we coding it such that when
>> a read event happens, available() always return -1
>
> available() would return 1 if there's a read event sent for whatever
> reason (meaning the servlet must read and continue reading until
> available() returns <= 0), including a disconnect.
>
> It's never going to be perfectly accurate, I know, there are too many
> buffers, including the native ones, and some are encoded ;) Where did
> you see -1 (it should not be returning -1, it's either 0, 1, or some
> slightly more accurate number if the buffer at the servlet layer
> contains something) ?
forget my -1, that was for reading the OS buffer.
>
>
> If there are ways to properly detect disconnects (like APR apparently
> does on Unix, but not on Windows) it would be much better. There are
> hacks which could emulate it, like doing a read in CoyoteAdapter.event
> before sending the read event, and modify the event accordingly (for
> example, calling InputBuffer.realReadBytes to attempt filling the
> first buffer and see if a horrible exception occurs would detect a
> disconnect).
>
> Rémy
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: dev-help@tomcat.apache.org
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org
Re: svn commit: r533881 - /tomcat/tc6.0.x/trunk/webapps/examples/WEB-INF/classes/chat/ChatServlet.java
Posted by Remy Maucherat <re...@apache.org>.
Filip Hanik - Dev Lists wrote:
> correct, but what does available() return on a client disconnect, since
> in NIO it's detected as a read. Are we coding it such that when a read
> event happens, available() always return -1
available() would return 1 if there's a read event sent for whatever
reason (meaning the servlet must read and continue reading until
available() returns <= 0), including a disconnect.
It's never going to be perfectly accurate, I know, there are too many
buffers, including the native ones, and some are encoded ;) Where did
you see -1 (it should not be returning -1, it's either 0, 1, or some
slightly more accurate number if the buffer at the servlet layer
contains something) ?
If there are ways to properly detect disconnects (like APR apparently
does on Unix, but not on Windows) it would be much better. There are
hacks which could emulate it, like doing a read in CoyoteAdapter.event
before sending the read event, and modify the event accordingly (for
example, calling InputBuffer.realReadBytes to attempt filling the first
buffer and see if a horrible exception occurs would detect a disconnect).
Rémy
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org
Re: svn commit: r533881 -
/tomcat/tc6.0.x/trunk/webapps/examples/WEB-INF/classes/chat/ChatServlet.java
Posted by Filip Hanik - Dev Lists <de...@hanik.com>.
Remy Maucherat wrote:
> Filip Hanik - Dev Lists wrote:
>> question about this one,
>> since a client disconnect is signaled by a READ for the NIO
>> connector, and the only way to find out if the client was
>> disconnected, the InputStream.read() is supposed to return -1, how
>> does that work with the is.available()?
>
> For a client disconnect, you'll get an IO exception during the read,
> not a -1. The only situation where you'll get a -1 (that I know about)
> is for the end of the request body, for example if the content-length
> header says 10, and 10 bytes of the body have been read (so bytes may
> still be available on the socket, but they belong to the next
> request). Ideally, there would be a way to detect a disconnect in the
> poller so that the correct event is sent (this would avoid most IO
> exceptions, which is IMO much cleaner and more efficient).
>
> I understand available() as "there's something to read, and it won't
> block". It also allows enforcing the rule saying that the servlet must
> read all the stuff that is available when it gets a read event, which
> doesn't hurt.
correct, but what does available() return on a client disconnect, since
in NIO it's detected as a read. Are we coding it such that when a read
event happens, available() always return -1
Filip
>
> Rémy
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: dev-help@tomcat.apache.org
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org
Re: svn commit: r533881 - /tomcat/tc6.0.x/trunk/webapps/examples/WEB-INF/classes/chat/ChatServlet.java
Posted by Remy Maucherat <re...@apache.org>.
Filip Hanik - Dev Lists wrote:
> question about this one,
> since a client disconnect is signaled by a READ for the NIO connector,
> and the only way to find out if the client was disconnected, the
> InputStream.read() is supposed to return -1, how does that work with the
> is.available()?
For a client disconnect, you'll get an IO exception during the read, not
a -1. The only situation where you'll get a -1 (that I know about) is
for the end of the request body, for example if the content-length
header says 10, and 10 bytes of the body have been read (so bytes may
still be available on the socket, but they belong to the next request).
Ideally, there would be a way to detect a disconnect in the poller so
that the correct event is sent (this would avoid most IO exceptions,
which is IMO much cleaner and more efficient).
I understand available() as "there's something to read, and it won't
block". It also allows enforcing the rule saying that the servlet must
read all the stuff that is available when it gets a read event, which
doesn't hurt.
Rémy
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org
Re: svn commit: r533881 -
/tomcat/tc6.0.x/trunk/webapps/examples/WEB-INF/classes/chat/ChatServlet.java
Posted by Filip Hanik - Dev Lists <de...@hanik.com>.
question about this one,
since a client disconnect is signaled by a READ for the NIO connector,
and the only way to find out if the client was disconnected, the
InputStream.read() is supposed to return -1, how does that work with the
is.available()?
Filip
remm@apache.org wrote:
> Author: remm
> Date: Mon Apr 30 16:36:10 2007
> New Revision: 533881
>
> URL: http://svn.apache.org/viewvc?view=rev&rev=533881
> Log:
> - New read loop (much more conventional using the more accurate InputStream.available() method).
>
> Modified:
> tomcat/tc6.0.x/trunk/webapps/examples/WEB-INF/classes/chat/ChatServlet.java
>
> Modified: tomcat/tc6.0.x/trunk/webapps/examples/WEB-INF/classes/chat/ChatServlet.java
> URL: http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/webapps/examples/WEB-INF/classes/chat/ChatServlet.java?view=diff&rev=533881&r1=533880&r2=533881
> ==============================================================================
> --- tomcat/tc6.0.x/trunk/webapps/examples/WEB-INF/classes/chat/ChatServlet.java (original)
> +++ tomcat/tc6.0.x/trunk/webapps/examples/WEB-INF/classes/chat/ChatServlet.java Mon Apr 30 16:36:10 2007
> @@ -149,16 +149,18 @@
> throws IOException, ServletException {
> InputStream is = request.getInputStream();
> byte[] buf = new byte[512];
> - do {
> + while (is.available() > 0) {
> + log("Available: " + is.available());
> int n = is.read(buf);
> if (n > 0) {
> log("Read " + n + " bytes: " + new String(buf, 0, n)
> + " for session: " + request.getSession(true).getId());
> } else if (n < 0) {
> - error(event, request, response);
> + log("End of file: " + n);
> + end(event, request, response);
> return;
> }
> - } while (is.available() > 0);
> + }
> }
>
> protected void service(HttpServletRequest request, HttpServletResponse response)
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: dev-help@tomcat.apache.org
>
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org