Mark,

On 3/11/15 2:57 PM, Mark Thomas wrote:
> On 11/03/2015 18:09, Christopher Schultz wrote:
>> Mark,
>>
>> On 3/11/15 1:02 PM, Christopher Schultz wrote:
>>> Mark,
>>>
>>> On 3/10/15 4:44 PM, ma...@apache.org wrote:
>>>> Author: markt
>>>> Date: Tue Mar 10 20:44:56 2015
>>>> New Revision: 1665682
>>>>
>>>> URL: http://svn.apache.org/r1665682
>>>> Log:
>>>> Follow-up to r1665297
>>>> Correct output loop. Forgot to increment offset for subsequent writes.
>>>>
>>>> Modified:
>>>>     tomcat/tc7.0.x/trunk/java/org/apache/coyote/ajp/AjpNioProcessor.java
>>>>
>>>> Modified: 
>>>> tomcat/tc7.0.x/trunk/java/org/apache/coyote/ajp/AjpNioProcessor.java
>>>> URL: 
>>>> http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/coyote/ajp/AjpNioProcessor.java?rev=1665682&r1=1665681&r2=1665682&view=diff
>>>> ==============================================================================
>>>> --- tomcat/tc7.0.x/trunk/java/org/apache/coyote/ajp/AjpNioProcessor.java 
>>>> (original)
>>>> +++ tomcat/tc7.0.x/trunk/java/org/apache/coyote/ajp/AjpNioProcessor.java 
>>>> Tue Mar 10 20:44:56 2015
>>>> @@ -302,11 +302,11 @@ public class AjpNioProcessor extends Abs
>>>>          ByteBuffer writeBuffer =
>>>>                  
>>>> socketWrapper.getSocket().getBufHandler().getWriteBuffer();
>>>>  
>>>> -        int left = length;
>>>> +        int thisTime = 0;
>>>>          int written = 0;
>>>> -        while (left > 0) {
>>>> -            int toWrite = Math.min(left, writeBuffer.remaining());
>>>> -            writeBuffer.put(src, offset, toWrite);
>>>> +        while (written < length) {
>>>> +            int toWrite = Math.min(length - written, 
>>>> writeBuffer.remaining());
>>>> +            writeBuffer.put(src, offset + written, toWrite);
>>>>              
>>>>              writeBuffer.flip();
>>>>      
>>>> @@ -318,13 +318,13 @@ public class AjpNioProcessor extends Abs
>>>>                  //ignore
>>>>              }
>>>>              try {
>>>> -                written = pool.write(writeBuffer, 
>>>> socketWrapper.getSocket(),
>>>> +                thisTime = pool.write(writeBuffer, 
>>>> socketWrapper.getSocket(),
>>>>                          selector, writeTimeout, true);
>>>>              } finally { 
>>>>                  writeBuffer.clear();
>>>>                  if ( selector != null ) pool.put(selector);
>>>>              }
>>>> -            left -= written;
>>>> +            written += thisTime;
>>>>          }
>>>>      }
>>>
>>> This patch fixes my latest problem. (yay!)
>>>
>>> I'm not sure how I didn't catch the failure to update "offset" in the
>>> original patch.
>>
>> Can you give me a quick explanation of what's going on with
>> writeBuffer.put()? Why is the second argument "offset + written" instead
>> of just zero?
> 
> Because it is the start point in src for the data you want to put into
> the writeBuffer.
> 
>> Do I misunderstand the way that NIO buffers are used?
> 
> Take a look at the Javadoc for put. The args are all about the source,
> not the destination.
> 
>> If I were using a byte[] to buffer data, I'd usually fill up that buffer
>> starting at offset=0, then "send" the data wherever, then treat the
>> buffer as empty and start the copy targeted again as offset=0.
> 
> That is what we are doing. I think you are confusing source and destination.

Yes, definitely. I first wrote the above message, then things clarified
in my brain and I discarded the message without sending it. Then, I went
back to look at the patch and started scratching my head again and
decided to ask.

>> Do NIO buffers also carry message-context information (such as exactly
>> what the starting offset in the whole message the current contents of
>> the buffer represent)?
> 
> Yes. Although the meaning varies depending on whether it has been
> flipped or not (which is why I ended up writing the wrapper around the
> I/O buffers to track it).

Time to dive-into NIO, I think.

I've got some Java code to contact a web resource, optionally POST/PUT
data and then fetch the response. It doesn't know how to do 10-Continue
and it won't check for a response until after it has sent the data (a
classic "stupid" client we always complain about around here). The use
of NIO can help, here, and I'm likely to use that as a motivating
example for myself.

At ApacheCon, don't be surprised if I ask you a bunch of questions about
NIO ;)

-chris

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to