View Javadoc
1   /*
2    * Copyright 2024 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.uring;
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;
22  import io.netty.channel.unix.PreferredDirectByteBufAllocator;
23  import io.netty.util.UncheckedBooleanSupplier;
24  
25  final class IoUringRecvByteAllocatorHandle extends RecvByteBufAllocator.DelegatingHandle
26          implements RecvByteBufAllocator.ExtendedHandle {
27      private final PreferredDirectByteBufAllocator preferredDirectByteBufAllocator =
28              new PreferredDirectByteBufAllocator();
29  
30      // We need to continue reading as long as we received something when using io_uring. Otherwise
31      // we will not be able to batch things in an efficient way.
32      private final UncheckedBooleanSupplier defaultSupplier = () -> lastBytesRead() > 0;
33  
34      IoUringRecvByteAllocatorHandle(RecvByteBufAllocator.ExtendedHandle handle) {
35          super(handle);
36      }
37  
38      private boolean rdHupReceived;
39      private boolean readComplete;
40  
41      @Override
42      public void reset(ChannelConfig config) {
43          super.reset(config);
44          readComplete = false;
45      }
46  
47      void rdHupReceived() {
48          this.rdHupReceived = true;
49      }
50  
51      @Override
52      public ByteBuf allocate(ByteBufAllocator alloc) {
53          // We need to ensure we always allocate a direct ByteBuf as we can only use a direct buffer to read via JNI.
54          preferredDirectByteBufAllocator.updateAllocator(alloc);
55          return delegate().allocate(preferredDirectByteBufAllocator);
56      }
57  
58      @Override
59      public boolean continueReading() {
60          // Ensure we use the our own supplier that will take care of reading data until there is nothing left.
61          return continueReading(defaultSupplier);
62      }
63  
64      @Override
65      public boolean continueReading(UncheckedBooleanSupplier maybeMoreDataSupplier) {
66          // If we received an POLLRDHUP we need to continue draining the input until there is nothing left.
67          return ((RecvByteBufAllocator.ExtendedHandle) delegate()).continueReading(maybeMoreDataSupplier)
68                  || rdHupReceived;
69      }
70  
71      @Override
72      public void readComplete() {
73          super.readComplete();
74          readComplete = true;
75      }
76  
77      boolean isReadComplete() {
78          return readComplete;
79      }
80  }