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 firstRead;
39      private boolean rdHupReceived;
40      private boolean readComplete;
41  
42      @Override
43      public void reset(ChannelConfig config) {
44          super.reset(config);
45          readComplete = false;
46          firstRead = true;
47      }
48  
49      void rdHupReceived() {
50          this.rdHupReceived = true;
51      }
52  
53      @Override
54      public ByteBuf allocate(ByteBufAllocator alloc) {
55          // We need to ensure we always allocate a direct ByteBuf as we can only use a direct buffer to read via JNI.
56          preferredDirectByteBufAllocator.updateAllocator(alloc);
57          return delegate().allocate(preferredDirectByteBufAllocator);
58      }
59  
60      @Override
61      public boolean continueReading() {
62          // Ensure we use the our own supplier that will take care of reading data until there is nothing left.
63          return continueReading(defaultSupplier);
64      }
65  
66      @Override
67      public boolean continueReading(UncheckedBooleanSupplier maybeMoreDataSupplier) {
68          // If we received an POLLRDHUP we need to continue draining the input until there is nothing left.
69          return ((RecvByteBufAllocator.ExtendedHandle) delegate()).continueReading(maybeMoreDataSupplier)
70                  || rdHupReceived;
71      }
72  
73      public boolean isFirstRead() {
74          return firstRead;
75      }
76  
77      @Override
78      public void readComplete() {
79          super.readComplete();
80          readComplete = true;
81      }
82  
83      boolean isReadComplete() {
84          return readComplete;
85      }
86  
87      @Override
88      public void lastBytesRead(int bytes) {
89          firstRead = false;
90          super.lastBytesRead(bytes);
91      }
92  
93      @Override
94      public void incMessagesRead(int numMessages) {
95          firstRead = false;
96          super.incMessagesRead(numMessages);
97      }
98  }