View Javadoc
1   /*
2    * Copyright 2016 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.channel.kqueue;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.buffer.ByteBufAllocator;
20  import io.netty.channel.ChannelConfig;
21  import io.netty.channel.RecvByteBufAllocator.DelegatingHandle;
22  import io.netty.channel.RecvByteBufAllocator.ExtendedHandle;
23  import io.netty.channel.unix.PreferredDirectByteBufAllocator;
24  import io.netty.util.UncheckedBooleanSupplier;
25  
26  import static java.lang.Math.max;
27  import static java.lang.Math.min;
28  
29  final class KQueueRecvByteAllocatorHandle extends DelegatingHandle implements ExtendedHandle {
30      private final PreferredDirectByteBufAllocator preferredDirectByteBufAllocator =
31              new PreferredDirectByteBufAllocator();
32  
33      private final UncheckedBooleanSupplier defaultMaybeMoreDataSupplier = new UncheckedBooleanSupplier() {
34          @Override
35          public boolean get() {
36              return maybeMoreDataToRead();
37          }
38      };
39      private boolean overrideGuess;
40      private boolean readEOF;
41      private long numberBytesPending;
42  
43      KQueueRecvByteAllocatorHandle(ExtendedHandle handle) {
44          super(handle);
45      }
46  
47      @Override
48      public int guess() {
49          return overrideGuess ? guess0() : delegate().guess();
50      }
51  
52      @Override
53      public void reset(ChannelConfig config) {
54          overrideGuess = ((KQueueChannelConfig) config).getRcvAllocTransportProvidesGuess();
55          delegate().reset(config);
56      }
57  
58      @Override
59      public ByteBuf allocate(ByteBufAllocator alloc) {
60          // We need to ensure we always allocate a direct ByteBuf as we can only use a direct buffer to read via JNI.
61          preferredDirectByteBufAllocator.updateAllocator(alloc);
62          return overrideGuess ? preferredDirectByteBufAllocator.ioBuffer(guess0()) :
63                  delegate().allocate(preferredDirectByteBufAllocator);
64      }
65  
66      @Override
67      public void lastBytesRead(int bytes) {
68          numberBytesPending = bytes < 0 ? 0 : max(0, numberBytesPending - bytes);
69          delegate().lastBytesRead(bytes);
70      }
71  
72      @Override
73      public boolean continueReading(UncheckedBooleanSupplier maybeMoreDataSupplier) {
74          // If we received EOF we need to continue reading until there is nothing left.
75          return isReadEOF() || ((ExtendedHandle) delegate()).continueReading(maybeMoreDataSupplier);
76      }
77  
78      @Override
79      public boolean continueReading() {
80          // We must override the supplier which determines if there maybe more data to read.
81          return continueReading(defaultMaybeMoreDataSupplier);
82      }
83  
84      void readEOF() {
85          readEOF = true;
86      }
87  
88      boolean isReadEOF() {
89          return readEOF;
90      }
91  
92      void numberBytesPending(long numberBytesPending) {
93          this.numberBytesPending = numberBytesPending;
94      }
95  
96      boolean maybeMoreDataToRead() {
97          /*
98           * kqueue with EV_CLEAR flag set requires that we read until we consume "data" bytes
99           * (see <a href="https://www.freebsd.org/cgi/man.cgi?kqueue">kqueue man</a>). However in order to
100          * respect auto read we supporting reading to stop if auto read is off. If auto read is on we force reading to
101          * continue to avoid a {@link StackOverflowError} between channelReadComplete and reading from the
102          * channel. It is expected that the {@link #KQueueSocketChannel} implementations will track if all data was not
103          * read, and will force a EVFILT_READ ready event.
104          *
105          * It is assumed EOF is handled externally by checking {@link #isReadEOF()}.
106          */
107         return numberBytesPending != 0;
108     }
109 
110     private int guess0() {
111         return (int) min(numberBytesPending, Integer.MAX_VALUE);
112     }
113 }